Repository: aquasecurity/starboard
Pull request #344 (2021-01-15 22:30:03)

refactor: Simplify struct used to configure the operator
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>

codecov[bot]: # [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=h1) Report
> Merging [#344](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=desc) (7cbe500) into [main](https://codecov.io/gh/aquasecurity/starboard/commit/ac82c147c9b249f99f83a73295549040af187202?el=desc) (ac82c14) will **decrease** coverage by `35.54%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/aquasecurity/starboard/pull/344/graphs/tree.svg?width=650&height=150&src=pr&token=qqTGKAJ0Wb)](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##             main     #344       +/-   ##
===========================================
- Coverage   65.68%   30.14%   -35.55%     
===========================================
  Files          57       53        -4     
  Lines        2818     2601      -217     
===========================================
- Hits         1851      784     -1067     
- Misses        725     1759     +1034     
+ Partials      242       58      -184     
```


| [Impacted Files](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/operator/controller/analyzer.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvYW5hbHl6ZXIuZ28=) | `0.00% <ø> (-70.28%)` | :arrow_down: |
| [pkg/operator/controller/reconciler.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvcmVjb25jaWxlci5nbw==) | `0.00% <ø> (-58.47%)` | :arrow_down: |
| [pkg/operator/etc/config.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL29wZXJhdG9yL2V0Yy9jb25maWcuZ28=) | `72.72% <ø> (ø)` | |
| [pkg/cmd/get.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC9nZXQuZ28=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pkg/cmd/find.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC9maW5kLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pkg/cmd/root.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC9yb290Lmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pkg/cmd/scan.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC9zY2FuLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pkg/cmd/polaris.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC9wb2xhcmlzLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pkg/cmd/version.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC92ZXJzaW9uLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [pkg/cmd/kube\_bench.go](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree#diff-cGtnL2NtZC9rdWJlX2JlbmNoLmdv) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| ... and [29 more](https://codecov.io/gh/aquasecurity/starboard/pull/344/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=footer). Last update [ac82c14...7cbe500](https://codecov.io/gh/aquasecurity/starboard/pull/344?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cmd/starboard-operator/main.go b/cmd/starboard-operator/main.go
index edc4779..f207026 100644
--- a/cmd/starboard-operator/main.go
+++ b/cmd/starboard-operator/main.go
@@ -40,7 +40,7 @@ func run() error {
 		return fmt.Errorf("getting operator config: %w", err)
 	}
 
-	log.SetLogger(zap.New(zap.UseDevMode(operatorConfig.Operator.LogDevMode)))
+	log.SetLogger(zap.New(zap.UseDevMode(operatorConfig.LogDevMode)))
 
 	setupLog.Info("Starting operator", "buildInfo", buildInfo)
 
diff --git a/go.mod b/go.mod
index 09f1dd2..f4b589b 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/aquasecurity/starboard
 go 1.15
 
 require (
-	github.com/caarlos0/env/v6 v6.3.0
+	github.com/caarlos0/env/v6 v6.4.0
 	github.com/davecgh/go-spew v1.1.1
 	github.com/google/go-containerregistry v0.1.1
 	github.com/google/uuid v1.1.1
diff --git a/go.sum b/go.sum
index 78f5489..26953e5 100644
--- a/go.sum
+++ b/go.sum
@@ -145,8 +145,8 @@ github.com/bombsimon/wsl/v2 v2.2.0/go.mod h1:Azh8c3XGEJl9LyX0/sFC+CKMc7Ssgua0g+6
 github.com/bombsimon/wsl/v3 v3.0.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2Pm1FNZIc=
 github.com/bombsimon/wsl/v3 v3.1.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2Pm1FNZIc=
 github.com/caarlos0/ctrlc v1.0.0/go.mod h1:CdXpj4rmq0q/1Eb44M9zi2nKB0QraNKuRGYGrrHhcQw=
-github.com/caarlos0/env/v6 v6.3.0 h1:PaqGnS5iHScZ5SnZNBPvQbA2VE/eMAwlp51mKGuEZLg=
-github.com/caarlos0/env/v6 v6.3.0/go.mod h1:nXKfztzgWXH0C5Adnp+gb+vXHmMjKdBnMrSVSczSkiw=
+github.com/caarlos0/env/v6 v6.4.0 h1:fUo2hQNR3O7Yb7E2sYy8cxY42BRvFxWa0G4XBMLJAQM=
+github.com/caarlos0/env/v6 v6.4.0/go.mod h1:MX/8qQ2zCofGGkb7FxjmDLOOjUylO2b7dbsIpN30bnY=
 github.com/campoy/unique v0.0.0-20180121183637-88950e537e7e/go.mod h1:9IOqJGCPMSc6E5ydlp5NIonxObaeu/Iub/X03EKPVYo=
 github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e/go.mod h1:oDpT4efm8tSYHXV5tHSdRvBet/b/QzxZ+XyyPehvm3A=
 github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -707,6 +707,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
+github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
diff --git a/pkg/operator/controller/analyzer.go b/pkg/operator/controller/analyzer.go
index 3dbc177..66acc1f 100644
--- a/pkg/operator/controller/analyzer.go
+++ b/pkg/operator/controller/analyzer.go
@@ -18,7 +18,7 @@ type Analyzer interface {
 	IsConcurrentScanJobsLimitExceeded(ctx context.Context) (bool, int, error)
 }
 
-func NewAnalyzer(config etc.Operator, store vulnerabilityreport.ReadWriter, client client.Client) Analyzer {
+func NewAnalyzer(config etc.Config, store vulnerabilityreport.ReadWriter, client client.Client) Analyzer {
 	return &analyzer{
 		config: config,
 		store:  store,
@@ -27,7 +27,7 @@ func NewAnalyzer(config etc.Operator, store vulnerabilityreport.ReadWriter, clie
 }
 
 type analyzer struct {
-	config etc.Operator
+	config etc.Config
 	client client.Client
 	store  vulnerabilityreport.ReadWriter
 }
diff --git a/pkg/operator/controller/job/job_controller.go b/pkg/operator/controller/job/job_controller.go
index 17341ca..8ef4eef 100644
--- a/pkg/operator/controller/job/job_controller.go
+++ b/pkg/operator/controller/job/job_controller.go
@@ -25,7 +25,7 @@ var (
 )
 
 type JobController struct {
-	etc.Operator
+	etc.Config
 	client.Client
 	controller.Analyzer
 	controller.Reconciler
diff --git a/pkg/operator/controller/pod/pod_controller.go b/pkg/operator/controller/pod/pod_controller.go
index 6f5212a..5bc8132 100644
--- a/pkg/operator/controller/pod/pod_controller.go
+++ b/pkg/operator/controller/pod/pod_controller.go
@@ -22,7 +22,7 @@ var (
 )
 
 type PodController struct {
-	etc.Operator
+	etc.Config
 	client.Client
 	controller.Analyzer
 	controller.Reconciler
diff --git a/pkg/operator/controller/reconciler.go b/pkg/operator/controller/reconciler.go
index 4214b4b..baece55 100644
--- a/pkg/operator/controller/reconciler.go
+++ b/pkg/operator/controller/reconciler.go
@@ -32,7 +32,7 @@ type Reconciler interface {
 }
 
 func NewReconciler(scheme *runtime.Scheme,
-	config etc.Operator,
+	config etc.Config,
 	client client.Client,
 	store vulnerabilityreport.ReadWriter,
 	idGenerator ext.IDGenerator,
@@ -53,7 +53,7 @@ func NewReconciler(scheme *runtime.Scheme,
 
 type reconciler struct {
 	scheme      *runtime.Scheme
-	config      etc.Operator
+	config      etc.Config
 	client      client.Client
 	store       vulnerabilityreport.ReadWriter
 	idGenerator ext.IDGenerator
diff --git a/pkg/operator/etc/config.go b/pkg/operator/etc/config.go
index ac19acc..e6e2550 100644
--- a/pkg/operator/etc/config.go
+++ b/pkg/operator/etc/config.go
@@ -9,10 +9,6 @@ import (
 )
 
 type Config struct {
-	Operator Operator
-}
-
-type Operator struct {
 	Namespace               string        `env:"OPERATOR_NAMESPACE"`
 	TargetNamespaces        string        `env:"OPERATOR_TARGET_NAMESPACES"`
 	ServiceAccount          string        `env:"OPERATOR_SERVICE_ACCOUNT" envDefault:"starboard-operator"`
@@ -31,7 +27,7 @@ func GetOperatorConfig() (Config, error) {
 }
 
 // GetOperatorNamespace returns the namespace the operator should be running in.
-func (c Operator) GetOperatorNamespace() (string, error) {
+func (c Config) GetOperatorNamespace() (string, error) {
 	namespace := c.Namespace
 	if namespace != "" {
 		return namespace, nil
@@ -40,7 +36,7 @@ func (c Operator) GetOperatorNamespace() (string, error) {
 }
 
 // GetTargetNamespaces returns namespaces the operator should be watching for changes.
-func (c Operator) GetTargetNamespaces() []string {
+func (c Config) GetTargetNamespaces() []string {
 	namespaces := c.TargetNamespaces
 	if namespaces != "" {
 		return strings.Split(namespaces, ",")
@@ -59,7 +55,7 @@ const (
 )
 
 // GetInstallMode resolves InstallMode based on configured operator and target namespaces.
-func (c Operator) GetInstallMode() (InstallMode, error) {
+func (c Config) GetInstallMode() (InstallMode, error) {
 	operatorNamespace, err := c.GetOperatorNamespace()
 	if err != nil {
 		return "", nil
diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go
index 417abf3..475cc5a 100644
--- a/pkg/operator/operator.go
+++ b/pkg/operator/operator.go
@@ -29,14 +29,14 @@ func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
 	setupLog.Info("Starting operator", "buildInfo", buildInfo)
 
 	// Validate configured namespaces to resolve install mode.
-	operatorNamespace, err := operatorConfig.Operator.GetOperatorNamespace()
+	operatorNamespace, err := operatorConfig.GetOperatorNamespace()
 	if err != nil {
 		return fmt.Errorf("getting operator namespace: %w", err)
 	}
 
-	targetNamespaces := operatorConfig.Operator.GetTargetNamespaces()
+	targetNamespaces := operatorConfig.GetTargetNamespaces()
 
-	installMode, err := operatorConfig.Operator.GetInstallMode()
+	installMode, err := operatorConfig.GetInstallMode()
 	if err != nil {
 		return fmt.Errorf("getting install mode: %w", err)
 	}
@@ -47,8 +47,8 @@ func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
 	// Set the default manager options.
 	options := manager.Options{
 		Scheme:                 starboard.NewScheme(),
-		MetricsBindAddress:     operatorConfig.Operator.MetricsBindAddress,
-		HealthProbeBindAddress: operatorConfig.Operator.HealthProbeBindAddress,
+		MetricsBindAddress:     operatorConfig.MetricsBindAddress,
+		HealthProbeBindAddress: operatorConfig.HealthProbeBindAddress,
 	}
 
 	switch installMode {
@@ -124,12 +124,12 @@ func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
 		return err
 	}
 
-	analyzer := controller.NewAnalyzer(operatorConfig.Operator,
+	analyzer := controller.NewAnalyzer(operatorConfig,
 		store,
 		mgr.GetClient())
 
 	reconciler := controller.NewReconciler(mgr.GetScheme(),
-		operatorConfig.Operator,
+		operatorConfig,
 		mgr.GetClient(),
 		store,
 		idGenerator,
@@ -137,7 +137,7 @@ func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
 		logs.NewReader(kubernetesClientset))
 
 	if err = (&pod.PodController{
-		Operator:   operatorConfig.Operator,
+		Config:     operatorConfig,
 		Client:     mgr.GetClient(),
 		Analyzer:   analyzer,
 		Reconciler: reconciler,
@@ -146,7 +146,7 @@ func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
 	}
 
 	if err = (&job.JobController{
-		Operator:   operatorConfig.Operator,
+		Config:     operatorConfig,
 		Client:     mgr.GetClient(),
 		Analyzer:   analyzer,
 		Reconciler: reconciler,


Test patch:
diff --git a/pkg/operator/etc/config_test.go b/pkg/operator/etc/config_test.go
index e974e8b..5c485f6 100644
--- a/pkg/operator/etc/config_test.go
+++ b/pkg/operator/etc/config_test.go
@@ -11,26 +11,26 @@ import (
 func TestOperator_GetTargetNamespaces(t *testing.T) {
 	testCases := []struct {
 		name                     string
-		operator                 etc.Operator
+		operator                 etc.Config
 		expectedTargetNamespaces []string
 	}{
 		{
 			name: "Should return all namespaces",
-			operator: etc.Operator{
+			operator: etc.Config{
 				TargetNamespaces: "",
 			},
 			expectedTargetNamespaces: []string{},
 		},
 		{
 			name: "Should return single namespace",
-			operator: etc.Operator{
+			operator: etc.Config{
 				TargetNamespaces: "operators",
 			},
 			expectedTargetNamespaces: []string{"operators"},
 		},
 		{
 			name: "Should return multiple namespaces",
-			operator: etc.Operator{
+			operator: etc.Config{
 				TargetNamespaces: "foo,bar,baz",
 			},
 			expectedTargetNamespaces: []string{"foo", "bar", "baz"},
@@ -48,13 +48,13 @@ func TestOperator_GetInstallMode(t *testing.T) {
 	testCases := []struct {
 		name string
 
-		operator            etc.Operator
+		operator            etc.Config
 		expectedInstallMode etc.InstallMode
 		expectedError       string
 	}{
 		{
 			name: "Should resolve OwnNamespace",
-			operator: etc.Operator{
+			operator: etc.Config{
 				Namespace:        "operators",
 				TargetNamespaces: "operators",
 			},
@@ -63,7 +63,7 @@ func TestOperator_GetInstallMode(t *testing.T) {
 		},
 		{
 			name: "Should resolve SingleNamespace",
-			operator: etc.Operator{
+			operator: etc.Config{
 				Namespace:        "operators",
 				TargetNamespaces: "foo",
 			},
@@ -72,7 +72,7 @@ func TestOperator_GetInstallMode(t *testing.T) {
 		},
 		{
 			name: "Should resolve MultiNamespace",
-			operator: etc.Operator{
+			operator: etc.Config{
 				Namespace:        "operators",
 				TargetNamespaces: "foo,bar,baz",
 			},
@@ -81,7 +81,7 @@ func TestOperator_GetInstallMode(t *testing.T) {
 		},
 		{
 			name: "Should resolve AllNamespaces",
-			operator: etc.Operator{
+			operator: etc.Config{
 				Namespace:        "operators",
 				TargetNamespaces: "",
 			},