Skip to content

Commit 326cdea

Browse files
author
Simon Emms
committed
[installer]: move proxy loadbalancer annotations
1 parent 9f2fd65 commit 326cdea

File tree

2 files changed

+90
-14
lines changed

2 files changed

+90
-14
lines changed

install/installer/pkg/components/proxy/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ func service(ctx *common.RenderContext) ([]runtime.Object, error) {
8181
service.Spec.Type = serviceType
8282
if serviceType == corev1.ServiceTypeLoadBalancer {
8383
service.Spec.LoadBalancerIP = loadBalancerIP
84-
}
8584

86-
service.Annotations["external-dns.alpha.kubernetes.io/hostname"] = fmt.Sprintf("%s,*.%s,*.ws.%s", ctx.Config.Domain, ctx.Config.Domain, ctx.Config.Domain)
87-
service.Annotations["cloud.google.com/neg"] = `{"exposed_ports": {"80":{},"443": {}}}`
85+
service.Annotations["external-dns.alpha.kubernetes.io/hostname"] = fmt.Sprintf("%s,*.%s,*.ws.%s", ctx.Config.Domain, ctx.Config.Domain, ctx.Config.Domain)
86+
service.Annotations["cloud.google.com/neg"] = `{"exposed_ports": {"80":{},"443": {}}}`
87+
}
8888

8989
for k, v := range annotations {
9090
service.Annotations[k] = v

install/installer/pkg/components/proxy/service_test.go

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package proxy
55

66
import (
7+
"fmt"
78
"testing"
89

910
"github.com/gitpod-io/gitpod/installer/pkg/common"
@@ -12,11 +13,12 @@ import (
1213
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
1314
"github.com/stretchr/testify/require"
1415
corev1 "k8s.io/api/core/v1"
16+
"k8s.io/utils/pointer"
1517
)
1618

1719
func TestServiceLoadBalancerIP(t *testing.T) {
1820
const loadBalancerIP = "123.456.789.0"
19-
ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{StaticIP: loadBalancerIP})
21+
ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{StaticIP: loadBalancerIP}, nil)
2022

2123
objects, err := service(ctx)
2224
require.NoError(t, err)
@@ -28,24 +30,98 @@ func TestServiceLoadBalancerIP(t *testing.T) {
2830
}
2931

3032
func TestServiceAnnotations(t *testing.T) {
31-
annotations := map[string]string{"hello": "world"}
33+
testCases := []struct {
34+
Name string
35+
Annotations map[string]string
36+
Components *config.Components
37+
Expect func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string)
38+
}{
39+
{
40+
Name: "Default to LoadBalancer",
41+
Annotations: map[string]string{"hello": "world"},
42+
Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {
43+
// Check standard load balancer annotations
44+
annotations = loadBalancerAnnotations(ctx, annotations)
3245

33-
ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{ServiceAnnotations: annotations})
46+
for k, v := range annotations {
47+
require.Equalf(t, annotations[k], svc.Annotations[k],
48+
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
49+
}
50+
},
51+
},
52+
{
53+
Name: "Set to LoadBalancer",
54+
Components: &config.Components{
55+
Proxy: &config.ProxyComponent{
56+
Service: &config.ComponentTypeService{
57+
ServiceType: (*corev1.ServiceType)(pointer.String(string(corev1.ServiceTypeLoadBalancer))),
58+
},
59+
},
60+
},
61+
Annotations: map[string]string{"hello": "world", "hello2": "world2"},
62+
Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {
63+
// Check standard load balancer annotations
64+
annotations = loadBalancerAnnotations(ctx, annotations)
3465

35-
objects, err := service(ctx)
36-
require.NoError(t, err)
66+
for k, v := range annotations {
67+
require.Equalf(t, annotations[k], svc.Annotations[k],
68+
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
69+
}
70+
},
71+
},
72+
{
73+
Name: "Set to ClusterIP",
74+
Components: &config.Components{
75+
Proxy: &config.ProxyComponent{
76+
Service: &config.ComponentTypeService{
77+
ServiceType: (*corev1.ServiceType)(pointer.String(string(corev1.ServiceTypeClusterIP))),
78+
},
79+
},
80+
},
81+
Annotations: map[string]string{"hello": "world"},
82+
Expect: func(ctx *common.RenderContext, svc *corev1.Service, annotations map[string]string) {
83+
// Check standard load balancer annotations not present
84+
lbAnnotations := loadBalancerAnnotations(ctx, make(map[string]string, 0))
3785

38-
require.Len(t, objects, 1, "must render only one object")
86+
for k := range lbAnnotations {
87+
require.NotContains(t, annotations, k)
88+
}
3989

40-
svc := objects[0].(*corev1.Service)
41-
for k, v := range annotations {
42-
require.Equalf(t, annotations[k], svc.Annotations[k],
43-
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
90+
for k, v := range annotations {
91+
require.Equalf(t, annotations[k], svc.Annotations[k],
92+
"expected to find annotation %q:%q on proxy service, but found %q:%q", k, v, k, svc.Annotations[k])
93+
}
94+
},
95+
},
96+
}
97+
98+
for _, testCase := range testCases {
99+
t.Run(testCase.Name, func(t *testing.T) {
100+
ctx := renderContextWithProxyConfig(t, &experimental.ProxyConfig{ServiceAnnotations: testCase.Annotations}, testCase.Components)
101+
102+
objects, err := service(ctx)
103+
require.NoError(t, err)
104+
105+
require.Len(t, objects, 1, "must render only one object")
106+
107+
svc := objects[0].(*corev1.Service)
108+
109+
testCase.Expect(ctx, svc, testCase.Annotations)
110+
})
44111
}
45112
}
46113

47-
func renderContextWithProxyConfig(t *testing.T, proxyConfig *experimental.ProxyConfig) *common.RenderContext {
114+
func loadBalancerAnnotations(ctx *common.RenderContext, annotations map[string]string) map[string]string {
115+
annotations["external-dns.alpha.kubernetes.io/hostname"] = fmt.Sprintf("%s,*.%s,*.ws.%s", ctx.Config.Domain, ctx.Config.Domain, ctx.Config.Domain)
116+
annotations["cloud.google.com/neg"] = `{"exposed_ports": {"80":{},"443": {}}}`
117+
118+
return annotations
119+
}
120+
121+
func renderContextWithProxyConfig(t *testing.T, proxyConfig *experimental.ProxyConfig, components *config.Components) *common.RenderContext {
48122
ctx, err := common.NewRenderContext(config.Config{
123+
Domain: "some-domain",
124+
Components: components,
49125
Experimental: &experimental.Config{
50126
WebApp: &experimental.WebAppConfig{
51127
ProxyConfig: proxyConfig,

0 commit comments

Comments
 (0)