Path: blob/main/install/installer/pkg/components/proxy/service_test.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1/// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package proxy56import (7"fmt"8"testing"910"github.com/stretchr/testify/require"11corev1 "k8s.io/api/core/v1"12"k8s.io/utils/pointer"1314"github.com/gitpod-io/gitpod/installer/pkg/common"15config "github.com/gitpod-io/gitpod/installer/pkg/config/v1"16"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"17"github.com/gitpod-io/gitpod/installer/pkg/config/versions"18)1920func TestServiceLoadBalancerIP(t *testing.T) {21const loadBalancerIP = "123.456.789.0"22ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{StaticIP: loadBalancerIP}, nil)2324objects, err := service(ctx)25require.NoError(t, err)2627require.Len(t, objects, 1, "must render only one object")2829svc := objects[0].(*corev1.Service)30require.Equal(t, loadBalancerIP, svc.Spec.LoadBalancerIP)31}3233func TestServiceAnnotations(t *testing.T) {34testCases := []struct {35Name string36Annotations map[string]string37Components *config.Components38Expect func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string)39}{40{41Name: "Default to LoadBalancer",42Annotations: map[string]string{"hello": "world"},43Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {44// Check standard load balancer annotations45annotations = loadBalancerAnnotations(ctx, annotations)4647for k, v := range annotations {48require.Equalf(t, annotations[k], svc.Annotations[k],49"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])50}51},52},53{54Name: "Set to LoadBalancer",55Components: &config.Components{56Proxy: &config.ProxyComponent{57Service: &config.ComponentTypeService{58ServiceType: (*corev1.ServiceType)(pointer.String(string(corev1.ServiceTypeLoadBalancer))),59},60},61},62Annotations: map[string]string{"hello": "world", "hello2": "world2"},63Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {64// Check standard load balancer annotations65annotations = loadBalancerAnnotations(ctx, annotations)6667for k, v := range annotations {68require.Equalf(t, annotations[k], svc.Annotations[k],69"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])70}71},72},73{74Name: "Set to ClusterIP",75Components: &config.Components{76Proxy: &config.ProxyComponent{77Service: &config.ComponentTypeService{78ServiceType: (*corev1.ServiceType)(pointer.String(string(corev1.ServiceTypeClusterIP))),79},80},81},82Annotations: map[string]string{"hello": "world"},83Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {84// Check standard load balancer annotations not present85lbAnnotations := loadBalancerAnnotations(ctx, make(map[string]string, 0))8687for k := range lbAnnotations {88require.NotContains(t, annotations, k)89}9091for k, v := range annotations {92require.Equalf(t, annotations[k], svc.Annotations[k],93"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])94}95},96},97}9899for _, testCase := range testCases {100t.Run(testCase.Name, func(t *testing.T) {101ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{ServiceAnnotations: testCase.Annotations}, testCase.Components)102103objects, err := service(ctx)104require.NoError(t, err)105106require.Len(t, objects, 1, "must render only one object")107108svc := objects[0].(*corev1.Service)109110testCase.Expect(ctx, svc, testCase.Annotations)111})112}113}114115func loadBalancerAnnotations(ctx *common.RenderContext, annotations map[string]string) map[string]string {116annotations["external-dns.alpha.kubernetes.io/hostname"] = fmt.Sprintf("%s,*.%s,*.ws.%s", ctx.Config.Domain, ctx.Config.Domain, ctx.Config.Domain)117annotations["cloud.google.com/neg"] = `{"exposed_ports": {"80":{},"443": {}}}`118119return annotations120}121122func renderContextWithProxyConfig(t *testing.T, proxyConfig *experimental.ProxyConfig, components *config.Components) *common.RenderContext {123ctx, err := common.NewRenderContext(config.Config{124Domain: "some-domain",125Components: components,126Experimental: &experimental.Config{127WebApp: &experimental.WebAppConfig{128ProxyConfig: proxyConfig,129},130},131}, versions.Manifest{132Components: versions.Components{133PublicAPIServer: versions.Versioned{134Version: "commit-test-latest",135},136},137}, "test-namespace")138require.NoError(t, err)139140return ctx141}142143144