Skip to content

Commit

Permalink
Allow ports to be configured as NodePort (#165)
Browse files Browse the repository at this point in the history
* Allow ports to be configured as `NodePort'

* PR fix: Do not update chart version

* PR fix: Remove setting null `nodePort` in case of `ClusterIP` service

* PR fix: Remove unreleased changelog entry
  • Loading branch information
plblueraven authored Apr 25, 2024
1 parent c5b1d3d commit e3037d2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions charts/qdrant/templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ spec:
- name: {{ .name }}
port: {{ .port }}
targetPort: {{ .targetPort }}
{{- if and (or (eq $.Values.service.type "NodePort") (eq $.Values.service.type "LoadBalancer")) (not (empty .nodePort)) }}
nodePort: {{ .nodePort }}
{{- end }}
protocol: {{ .protocol | default "TCP" }}
{{- end }}
selector:
Expand Down
110 changes: 110 additions & 0 deletions test/qdrant_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package test

import (
"k8s.io/apimachinery/pkg/util/intstr"
"path/filepath"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/helm"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)

func TestNodePortsAreSetOnNodePortService(t *testing.T) {
t.Parallel()

helmChartPath, err := filepath.Abs("../charts/qdrant")
releaseName := "qdrant"
require.NoError(t, err)

namespaceName := "qdrant-" + strings.ToLower(random.UniqueId())
logger.Log(t, "Namespace: %s\n", namespaceName)

options := &helm.Options{
SetJsonValues: map[string]string{
"service": `{"type": "NodePort", "ports": [{"name": "http", "port": 6333, "targetPort": 6333, "protocol": "TCP", "nodePort": 30333}, {"name": "grpc", "port": 6334, "targetPort": 6334, "protocol": "TCP", "nodePort": 30334}]}`,
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}

statefulsetOutput := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})

var statefulSet appsv1.StatefulSet
helm.UnmarshalK8SYaml(t, statefulsetOutput, &statefulSet)

require.Equal(t, "http", statefulSet.Spec.Template.Spec.Containers[0].Ports[0].Name)
require.EqualValues(t, 6333, statefulSet.Spec.Template.Spec.Containers[0].Ports[0].ContainerPort)
require.Equal(t, corev1.ProtocolTCP, statefulSet.Spec.Template.Spec.Containers[0].Ports[0].Protocol)
require.Equal(t, "grpc", statefulSet.Spec.Template.Spec.Containers[0].Ports[1].Name)
require.EqualValues(t, 6334, statefulSet.Spec.Template.Spec.Containers[0].Ports[1].ContainerPort)
require.Equal(t, corev1.ProtocolTCP, statefulSet.Spec.Template.Spec.Containers[0].Ports[1].Protocol)

serviceOutput := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/service.yaml"})

var service corev1.Service
helm.UnmarshalK8SYaml(t, serviceOutput, &service)

require.Equal(t, corev1.ServiceType("NodePort"), service.Spec.Type)
require.Equal(t, "http", service.Spec.Ports[0].Name)
require.EqualValues(t, 6333, service.Spec.Ports[0].Port)
require.EqualValues(t, intstr.IntOrString{IntVal: 6333}, service.Spec.Ports[0].TargetPort)
require.EqualValues(t, 30333, service.Spec.Ports[0].NodePort)
require.Equal(t, corev1.ProtocolTCP, service.Spec.Ports[0].Protocol)
require.Equal(t, "grpc", service.Spec.Ports[1].Name)
require.EqualValues(t, 6334, service.Spec.Ports[1].Port)
require.EqualValues(t, intstr.IntOrString{IntVal: 6334}, service.Spec.Ports[1].TargetPort)
require.EqualValues(t, 30334, service.Spec.Ports[1].NodePort)
require.Equal(t, corev1.ProtocolTCP, service.Spec.Ports[1].Protocol)
}

func TestNodePortsAreDefaultsOnClusterIpService(t *testing.T) {
t.Parallel()

helmChartPath, err := filepath.Abs("../charts/qdrant")
releaseName := "qdrant"
require.NoError(t, err)

namespaceName := "qdrant-" + strings.ToLower(random.UniqueId())
logger.Log(t, "Namespace: %s\n", namespaceName)

options := &helm.Options{
SetJsonValues: map[string]string{
"service": `{"type": "ClusterIP", "ports": [{"name": "http", "port": 6333, "targetPort": 6333, "protocol": "TCP"}, {"name": "grpc", "port": 6334, "targetPort": 6334, "protocol": "TCP"}]}`,
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}

statefulSetOutput := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"})

var statefulSet appsv1.StatefulSet
helm.UnmarshalK8SYaml(t, statefulSetOutput, &statefulSet)

require.Equal(t, "http", statefulSet.Spec.Template.Spec.Containers[0].Ports[0].Name)
require.EqualValues(t, 6333, statefulSet.Spec.Template.Spec.Containers[0].Ports[0].ContainerPort)
require.Equal(t, corev1.ProtocolTCP, statefulSet.Spec.Template.Spec.Containers[0].Ports[0].Protocol)
require.Equal(t, "grpc", statefulSet.Spec.Template.Spec.Containers[0].Ports[1].Name)
require.EqualValues(t, 6334, statefulSet.Spec.Template.Spec.Containers[0].Ports[1].ContainerPort)
require.Equal(t, corev1.ProtocolTCP, statefulSet.Spec.Template.Spec.Containers[0].Ports[1].Protocol)

serviceOutput := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/service.yaml"})

var service corev1.Service
helm.UnmarshalK8SYaml(t, serviceOutput, &service)

require.Equal(t, corev1.ServiceType("ClusterIP"), service.Spec.Type)
require.Equal(t, "http", service.Spec.Ports[0].Name)
require.EqualValues(t, 6333, service.Spec.Ports[0].Port)
require.EqualValues(t, intstr.IntOrString{IntVal: 6333}, service.Spec.Ports[0].TargetPort)
require.EqualValues(t, 0, service.Spec.Ports[0].NodePort)
require.Equal(t, corev1.ProtocolTCP, service.Spec.Ports[0].Protocol)
require.Equal(t, "grpc", service.Spec.Ports[1].Name)
require.EqualValues(t, 6334, service.Spec.Ports[1].Port)
require.EqualValues(t, intstr.IntOrString{IntVal: 6334}, service.Spec.Ports[1].TargetPort)
require.EqualValues(t, 0, service.Spec.Ports[1].NodePort)
require.Equal(t, corev1.ProtocolTCP, service.Spec.Ports[1].Protocol)
}

0 comments on commit e3037d2

Please sign in to comment.