From ea64f95726b8648f02b2a43d4b1314a159085a06 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 16:09:23 +0100 Subject: [PATCH 01/19] pkg/platform/aks: add NewConfig function to align with other platforms As a preparation for solving #992. Signed-off-by: Mateusz Gozdek --- pkg/platform/aks/aks.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/platform/aks/aks.go b/pkg/platform/aks/aks.go index f872aee11..15fee295f 100644 --- a/pkg/platform/aks/aks.go +++ b/pkg/platform/aks/aks.go @@ -87,13 +87,18 @@ const ( // init registers AKS as a platform. func init() { //nolint:gochecknoinits - c := &config{ + platform.Register(name, NewConfig()) +} + +// NewConfig returns new AKS platform configuration with default values set. +// +//nolint:golint +func NewConfig() *config { + return &config{ Location: "West Europe", ManageResourceGroup: true, KubernetesVersion: kubernetesVersion, } - - platform.Register(name, c) } // LoadConfig loads configuration values into the config struct from given HCL configuration. From 2bdf16b64979957f7f8c9376fe8a7fc202e90976 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 16:59:37 +0100 Subject: [PATCH 02/19] pkg/platform/aks: export Name const As a preparation for #992. Signed-off-by: Mateusz Gozdek --- pkg/platform/aks/aks.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/platform/aks/aks.go b/pkg/platform/aks/aks.go index 15fee295f..a756c2726 100644 --- a/pkg/platform/aks/aks.go +++ b/pkg/platform/aks/aks.go @@ -74,7 +74,8 @@ type config struct { } const ( - name = "aks" + // Name represents AKS platform name as it should be referenced in function calls and configuration. + Name = "aks" // Environment variables used to load sensitive parts of the configuration. clientIDEnv = "LOKOMOTIVE_AKS_CLIENT_ID" @@ -87,7 +88,7 @@ const ( // init registers AKS as a platform. func init() { //nolint:gochecknoinits - platform.Register(name, NewConfig()) + platform.Register(Name, NewConfig()) } // NewConfig returns new AKS platform configuration with default values set. From 79ad7c7ec55a0f8aa118b6f0fee14dc668667e79 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 17:03:18 +0100 Subject: [PATCH 03/19] pkg/platform/{aws,baremetal,packet,tinkerbell}: add Name const As a preparation to #992, so platform name from the configuration don't need to be duplicated. Signed-off-by: Mateusz Gozdek --- pkg/platform/aws/aws.go | 7 ++++++- pkg/platform/baremetal/baremetal.go | 7 ++++++- pkg/platform/packet/packet.go | 7 ++++++- pkg/platform/tinkerbell/tinkerbell.go | 7 ++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/platform/aws/aws.go b/pkg/platform/aws/aws.go index 3c912ccd6..aae1dfb7e 100644 --- a/pkg/platform/aws/aws.go +++ b/pkg/platform/aws/aws.go @@ -88,9 +88,14 @@ type config struct { KubeAPIServerExtraFlags []string } +const ( + // Name represents AWS platform name as it should be referenced in function calls and configuration. + Name = "aws" +) + // init registers aws as a platform func init() { - platform.Register("aws", NewConfig()) + platform.Register(Name, NewConfig()) } func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { diff --git a/pkg/platform/baremetal/baremetal.go b/pkg/platform/baremetal/baremetal.go index 6d0d49dbe..25db678a7 100644 --- a/pkg/platform/baremetal/baremetal.go +++ b/pkg/platform/baremetal/baremetal.go @@ -60,9 +60,14 @@ type config struct { KubeAPIServerExtraFlags []string } +const ( + // Name represents Bare Metal platform name as it should be referenced in function calls and configuration. + Name = "bare-metal" +) + // init registers bare-metal as a platform func init() { - platform.Register("bare-metal", NewConfig()) + platform.Register(Name, NewConfig()) } func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { diff --git a/pkg/platform/packet/packet.go b/pkg/platform/packet/packet.go index 0b76d0ede..4d5e3ade2 100644 --- a/pkg/platform/packet/packet.go +++ b/pkg/platform/packet/packet.go @@ -105,9 +105,14 @@ type config struct { NodesDependOn []string } +const ( + // Name represents Packet platform name as it should be referenced in function calls and configuration. + Name = "packet" +) + // init registers packet as a platform func init() { - platform.Register("packet", NewConfig()) + platform.Register(Name, NewConfig()) } func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { diff --git a/pkg/platform/tinkerbell/tinkerbell.go b/pkg/platform/tinkerbell/tinkerbell.go index 8297f67fd..727bcfbb8 100644 --- a/pkg/platform/tinkerbell/tinkerbell.go +++ b/pkg/platform/tinkerbell/tinkerbell.go @@ -29,6 +29,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/terraform" ) +const ( + // Name represents Tinkerbell platform name as it should be referenced in function calls and configuration. + Name = "tinkerbell" +) + // Config represents Tinkerbell platform configuration. type Config struct { //nolint:maligned AssetDir string `hcl:"asset_dir"` @@ -91,7 +96,7 @@ func (w *WorkerPool) Name() string { // init registers tinkerbell as a platform. func init() { //nolint:gochecknoinits - platform.Register("tinkerbell", NewConfig()) + platform.Register(Name, NewConfig()) } // LoadConfig loads platform configuration using given HCL structs. From 392d1e9308c63015a427174433140196abe2e624 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 17:07:51 +0100 Subject: [PATCH 04/19] Don't use globals for registering platforms configuration structs This commit moves platforms registration logic from using globals to single function in cli/cmd/cluster package, as this is the only user of old GetPlatforms() function. For platform unit tests, NewConfig() function exported by each platform package should be used instead. Refs #992 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/utils.go | 23 ++++++++++++++++++++++- pkg/platform/aks/aks.go | 5 ----- pkg/platform/aws/aws.go | 5 ----- pkg/platform/baremetal/baremetal.go | 5 ----- pkg/platform/packet/packet.go | 5 ----- pkg/platform/platform.go | 25 ------------------------- pkg/platform/tinkerbell/tinkerbell.go | 5 ----- 7 files changed, 22 insertions(+), 51 deletions(-) diff --git a/cli/cmd/cluster/utils.go b/cli/cmd/cluster/utils.go index 3171e1ff4..747038722 100644 --- a/cli/cmd/cluster/utils.go +++ b/cli/cmd/cluster/utils.go @@ -27,6 +27,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/backend" "github.com/kinvolk/lokomotive/pkg/config" "github.com/kinvolk/lokomotive/pkg/platform" + "github.com/kinvolk/lokomotive/pkg/platform/aks" + "github.com/kinvolk/lokomotive/pkg/platform/aws" + "github.com/kinvolk/lokomotive/pkg/platform/baremetal" + "github.com/kinvolk/lokomotive/pkg/platform/packet" + "github.com/kinvolk/lokomotive/pkg/platform/tinkerbell" ) const ( @@ -54,6 +59,22 @@ func getConfiguredBackend(lokoConfig *config.Config) (backend.Backend, hcl.Diagn return backend, backend.LoadConfig(&lokoConfig.RootConfig.Backend.Config, lokoConfig.EvalContext) } +func getPlatform(name string) (platform.Platform, error) { + platforms := map[string]platform.Platform{ + aks.Name: aks.NewConfig(), + aws.Name: aws.NewConfig(), + packet.Name: packet.NewConfig(), + baremetal.Name: baremetal.NewConfig(), + tinkerbell.Name: tinkerbell.NewConfig(), + } + + if p, ok := platforms[name]; ok { + return p, nil + } + + return nil, fmt.Errorf("platform %q not found", name) +} + // getConfiguredPlatform loads a platform from the given configuration file. func getConfiguredPlatform(lokoConfig *config.Config, require bool) (platform.Platform, hcl.Diagnostics) { if lokoConfig.RootConfig.Cluster == nil && !require { @@ -70,7 +91,7 @@ func getConfiguredPlatform(lokoConfig *config.Config, require bool) (platform.Pl } } - platform, err := platform.GetPlatform(lokoConfig.RootConfig.Cluster.Name) + platform, err := getPlatform(lokoConfig.RootConfig.Cluster.Name) if err != nil { diag := &hcl.Diagnostic{ Severity: hcl.DiagError, diff --git a/pkg/platform/aks/aks.go b/pkg/platform/aks/aks.go index a756c2726..e36c29772 100644 --- a/pkg/platform/aks/aks.go +++ b/pkg/platform/aks/aks.go @@ -86,11 +86,6 @@ const ( kubernetesVersion = "1.18.10" ) -// init registers AKS as a platform. -func init() { //nolint:gochecknoinits - platform.Register(Name, NewConfig()) -} - // NewConfig returns new AKS platform configuration with default values set. // //nolint:golint diff --git a/pkg/platform/aws/aws.go b/pkg/platform/aws/aws.go index aae1dfb7e..89c106829 100644 --- a/pkg/platform/aws/aws.go +++ b/pkg/platform/aws/aws.go @@ -93,11 +93,6 @@ const ( Name = "aws" ) -// init registers aws as a platform -func init() { - platform.Register(Name, NewConfig()) -} - func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { return hcl.Diagnostics{} diff --git a/pkg/platform/baremetal/baremetal.go b/pkg/platform/baremetal/baremetal.go index 25db678a7..d066f45f6 100644 --- a/pkg/platform/baremetal/baremetal.go +++ b/pkg/platform/baremetal/baremetal.go @@ -65,11 +65,6 @@ const ( Name = "bare-metal" ) -// init registers bare-metal as a platform -func init() { - platform.Register(Name, NewConfig()) -} - func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { return hcl.Diagnostics{} diff --git a/pkg/platform/packet/packet.go b/pkg/platform/packet/packet.go index 4d5e3ade2..dd88ae408 100644 --- a/pkg/platform/packet/packet.go +++ b/pkg/platform/packet/packet.go @@ -110,11 +110,6 @@ const ( Name = "packet" ) -// init registers packet as a platform -func init() { - platform.Register(Name, NewConfig()) -} - func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { return hcl.Diagnostics{} diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go index 25e3a67b6..411549d8d 100644 --- a/pkg/platform/platform.go +++ b/pkg/platform/platform.go @@ -118,31 +118,6 @@ type Meta struct { ControlplaneCharts []helm.LokomotiveChart } -// platforms is a collection where all platforms gets automatically registered -var platforms map[string]Platform - -// initialize package's global variable when package is imported -func init() { - platforms = make(map[string]Platform) -} - -// Register adds platform into internal map -func Register(name string, p Platform) { - if _, exists := platforms[name]; exists { - panic(fmt.Sprintf("platform with name %q registered already", name)) - } - platforms[name] = p -} - -// GetPlatform returns platform based on the name -func GetPlatform(name string) (Platform, error) { - platform, exists := platforms[name] - if !exists { - return nil, fmt.Errorf("no platform with name %q found", name) - } - return platform, nil -} - // AppendVersionTag appends the lokoctl-version tag to a given tags map. func AppendVersionTag(tags *map[string]string) { if tags == nil { diff --git a/pkg/platform/tinkerbell/tinkerbell.go b/pkg/platform/tinkerbell/tinkerbell.go index 727bcfbb8..621d82932 100644 --- a/pkg/platform/tinkerbell/tinkerbell.go +++ b/pkg/platform/tinkerbell/tinkerbell.go @@ -94,11 +94,6 @@ func (w *WorkerPool) Name() string { return w.PoolName } -// init registers tinkerbell as a platform. -func init() { //nolint:gochecknoinits - platform.Register(Name, NewConfig()) -} - // LoadConfig loads platform configuration using given HCL structs. func (c *Config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { From 6fd798117c320ab40a220bfb15c51ec8a6862750 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 17:10:02 +0100 Subject: [PATCH 05/19] cli/cmd/cluster: remove unused anonymous imports We no longer rely on the init function call registration method for all platform packages, so anonymous imports are no longer needed. Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/cluster.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cli/cmd/cluster/cluster.go b/cli/cmd/cluster/cluster.go index dcb9c28c9..97a9c7b8c 100644 --- a/cli/cmd/cluster/cluster.go +++ b/cli/cmd/cluster/cluster.go @@ -34,13 +34,6 @@ import ( "github.com/kinvolk/lokomotive/pkg/platform" "github.com/kinvolk/lokomotive/pkg/terraform" - // Register platforms by adding an anonymous import. - _ "github.com/kinvolk/lokomotive/pkg/platform/aks" - _ "github.com/kinvolk/lokomotive/pkg/platform/aws" - _ "github.com/kinvolk/lokomotive/pkg/platform/baremetal" - _ "github.com/kinvolk/lokomotive/pkg/platform/packet" - _ "github.com/kinvolk/lokomotive/pkg/platform/tinkerbell" - // Register backends by adding an anonymous import. _ "github.com/kinvolk/lokomotive/pkg/backend/s3" ) From 633ea1af88fecc07fabdbe99df28a4730cb4780f Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 29 Oct 2020 17:39:24 +0100 Subject: [PATCH 06/19] pkg/components: export component names using Name constant As a preparation for #992. Signed-off-by: Mateusz Gozdek --- .../aws-ebs-csi-driver/component.go | 16 +++++++++----- .../aws-ebs-csi-driver/component_test.go | 6 ++--- pkg/components/cert-manager/component.go | 14 +++++++----- .../cluster-autoscaler/component.go | 16 +++++++++----- .../cluster-autoscaler/component_test.go | 4 ++-- pkg/components/contour/component.go | 13 ++++++----- pkg/components/contour/component_test.go | 2 +- pkg/components/dex/component.go | 16 +++++++++----- pkg/components/external-dns/component.go | 22 +++++++++++-------- pkg/components/external-dns/component_test.go | 12 +++++----- .../component.go | 14 +++++++----- .../component_test.go | 2 +- pkg/components/gangway/component.go | 16 +++++++++----- pkg/components/httpbin/component.go | 16 +++++++++----- pkg/components/inspektor-gadget/component.go | 14 +++++++----- pkg/components/istio-operator/component.go | 11 ++++++---- .../istio-operator/component_test.go | 4 ++-- pkg/components/linkerd/component.go | 9 +++++--- pkg/components/metallb/component.go | 10 ++++++--- pkg/components/metallb/component_test.go | 2 +- pkg/components/metrics-server/component.go | 14 +++++++----- .../metrics-server/component_test.go | 2 +- pkg/components/openebs-operator/component.go | 14 +++++++----- .../openebs-storage-class/component.go | 13 ++++++----- .../openebs-storage-class/component_test.go | 2 +- .../prometheus-operator/component.go | 14 +++++++----- .../prometheus-operator/component_test.go | 4 ++-- pkg/components/rook-ceph/component.go | 10 ++++++--- pkg/components/rook-ceph/component_test.go | 2 +- pkg/components/rook/component.go | 14 +++++++----- pkg/components/velero/component.go | 14 +++++++----- pkg/components/velero/component_test.go | 10 ++++----- pkg/components/web-ui/component.go | 14 +++++++----- 33 files changed, 214 insertions(+), 132 deletions(-) diff --git a/pkg/components/aws-ebs-csi-driver/component.go b/pkg/components/aws-ebs-csi-driver/component.go index 9dc4ee22b..d9b4ddf0a 100644 --- a/pkg/components/aws-ebs-csi-driver/component.go +++ b/pkg/components/aws-ebs-csi-driver/component.go @@ -26,15 +26,19 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "aws-ebs-csi-driver" +const ( + // Name represents AWS EBS CSI driver component name as it should be referenced in function calls + // and in configuration. + Name = "aws-ebs-csi-driver" -const chartValuesTmpl = ` + chartValuesTmpl = ` enableDefaultStorageClass: {{ .EnableDefaultStorageClass }} ` +) //nolint:gochecknoinits func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -58,7 +62,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex // RenderManifests renders the Helm chart templates with values provided. func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -68,7 +72,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template failed: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -78,7 +82,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: "kube-system", }, diff --git a/pkg/components/aws-ebs-csi-driver/component_test.go b/pkg/components/aws-ebs-csi-driver/component_test.go index 608a30fcb..7c1362260 100644 --- a/pkg/components/aws-ebs-csi-driver/component_test.go +++ b/pkg/components/aws-ebs-csi-driver/component_test.go @@ -28,7 +28,7 @@ func TestStorageClassEmptyConfig(t *testing.T) { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -67,7 +67,7 @@ func TestStorageClassEnabled(t *testing.T) { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -106,7 +106,7 @@ func TestStorageClassDisabled(t *testing.T) { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/cert-manager/component.go b/pkg/components/cert-manager/component.go index b1974da75..5adb6bcdb 100644 --- a/pkg/components/cert-manager/component.go +++ b/pkg/components/cert-manager/component.go @@ -26,10 +26,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "cert-manager" +const ( + // Name represents cert-manager component name as it should be referenced in function calls + // and in configuration. + Name = "cert-manager" +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -75,7 +79,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -85,7 +89,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Namespace, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Namespace, values) if err != nil { return nil, fmt.Errorf("rendering chart: %w", err) } @@ -95,7 +99,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, Labels: map[string]string{ diff --git a/pkg/components/cluster-autoscaler/component.go b/pkg/components/cluster-autoscaler/component.go index 505bec7bc..debc75f00 100644 --- a/pkg/components/cluster-autoscaler/component.go +++ b/pkg/components/cluster-autoscaler/component.go @@ -31,9 +31,12 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "cluster-autoscaler" +const ( + // Name represents Cluster Autoscaler component name as it should be referenced in function calls + // and in configuration. + Name = "cluster-autoscaler" -const chartValuesTmpl = ` + chartValuesTmpl = ` cloudProvider: {{ .Provider }} nodeSelector: node.kubernetes.io/controller: "true" @@ -73,9 +76,10 @@ serviceMonitor: release: prometheus-operator {{ end }} ` +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -346,7 +350,7 @@ func (c *component) validatePacket(diagnostics hcl.Diagnostics) hcl.Diagnostics } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -376,12 +380,12 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template: %w", err) } - return util.RenderChart(helmChart, name, c.Namespace, values) + return util.RenderChart(helmChart, Name, c.Namespace, values) } func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/cluster-autoscaler/component_test.go b/pkg/components/cluster-autoscaler/component_test.go index d05e3a697..caa7a05cc 100644 --- a/pkg/components/cluster-autoscaler/component_test.go +++ b/pkg/components/cluster-autoscaler/component_test.go @@ -40,7 +40,7 @@ func TestEmptyBody(t *testing.T) { config := `component "cluster-autoscaler" {}` - body, diagnostics := util.GetComponentBody(config, name) + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -66,7 +66,7 @@ func TestRender(t *testing.T) { } ` - body, diagnostics := util.GetComponentBody(config, name) + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/contour/component.go b/pkg/components/contour/component.go index 41feb6818..d2365b3da 100644 --- a/pkg/components/contour/component.go +++ b/pkg/components/contour/component.go @@ -27,13 +27,16 @@ import ( ) const ( - name = "contour" + // Name represents Contour component name as it should be referenced in function calls + // and in configuration. + Name = "contour" + serviceTypeNodePort = "NodePort" serviceTypeLoadBalancer = "LoadBalancer" ) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } // This annotation is added to Envoy service. @@ -79,7 +82,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -100,7 +103,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the Contour deployment. - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -110,7 +113,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: "projectcontour", }, diff --git a/pkg/components/contour/component_test.go b/pkg/components/contour/component_test.go index dde3d6217..3bfd85450 100644 --- a/pkg/components/contour/component_test.go +++ b/pkg/components/contour/component_test.go @@ -64,7 +64,7 @@ component "contour" { } for _, tc := range tests { - b, d := util.GetComponentBody(tc.hcl, name) + b, d := util.GetComponentBody(tc.hcl, Name) if d != nil { t.Errorf("%s - Error getting component body: %v", tc.desc, d) } diff --git a/pkg/components/dex/component.go b/pkg/components/dex/component.go index 4607b8d05..9bddbf7c7 100644 --- a/pkg/components/dex/component.go +++ b/pkg/components/dex/component.go @@ -27,10 +27,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "dex" +const ( + // Name represents Dex component name as it should be referenced in function calls + // and in configuration. + Name = "dex" +) func init() { //nolint:gochecknoinits - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type org struct { @@ -105,7 +109,7 @@ func marshalToStr(obj interface{}) (string, error) { } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -141,7 +145,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the dex deployment. - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -151,9 +155,9 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ - Name: name, + Name: Name, }, } } diff --git a/pkg/components/external-dns/component.go b/pkg/components/external-dns/component.go index 36a9b5616..7213dbb74 100644 --- a/pkg/components/external-dns/component.go +++ b/pkg/components/external-dns/component.go @@ -27,11 +27,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "external-dns" - -// TODO Currently supporting only AWS Route53. Provide better conditional templates -// when multiple provider support is added. -const chartValuesTmpl = ` +const ( + // Name represents ExternalDNS component name as it should be referenced in function calls + // and in configuration. + Name = "external-dns" + + // TODO Currently supporting only AWS Route53. Provide better conditional templates + // when multiple provider support is added. + chartValuesTmpl = ` provider: aws {{- if .Sources }} sources: @@ -60,9 +63,10 @@ metrics: release: prometheus-operator {{ end }} ` +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } // AwsConfig provides configuration for AWS Route53 DNS. @@ -108,7 +112,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex // RenderManifests renders the helm chart templates with values provided. func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -135,7 +139,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Namespace, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Namespace, values) if err != nil { return nil, fmt.Errorf("rendering chart: %w", err) } @@ -145,7 +149,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/external-dns/component_test.go b/pkg/components/external-dns/component_test.go index 9b71a4ef1..433705b40 100644 --- a/pkg/components/external-dns/component_test.go +++ b/pkg/components/external-dns/component_test.go @@ -35,7 +35,7 @@ func TestEmptyConfig(t *testing.T) { func TestEmptyBody(t *testing.T) { c := newComponent() config := `component "external-dns" {}` - body, diagnostics := util.GetComponentBody(config, name) + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -81,7 +81,7 @@ func TestAwsConfigWithoutProvidingCredentials(t *testing.T) { os.Unsetenv("AWS_ACCESS_KEY_ID") os.Unsetenv("AWS_SECRET_ACCESS_KEY") - body, diagnostics := util.GetComponentBody(config, name) + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -114,7 +114,8 @@ func TestAwsConfigBySettingEnvVariables(t *testing.T) { if err := os.Setenv("AWS_SECRET_ACCESS_KEY", "TESTSECRETACCESSKEY"); err != nil { t.Fatalf("Error setting env variable: %s", err) } - body, diagnostics := util.GetComponentBody(config, name) + + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -153,7 +154,8 @@ func TestAwsConfigBySettingEmptyEnvVariables(t *testing.T) { if err != nil { t.Fatalf("Error setting env variable: %s", err) } - body, diagnostics := util.GetComponentBody(config, name) + + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -182,7 +184,7 @@ func TestAwsConfigBySettingConfigFields(t *testing.T) { } } ` - body, diagnostics := util.GetComponentBody(config, name) + body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/flatcar-linux-update-operator/component.go b/pkg/components/flatcar-linux-update-operator/component.go index cef740285..705ed2c18 100644 --- a/pkg/components/flatcar-linux-update-operator/component.go +++ b/pkg/components/flatcar-linux-update-operator/component.go @@ -25,10 +25,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "flatcar-linux-update-operator" +const ( + // Name represents Flatcar Linux Update Operator component name as it should be referenced in function calls + // and in configuration. + Name = "flatcar-linux-update-operator" +) func init() { - components.Register(name, &component{}) + components.Register(Name, &component{}) } type component struct{} @@ -42,17 +46,17 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("loading chart from assets: %w", err) } - return util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, "") + return util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, "") } func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: "reboot-coordinator", }, diff --git a/pkg/components/flatcar-linux-update-operator/component_test.go b/pkg/components/flatcar-linux-update-operator/component_test.go index 6d80540ea..675c88959 100644 --- a/pkg/components/flatcar-linux-update-operator/component_test.go +++ b/pkg/components/flatcar-linux-update-operator/component_test.go @@ -29,7 +29,7 @@ component "flatcar-linux-update-operator" {} component := &component{} - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/gangway/component.go b/pkg/components/gangway/component.go index 2071ebda9..a576577cf 100644 --- a/pkg/components/gangway/component.go +++ b/pkg/components/gangway/component.go @@ -26,10 +26,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "gangway" +const ( + // Name represents Gangway component name as it should be referenced in function calls + // and in configuration. + Name = "gangway" +) func init() { //nolint:gochecknoinits - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -62,7 +66,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -73,7 +77,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the gangway deployment. - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -83,9 +87,9 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ - Name: name, + Name: Name, }, } } diff --git a/pkg/components/httpbin/component.go b/pkg/components/httpbin/component.go index 2d2bd5f9d..97b41d803 100644 --- a/pkg/components/httpbin/component.go +++ b/pkg/components/httpbin/component.go @@ -26,10 +26,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "httpbin" +const ( + // Name represents httpbin component name as it should be referenced in function calls + // and in configuration. + Name = "httpbin" +) func init() { //nolint:gochecknoinits - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -54,7 +58,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -65,7 +69,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the httpbin deployment. - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -75,9 +79,9 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ - Name: name, + Name: Name, }, } } diff --git a/pkg/components/inspektor-gadget/component.go b/pkg/components/inspektor-gadget/component.go index 30e48e82e..a064c7bcf 100644 --- a/pkg/components/inspektor-gadget/component.go +++ b/pkg/components/inspektor-gadget/component.go @@ -26,11 +26,15 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "inspektor-gadget" +const ( + // Name represents Inspektor Gadget component name as it should be referenced in function calls + // and in configuration. + Name = "inspektor-gadget" +) //nolint:gochecknoinits func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -58,7 +62,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex // RenderManifests renders the Helm chart templates with values provided. func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -68,7 +72,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template failed: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -78,7 +82,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/istio-operator/component.go b/pkg/components/istio-operator/component.go index f6a69bbc6..13c772488 100644 --- a/pkg/components/istio-operator/component.go +++ b/pkg/components/istio-operator/component.go @@ -27,13 +27,16 @@ import ( ) const ( - name = "experimental-istio-operator" + // Name represents Istio Operator component name as it should be referenced in function calls + // and in configuration. + Name = "experimental-istio-operator" + namespace = "istio-operator" ) //nolint:gochecknoinits func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -75,7 +78,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the istio deployment. - renderedFiles, err := util.RenderChart(helmChart, name, namespace, values) + renderedFiles, err := util.RenderChart(helmChart, Name, namespace, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -85,7 +88,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: namespace, Labels: map[string]string{ diff --git a/pkg/components/istio-operator/component_test.go b/pkg/components/istio-operator/component_test.go index 45f4efa98..f22265ef3 100644 --- a/pkg/components/istio-operator/component_test.go +++ b/pkg/components/istio-operator/component_test.go @@ -52,7 +52,7 @@ func TestConversion(t *testing.T) { t.Parallel() component := newComponent() - m := testutil.RenderManifests(t, component, name, tc.inputConfig) + m := testutil.RenderManifests(t, component, Name, tc.inputConfig) gotConfig := testutil.ConfigFromMap(t, m, tc.expectedManifestName) testutil.MatchJSONPathStringValue(t, gotConfig, tc.jsonPath, tc.expected) @@ -66,6 +66,6 @@ func TestVerifyServiceMonitor(t *testing.T) { }` component := newComponent() - m := testutil.RenderManifests(t, component, name, inputConfig) + m := testutil.RenderManifests(t, component, Name, inputConfig) testutil.ConfigFromMap(t, m, "istio-operator/templates/service-monitor.yaml") } diff --git a/pkg/components/linkerd/component.go b/pkg/components/linkerd/component.go index 5b6e04055..bb77e60ff 100644 --- a/pkg/components/linkerd/component.go +++ b/pkg/components/linkerd/component.go @@ -31,7 +31,10 @@ import ( ) const ( - name = "experimental-linkerd" + // Name represents Linkerd component name as it should be referenced in function calls + // and in configuration. + Name = "experimental-linkerd" + certCommonName = "identity.linkerd.cluster.local" ) @@ -108,7 +111,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the Linkerd deployment. - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -118,7 +121,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: "linkerd", Annotations: map[string]string{ diff --git a/pkg/components/metallb/component.go b/pkg/components/metallb/component.go index 5e7ebb14b..b7d80e7de 100644 --- a/pkg/components/metallb/component.go +++ b/pkg/components/metallb/component.go @@ -26,10 +26,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "metallb" +const ( + // Name represents MetalLB component name as it should be referenced in function calls + // and in configuration. + Name = "metallb" +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -138,7 +142,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: "metallb-system", }, diff --git a/pkg/components/metallb/component_test.go b/pkg/components/metallb/component_test.go index b898b92a3..165c93c00 100644 --- a/pkg/components/metallb/component_test.go +++ b/pkg/components/metallb/component_test.go @@ -39,7 +39,7 @@ func TestEmptyConfig(t *testing.T) { func renderManifest(t *testing.T, configHCL string) map[string]string { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/metrics-server/component.go b/pkg/components/metrics-server/component.go index 596da5576..e6632e6bb 100644 --- a/pkg/components/metrics-server/component.go +++ b/pkg/components/metrics-server/component.go @@ -26,7 +26,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "metrics-server" +const ( + // Name represents metrics-server component name as it should be referenced in function calls + // and in configuration. + Name = "metrics-server" +) // * --kubelet-preferred-address-types=InternalIP to be able to properly the kubelet. // I am not sure why this option is needed, but tried the alternatives @@ -49,7 +53,7 @@ args: ` func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -71,7 +75,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -81,7 +85,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Namespace, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Namespace, values) if err != nil { return nil, fmt.Errorf("rendering chart: %w", err) } @@ -91,7 +95,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/metrics-server/component_test.go b/pkg/components/metrics-server/component_test.go index d8670c812..79774cdda 100644 --- a/pkg/components/metrics-server/component_test.go +++ b/pkg/components/metrics-server/component_test.go @@ -39,7 +39,7 @@ component "metrics-server" {} component := &component{} - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/openebs-operator/component.go b/pkg/components/openebs-operator/component.go index 7c1a00678..421d16e3e 100644 --- a/pkg/components/openebs-operator/component.go +++ b/pkg/components/openebs-operator/component.go @@ -26,10 +26,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "openebs-operator" +const ( + // Name represents OpenEBS Operator component name as it should be referenced in function calls + // and in configuration. + Name = "openebs-operator" +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -102,7 +106,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -112,7 +116,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("render chart values template: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("render chart: %w", err) } @@ -122,7 +126,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: "openebs", }, diff --git a/pkg/components/openebs-storage-class/component.go b/pkg/components/openebs-storage-class/component.go index e4aab70af..0bab6d9a8 100644 --- a/pkg/components/openebs-storage-class/component.go +++ b/pkg/components/openebs-storage-class/component.go @@ -27,12 +27,15 @@ import ( ) const ( - name = "openebs-storage-class" + // Name represents OpenEBS Storage Class component name as it should be referenced in function calls + // and in configuration. + Name = "openebs-storage-class" + poolName = "openebs-storage-pool" ) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type Storageclass struct { @@ -107,7 +110,7 @@ func (c *component) validateConfig() error { // TODO: Convert to Helm chart. func (c *component) RenderManifests() (map[string]string, error) { - scTmpl, err := template.New(name).Parse(storageClassTmpl) + scTmpl, err := template.New(Name).Parse(storageClassTmpl) if err != nil { return nil, fmt.Errorf("parsing storage class template: %w", err) } @@ -127,7 +130,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("executing storage class %q template: %w", sc.Name, err) } - filename := fmt.Sprintf("%s-%s.yml", name, sc.Name) + filename := fmt.Sprintf("%s-%s.yml", Name, sc.Name) manifestsMap[filename] = scBuffer.String() if err := spTmpl.Execute(&spBuffer, sc); err != nil { @@ -143,7 +146,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, // Return the same namespace which the openebs-operator component is using. Namespace: k8sutil.Namespace{ Name: "openebs", diff --git a/pkg/components/openebs-storage-class/component_test.go b/pkg/components/openebs-storage-class/component_test.go index 613fd0448..406ad3951 100644 --- a/pkg/components/openebs-storage-class/component_test.go +++ b/pkg/components/openebs-storage-class/component_test.go @@ -70,7 +70,7 @@ func TestUserInputValues(t *testing.T) { func testRenderManifest(t *testing.T, configHCL string) { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/prometheus-operator/component.go b/pkg/components/prometheus-operator/component.go index 2989a16f7..3c9fc1128 100644 --- a/pkg/components/prometheus-operator/component.go +++ b/pkg/components/prometheus-operator/component.go @@ -28,10 +28,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "prometheus-operator" +const ( + // Name represents Prometheus Operator component name as it should be referenced in function calls + // and in configuration. + Name = "prometheus-operator" +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } // Monitor holds information about which Kubernetes components should be monitored with the default Prometheus instance. @@ -192,7 +196,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -202,7 +206,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Namespace, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Namespace, values) if err != nil { return nil, fmt.Errorf("rendering chart: %w", err) } @@ -212,7 +216,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/prometheus-operator/component_test.go b/pkg/components/prometheus-operator/component_test.go index 575e9ab96..53f7b51ea 100644 --- a/pkg/components/prometheus-operator/component_test.go +++ b/pkg/components/prometheus-operator/component_test.go @@ -96,7 +96,7 @@ component "prometheus-operator" { for _, tc := range tests { tc := tc t.Run(tc.desc, func(t *testing.T) { - b, d := util.GetComponentBody(tc.hcl, name) + b, d := util.GetComponentBody(tc.hcl, Name) if d != nil { t.Fatalf("error getting component body: %v", d) } @@ -209,7 +209,7 @@ providers: t.Parallel() component := newComponent() - m := testutil.RenderManifests(t, component, name, tc.inputConfig) + m := testutil.RenderManifests(t, component, Name, tc.inputConfig) gotConfig := testutil.ConfigFromMap(t, m, tc.expectedManifestName) testutil.MatchJSONPathStringValue(t, gotConfig, tc.jsonPath, tc.expected) diff --git a/pkg/components/rook-ceph/component.go b/pkg/components/rook-ceph/component.go index e407b15fc..7b1b5c884 100644 --- a/pkg/components/rook-ceph/component.go +++ b/pkg/components/rook-ceph/component.go @@ -26,10 +26,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "rook-ceph" +const ( + // Name represents Rook Ceph component name as it should be referenced in function calls + // and in configuration. + Name = "rook-ceph" +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -91,7 +95,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/rook-ceph/component_test.go b/pkg/components/rook-ceph/component_test.go index 0a8bb938e..cb46b44a9 100644 --- a/pkg/components/rook-ceph/component_test.go +++ b/pkg/components/rook-ceph/component_test.go @@ -64,7 +64,7 @@ component "rook-ceph" { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } diff --git a/pkg/components/rook/component.go b/pkg/components/rook/component.go index e6e536788..adc7c1f7d 100644 --- a/pkg/components/rook/component.go +++ b/pkg/components/rook/component.go @@ -27,10 +27,14 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "rook" +const ( + // Name represents Rook component name as it should be referenced in function calls + // and in configuration. + Name = "rook" +) func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type component struct { @@ -66,7 +70,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex } func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -95,7 +99,7 @@ func (c *component) RenderManifests() (map[string]string, error) { } // Generate YAML for the Rook operator deployment. - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart failed: %w", err) } @@ -105,7 +109,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/velero/component.go b/pkg/components/velero/component.go index 7cee0849a..e198539a1 100644 --- a/pkg/components/velero/component.go +++ b/pkg/components/velero/component.go @@ -30,11 +30,15 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "velero" +const ( + // Name represents Velero component name as it should be referenced in function calls + // and in configuration. + Name = "velero" +) // init registers velero component to components list, so it shows up as available to install func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } // component represents component configuration data @@ -123,7 +127,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex // RenderManifest read helm chart from assets and renders it into list of files func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -133,7 +137,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("getting values: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Namespace, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Namespace, values) if err != nil { return nil, fmt.Errorf("rendering chart: %w", err) } @@ -205,7 +209,7 @@ func (c *component) getProvider() (provider, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, diff --git a/pkg/components/velero/component_test.go b/pkg/components/velero/component_test.go index 4fb11122a..5d695e11c 100644 --- a/pkg/components/velero/component_test.go +++ b/pkg/components/velero/component_test.go @@ -56,7 +56,7 @@ component "velero" { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -101,7 +101,7 @@ component "velero" { component := newComponent() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -132,7 +132,7 @@ component "velero" { component := newComponent() - body, d := util.GetComponentBody(configHCL, name) + body, d := util.GetComponentBody(configHCL, Name) if d != nil { t.Fatalf("Error getting component body: %v", d) } @@ -149,7 +149,7 @@ component "velero" {} component := newComponent() - body, d := util.GetComponentBody(configHCL, name) + body, d := util.GetComponentBody(configHCL, Name) if d != nil { t.Fatalf("Error getting component body: %v", d) } @@ -176,7 +176,7 @@ component "velero" { component := newComponent() - body, d := util.GetComponentBody(configHCL, name) + body, d := util.GetComponentBody(configHCL, Name) if d != nil { t.Fatalf("Error getting component body: %v", d) } diff --git a/pkg/components/web-ui/component.go b/pkg/components/web-ui/component.go index 32abdb3fa..9faeedfb7 100644 --- a/pkg/components/web-ui/component.go +++ b/pkg/components/web-ui/component.go @@ -27,11 +27,15 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "web-ui" +const ( + // Name represents Web UI component name as it should be referenced in function calls + // and in configuration. + Name = "web-ui" +) //nolint:gochecknoinits func init() { - components.Register(name, newComponent()) + components.Register(Name, newComponent()) } type oidc struct { @@ -73,7 +77,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex // RenderManifests renders the Helm chart templates with values provided. func (c *component) RenderManifests() (map[string]string, error) { - helmChart, err := components.Chart(name) + helmChart, err := components.Chart(Name) if err != nil { return nil, fmt.Errorf("retrieving chart from assets: %w", err) } @@ -83,7 +87,7 @@ func (c *component) RenderManifests() (map[string]string, error) { return nil, fmt.Errorf("rendering chart values template: %w", err) } - renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + renderedFiles, err := util.RenderChart(helmChart, Name, c.Metadata().Namespace.Name, values) if err != nil { return nil, fmt.Errorf("rendering chart: %w", err) } @@ -93,7 +97,7 @@ func (c *component) RenderManifests() (map[string]string, error) { func (c *component) Metadata() components.Metadata { return components.Metadata{ - Name: name, + Name: Name, Namespace: k8sutil.Namespace{ Name: c.Namespace, }, From 0988eb5d663c4d396d145d8911cb0e229d2bd4e6 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 17:11:53 +0100 Subject: [PATCH 07/19] pkg/components: rename newComponent() functions to NewConfig() To make it consistent with pkg/platform/* packages and to make it available outside of package, so we can avoid using globals and init() functions for collecting available components default configurations. Refs #992 Signed-off-by: Mateusz Gozdek --- pkg/components/aws-ebs-csi-driver/component.go | 7 +++++-- .../aws-ebs-csi-driver/component_test.go | 6 +++--- pkg/components/cert-manager/component.go | 7 +++++-- pkg/components/cluster-autoscaler/component.go | 7 +++++-- .../cluster-autoscaler/component_test.go | 6 +++--- pkg/components/contour/component.go | 7 +++++-- pkg/components/contour/component_test.go | 2 +- pkg/components/dex/component.go | 7 +++++-- pkg/components/external-dns/component.go | 7 +++++-- pkg/components/external-dns/component_test.go | 14 +++++++------- pkg/components/gangway/component.go | 7 +++++-- pkg/components/httpbin/component.go | 7 +++++-- pkg/components/inspektor-gadget/component.go | 7 +++++-- pkg/components/istio-operator/component.go | 7 +++++-- pkg/components/istio-operator/component_test.go | 4 ++-- pkg/components/linkerd/component.go | 7 +++++-- pkg/components/metallb/component.go | 7 +++++-- pkg/components/metallb/component_test.go | 4 ++-- pkg/components/metrics-server/component.go | 7 +++++-- pkg/components/metrics-server/component_test.go | 2 +- pkg/components/openebs-operator/component.go | 7 +++++-- pkg/components/openebs-storage-class/component.go | 7 +++++-- .../openebs-storage-class/component_test.go | 4 ++-- pkg/components/prometheus-operator/component.go | 7 +++++-- .../prometheus-operator/component_test.go | 4 ++-- pkg/components/rook-ceph/component.go | 7 +++++-- pkg/components/rook-ceph/component_test.go | 4 ++-- pkg/components/rook/component.go | 7 +++++-- pkg/components/velero/azure/azure.go | 2 +- pkg/components/velero/component.go | 8 +++++--- pkg/components/velero/component_test.go | 12 ++++++------ pkg/components/web-ui/component.go | 7 +++++-- 32 files changed, 132 insertions(+), 73 deletions(-) diff --git a/pkg/components/aws-ebs-csi-driver/component.go b/pkg/components/aws-ebs-csi-driver/component.go index d9b4ddf0a..76380d2a2 100644 --- a/pkg/components/aws-ebs-csi-driver/component.go +++ b/pkg/components/aws-ebs-csi-driver/component.go @@ -38,14 +38,17 @@ enableDefaultStorageClass: {{ .EnableDefaultStorageClass }} //nolint:gochecknoinits func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { EnableDefaultStorageClass bool `hcl:"enable_default_storage_class,optional"` } -func newComponent() *component { +// NewConfig returns new AWS EBS CSI driver component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ EnableDefaultStorageClass: true, } diff --git a/pkg/components/aws-ebs-csi-driver/component_test.go b/pkg/components/aws-ebs-csi-driver/component_test.go index 7c1362260..f090dce37 100644 --- a/pkg/components/aws-ebs-csi-driver/component_test.go +++ b/pkg/components/aws-ebs-csi-driver/component_test.go @@ -26,7 +26,7 @@ import ( func TestStorageClassEmptyConfig(t *testing.T) { configHCL := `component "aws-ebs-csi-driver" {}` - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { @@ -65,7 +65,7 @@ func TestStorageClassEnabled(t *testing.T) { enable_default_storage_class = true }` - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { @@ -104,7 +104,7 @@ func TestStorageClassDisabled(t *testing.T) { enable_default_storage_class = false }` - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { diff --git a/pkg/components/cert-manager/component.go b/pkg/components/cert-manager/component.go index 5adb6bcdb..cca8d4237 100644 --- a/pkg/components/cert-manager/component.go +++ b/pkg/components/cert-manager/component.go @@ -33,7 +33,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -43,7 +43,10 @@ type component struct { ServiceMonitor bool `hcl:"service_monitor,optional"` } -func newComponent() *component { +// NewConfig returns new cert-manager component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "cert-manager", Webhooks: true, diff --git a/pkg/components/cluster-autoscaler/component.go b/pkg/components/cluster-autoscaler/component.go index debc75f00..c0c35ae30 100644 --- a/pkg/components/cluster-autoscaler/component.go +++ b/pkg/components/cluster-autoscaler/component.go @@ -79,7 +79,7 @@ serviceMonitor: ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -118,7 +118,10 @@ type packetConfiguration struct { AuthToken string } -func newComponent() *component { +// NewConfig returns new Cluster Autoscaler component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { c := &component{ Provider: "packet", Namespace: "kube-system", diff --git a/pkg/components/cluster-autoscaler/component_test.go b/pkg/components/cluster-autoscaler/component_test.go index caa7a05cc..779297421 100644 --- a/pkg/components/cluster-autoscaler/component_test.go +++ b/pkg/components/cluster-autoscaler/component_test.go @@ -23,7 +23,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() @@ -36,7 +36,7 @@ func TestEmptyConfig(t *testing.T) { } func TestEmptyBody(t *testing.T) { - c := newComponent() + c := NewConfig() config := `component "cluster-autoscaler" {}` @@ -51,7 +51,7 @@ func TestEmptyBody(t *testing.T) { } func TestRender(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "cluster-autoscaler" { diff --git a/pkg/components/contour/component.go b/pkg/components/contour/component.go index d2365b3da..652cf3a32 100644 --- a/pkg/components/contour/component.go +++ b/pkg/components/contour/component.go @@ -36,7 +36,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } // This annotation is added to Envoy service. @@ -49,7 +49,10 @@ type component struct { TolerationsRaw string } -func newComponent() *component { +// NewConfig returns new Contour component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ ServiceType: serviceTypeLoadBalancer, } diff --git a/pkg/components/contour/component_test.go b/pkg/components/contour/component_test.go index 3bfd85450..a1ce8ecae 100644 --- a/pkg/components/contour/component_test.go +++ b/pkg/components/contour/component_test.go @@ -69,7 +69,7 @@ component "contour" { t.Errorf("%s - Error getting component body: %v", tc.desc, d) } - c := newComponent() + c := NewConfig() d = c.LoadConfig(b, nil) if !tc.wantErr && d.HasErrors() { diff --git a/pkg/components/dex/component.go b/pkg/components/dex/component.go index 9bddbf7c7..9d91e9a8e 100644 --- a/pkg/components/dex/component.go +++ b/pkg/components/dex/component.go @@ -34,7 +34,7 @@ const ( ) func init() { //nolint:gochecknoinits - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type org struct { @@ -81,7 +81,10 @@ type component struct { StaticClientsRaw string } -func newComponent() *component { +// NewConfig returns new Dex component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ CertManagerClusterIssuer: "letsencrypt-production", } diff --git a/pkg/components/external-dns/component.go b/pkg/components/external-dns/component.go index 7213dbb74..60e279e76 100644 --- a/pkg/components/external-dns/component.go +++ b/pkg/components/external-dns/component.go @@ -66,7 +66,7 @@ metrics: ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } // AwsConfig provides configuration for AWS Route53 DNS. @@ -88,7 +88,10 @@ type component struct { OwnerID string `hcl:"owner_id"` } -func newComponent() *component { +// NewConfig returns new ExternalDNS component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "external-dns", Sources: []string{"ingress"}, diff --git a/pkg/components/external-dns/component_test.go b/pkg/components/external-dns/component_test.go index 433705b40..66e299c4d 100644 --- a/pkg/components/external-dns/component_test.go +++ b/pkg/components/external-dns/component_test.go @@ -23,7 +23,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() evalContext := hcl.EvalContext{} diagnostics := c.LoadConfig(&emptyConfig, &evalContext) @@ -33,7 +33,7 @@ func TestEmptyConfig(t *testing.T) { } func TestEmptyBody(t *testing.T) { - c := newComponent() + c := NewConfig() config := `component "external-dns" {}` body, diagnostics := util.GetComponentBody(config, Name) if diagnostics != nil { @@ -44,7 +44,7 @@ func TestEmptyBody(t *testing.T) { } } func TestDefaultValues(t *testing.T) { - c := newComponent() + c := NewConfig() if c.Namespace != "external-dns" { t.Fatal("Default namespace for installation should be external-dns.") } @@ -64,7 +64,7 @@ func TestDefaultValues(t *testing.T) { } func TestAwsConfigWithoutProvidingCredentials(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] @@ -94,7 +94,7 @@ func TestAwsConfigWithoutProvidingCredentials(t *testing.T) { } func TestAwsConfigBySettingEnvVariables(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] @@ -132,7 +132,7 @@ func TestAwsConfigBySettingEnvVariables(t *testing.T) { } func TestAwsConfigBySettingEmptyEnvVariables(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] @@ -169,7 +169,7 @@ func TestAwsConfigBySettingEmptyEnvVariables(t *testing.T) { } func TestAwsConfigBySettingConfigFields(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] diff --git a/pkg/components/gangway/component.go b/pkg/components/gangway/component.go index a576577cf..d16c67c9f 100644 --- a/pkg/components/gangway/component.go +++ b/pkg/components/gangway/component.go @@ -33,7 +33,7 @@ const ( ) func init() { //nolint:gochecknoinits - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -49,7 +49,10 @@ type component struct { CertManagerClusterIssuer string `hcl:"certmanager_cluster_issuer,optional"` } -func newComponent() *component { +// NewConfig returns new Gangway component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ CertManagerClusterIssuer: "letsencrypt-production", } diff --git a/pkg/components/httpbin/component.go b/pkg/components/httpbin/component.go index 97b41d803..d17762fca 100644 --- a/pkg/components/httpbin/component.go +++ b/pkg/components/httpbin/component.go @@ -33,7 +33,7 @@ const ( ) func init() { //nolint:gochecknoinits - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -41,7 +41,10 @@ type component struct { CertManagerClusterIssuer string `hcl:"certmanager_cluster_issuer,optional"` } -func newComponent() *component { +// NewConfig returns new httpbin component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ CertManagerClusterIssuer: "letsencrypt-production", } diff --git a/pkg/components/inspektor-gadget/component.go b/pkg/components/inspektor-gadget/component.go index a064c7bcf..c0916ef75 100644 --- a/pkg/components/inspektor-gadget/component.go +++ b/pkg/components/inspektor-gadget/component.go @@ -34,7 +34,7 @@ const ( //nolint:gochecknoinits func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -42,7 +42,10 @@ type component struct { EnableTraceloop bool `hcl:"enable_traceloop,optional"` } -func newComponent() *component { +// NewConfig returns new Inspektor Gadget component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "kube-system", EnableTraceloop: true, diff --git a/pkg/components/istio-operator/component.go b/pkg/components/istio-operator/component.go index 13c772488..39aba507e 100644 --- a/pkg/components/istio-operator/component.go +++ b/pkg/components/istio-operator/component.go @@ -36,7 +36,7 @@ const ( //nolint:gochecknoinits func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -44,7 +44,10 @@ type component struct { EnableMonitoring bool `hcl:"enable_monitoring,optional"` } -func newComponent() *component { +// NewConfig returns new Istio Operator component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Profile: "minimal", EnableMonitoring: false, diff --git a/pkg/components/istio-operator/component_test.go b/pkg/components/istio-operator/component_test.go index f22265ef3..1b5db9d64 100644 --- a/pkg/components/istio-operator/component_test.go +++ b/pkg/components/istio-operator/component_test.go @@ -51,7 +51,7 @@ func TestConversion(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - component := newComponent() + component := NewConfig() m := testutil.RenderManifests(t, component, Name, tc.inputConfig) gotConfig := testutil.ConfigFromMap(t, m, tc.expectedManifestName) @@ -65,7 +65,7 @@ func TestVerifyServiceMonitor(t *testing.T) { enable_monitoring = true }` - component := newComponent() + component := NewConfig() m := testutil.RenderManifests(t, component, Name, inputConfig) testutil.ConfigFromMap(t, m, "istio-operator/templates/service-monitor.yaml") } diff --git a/pkg/components/linkerd/component.go b/pkg/components/linkerd/component.go index bb77e60ff..12b6a0631 100644 --- a/pkg/components/linkerd/component.go +++ b/pkg/components/linkerd/component.go @@ -40,7 +40,7 @@ const ( //nolint:gochecknoinits func init() { - components.Register(name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -58,7 +58,10 @@ type cert struct { Expiry string } -func newComponent() *component { +// NewConfig returns new Linkerd component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ ControllerReplicas: 1, EnableMonitoring: false, diff --git a/pkg/components/metallb/component.go b/pkg/components/metallb/component.go index b7d80e7de..57f0ad4c1 100644 --- a/pkg/components/metallb/component.go +++ b/pkg/components/metallb/component.go @@ -33,7 +33,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -48,7 +48,10 @@ type component struct { SpeakerTolerationsJSON string } -func newComponent() *component { +// NewConfig returns new MetalLB component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{} } diff --git a/pkg/components/metallb/component_test.go b/pkg/components/metallb/component_test.go index 165c93c00..1d0386b23 100644 --- a/pkg/components/metallb/component_test.go +++ b/pkg/components/metallb/component_test.go @@ -27,7 +27,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() evalContext := hcl.EvalContext{} diagnostics := c.LoadConfig(&emptyConfig, &evalContext) @@ -37,7 +37,7 @@ func TestEmptyConfig(t *testing.T) { } func renderManifest(t *testing.T, configHCL string) map[string]string { - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { diff --git a/pkg/components/metrics-server/component.go b/pkg/components/metrics-server/component.go index e6632e6bb..12af42514 100644 --- a/pkg/components/metrics-server/component.go +++ b/pkg/components/metrics-server/component.go @@ -53,14 +53,17 @@ args: ` func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { Namespace string `hcl:"namespace,optional"` } -func newComponent() *component { +// NewConfig returns new metrics-server component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "kube-system", } diff --git a/pkg/components/metrics-server/component_test.go b/pkg/components/metrics-server/component_test.go index 79774cdda..f8f95fa90 100644 --- a/pkg/components/metrics-server/component_test.go +++ b/pkg/components/metrics-server/component_test.go @@ -23,7 +23,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() evalContext := hcl.EvalContext{} diagnostics := c.LoadConfig(&emptyConfig, &evalContext) diff --git a/pkg/components/openebs-operator/component.go b/pkg/components/openebs-operator/component.go index 421d16e3e..b37db8822 100644 --- a/pkg/components/openebs-operator/component.go +++ b/pkg/components/openebs-operator/component.go @@ -33,7 +33,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -92,7 +92,10 @@ webhook: {{- end }} ` -func newComponent() *component { +// NewConfig returns new OpenEBS Operator component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{} } diff --git a/pkg/components/openebs-storage-class/component.go b/pkg/components/openebs-storage-class/component.go index 0bab6d9a8..8afe6b88c 100644 --- a/pkg/components/openebs-storage-class/component.go +++ b/pkg/components/openebs-storage-class/component.go @@ -35,7 +35,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type Storageclass struct { @@ -61,7 +61,10 @@ func defaultStorageClass() *Storageclass { } } -func newComponent() *component { +// NewConfig returns new OpenEBS Storage Class component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Storageclasses: []*Storageclass{}, } diff --git a/pkg/components/openebs-storage-class/component_test.go b/pkg/components/openebs-storage-class/component_test.go index 406ad3951..cb65a2041 100644 --- a/pkg/components/openebs-storage-class/component_test.go +++ b/pkg/components/openebs-storage-class/component_test.go @@ -23,7 +23,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() evalContext := hcl.EvalContext{} diagnostics := c.LoadConfig(&emptyConfig, &evalContext) @@ -68,7 +68,7 @@ func TestUserInputValues(t *testing.T) { } func testRenderManifest(t *testing.T, configHCL string) { - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { diff --git a/pkg/components/prometheus-operator/component.go b/pkg/components/prometheus-operator/component.go index 3c9fc1128..9166cfa63 100644 --- a/pkg/components/prometheus-operator/component.go +++ b/pkg/components/prometheus-operator/component.go @@ -35,7 +35,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } // Monitor holds information about which Kubernetes components should be monitored with the default Prometheus instance. @@ -94,7 +94,10 @@ type component struct { CoreDNS *CoreDNS `hcl:"coredns,block"` } -func newComponent() *component { +// NewConfig returns new Prometheus Operator component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { defaultAlertManagerConfig := ` config: global: diff --git a/pkg/components/prometheus-operator/component_test.go b/pkg/components/prometheus-operator/component_test.go index 53f7b51ea..edc918f7f 100644 --- a/pkg/components/prometheus-operator/component_test.go +++ b/pkg/components/prometheus-operator/component_test.go @@ -101,7 +101,7 @@ component "prometheus-operator" { t.Fatalf("error getting component body: %v", d) } - c := newComponent() + c := NewConfig() d = c.LoadConfig(b, nil) if !tc.wantErr && d.HasErrors() { @@ -208,7 +208,7 @@ providers: t.Run(tc.name, func(t *testing.T) { t.Parallel() - component := newComponent() + component := NewConfig() m := testutil.RenderManifests(t, component, Name, tc.inputConfig) gotConfig := testutil.ConfigFromMap(t, m, tc.expectedManifestName) diff --git a/pkg/components/rook-ceph/component.go b/pkg/components/rook-ceph/component.go index 7b1b5c884..5b046e4a4 100644 --- a/pkg/components/rook-ceph/component.go +++ b/pkg/components/rook-ceph/component.go @@ -33,7 +33,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -53,7 +53,10 @@ type StorageClass struct { Default bool `hcl:"default,optional"` } -func newComponent() *component { +// NewConfig returns new Rook Ceph component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "rook", MonitorCount: 1, diff --git a/pkg/components/rook-ceph/component_test.go b/pkg/components/rook-ceph/component_test.go index cb46b44a9..4d7104cc7 100644 --- a/pkg/components/rook-ceph/component_test.go +++ b/pkg/components/rook-ceph/component_test.go @@ -23,7 +23,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() evalContext := hcl.EvalContext{} diagnostics := c.LoadConfig(&emptyConfig, &evalContext) @@ -62,7 +62,7 @@ component "rook-ceph" { } ` - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { diff --git a/pkg/components/rook/component.go b/pkg/components/rook/component.go index adc7c1f7d..a4b5409c0 100644 --- a/pkg/components/rook/component.go +++ b/pkg/components/rook/component.go @@ -34,7 +34,7 @@ const ( ) func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type component struct { @@ -55,7 +55,10 @@ type component struct { CSIPluginTolerationsRaw string } -func newComponent() *component { +// NewConfig returns new Rook component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "rook", } diff --git a/pkg/components/velero/azure/azure.go b/pkg/components/velero/azure/azure.go index b5f8e836f..912f063fa 100644 --- a/pkg/components/velero/azure/azure.go +++ b/pkg/components/velero/azure/azure.go @@ -149,7 +149,7 @@ func (c *Configuration) Validate() hcl.Diagnostics { // Since nested blocks in hcl2 does not support default values during DecodeBody, // we need to set the default value here, rather than adding diagnostics. // Once PR https://github.com/hashicorp/hcl2/pull/120 is released, this value can be set in - // newComponent() and diagnostic can be added. + // NewConfig() and diagnostic can be added. defaultAPITimeout := "10m" if c.VolumeSnapshotLocation == nil { c.VolumeSnapshotLocation = &VolumeSnapshotLocation{ diff --git a/pkg/components/velero/component.go b/pkg/components/velero/component.go index e198539a1..18e2be9ac 100644 --- a/pkg/components/velero/component.go +++ b/pkg/components/velero/component.go @@ -38,7 +38,7 @@ const ( // init registers velero component to components list, so it shows up as available to install func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } // component represents component configuration data @@ -69,8 +69,10 @@ type provider interface { Validate() hcl.Diagnostics } -// newComponent creates new velero component struct with default values initialized -func newComponent() *component { +// NewConfig returns new Velero component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "velero", Metrics: &Metrics{ diff --git a/pkg/components/velero/component_test.go b/pkg/components/velero/component_test.go index 5d695e11c..fee1fa586 100644 --- a/pkg/components/velero/component_test.go +++ b/pkg/components/velero/component_test.go @@ -23,7 +23,7 @@ import ( ) func TestEmptyConfig(t *testing.T) { - c := newComponent() + c := NewConfig() emptyConfig := hcl.EmptyBody() evalContext := hcl.EvalContext{} @@ -54,7 +54,7 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { @@ -99,7 +99,7 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { @@ -130,7 +130,7 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() body, d := util.GetComponentBody(configHCL, Name) if d != nil { @@ -147,7 +147,7 @@ func TestRenderManifestNoProviderConfigured(t *testing.T) { component "velero" {} ` - component := newComponent() + component := NewConfig() body, d := util.GetComponentBody(configHCL, Name) if d != nil { @@ -174,7 +174,7 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() body, d := util.GetComponentBody(configHCL, Name) if d != nil { diff --git a/pkg/components/web-ui/component.go b/pkg/components/web-ui/component.go index 9faeedfb7..422ba2209 100644 --- a/pkg/components/web-ui/component.go +++ b/pkg/components/web-ui/component.go @@ -35,7 +35,7 @@ const ( //nolint:gochecknoinits func init() { - components.Register(Name, newComponent()) + components.Register(Name, NewConfig()) } type oidc struct { @@ -50,7 +50,10 @@ type component struct { OIDC *oidc `hcl:"oidc,block"` } -func newComponent() *component { +// NewConfig returns new Web UI component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{ Namespace: "lokomotive-system", } From 3aaba3867d2c592be389d5c867d571decd9ae224 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 20:28:44 +0100 Subject: [PATCH 08/19] pkg/components/flatcar-linux-update-operator: add NewConfig function To be consistent with other components. Signed-off-by: Mateusz Gozdek --- .../flatcar-linux-update-operator/component.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/components/flatcar-linux-update-operator/component.go b/pkg/components/flatcar-linux-update-operator/component.go index 705ed2c18..7b87767ff 100644 --- a/pkg/components/flatcar-linux-update-operator/component.go +++ b/pkg/components/flatcar-linux-update-operator/component.go @@ -32,7 +32,14 @@ const ( ) func init() { - components.Register(Name, &component{}) + components.Register(Name, NewConfig()) +} + +// NewConfig returns new Flatcar Linux Update Operator component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { + return &component{} } type component struct{} From 26d51212dbb041e714d9d916aa1f2efac81a1037 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 20:29:34 +0100 Subject: [PATCH 09/19] test/components: use NewConfig() instead of components.Get() So no globals or HCL parsing is involved in testing. Also components.Get() is now deprecated, as we want to avoid relying on globals and init() functions. Part of #992 Signed-off-by: Mateusz Gozdek --- .../component_delete_multiple_release_test.go | 10 ++----- test/components/install_test.go | 26 ++----------------- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/test/components/component_delete_multiple_release_test.go b/test/components/component_delete_multiple_release_test.go index 153ec7db1..a8f9278cb 100644 --- a/test/components/component_delete_multiple_release_test.go +++ b/test/components/component_delete_multiple_release_test.go @@ -20,19 +20,13 @@ package components_test import ( "testing" - "github.com/kinvolk/lokomotive/pkg/components" "github.com/kinvolk/lokomotive/pkg/components/util" - _ "github.com/kinvolk/lokomotive/pkg/components/web-ui" + webui "github.com/kinvolk/lokomotive/pkg/components/web-ui" testutil "github.com/kinvolk/lokomotive/test/components/util" ) func TestDeleteNamespaceMultipleRelease(t *testing.T) { - n := "web-ui" - - c, err := components.Get(n) - if err != nil { - t.Fatalf("failed getting component: %v", err) - } + c := webui.NewConfig() k := testutil.Kubeconfig(t) diff --git a/test/components/install_test.go b/test/components/install_test.go index 50abb892b..d16abaf35 100644 --- a/test/components/install_test.go +++ b/test/components/install_test.go @@ -20,35 +20,13 @@ package components import ( "testing" - "github.com/hashicorp/hcl/v2" - - "github.com/kinvolk/lokomotive/pkg/components" - _ "github.com/kinvolk/lokomotive/pkg/components/flatcar-linux-update-operator" + fluo "github.com/kinvolk/lokomotive/pkg/components/flatcar-linux-update-operator" "github.com/kinvolk/lokomotive/pkg/components/util" testutil "github.com/kinvolk/lokomotive/test/components/util" ) func TestInstallIdempotent(t *testing.T) { - configHCL := ` -component "flatcar-linux-update-operator" {} - ` - - n := "flatcar-linux-update-operator" - - c, err := components.Get(n) - if err != nil { - t.Fatalf("failed getting component: %v", err) - } - - body, diagnostics := util.GetComponentBody(configHCL, n) - if diagnostics != nil { - t.Fatalf("Error getting component body: %v", diagnostics) - } - - diagnostics = c.LoadConfig(body, &hcl.EvalContext{}) - if diagnostics.HasErrors() { - t.Fatalf("Valid config should not return error, got: %s", diagnostics) - } + c := fluo.NewConfig() k := testutil.Kubeconfig(t) From 10159518b2a987e687d9072624a64081ff29eab6 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 20:30:55 +0100 Subject: [PATCH 10/19] pkg/components: use NewConfig() when getting default config for tests So we don't rely on other packages while running unit tests. Refs #992 Signed-off-by: Mateusz Gozdek --- pkg/components/dex/component_test.go | 12 +++--------- pkg/components/gangway/component_test.go | 7 ++----- pkg/components/httpbin/component_test.go | 7 ++----- pkg/components/inspektor-gadget/component_test.go | 7 ++----- pkg/components/rook/component_conversion_test.go | 7 ++----- pkg/components/web-ui/component_test.go | 7 ++----- 6 files changed, 13 insertions(+), 34 deletions(-) diff --git a/pkg/components/dex/component_test.go b/pkg/components/dex/component_test.go index f32b46fca..4c92b7348 100644 --- a/pkg/components/dex/component_test.go +++ b/pkg/components/dex/component_test.go @@ -17,7 +17,7 @@ package dex_test import ( "testing" - "github.com/kinvolk/lokomotive/pkg/components" + "github.com/kinvolk/lokomotive/pkg/components/dex" "github.com/kinvolk/lokomotive/pkg/components/util" ) @@ -110,10 +110,7 @@ func TestRenderManifest(t *testing.T) { t.Fatalf("%s - Error getting component body: %v", tc.desc, d) } - c, err := components.Get(name) - if err != nil { - t.Fatalf("failed getting component: %v", err) - } + c := dex.NewConfig() d = c.LoadConfig(b, nil) @@ -145,10 +142,7 @@ func TestDeploymentAnnotationHashChange(t *testing.T) { t.Fatalf("%s - Error getting component body: %v", tc.desc, d) } - c, err := components.Get(name) - if err != nil { - t.Fatalf("failed getting component: %v", err) - } + c := dex.NewConfig() d = c.LoadConfig(b, nil) if !tc.wantErr && d.HasErrors() { diff --git a/pkg/components/gangway/component_test.go b/pkg/components/gangway/component_test.go index 2f5b6f1de..0db22a519 100644 --- a/pkg/components/gangway/component_test.go +++ b/pkg/components/gangway/component_test.go @@ -17,7 +17,7 @@ package gangway_test import ( "testing" - "github.com/kinvolk/lokomotive/pkg/components" + "github.com/kinvolk/lokomotive/pkg/components/gangway" "github.com/kinvolk/lokomotive/pkg/components/util" ) @@ -62,10 +62,7 @@ component "gangway" { t.Errorf("%s - Error getting component body: %v", tc.desc, d) } - c, err := components.Get(name) - if err != nil { - t.Fatalf("failed getting component: %v", err) - } + c := gangway.NewConfig() d = c.LoadConfig(b, nil) diff --git a/pkg/components/httpbin/component_test.go b/pkg/components/httpbin/component_test.go index 456bcad23..f17c7cf6f 100644 --- a/pkg/components/httpbin/component_test.go +++ b/pkg/components/httpbin/component_test.go @@ -17,7 +17,7 @@ package httpbin_test import ( "testing" - "github.com/kinvolk/lokomotive/pkg/components" + "github.com/kinvolk/lokomotive/pkg/components/httpbin" "github.com/kinvolk/lokomotive/pkg/components/util" ) @@ -54,10 +54,7 @@ component "httpbin" { t.Errorf("%s - Error getting component body: %v", tc.desc, d) } - c, err := components.Get(name) - if err != nil { - t.Fatalf("failed getting component: %v", err) - } + c := httpbin.NewConfig() d = c.LoadConfig(b, nil) diff --git a/pkg/components/inspektor-gadget/component_test.go b/pkg/components/inspektor-gadget/component_test.go index 8309acd9d..014d40fe9 100644 --- a/pkg/components/inspektor-gadget/component_test.go +++ b/pkg/components/inspektor-gadget/component_test.go @@ -23,15 +23,12 @@ import ( appsv1 "k8s.io/api/apps/v1" "sigs.k8s.io/yaml" - "github.com/kinvolk/lokomotive/pkg/components" + inspektorgadget "github.com/kinvolk/lokomotive/pkg/components/inspektor-gadget" "github.com/kinvolk/lokomotive/pkg/components/util" ) func renderManifests(configHCL string) (map[string]string, error) { - component, err := components.Get("inspektor-gadget") - if err != nil { - return nil, err - } + component := inspektorgadget.NewConfig() body, diagnostics := util.GetComponentBody(configHCL, "inspektor-gadget") if diagnostics != nil { diff --git a/pkg/components/rook/component_conversion_test.go b/pkg/components/rook/component_conversion_test.go index 384601876..979e9899e 100644 --- a/pkg/components/rook/component_conversion_test.go +++ b/pkg/components/rook/component_conversion_test.go @@ -23,7 +23,7 @@ import ( corev1 "k8s.io/api/core/v1" "sigs.k8s.io/yaml" - "github.com/kinvolk/lokomotive/pkg/components" + "github.com/kinvolk/lokomotive/pkg/components/rook" "github.com/kinvolk/lokomotive/pkg/components/util" ) @@ -102,10 +102,7 @@ component "rook" { func renderManifests(t *testing.T, configHCL string) map[string]string { name := "rook" - component, err := components.Get(name) - if err != nil { - t.Fatalf("Getting component %q: %v", name, err) - } + component := rook.NewConfig() body, diagnostics := util.GetComponentBody(configHCL, name) if diagnostics != nil { diff --git a/pkg/components/web-ui/component_test.go b/pkg/components/web-ui/component_test.go index 2f6bb09cf..2572f39fd 100644 --- a/pkg/components/web-ui/component_test.go +++ b/pkg/components/web-ui/component_test.go @@ -24,15 +24,12 @@ import ( networkingv1beta1 "k8s.io/api/networking/v1beta1" "sigs.k8s.io/yaml" - "github.com/kinvolk/lokomotive/pkg/components" "github.com/kinvolk/lokomotive/pkg/components/util" + webui "github.com/kinvolk/lokomotive/pkg/components/web-ui" ) func renderManifests(configHCL string) (map[string]string, error) { - component, err := components.Get("web-ui") - if err != nil { - return nil, err - } + component := webui.NewConfig() body, diagnostics := util.GetComponentBody(configHCL, "web-ui") if diagnostics != nil { From 0d0db10aae111db971efb1650ad48890775df19d Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 21:09:04 +0100 Subject: [PATCH 11/19] cli/cmd/cluster: drop dependency on pkg/components.ListNames() Instead, add few helper functions, which will also replace components.Get(). Refs #992 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/component.go | 87 ++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/cli/cmd/cluster/component.go b/cli/cmd/cluster/component.go index df23fb4b4..215957b86 100644 --- a/cli/cmd/cluster/component.go +++ b/cli/cmd/cluster/component.go @@ -15,33 +15,74 @@ package cluster import ( - // Register a component by adding an anonymous import. - _ "github.com/kinvolk/lokomotive/pkg/components/aws-ebs-csi-driver" - _ "github.com/kinvolk/lokomotive/pkg/components/cert-manager" - _ "github.com/kinvolk/lokomotive/pkg/components/cluster-autoscaler" - _ "github.com/kinvolk/lokomotive/pkg/components/contour" - _ "github.com/kinvolk/lokomotive/pkg/components/dex" - _ "github.com/kinvolk/lokomotive/pkg/components/external-dns" - _ "github.com/kinvolk/lokomotive/pkg/components/flatcar-linux-update-operator" - _ "github.com/kinvolk/lokomotive/pkg/components/gangway" - _ "github.com/kinvolk/lokomotive/pkg/components/httpbin" - _ "github.com/kinvolk/lokomotive/pkg/components/inspektor-gadget" - _ "github.com/kinvolk/lokomotive/pkg/components/istio-operator" - _ "github.com/kinvolk/lokomotive/pkg/components/linkerd" - _ "github.com/kinvolk/lokomotive/pkg/components/metallb" - _ "github.com/kinvolk/lokomotive/pkg/components/metrics-server" - _ "github.com/kinvolk/lokomotive/pkg/components/openebs-operator" - _ "github.com/kinvolk/lokomotive/pkg/components/openebs-storage-class" - _ "github.com/kinvolk/lokomotive/pkg/components/prometheus-operator" - _ "github.com/kinvolk/lokomotive/pkg/components/rook" - _ "github.com/kinvolk/lokomotive/pkg/components/rook-ceph" - _ "github.com/kinvolk/lokomotive/pkg/components/velero" - _ "github.com/kinvolk/lokomotive/pkg/components/web-ui" + "fmt" "github.com/kinvolk/lokomotive/pkg/components" + awsebscsidriver "github.com/kinvolk/lokomotive/pkg/components/aws-ebs-csi-driver" + certmanager "github.com/kinvolk/lokomotive/pkg/components/cert-manager" + clusterautoscaler "github.com/kinvolk/lokomotive/pkg/components/cluster-autoscaler" + "github.com/kinvolk/lokomotive/pkg/components/contour" + "github.com/kinvolk/lokomotive/pkg/components/dex" + externaldns "github.com/kinvolk/lokomotive/pkg/components/external-dns" + flatcarlinuxupdateoperator "github.com/kinvolk/lokomotive/pkg/components/flatcar-linux-update-operator" + "github.com/kinvolk/lokomotive/pkg/components/gangway" + "github.com/kinvolk/lokomotive/pkg/components/httpbin" + inspektorgadget "github.com/kinvolk/lokomotive/pkg/components/inspektor-gadget" + istiooperator "github.com/kinvolk/lokomotive/pkg/components/istio-operator" + "github.com/kinvolk/lokomotive/pkg/components/linkerd" + "github.com/kinvolk/lokomotive/pkg/components/metallb" + metricsserver "github.com/kinvolk/lokomotive/pkg/components/metrics-server" + openebsoperator "github.com/kinvolk/lokomotive/pkg/components/openebs-operator" + openebsstorageclass "github.com/kinvolk/lokomotive/pkg/components/openebs-storage-class" + "github.com/kinvolk/lokomotive/pkg/components/prometheus-operator" + "github.com/kinvolk/lokomotive/pkg/components/rook" + rookceph "github.com/kinvolk/lokomotive/pkg/components/rook-ceph" + "github.com/kinvolk/lokomotive/pkg/components/velero" + webui "github.com/kinvolk/lokomotive/pkg/components/web-ui" ) +func componentsConfigs() map[string]components.Component { + return map[string]components.Component{ + awsebscsidriver.Name: awsebscsidriver.NewConfig(), + certmanager.Name: certmanager.NewConfig(), + clusterautoscaler.Name: clusterautoscaler.NewConfig(), + contour.Name: contour.NewConfig(), + dex.Name: dex.NewConfig(), + externaldns.Name: externaldns.NewConfig(), + flatcarlinuxupdateoperator.Name: flatcarlinuxupdateoperator.NewConfig(), + gangway.Name: gangway.NewConfig(), + httpbin.Name: httpbin.NewConfig(), + inspektorgadget.Name: inspektorgadget.NewConfig(), + istiooperator.Name: istiooperator.NewConfig(), + linkerd.Name: linkerd.NewConfig(), + metallb.Name: metallb.NewConfig(), + metricsserver.Name: metricsserver.NewConfig(), + openebsoperator.Name: openebsoperator.NewConfig(), + openebsstorageclass.Name: openebsstorageclass.NewConfig(), + prometheus.Name: prometheus.NewConfig(), + rook.Name: rook.NewConfig(), + rookceph.Name: rookceph.NewConfig(), + velero.Name: velero.NewConfig(), + webui.Name: webui.NewConfig(), + } +} + // AvailableComponents returns list of valid component names. func AvailableComponents() []string { - return components.ListNames() + c := []string{} + + for n := range componentsConfigs() { + c = append(c, n) + } + + return c +} + +func componentConfig(name string) (components.Component, error) { + c, ok := componentsConfigs()[name] + if !ok { + return nil, fmt.Errorf("no component with name %q found", name) + } + + return c, nil } From ac5dd1c8189dc912ed630fefe3cf4964b7ea6845 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 21:11:29 +0100 Subject: [PATCH 12/19] cli/cmd/cluster: use local componentConfig() instead of components.Get() As a next step of not using globals and init() functions across our code. Refs #992 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/component-delete.go | 2 +- cli/cmd/cluster/component-render-manifest.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/cmd/cluster/component-delete.go b/cli/cmd/cluster/component-delete.go index 2092d32dc..8a4358b72 100644 --- a/cli/cmd/cluster/component-delete.go +++ b/cli/cmd/cluster/component-delete.go @@ -103,7 +103,7 @@ func componentNamesToObjects(componentNames []string) ([]components.Component, e c := []components.Component{} for _, componentName := range componentNames { - component, err := components.Get(componentName) + component, err := componentConfig(componentName) if err != nil { return nil, fmt.Errorf("getting component %q: %w", componentName, err) } diff --git a/cli/cmd/cluster/component-render-manifest.go b/cli/cmd/cluster/component-render-manifest.go index 09bf9fd1d..911b0c34c 100644 --- a/cli/cmd/cluster/component-render-manifest.go +++ b/cli/cmd/cluster/component-render-manifest.go @@ -19,7 +19,6 @@ import ( log "github.com/sirupsen/logrus" - "github.com/kinvolk/lokomotive/pkg/components" "github.com/kinvolk/lokomotive/pkg/config" ) @@ -57,7 +56,7 @@ func renderComponentManifests(lokoConfig *config.Config, componentNames []string "component": componentName, }) - component, err := components.Get(componentName) + component, err := componentConfig(componentName) if err != nil { return fmt.Errorf("getting component %q: %w", componentName, err) } From f3403b25afab3aba2a895f045a36dd1179173793 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 21:12:19 +0100 Subject: [PATCH 13/19] pkg/components: remove Register(), Get() and other related functions They are not used anymore, so can be removed. Refs #992. Signed-off-by: Mateusz Gozdek --- .../aws-ebs-csi-driver/component.go | 5 --- pkg/components/cert-manager/component.go | 4 --- .../cluster-autoscaler/component.go | 4 --- pkg/components/components.go | 31 ------------------- pkg/components/contour/component.go | 4 --- pkg/components/dex/component.go | 4 --- pkg/components/external-dns/component.go | 4 --- .../component.go | 4 --- pkg/components/gangway/component.go | 4 --- pkg/components/httpbin/component.go | 4 --- pkg/components/inspektor-gadget/component.go | 5 --- pkg/components/istio-operator/component.go | 5 --- pkg/components/linkerd/component.go | 5 --- pkg/components/metallb/component.go | 4 --- pkg/components/metrics-server/component.go | 4 --- pkg/components/openebs-operator/component.go | 4 --- .../openebs-storage-class/component.go | 4 --- .../prometheus-operator/component.go | 4 --- pkg/components/rook-ceph/component.go | 4 --- pkg/components/rook/component.go | 4 --- pkg/components/velero/component.go | 5 --- pkg/components/web-ui/component.go | 5 --- 22 files changed, 121 deletions(-) diff --git a/pkg/components/aws-ebs-csi-driver/component.go b/pkg/components/aws-ebs-csi-driver/component.go index 76380d2a2..d6114d015 100644 --- a/pkg/components/aws-ebs-csi-driver/component.go +++ b/pkg/components/aws-ebs-csi-driver/component.go @@ -36,11 +36,6 @@ enableDefaultStorageClass: {{ .EnableDefaultStorageClass }} ` ) -//nolint:gochecknoinits -func init() { - components.Register(Name, NewConfig()) -} - type component struct { EnableDefaultStorageClass bool `hcl:"enable_default_storage_class,optional"` } diff --git a/pkg/components/cert-manager/component.go b/pkg/components/cert-manager/component.go index cca8d4237..4eb658317 100644 --- a/pkg/components/cert-manager/component.go +++ b/pkg/components/cert-manager/component.go @@ -32,10 +32,6 @@ const ( Name = "cert-manager" ) -func init() { - components.Register(Name, NewConfig()) -} - type component struct { Email string `hcl:"email,attr"` Namespace string `hcl:"namespace,optional"` diff --git a/pkg/components/cluster-autoscaler/component.go b/pkg/components/cluster-autoscaler/component.go index c0c35ae30..6712be6e9 100644 --- a/pkg/components/cluster-autoscaler/component.go +++ b/pkg/components/cluster-autoscaler/component.go @@ -78,10 +78,6 @@ serviceMonitor: ` ) -func init() { - components.Register(Name, NewConfig()) -} - type component struct { // required parameters Provider string `hcl:"provider,optional"` diff --git a/pkg/components/components.go b/pkg/components/components.go index 942753ed5..058213e46 100644 --- a/pkg/components/components.go +++ b/pkg/components/components.go @@ -15,7 +15,6 @@ package components import ( - "fmt" "path/filepath" "github.com/kinvolk/lokomotive/pkg/assets" @@ -23,36 +22,6 @@ import ( "helm.sh/helm/v3/pkg/chart" ) -// components is the map of registered components -var components map[string]Component - -func init() { - components = make(map[string]Component) -} - -func Register(name string, obj Component) { - if _, exists := components[name]; exists { - panic(fmt.Sprintf("component with name %q registered already", name)) - } - components[name] = obj -} - -func ListNames() []string { - var componentList []string - for name := range components { - componentList = append(componentList, name) - } - return componentList -} - -func Get(name string) (Component, error) { - component, exists := components[name] - if !exists { - return nil, fmt.Errorf("no component with name %q found", name) - } - return component, nil -} - // Chart is a convenience function which returns a pointer to a chart.Chart representing the // component named name. func Chart(name string) (*chart.Chart, error) { diff --git a/pkg/components/contour/component.go b/pkg/components/contour/component.go index 652cf3a32..e13d91e1a 100644 --- a/pkg/components/contour/component.go +++ b/pkg/components/contour/component.go @@ -35,10 +35,6 @@ const ( serviceTypeLoadBalancer = "LoadBalancer" ) -func init() { - components.Register(Name, NewConfig()) -} - // This annotation is added to Envoy service. type component struct { EnableMonitoring bool `hcl:"enable_monitoring,optional"` diff --git a/pkg/components/dex/component.go b/pkg/components/dex/component.go index 9d91e9a8e..e9f1028f6 100644 --- a/pkg/components/dex/component.go +++ b/pkg/components/dex/component.go @@ -33,10 +33,6 @@ const ( Name = "dex" ) -func init() { //nolint:gochecknoinits - components.Register(Name, NewConfig()) -} - type org struct { Name string `hcl:"name,attr" json:"name"` Teams []string `hcl:"teams,attr" json:"teams"` diff --git a/pkg/components/external-dns/component.go b/pkg/components/external-dns/component.go index 60e279e76..541630ac5 100644 --- a/pkg/components/external-dns/component.go +++ b/pkg/components/external-dns/component.go @@ -65,10 +65,6 @@ metrics: ` ) -func init() { - components.Register(Name, NewConfig()) -} - // AwsConfig provides configuration for AWS Route53 DNS. type AwsConfig struct { ZoneID string `hcl:"zone_id"` diff --git a/pkg/components/flatcar-linux-update-operator/component.go b/pkg/components/flatcar-linux-update-operator/component.go index 7b87767ff..af7dbc4c4 100644 --- a/pkg/components/flatcar-linux-update-operator/component.go +++ b/pkg/components/flatcar-linux-update-operator/component.go @@ -31,10 +31,6 @@ const ( Name = "flatcar-linux-update-operator" ) -func init() { - components.Register(Name, NewConfig()) -} - // NewConfig returns new Flatcar Linux Update Operator component configuration with default values set. // //nolint:golint diff --git a/pkg/components/gangway/component.go b/pkg/components/gangway/component.go index d16c67c9f..c36900c5d 100644 --- a/pkg/components/gangway/component.go +++ b/pkg/components/gangway/component.go @@ -32,10 +32,6 @@ const ( Name = "gangway" ) -func init() { //nolint:gochecknoinits - components.Register(Name, NewConfig()) -} - type component struct { ClusterName string `hcl:"cluster_name,attr"` IngressHost string `hcl:"ingress_host,attr"` diff --git a/pkg/components/httpbin/component.go b/pkg/components/httpbin/component.go index d17762fca..09d452553 100644 --- a/pkg/components/httpbin/component.go +++ b/pkg/components/httpbin/component.go @@ -32,10 +32,6 @@ const ( Name = "httpbin" ) -func init() { //nolint:gochecknoinits - components.Register(Name, NewConfig()) -} - type component struct { IngressHost string `hcl:"ingress_host,attr"` CertManagerClusterIssuer string `hcl:"certmanager_cluster_issuer,optional"` diff --git a/pkg/components/inspektor-gadget/component.go b/pkg/components/inspektor-gadget/component.go index c0916ef75..518101df5 100644 --- a/pkg/components/inspektor-gadget/component.go +++ b/pkg/components/inspektor-gadget/component.go @@ -32,11 +32,6 @@ const ( Name = "inspektor-gadget" ) -//nolint:gochecknoinits -func init() { - components.Register(Name, NewConfig()) -} - type component struct { Namespace string `hcl:"namespace,optional"` EnableTraceloop bool `hcl:"enable_traceloop,optional"` diff --git a/pkg/components/istio-operator/component.go b/pkg/components/istio-operator/component.go index 39aba507e..2ab97171b 100644 --- a/pkg/components/istio-operator/component.go +++ b/pkg/components/istio-operator/component.go @@ -34,11 +34,6 @@ const ( namespace = "istio-operator" ) -//nolint:gochecknoinits -func init() { - components.Register(Name, NewConfig()) -} - type component struct { Profile string `hcl:"profile,optional"` EnableMonitoring bool `hcl:"enable_monitoring,optional"` diff --git a/pkg/components/linkerd/component.go b/pkg/components/linkerd/component.go index 12b6a0631..0080e285d 100644 --- a/pkg/components/linkerd/component.go +++ b/pkg/components/linkerd/component.go @@ -38,11 +38,6 @@ const ( certCommonName = "identity.linkerd.cluster.local" ) -//nolint:gochecknoinits -func init() { - components.Register(Name, NewConfig()) -} - type component struct { ControllerReplicas int `hcl:"controller_replicas,optional"` EnableMonitoring bool `hcl:"enable_monitoring,optional"` diff --git a/pkg/components/metallb/component.go b/pkg/components/metallb/component.go index 57f0ad4c1..c2a883c41 100644 --- a/pkg/components/metallb/component.go +++ b/pkg/components/metallb/component.go @@ -32,10 +32,6 @@ const ( Name = "metallb" ) -func init() { - components.Register(Name, NewConfig()) -} - type component struct { AddressPools map[string][]string `hcl:"address_pools"` ControllerNodeSelectors map[string]string `hcl:"controller_node_selectors,optional"` diff --git a/pkg/components/metrics-server/component.go b/pkg/components/metrics-server/component.go index 12af42514..f9c766122 100644 --- a/pkg/components/metrics-server/component.go +++ b/pkg/components/metrics-server/component.go @@ -52,10 +52,6 @@ args: - --kubelet-preferred-address-types=InternalIP ` -func init() { - components.Register(Name, NewConfig()) -} - type component struct { Namespace string `hcl:"namespace,optional"` } diff --git a/pkg/components/openebs-operator/component.go b/pkg/components/openebs-operator/component.go index b37db8822..124e81679 100644 --- a/pkg/components/openebs-operator/component.go +++ b/pkg/components/openebs-operator/component.go @@ -32,10 +32,6 @@ const ( Name = "openebs-operator" ) -func init() { - components.Register(Name, NewConfig()) -} - type component struct { NDMSelectorLabel string `hcl:"ndm_selector_label,optional"` NDMSelectorValue string `hcl:"ndm_selector_value,optional"` diff --git a/pkg/components/openebs-storage-class/component.go b/pkg/components/openebs-storage-class/component.go index 8afe6b88c..72b77f18b 100644 --- a/pkg/components/openebs-storage-class/component.go +++ b/pkg/components/openebs-storage-class/component.go @@ -34,10 +34,6 @@ const ( poolName = "openebs-storage-pool" ) -func init() { - components.Register(Name, NewConfig()) -} - type Storageclass struct { Name string `hcl:"name,label"` ReplicaCount int `hcl:"replica_count,optional"` diff --git a/pkg/components/prometheus-operator/component.go b/pkg/components/prometheus-operator/component.go index 9166cfa63..6f486bd92 100644 --- a/pkg/components/prometheus-operator/component.go +++ b/pkg/components/prometheus-operator/component.go @@ -34,10 +34,6 @@ const ( Name = "prometheus-operator" ) -func init() { - components.Register(Name, NewConfig()) -} - // Monitor holds information about which Kubernetes components should be monitored with the default Prometheus instance. type Monitor struct { Etcd bool `hcl:"etcd,optional"` diff --git a/pkg/components/rook-ceph/component.go b/pkg/components/rook-ceph/component.go index 5b046e4a4..24f9fb3aa 100644 --- a/pkg/components/rook-ceph/component.go +++ b/pkg/components/rook-ceph/component.go @@ -32,10 +32,6 @@ const ( Name = "rook-ceph" ) -func init() { - components.Register(Name, NewConfig()) -} - type component struct { Namespace string `hcl:"namespace,optional"` MonitorCount int `hcl:"monitor_count,optional"` diff --git a/pkg/components/rook/component.go b/pkg/components/rook/component.go index a4b5409c0..19aa379e8 100644 --- a/pkg/components/rook/component.go +++ b/pkg/components/rook/component.go @@ -33,10 +33,6 @@ const ( Name = "rook" ) -func init() { - components.Register(Name, NewConfig()) -} - type component struct { Namespace string `hcl:"namespace,optional"` NodeSelector util.NodeSelector `hcl:"node_selector,optional"` diff --git a/pkg/components/velero/component.go b/pkg/components/velero/component.go index 18e2be9ac..1645c6a68 100644 --- a/pkg/components/velero/component.go +++ b/pkg/components/velero/component.go @@ -36,11 +36,6 @@ const ( Name = "velero" ) -// init registers velero component to components list, so it shows up as available to install -func init() { - components.Register(Name, NewConfig()) -} - // component represents component configuration data type component struct { // Namespace where velero resources should be installed. Defaults to 'velero'. diff --git a/pkg/components/web-ui/component.go b/pkg/components/web-ui/component.go index 422ba2209..54e53fd83 100644 --- a/pkg/components/web-ui/component.go +++ b/pkg/components/web-ui/component.go @@ -33,11 +33,6 @@ const ( Name = "web-ui" ) -//nolint:gochecknoinits -func init() { - components.Register(Name, NewConfig()) -} - type oidc struct { ClientID string `hcl:"client_id"` ClientSecret string `hcl:"client_secret"` From b5e3db02c959cffeefd7f5f8bf68e6176227fd3e Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 21:33:27 +0100 Subject: [PATCH 14/19] pkg/backend: rename functions returning new config to NewConfig() To be consistent with platform and component packages. Refs #992 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/cluster.go | 2 +- pkg/backend/local/local.go | 7 +++++-- pkg/backend/s3/s3.go | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cli/cmd/cluster/cluster.go b/cli/cmd/cluster/cluster.go index 97a9c7b8c..7e13384d2 100644 --- a/cli/cmd/cluster/cluster.go +++ b/cli/cmd/cluster/cluster.go @@ -82,7 +82,7 @@ func (cc clusterConfig) initialize(contextLogger *log.Entry) (*cluster, error) { // Use a local backend if no backend is configured. if b == nil { - b = local.NewLocalBackend() + b = local.NewConfig() } assetDir, err := homedir.Expand(p.Meta().AssetDir) diff --git a/pkg/backend/local/local.go b/pkg/backend/local/local.go index 4f494fd90..f36bb990a 100644 --- a/pkg/backend/local/local.go +++ b/pkg/backend/local/local.go @@ -28,7 +28,7 @@ type local struct { // init registers local as a backend. func init() { - backend.Register("local", NewLocalBackend()) + backend.Register("local", NewConfig()) } // LoadConfig loads the configuration for the local backend. @@ -39,7 +39,10 @@ func (l *local) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) h return gohcl.DecodeBody(*configBody, evalContext, l) } -func NewLocalBackend() *local { +// NewConfig returns new Local backend configuration with default values set. +// +//nolint:golint +func NewConfig() *local { return &local{} } diff --git a/pkg/backend/s3/s3.go b/pkg/backend/s3/s3.go index 6453c78d6..ad9645977 100644 --- a/pkg/backend/s3/s3.go +++ b/pkg/backend/s3/s3.go @@ -35,7 +35,7 @@ type s3 struct { // init registers s3 as a backend. func init() { - backend.Register("s3", NewS3Backend()) + backend.Register("s3", NewConfig()) } // LoadConfig loads the configuration for the s3 backend. @@ -46,7 +46,10 @@ func (s *s3) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl. return gohcl.DecodeBody(*configBody, evalContext, s) } -func NewS3Backend() *s3 { +// NewConfig returns new Local backend configuration with default values set. +// +//nolint:golint +func NewConfig() *s3 { return &s3{} } From 7c2c0dc316891c78315197e7a7c3ba2da3bccebd Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 21:41:58 +0100 Subject: [PATCH 15/19] pkg/backend: export Name const for consistency To be consistent with platform and component packages. Refs #992 Signed-off-by: Mateusz Gozdek --- pkg/backend/local/local.go | 8 +++++++- pkg/backend/s3/s3.go | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/backend/local/local.go b/pkg/backend/local/local.go index f36bb990a..ee56f0825 100644 --- a/pkg/backend/local/local.go +++ b/pkg/backend/local/local.go @@ -26,9 +26,15 @@ type local struct { Path string `hcl:"path,optional"` } +const ( + // Name represents Local backend name as it should be referenced in function calls + // and in configuration. + Name = "local" +) + // init registers local as a backend. func init() { - backend.Register("local", NewConfig()) + backend.Register(Name, NewConfig()) } // LoadConfig loads the configuration for the local backend. diff --git a/pkg/backend/s3/s3.go b/pkg/backend/s3/s3.go index ad9645977..65306726e 100644 --- a/pkg/backend/s3/s3.go +++ b/pkg/backend/s3/s3.go @@ -33,9 +33,15 @@ type s3 struct { DynamoDBTable string `hcl:"dynamodb_table,optional"` } +const ( + // Name represents S3 backend name as it should be referenced in function calls + // and in configuration. + Name = "s3" +) + // init registers s3 as a backend. func init() { - backend.Register("s3", NewConfig()) + backend.Register(Name, NewConfig()) } // LoadConfig loads the configuration for the s3 backend. From 1a039f67331480fd2eb1c6fa5a9e985ff26a2903 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 21:55:08 +0100 Subject: [PATCH 16/19] cli/cmd/cluster: don't use backend.GetBackend for getting backend config Import all available backends locally instead, to avoid using globals. Refs #992 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/utils.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cli/cmd/cluster/utils.go b/cli/cmd/cluster/utils.go index 747038722..d9f177c09 100644 --- a/cli/cmd/cluster/utils.go +++ b/cli/cmd/cluster/utils.go @@ -25,6 +25,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/kinvolk/lokomotive/pkg/backend" + "github.com/kinvolk/lokomotive/pkg/backend/local" + "github.com/kinvolk/lokomotive/pkg/backend/s3" "github.com/kinvolk/lokomotive/pkg/config" "github.com/kinvolk/lokomotive/pkg/platform" "github.com/kinvolk/lokomotive/pkg/platform/aks" @@ -47,11 +49,16 @@ func getConfiguredBackend(lokoConfig *config.Config) (backend.Backend, hcl.Diagn return nil, hcl.Diagnostics{} } - backend, err := backend.GetBackend(lokoConfig.RootConfig.Backend.Name) - if err != nil { + backends := map[string]backend.Backend{ + s3.Name: s3.NewConfig(), + local.Name: local.NewConfig(), + } + + backend, ok := backends[lokoConfig.RootConfig.Backend.Name] + if !ok { diag := &hcl.Diagnostic{ Severity: hcl.DiagError, - Summary: err.Error(), + Summary: fmt.Sprintf("no backend with name %q found", lokoConfig.RootConfig.Backend.Name), } return nil, hcl.Diagnostics{diag} } From c435f744e433e4081c370e0734abde42b0642965 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 22:00:56 +0100 Subject: [PATCH 17/19] cli/cmd/cluster: move Backend interface definition to only consumer No other packages really need backend.Backend interface, so we can move it to cli/cmd/cluster package where it is actually consumed, which is an recommendation in Go. This way we can further reduce exported API of the codebase. Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/cluster.go | 3 +-- cli/cmd/cluster/utils.go | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cli/cmd/cluster/cluster.go b/cli/cmd/cluster/cluster.go index 7e13384d2..049b605ed 100644 --- a/cli/cmd/cluster/cluster.go +++ b/cli/cmd/cluster/cluster.go @@ -27,7 +27,6 @@ import ( "sigs.k8s.io/yaml" "github.com/kinvolk/lokomotive/pkg/assets" - "github.com/kinvolk/lokomotive/pkg/backend" "github.com/kinvolk/lokomotive/pkg/backend/local" "github.com/kinvolk/lokomotive/pkg/components/util" "github.com/kinvolk/lokomotive/pkg/config" @@ -125,7 +124,7 @@ func (c *cluster) unpackControlplaneCharts() error { // initializeTerraform initialized Terraform directory using given backend and platform // and returns configured executor. -func (cc clusterConfig) initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Executor, error) { +func (cc clusterConfig) initializeTerraform(p platform.Platform, b backend) (*terraform.Executor, error) { assetDir, err := homedir.Expand(p.Meta().AssetDir) if err != nil { return nil, fmt.Errorf("expanding path %q: %w", p.Meta().AssetDir, err) diff --git a/cli/cmd/cluster/utils.go b/cli/cmd/cluster/utils.go index d9f177c09..09ff1b022 100644 --- a/cli/cmd/cluster/utils.go +++ b/cli/cmd/cluster/utils.go @@ -24,7 +24,6 @@ import ( "github.com/mitchellh/go-homedir" log "github.com/sirupsen/logrus" - "github.com/kinvolk/lokomotive/pkg/backend" "github.com/kinvolk/lokomotive/pkg/backend/local" "github.com/kinvolk/lokomotive/pkg/backend/s3" "github.com/kinvolk/lokomotive/pkg/config" @@ -42,14 +41,24 @@ const ( kubeconfigTerraformOutputKey = "kubeconfig" ) +// backend describes the Terraform state storage location. +type backend interface { + // LoadConfig loads the backend config provided by the user. + LoadConfig(*hcl.Body, *hcl.EvalContext) hcl.Diagnostics + // Render renders the backend template with user backend configuration. + Render() (string, error) + // Validate validates backend configuration. + Validate() error +} + // getConfiguredBackend loads a backend from the given configuration file. -func getConfiguredBackend(lokoConfig *config.Config) (backend.Backend, hcl.Diagnostics) { +func getConfiguredBackend(lokoConfig *config.Config) (backend, hcl.Diagnostics) { if lokoConfig.RootConfig.Backend == nil { // No backend defined and no configuration error return nil, hcl.Diagnostics{} } - backends := map[string]backend.Backend{ + backends := map[string]backend{ s3.Name: s3.NewConfig(), local.Name: local.NewConfig(), } From b9793dfa0853fa3b96ee7a94317cd6890fe1b750 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Mon, 2 Nov 2020 22:02:53 +0100 Subject: [PATCH 18/19] Remove pkg/backend package We no longer use registration pattern for backends, as it is using globals and init() which we want to avoid using. We also moved the interface to cli/cmd/cluster package, as it was the only consumer of it, so actually the entire package can be now removed, as it no longer has any use. Refs #992 Signed-off-by: Mateusz Gozdek --- pkg/backend/backend.go | 27 --------------------------- pkg/backend/local/local.go | 6 ------ pkg/backend/s3/s3.go | 6 ------ 3 files changed, 39 deletions(-) diff --git a/pkg/backend/backend.go b/pkg/backend/backend.go index 8df9df010..9c06fc9f0 100644 --- a/pkg/backend/backend.go +++ b/pkg/backend/backend.go @@ -15,8 +15,6 @@ package backend import ( - "fmt" - "github.com/hashicorp/hcl/v2" ) @@ -29,28 +27,3 @@ type Backend interface { // Validate validates backend configuration. Validate() error } - -// backends is a collection in which all Backends get automatically registered. -var backends map[string]Backend - -// Initialize package's global variable on import. -func init() { - backends = make(map[string]Backend) -} - -// Register registers Backend b in the internal backends map. -func Register(name string, b Backend) { - if _, exists := backends[name]; exists { - panic(fmt.Sprintf("backend with name %q registered already", name)) - } - backends[name] = b -} - -// GetBackend returns the Backend referred to by name. -func GetBackend(name string) (Backend, error) { - backend, exists := backends[name] - if !exists { - return nil, fmt.Errorf("no backend with name %q found", name) - } - return backend, nil -} diff --git a/pkg/backend/local/local.go b/pkg/backend/local/local.go index ee56f0825..ab86df339 100644 --- a/pkg/backend/local/local.go +++ b/pkg/backend/local/local.go @@ -19,7 +19,6 @@ import ( "github.com/hashicorp/hcl/v2/gohcl" "github.com/kinvolk/lokomotive/internal/template" - "github.com/kinvolk/lokomotive/pkg/backend" ) type local struct { @@ -32,11 +31,6 @@ const ( Name = "local" ) -// init registers local as a backend. -func init() { - backend.Register(Name, NewConfig()) -} - // LoadConfig loads the configuration for the local backend. func (l *local) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { diff --git a/pkg/backend/s3/s3.go b/pkg/backend/s3/s3.go index 65306726e..2e948ed38 100644 --- a/pkg/backend/s3/s3.go +++ b/pkg/backend/s3/s3.go @@ -22,7 +22,6 @@ import ( "github.com/hashicorp/hcl/v2/gohcl" "github.com/kinvolk/lokomotive/internal/template" - "github.com/kinvolk/lokomotive/pkg/backend" ) type s3 struct { @@ -39,11 +38,6 @@ const ( Name = "s3" ) -// init registers s3 as a backend. -func init() { - backend.Register(Name, NewConfig()) -} - // LoadConfig loads the configuration for the s3 backend. func (s *s3) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { From f728c602b3237778528a32fe8fe96bb3e508928e Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Thu, 3 Dec 2020 10:57:57 +0100 Subject: [PATCH 19/19] cli/cmd/cluster/cluster.go: remove unused anonymous import We no longer rely on the init function call registration method for all backend packages, so anonymous imports are no longer needed. Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster/cluster.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/cli/cmd/cluster/cluster.go b/cli/cmd/cluster/cluster.go index 049b605ed..954f6b32a 100644 --- a/cli/cmd/cluster/cluster.go +++ b/cli/cmd/cluster/cluster.go @@ -32,9 +32,6 @@ import ( "github.com/kinvolk/lokomotive/pkg/config" "github.com/kinvolk/lokomotive/pkg/platform" "github.com/kinvolk/lokomotive/pkg/terraform" - - // Register backends by adding an anonymous import. - _ "github.com/kinvolk/lokomotive/pkg/backend/s3" ) // cluster is a temporary helper struct to aggregate objects which are used