Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Don't use globals for platforms and components registration #1147

Merged
merged 19 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ea64f95
pkg/platform/aks: add NewConfig function to align with other platforms
invidian Oct 29, 2020
2bdf16b
pkg/platform/aks: export Name const
invidian Oct 29, 2020
79ad7c7
pkg/platform/{aws,baremetal,packet,tinkerbell}: add Name const
invidian Oct 29, 2020
392d1e9
Don't use globals for registering platforms configuration structs
invidian Oct 29, 2020
6fd7981
cli/cmd/cluster: remove unused anonymous imports
invidian Oct 29, 2020
633ea1a
pkg/components: export component names using Name constant
invidian Oct 29, 2020
0988eb5
pkg/components: rename newComponent() functions to NewConfig()
invidian Nov 2, 2020
3aaba38
pkg/components/flatcar-linux-update-operator: add NewConfig function
invidian Nov 2, 2020
26d5121
test/components: use NewConfig() instead of components.Get()
invidian Nov 2, 2020
1015951
pkg/components: use NewConfig() when getting default config for tests
invidian Nov 2, 2020
0d0db10
cli/cmd/cluster: drop dependency on pkg/components.ListNames()
invidian Nov 2, 2020
ac5dd1c
cli/cmd/cluster: use local componentConfig() instead of components.Get()
invidian Nov 2, 2020
f3403b2
pkg/components: remove Register(), Get() and other related functions
invidian Nov 2, 2020
b5e3db0
pkg/backend: rename functions returning new config to NewConfig()
invidian Nov 2, 2020
7c2c0dc
pkg/backend: export Name const for consistency
invidian Nov 2, 2020
1a039f6
cli/cmd/cluster: don't use backend.GetBackend for getting backend config
invidian Nov 2, 2020
c435f74
cli/cmd/cluster: move Backend interface definition to only consumer
invidian Nov 2, 2020
b9793df
Remove pkg/backend package
invidian Nov 2, 2020
f728c60
cli/cmd/cluster/cluster.go: remove unused anonymous import
invidian Dec 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions cli/cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/cluster/component-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions cli/cmd/cluster/component-render-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

log "github.com/sirupsen/logrus"

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/config"
)

Expand Down Expand Up @@ -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)
}
Expand Down
87 changes: 64 additions & 23 deletions cli/cmd/cluster/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
49 changes: 43 additions & 6 deletions cli/cmd/cluster/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -35,25 +41,56 @@ 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}
}

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 {
Expand All @@ -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,
Expand Down
27 changes: 0 additions & 27 deletions pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package backend

import (
"fmt"

"github.com/hashicorp/hcl/v2"
)

Expand All @@ -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
}
15 changes: 9 additions & 6 deletions pkg/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}
}

Expand Down
15 changes: 9 additions & 6 deletions pkg/backend/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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{}
}

Expand Down
Loading