Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/components/proxy/service_test.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package proxy
6
7
import (
8
"fmt"
9
"testing"
10
11
"github.com/stretchr/testify/require"
12
corev1 "k8s.io/api/core/v1"
13
"k8s.io/utils/pointer"
14
15
"github.com/gitpod-io/gitpod/installer/pkg/common"
16
config "github.com/gitpod-io/gitpod/installer/pkg/config/v1"
17
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
18
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
19
)
20
21
func TestServiceLoadBalancerIP(t *testing.T) {
22
const loadBalancerIP = "123.456.789.0"
23
ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{StaticIP: loadBalancerIP}, nil)
24
25
objects, err := service(ctx)
26
require.NoError(t, err)
27
28
require.Len(t, objects, 1, "must render only one object")
29
30
svc := objects[0].(*corev1.Service)
31
require.Equal(t, loadBalancerIP, svc.Spec.LoadBalancerIP)
32
}
33
34
func TestServiceAnnotations(t *testing.T) {
35
testCases := []struct {
36
Name string
37
Annotations map[string]string
38
Components *config.Components
39
Expect func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string)
40
}{
41
{
42
Name: "Default to LoadBalancer",
43
Annotations: map[string]string{"hello": "world"},
44
Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {
45
// Check standard load balancer annotations
46
annotations = loadBalancerAnnotations(ctx, annotations)
47
48
for k, v := range annotations {
49
require.Equalf(t, annotations[k], svc.Annotations[k],
50
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
51
}
52
},
53
},
54
{
55
Name: "Set to LoadBalancer",
56
Components: &config.Components{
57
Proxy: &config.ProxyComponent{
58
Service: &config.ComponentTypeService{
59
ServiceType: (*corev1.ServiceType)(pointer.String(string(corev1.ServiceTypeLoadBalancer))),
60
},
61
},
62
},
63
Annotations: map[string]string{"hello": "world", "hello2": "world2"},
64
Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {
65
// Check standard load balancer annotations
66
annotations = loadBalancerAnnotations(ctx, annotations)
67
68
for k, v := range annotations {
69
require.Equalf(t, annotations[k], svc.Annotations[k],
70
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
71
}
72
},
73
},
74
{
75
Name: "Set to ClusterIP",
76
Components: &config.Components{
77
Proxy: &config.ProxyComponent{
78
Service: &config.ComponentTypeService{
79
ServiceType: (*corev1.ServiceType)(pointer.String(string(corev1.ServiceTypeClusterIP))),
80
},
81
},
82
},
83
Annotations: map[string]string{"hello": "world"},
84
Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {
85
// Check standard load balancer annotations not present
86
lbAnnotations := loadBalancerAnnotations(ctx, make(map[string]string, 0))
87
88
for k := range lbAnnotations {
89
require.NotContains(t, annotations, k)
90
}
91
92
for k, v := range annotations {
93
require.Equalf(t, annotations[k], svc.Annotations[k],
94
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
95
}
96
},
97
},
98
}
99
100
for _, testCase := range testCases {
101
t.Run(testCase.Name, func(t *testing.T) {
102
ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{ServiceAnnotations: testCase.Annotations}, testCase.Components)
103
104
objects, err := service(ctx)
105
require.NoError(t, err)
106
107
require.Len(t, objects, 1, "must render only one object")
108
109
svc := objects[0].(*corev1.Service)
110
111
testCase.Expect(ctx, svc, testCase.Annotations)
112
})
113
}
114
}
115
116
func loadBalancerAnnotations(ctx *common.RenderContext, annotations map[string]string) map[string]string {
117
annotations["external-dns.alpha.kubernetes.io/hostname"] = fmt.Sprintf("%s,*.%s,*.ws.%s", ctx.Config.Domain, ctx.Config.Domain, ctx.Config.Domain)
118
annotations["cloud.google.com/neg"] = `{"exposed_ports": {"80":{},"443": {}}}`
119
120
return annotations
121
}
122
123
func renderContextWithProxyConfig(t *testing.T, proxyConfig *experimental.ProxyConfig, components *config.Components) *common.RenderContext {
124
ctx, err := common.NewRenderContext(config.Config{
125
Domain: "some-domain",
126
Components: components,
127
Experimental: &experimental.Config{
128
WebApp: &experimental.WebAppConfig{
129
ProxyConfig: proxyConfig,
130
},
131
},
132
}, versions.Manifest{
133
Components: versions.Components{
134
PublicAPIServer: versions.Versioned{
135
Version: "commit-test-latest",
136
},
137
},
138
}, "test-namespace")
139
require.NoError(t, err)
140
141
return ctx
142
}
143
144