diff --git a/cli/cmd/cluster/cluster.go b/cli/cmd/cluster/cluster.go index dcb9c28c9..954f6b32a 100644 --- a/cli/cmd/cluster/cluster.go +++ b/cli/cmd/cluster/cluster.go @@ -27,22 +27,11 @@ 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" "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" ) // cluster is a temporary helper struct to aggregate objects which are used @@ -89,7 +78,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) @@ -132,7 +121,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/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) } 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 } diff --git a/cli/cmd/cluster/utils.go b/cli/cmd/cluster/utils.go index 3171e1ff4..09ff1b022 100644 --- a/cli/cmd/cluster/utils.go +++ b/cli/cmd/cluster/utils.go @@ -24,9 +24,15 @@ 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" "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 ( @@ -35,18 +41,33 @@ 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{} } - backend, err := backend.GetBackend(lokoConfig.RootConfig.Backend.Name) - if err != nil { + backends := map[string]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} } @@ -54,6 +75,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 +107,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/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 4f494fd90..ab86df339 100644 --- a/pkg/backend/local/local.go +++ b/pkg/backend/local/local.go @@ -19,17 +19,17 @@ import ( "github.com/hashicorp/hcl/v2/gohcl" "github.com/kinvolk/lokomotive/internal/template" - "github.com/kinvolk/lokomotive/pkg/backend" ) type local struct { Path string `hcl:"path,optional"` } -// init registers local as a backend. -func init() { - backend.Register("local", NewLocalBackend()) -} +const ( + // Name represents Local backend name as it should be referenced in function calls + // and in configuration. + Name = "local" +) // LoadConfig loads the configuration for the local backend. func (l *local) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { @@ -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..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 { @@ -33,10 +32,11 @@ type s3 struct { DynamoDBTable string `hcl:"dynamodb_table,optional"` } -// init registers s3 as a backend. -func init() { - backend.Register("s3", NewS3Backend()) -} +const ( + // Name represents S3 backend name as it should be referenced in function calls + // and in configuration. + Name = "s3" +) // LoadConfig loads the configuration for the s3 backend. func (s *s3) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { @@ -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{} } diff --git a/pkg/components/aws-ebs-csi-driver/component.go b/pkg/components/aws-ebs-csi-driver/component.go index 9dc4ee22b..d6114d015 100644 --- a/pkg/components/aws-ebs-csi-driver/component.go +++ b/pkg/components/aws-ebs-csi-driver/component.go @@ -26,22 +26,24 @@ 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()) -} +) 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, } @@ -58,7 +60,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 +70,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 +80,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..f090dce37 100644 --- a/pkg/components/aws-ebs-csi-driver/component_test.go +++ b/pkg/components/aws-ebs-csi-driver/component_test.go @@ -26,9 +26,9 @@ import ( func TestStorageClassEmptyConfig(t *testing.T) { configHCL := `component "aws-ebs-csi-driver" {}` - component := newComponent() + component := NewConfig() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -65,9 +65,9 @@ func TestStorageClassEnabled(t *testing.T) { enable_default_storage_class = true }` - component := newComponent() + component := NewConfig() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -104,9 +104,9 @@ func TestStorageClassDisabled(t *testing.T) { enable_default_storage_class = false }` - component := newComponent() + component := NewConfig() - 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..4eb658317 100644 --- a/pkg/components/cert-manager/component.go +++ b/pkg/components/cert-manager/component.go @@ -26,11 +26,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "cert-manager" - -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents cert-manager component name as it should be referenced in function calls + // and in configuration. + Name = "cert-manager" +) type component struct { Email string `hcl:"email,attr"` @@ -39,7 +39,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, @@ -75,7 +78,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 +88,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 +98,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..6712be6e9 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,10 +76,7 @@ serviceMonitor: release: prometheus-operator {{ end }} ` - -func init() { - components.Register(name, newComponent()) -} +) type component struct { // required parameters @@ -114,7 +114,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", @@ -346,7 +349,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 +379,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..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,11 +36,11 @@ func TestEmptyConfig(t *testing.T) { } func TestEmptyBody(t *testing.T) { - c := newComponent() + c := NewConfig() 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) } @@ -51,7 +51,7 @@ func TestEmptyBody(t *testing.T) { } func TestRender(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "cluster-autoscaler" { @@ -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/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 41feb6818..e13d91e1a 100644 --- a/pkg/components/contour/component.go +++ b/pkg/components/contour/component.go @@ -27,15 +27,14 @@ 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()) -} - // This annotation is added to Envoy service. type component struct { EnableMonitoring bool `hcl:"enable_monitoring,optional"` @@ -46,7 +45,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, } @@ -79,7 +81,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 +102,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 +112,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..a1ce8ecae 100644 --- a/pkg/components/contour/component_test.go +++ b/pkg/components/contour/component_test.go @@ -64,12 +64,12 @@ 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) } - 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 4607b8d05..e9f1028f6 100644 --- a/pkg/components/dex/component.go +++ b/pkg/components/dex/component.go @@ -27,11 +27,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "dex" - -func init() { //nolint:gochecknoinits - components.Register(name, newComponent()) -} +const ( + // Name represents Dex component name as it should be referenced in function calls + // and in configuration. + Name = "dex" +) type org struct { Name string `hcl:"name,attr" json:"name"` @@ -77,7 +77,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", } @@ -105,7 +108,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 +144,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 +154,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/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/external-dns/component.go b/pkg/components/external-dns/component.go index 36a9b5616..541630ac5 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,10 +63,7 @@ metrics: release: prometheus-operator {{ end }} ` - -func init() { - components.Register(name, newComponent()) -} +) // AwsConfig provides configuration for AWS Route53 DNS. type AwsConfig struct { @@ -84,7 +84,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"}, @@ -108,7 +111,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 +138,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 +148,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..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,9 +33,9 @@ func TestEmptyConfig(t *testing.T) { } func TestEmptyBody(t *testing.T) { - c := newComponent() + c := NewConfig() 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) } @@ -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"] @@ -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) } @@ -94,7 +94,7 @@ func TestAwsConfigWithoutProvidingCredentials(t *testing.T) { } func TestAwsConfigBySettingEnvVariables(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] @@ -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) } @@ -131,7 +132,7 @@ func TestAwsConfigBySettingEnvVariables(t *testing.T) { } func TestAwsConfigBySettingEmptyEnvVariables(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] @@ -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) } @@ -167,7 +169,7 @@ func TestAwsConfigBySettingEmptyEnvVariables(t *testing.T) { } func TestAwsConfigBySettingConfigFields(t *testing.T) { - c := newComponent() + c := NewConfig() config := ` component "external-dns" { sources = ["ingress"] @@ -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..af7dbc4c4 100644 --- a/pkg/components/flatcar-linux-update-operator/component.go +++ b/pkg/components/flatcar-linux-update-operator/component.go @@ -25,10 +25,17 @@ 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{}) +// NewConfig returns new Flatcar Linux Update Operator component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { + return &component{} } type component struct{} @@ -42,17 +49,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..c36900c5d 100644 --- a/pkg/components/gangway/component.go +++ b/pkg/components/gangway/component.go @@ -26,11 +26,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "gangway" - -func init() { //nolint:gochecknoinits - components.Register(name, newComponent()) -} +const ( + // Name represents Gangway component name as it should be referenced in function calls + // and in configuration. + Name = "gangway" +) type component struct { ClusterName string `hcl:"cluster_name,attr"` @@ -45,7 +45,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", } @@ -62,7 +65,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 +76,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 +86,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/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.go b/pkg/components/httpbin/component.go index 2d2bd5f9d..09d452553 100644 --- a/pkg/components/httpbin/component.go +++ b/pkg/components/httpbin/component.go @@ -26,18 +26,21 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "httpbin" - -func init() { //nolint:gochecknoinits - components.Register(name, newComponent()) -} +const ( + // Name represents httpbin component name as it should be referenced in function calls + // and in configuration. + Name = "httpbin" +) type component struct { IngressHost string `hcl:"ingress_host,attr"` 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", } @@ -54,7 +57,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 +68,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 +78,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_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.go b/pkg/components/inspektor-gadget/component.go index 30e48e82e..518101df5 100644 --- a/pkg/components/inspektor-gadget/component.go +++ b/pkg/components/inspektor-gadget/component.go @@ -26,19 +26,21 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "inspektor-gadget" - -//nolint:gochecknoinits -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents Inspektor Gadget component name as it should be referenced in function calls + // and in configuration. + Name = "inspektor-gadget" +) type component struct { Namespace string `hcl:"namespace,optional"` 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, @@ -58,7 +60,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 +70,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 +80,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/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/istio-operator/component.go b/pkg/components/istio-operator/component.go index f6a69bbc6..2ab97171b 100644 --- a/pkg/components/istio-operator/component.go +++ b/pkg/components/istio-operator/component.go @@ -27,21 +27,22 @@ 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()) -} - type component struct { Profile string `hcl:"profile,optional"` 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, @@ -75,7 +76,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 +86,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..1b5db9d64 100644 --- a/pkg/components/istio-operator/component_test.go +++ b/pkg/components/istio-operator/component_test.go @@ -51,8 +51,8 @@ func TestConversion(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - component := newComponent() - m := testutil.RenderManifests(t, component, name, tc.inputConfig) + component := NewConfig() + m := testutil.RenderManifests(t, component, Name, tc.inputConfig) gotConfig := testutil.ConfigFromMap(t, m, tc.expectedManifestName) testutil.MatchJSONPathStringValue(t, gotConfig, tc.jsonPath, tc.expected) @@ -65,7 +65,7 @@ func TestVerifyServiceMonitor(t *testing.T) { enable_monitoring = true }` - component := newComponent() - m := testutil.RenderManifests(t, component, name, inputConfig) + 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 5b6e04055..0080e285d 100644 --- a/pkg/components/linkerd/component.go +++ b/pkg/components/linkerd/component.go @@ -31,15 +31,13 @@ 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" ) -//nolint:gochecknoinits -func init() { - components.Register(name, newComponent()) -} - type component struct { ControllerReplicas int `hcl:"controller_replicas,optional"` EnableMonitoring bool `hcl:"enable_monitoring,optional"` @@ -55,7 +53,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, @@ -108,7 +109,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 +119,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..c2a883c41 100644 --- a/pkg/components/metallb/component.go +++ b/pkg/components/metallb/component.go @@ -26,11 +26,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "metallb" - -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents MetalLB component name as it should be referenced in function calls + // and in configuration. + Name = "metallb" +) type component struct { AddressPools map[string][]string `hcl:"address_pools"` @@ -44,7 +44,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{} } @@ -138,7 +141,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..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,9 +37,9 @@ 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) + 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..f9c766122 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 @@ -48,15 +52,14 @@ args: - --kubelet-preferred-address-types=InternalIP ` -func init() { - components.Register(name, newComponent()) -} - 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", } @@ -71,7 +74,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 +84,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 +94,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..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) @@ -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..124e81679 100644 --- a/pkg/components/openebs-operator/component.go +++ b/pkg/components/openebs-operator/component.go @@ -26,11 +26,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "openebs-operator" - -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents OpenEBS Operator component name as it should be referenced in function calls + // and in configuration. + Name = "openebs-operator" +) type component struct { NDMSelectorLabel string `hcl:"ndm_selector_label,optional"` @@ -88,7 +88,10 @@ webhook: {{- end }} ` -func newComponent() *component { +// NewConfig returns new OpenEBS Operator component configuration with default values set. +// +//nolint:golint +func NewConfig() *component { return &component{} } @@ -102,7 +105,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 +115,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 +125,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..72b77f18b 100644 --- a/pkg/components/openebs-storage-class/component.go +++ b/pkg/components/openebs-storage-class/component.go @@ -27,14 +27,13 @@ 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()) -} - type Storageclass struct { Name string `hcl:"name,label"` ReplicaCount int `hcl:"replica_count,optional"` @@ -58,7 +57,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{}, } @@ -107,7 +109,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 +129,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 +145,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..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,9 +68,9 @@ func TestUserInputValues(t *testing.T) { } func testRenderManifest(t *testing.T, configHCL string) { - component := newComponent() + component := NewConfig() - 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..6f486bd92 100644 --- a/pkg/components/prometheus-operator/component.go +++ b/pkg/components/prometheus-operator/component.go @@ -28,11 +28,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "prometheus-operator" - -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents Prometheus Operator component name as it should be referenced in function calls + // and in configuration. + Name = "prometheus-operator" +) // Monitor holds information about which Kubernetes components should be monitored with the default Prometheus instance. type Monitor struct { @@ -90,7 +90,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: @@ -192,7 +195,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 +205,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 +215,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..edc918f7f 100644 --- a/pkg/components/prometheus-operator/component_test.go +++ b/pkg/components/prometheus-operator/component_test.go @@ -96,12 +96,12 @@ 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) } - c := newComponent() + c := NewConfig() d = c.LoadConfig(b, nil) if !tc.wantErr && d.HasErrors() { @@ -208,8 +208,8 @@ providers: t.Run(tc.name, func(t *testing.T) { t.Parallel() - component := newComponent() - m := testutil.RenderManifests(t, component, name, tc.inputConfig) + component := NewConfig() + 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..24f9fb3aa 100644 --- a/pkg/components/rook-ceph/component.go +++ b/pkg/components/rook-ceph/component.go @@ -26,11 +26,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "rook-ceph" - -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents Rook Ceph component name as it should be referenced in function calls + // and in configuration. + Name = "rook-ceph" +) type component struct { Namespace string `hcl:"namespace,optional"` @@ -49,7 +49,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, @@ -91,7 +94,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..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,9 +62,9 @@ component "rook-ceph" { } ` - component := newComponent() + component := NewConfig() - 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..19aa379e8 100644 --- a/pkg/components/rook/component.go +++ b/pkg/components/rook/component.go @@ -27,11 +27,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "rook" - -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents Rook component name as it should be referenced in function calls + // and in configuration. + Name = "rook" +) type component struct { Namespace string `hcl:"namespace,optional"` @@ -51,7 +51,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", } @@ -66,7 +69,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 +98,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 +108,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/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/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 7cee0849a..1645c6a68 100644 --- a/pkg/components/velero/component.go +++ b/pkg/components/velero/component.go @@ -30,12 +30,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "velero" - -// init registers velero component to components list, so it shows up as available to install -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents Velero component name as it should be referenced in function calls + // and in configuration. + Name = "velero" +) // component represents component configuration data type component struct { @@ -65,8 +64,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{ @@ -123,7 +124,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 +134,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 +206,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..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,9 +54,9 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -99,9 +99,9 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() - body, diagnostics := util.GetComponentBody(configHCL, name) + body, diagnostics := util.GetComponentBody(configHCL, Name) if diagnostics != nil { t.Fatalf("Error getting component body: %v", diagnostics) } @@ -130,9 +130,9 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() - body, d := util.GetComponentBody(configHCL, name) + body, d := util.GetComponentBody(configHCL, Name) if d != nil { t.Fatalf("Error getting component body: %v", d) } @@ -147,9 +147,9 @@ func TestRenderManifestNoProviderConfigured(t *testing.T) { component "velero" {} ` - component := newComponent() + component := NewConfig() - body, d := util.GetComponentBody(configHCL, name) + body, d := util.GetComponentBody(configHCL, Name) if d != nil { t.Fatalf("Error getting component body: %v", d) } @@ -174,9 +174,9 @@ component "velero" { } ` - component := newComponent() + component := NewConfig() - 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..54e53fd83 100644 --- a/pkg/components/web-ui/component.go +++ b/pkg/components/web-ui/component.go @@ -27,12 +27,11 @@ import ( "github.com/kinvolk/lokomotive/pkg/k8sutil" ) -const name = "web-ui" - -//nolint:gochecknoinits -func init() { - components.Register(name, newComponent()) -} +const ( + // Name represents Web UI component name as it should be referenced in function calls + // and in configuration. + Name = "web-ui" +) type oidc struct { ClientID string `hcl:"client_id"` @@ -46,7 +45,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", } @@ -73,7 +75,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 +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.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 +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/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 { diff --git a/pkg/platform/aks/aks.go b/pkg/platform/aks/aks.go index f872aee11..e36c29772 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" @@ -85,15 +86,15 @@ const ( kubernetesVersion = "1.18.10" ) -// init registers AKS as a platform. -func init() { //nolint:gochecknoinits - c := &config{ +// 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. diff --git a/pkg/platform/aws/aws.go b/pkg/platform/aws/aws.go index 3c912ccd6..89c106829 100644 --- a/pkg/platform/aws/aws.go +++ b/pkg/platform/aws/aws.go @@ -88,10 +88,10 @@ type config struct { KubeAPIServerExtraFlags []string } -// init registers aws as a platform -func init() { - platform.Register("aws", NewConfig()) -} +const ( + // Name represents AWS platform name as it should be referenced in function calls and configuration. + Name = "aws" +) func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { diff --git a/pkg/platform/baremetal/baremetal.go b/pkg/platform/baremetal/baremetal.go index 6d0d49dbe..d066f45f6 100644 --- a/pkg/platform/baremetal/baremetal.go +++ b/pkg/platform/baremetal/baremetal.go @@ -60,10 +60,10 @@ type config struct { KubeAPIServerExtraFlags []string } -// init registers bare-metal as a platform -func init() { - platform.Register("bare-metal", NewConfig()) -} +const ( + // Name represents Bare Metal platform name as it should be referenced in function calls and configuration. + Name = "bare-metal" +) func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { diff --git a/pkg/platform/packet/packet.go b/pkg/platform/packet/packet.go index 0b76d0ede..dd88ae408 100644 --- a/pkg/platform/packet/packet.go +++ b/pkg/platform/packet/packet.go @@ -105,10 +105,10 @@ type config struct { NodesDependOn []string } -// init registers packet as a platform -func init() { - platform.Register("packet", NewConfig()) -} +const ( + // Name represents Packet platform name as it should be referenced in function calls and configuration. + Name = "packet" +) func (c *config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { 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 8297f67fd..621d82932 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"` @@ -89,11 +94,6 @@ func (w *WorkerPool) Name() string { return w.PoolName } -// init registers tinkerbell as a platform. -func init() { //nolint:gochecknoinits - platform.Register("tinkerbell", NewConfig()) -} - // LoadConfig loads platform configuration using given HCL structs. func (c *Config) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContext) hcl.Diagnostics { if configBody == nil { 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)