Skip to content

Commit

Permalink
refactor: Simplify struct used to configure the operator (#344)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
  • Loading branch information
danielpacak committed Jan 15, 2021
1 parent ac82c14 commit dbc3726
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/starboard-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/controller/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/job/job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
)

type JobController struct {
etc.Operator
etc.Config
client.Client
controller.Analyzer
controller.Reconciler
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/pod/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

type PodController struct {
etc.Operator
etc.Config
client.Client
controller.Analyzer
controller.Reconciler
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions pkg/operator/etc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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, ",")
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions pkg/operator/etc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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",
},
Expand All @@ -63,7 +63,7 @@ func TestOperator_GetInstallMode(t *testing.T) {
},
{
name: "Should resolve SingleNamespace",
operator: etc.Operator{
operator: etc.Config{
Namespace: "operators",
TargetNamespaces: "foo",
},
Expand All @@ -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",
},
Expand All @@ -81,7 +81,7 @@ func TestOperator_GetInstallMode(t *testing.T) {
},
{
name: "Should resolve AllNamespaces",
operator: etc.Operator{
operator: etc.Config{
Namespace: "operators",
TargetNamespaces: "",
},
Expand Down
18 changes: 9 additions & 9 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -124,20 +124,20 @@ 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,
scanner,
logs.NewReader(kubernetesClientset))

if err = (&pod.PodController{
Operator: operatorConfig.Operator,
Config: operatorConfig,
Client: mgr.GetClient(),
Analyzer: analyzer,
Reconciler: reconciler,
Expand All @@ -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,
Expand Down

0 comments on commit dbc3726

Please sign in to comment.