From 4874c9fef5287f8f823ca81f19e28bda280aaff5 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Thu, 5 May 2022 09:26:36 +0000 Subject: [PATCH 1/3] Refactor: Move proxy services into their own files --- .../pkg/components/ide-proxy/objects.go | 7 +----- .../pkg/components/ide-proxy/service.go | 20 ++++++++++++++++ .../pkg/components/openvsx-proxy/objects.go | 11 +-------- .../pkg/components/openvsx-proxy/service.go | 24 +++++++++++++++++++ 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 install/installer/pkg/components/ide-proxy/service.go create mode 100644 install/installer/pkg/components/openvsx-proxy/service.go diff --git a/install/installer/pkg/components/ide-proxy/objects.go b/install/installer/pkg/components/ide-proxy/objects.go index 998d627f043b52..5654ba54f9feed 100644 --- a/install/installer/pkg/components/ide-proxy/objects.go +++ b/install/installer/pkg/components/ide-proxy/objects.go @@ -9,11 +9,6 @@ import "github.com/gitpod-io/gitpod/installer/pkg/common" var Objects = common.CompositeRenderFunc( deployment, rolebinding, - common.GenerateService(Component, map[string]common.ServicePort{ - PortName: { - ContainerPort: ContainerPort, - ServicePort: ServicePort, - }, - }), + service, common.DefaultServiceAccount(Component), ) diff --git a/install/installer/pkg/components/ide-proxy/service.go b/install/installer/pkg/components/ide-proxy/service.go new file mode 100644 index 00000000000000..bbfe0fd1cd5fec --- /dev/null +++ b/install/installer/pkg/components/ide-proxy/service.go @@ -0,0 +1,20 @@ +// Copyright (c) 2021 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package ide_proxy + +import ( + "github.com/gitpod-io/gitpod/installer/pkg/common" + + "k8s.io/apimachinery/pkg/runtime" +) + +func service(ctx *common.RenderContext) ([]runtime.Object, error) { + return common.GenerateService(Component, map[string]common.ServicePort{ + PortName: { + ContainerPort: ContainerPort, + ServicePort: ServicePort, + }, + })(ctx) +} diff --git a/install/installer/pkg/components/openvsx-proxy/objects.go b/install/installer/pkg/components/openvsx-proxy/objects.go index d21c98225e4ba2..7874617931a016 100644 --- a/install/installer/pkg/components/openvsx-proxy/objects.go +++ b/install/installer/pkg/components/openvsx-proxy/objects.go @@ -13,15 +13,6 @@ var Objects = common.CompositeRenderFunc( networkpolicy, rolebinding, statefulset, - common.GenerateService(Component, map[string]common.ServicePort{ - PortName: { - ContainerPort: ContainerPort, - ServicePort: ServicePort, - }, - PrometheusPortName: { - ContainerPort: PrometheusPort, - ServicePort: PrometheusPort, - }, - }), + service, common.DefaultServiceAccount(Component), ) diff --git a/install/installer/pkg/components/openvsx-proxy/service.go b/install/installer/pkg/components/openvsx-proxy/service.go new file mode 100644 index 00000000000000..6f681955b6036a --- /dev/null +++ b/install/installer/pkg/components/openvsx-proxy/service.go @@ -0,0 +1,24 @@ +// Copyright (c) 2021 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package openvsx_proxy + +import ( + "github.com/gitpod-io/gitpod/installer/pkg/common" + + "k8s.io/apimachinery/pkg/runtime" +) + +func service(ctx *common.RenderContext) ([]runtime.Object, error) { + return common.GenerateService(Component, map[string]common.ServicePort{ + PortName: { + ContainerPort: ContainerPort, + ServicePort: ServicePort, + }, + PrometheusPortName: { + ContainerPort: PrometheusPort, + ServicePort: PrometheusPort, + }, + })(ctx) +} From ec33f7c2ab6859d78d3f8bb58da43ec0c61947ac Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Thu, 5 May 2022 09:32:16 +0000 Subject: [PATCH 2/3] Add experimental config sections To allow the proxies to have extra annotations on their service. --- .../pkg/components/ide-proxy/service.go | 18 +++++++++++++++++- .../pkg/components/openvsx-proxy/service.go | 18 +++++++++++++++++- .../pkg/config/v1/experimental/experimental.go | 12 +++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/install/installer/pkg/components/ide-proxy/service.go b/install/installer/pkg/components/ide-proxy/service.go index bbfe0fd1cd5fec..b01586aa5c4303 100644 --- a/install/installer/pkg/components/ide-proxy/service.go +++ b/install/installer/pkg/components/ide-proxy/service.go @@ -6,15 +6,31 @@ package ide_proxy import ( "github.com/gitpod-io/gitpod/installer/pkg/common" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" ) func service(ctx *common.RenderContext) ([]runtime.Object, error) { - return common.GenerateService(Component, map[string]common.ServicePort{ + var annotations map[string]string + _ = ctx.WithExperimental(func(cfg *experimental.Config) error { + if cfg.IDE != nil && cfg.IDE.IDEProxyConfig != nil { + annotations = cfg.IDE.IDEProxyConfig.ServiceAnnotations + } + return nil + }) + + ports := map[string]common.ServicePort{ PortName: { ContainerPort: ContainerPort, ServicePort: ServicePort, }, + } + + return common.GenerateService(Component, ports, func(service *corev1.Service) { + for k, v := range annotations { + service.Annotations[k] = v + } })(ctx) } diff --git a/install/installer/pkg/components/openvsx-proxy/service.go b/install/installer/pkg/components/openvsx-proxy/service.go index 6f681955b6036a..cd912fba7c57f4 100644 --- a/install/installer/pkg/components/openvsx-proxy/service.go +++ b/install/installer/pkg/components/openvsx-proxy/service.go @@ -6,12 +6,22 @@ package openvsx_proxy import ( "github.com/gitpod-io/gitpod/installer/pkg/common" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" ) func service(ctx *common.RenderContext) ([]runtime.Object, error) { - return common.GenerateService(Component, map[string]common.ServicePort{ + var annotations map[string]string + _ = ctx.WithExperimental(func(cfg *experimental.Config) error { + if cfg.IDE != nil && cfg.IDE.VSXProxyConfig != nil { + annotations = cfg.IDE.VSXProxyConfig.ServiceAnnotations + } + return nil + }) + + ports := map[string]common.ServicePort{ PortName: { ContainerPort: ContainerPort, ServicePort: ServicePort, @@ -20,5 +30,11 @@ func service(ctx *common.RenderContext) ([]runtime.Object, error) { ContainerPort: PrometheusPort, ServicePort: PrometheusPort, }, + } + + return common.GenerateService(Component, ports, func(service *corev1.Service) { + for k, v := range annotations { + service.Annotations[k] = v + } })(ctx) } diff --git a/install/installer/pkg/config/v1/experimental/experimental.go b/install/installer/pkg/config/v1/experimental/experimental.go index 2b85899798e4d7..d3979c2b1dca6c 100644 --- a/install/installer/pkg/config/v1/experimental/experimental.go +++ b/install/installer/pkg/config/v1/experimental/experimental.go @@ -149,7 +149,17 @@ type PublicAPIConfig struct { type IDEConfig struct { // Disable resolution of latest images and use bundled latest versions instead - ResolveLatest *bool `json:"resolveLatest,omitempty"` + ResolveLatest *bool `json:"resolveLatest,omitempty"` + IDEProxyConfig *IDEProxyConfig `json:"ideProxy,omitempty"` + VSXProxyConfig *VSXProxyConfig `json:"openvsxProxy,omitempty"` +} + +type IDEProxyConfig struct { + ServiceAnnotations map[string]string `json:"serviceAnnotations"` +} + +type VSXProxyConfig struct { + ServiceAnnotations map[string]string `json:"serviceAnnotations"` } type TracingSampleType string From 3455274e20cddc73ff05d57cbfd7161791dcab63 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Thu, 5 May 2022 09:40:06 +0000 Subject: [PATCH 3/3] Add tests for proxy services Ensure that annotations added through the experimental config are present on the rendered services. --- .../pkg/components/ide-proxy/service_test.go | 51 +++++++++++++++++++ .../components/openvsx-proxy/service_test.go | 51 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 install/installer/pkg/components/ide-proxy/service_test.go create mode 100644 install/installer/pkg/components/openvsx-proxy/service_test.go diff --git a/install/installer/pkg/components/ide-proxy/service_test.go b/install/installer/pkg/components/ide-proxy/service_test.go new file mode 100644 index 00000000000000..bca306ae301ba9 --- /dev/null +++ b/install/installer/pkg/components/ide-proxy/service_test.go @@ -0,0 +1,51 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the MIT License. See License-MIT.txt in the project root for license information. + +package ide_proxy + +import ( + "testing" + + "github.com/gitpod-io/gitpod/installer/pkg/common" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental" + "github.com/gitpod-io/gitpod/installer/pkg/config/versions" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" +) + +func TestServiceAnnotations(t *testing.T) { + annotations := map[string]string{"hello": "world"} + + ctx := renderContextWithIDEProxyConfig(t, &experimental.IDEProxyConfig{ServiceAnnotations: annotations}) + + objects, err := service(ctx) + require.NoError(t, err) + + require.Len(t, objects, 1, "must render only one object") + + svc := objects[0].(*corev1.Service) + for k, v := range annotations { + require.Equalf(t, annotations[k], svc.Annotations[k], + "expected to find annotation %q:%q on ide-proxy service, but found %q:%q", k, v, k, svc.Annotations[k]) + } +} + +func renderContextWithIDEProxyConfig(t *testing.T, proxyConfig *experimental.IDEProxyConfig) *common.RenderContext { + ctx, err := common.NewRenderContext(config.Config{ + Experimental: &experimental.Config{ + IDE: &experimental.IDEConfig{ + IDEProxyConfig: proxyConfig, + }, + }, + }, versions.Manifest{ + Components: versions.Components{ + PublicAPIServer: versions.Versioned{ + Version: "commit-test-latest", + }, + }, + }, "test-namespace") + require.NoError(t, err) + + return ctx +} diff --git a/install/installer/pkg/components/openvsx-proxy/service_test.go b/install/installer/pkg/components/openvsx-proxy/service_test.go new file mode 100644 index 00000000000000..403ae62ef22d95 --- /dev/null +++ b/install/installer/pkg/components/openvsx-proxy/service_test.go @@ -0,0 +1,51 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the MIT License. See License-MIT.txt in the project root for license information. + +package openvsx_proxy + +import ( + "testing" + + "github.com/gitpod-io/gitpod/installer/pkg/common" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental" + "github.com/gitpod-io/gitpod/installer/pkg/config/versions" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" +) + +func TestServiceAnnotations(t *testing.T) { + annotations := map[string]string{"hello": "world"} + + ctx := renderContextWithVSXProxyConfig(t, &experimental.VSXProxyConfig{ServiceAnnotations: annotations}) + + objects, err := service(ctx) + require.NoError(t, err) + + require.Len(t, objects, 1, "must render only one object") + + svc := objects[0].(*corev1.Service) + for k, v := range annotations { + require.Equalf(t, annotations[k], svc.Annotations[k], + "expected to find annotation %q:%q on openvsx-proxy service, but found %q:%q", k, v, k, svc.Annotations[k]) + } +} + +func renderContextWithVSXProxyConfig(t *testing.T, proxyConfig *experimental.VSXProxyConfig) *common.RenderContext { + ctx, err := common.NewRenderContext(config.Config{ + Experimental: &experimental.Config{ + IDE: &experimental.IDEConfig{ + VSXProxyConfig: proxyConfig, + }, + }, + }, versions.Manifest{ + Components: versions.Components{ + PublicAPIServer: versions.Versioned{ + Version: "commit-test-latest", + }, + }, + }, "test-namespace") + require.NoError(t, err) + + return ctx +}