Skip to content

Commit

Permalink
Fix viper
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman committed Feb 27, 2024
1 parent 1da0584 commit 4cf8459
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 108 deletions.
12 changes: 12 additions & 0 deletions api/v1/create_job_coherencespec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ func TestCreateJobWithGlobalLabels(t *testing.T) {
labelsExpected["one"] = "value-one"
labelsExpected["two"] = "value-two"

podLabelsExpected := jobExpected.Spec.Template.Labels
podLabelsExpected["one"] = "value-one"
podLabelsExpected["two"] = "value-two"

// assert that the Job is as expected
assertJobCreation(t, deployment, jobExpected)
}
Expand Down Expand Up @@ -477,6 +481,14 @@ func TestCreateJobWithGlobalAnnotations(t *testing.T) {
annExpected["two"] = "value-two"
jobExpected.Annotations = annExpected

podAnnExpected := jobExpected.Spec.Template.Annotations
if podAnnExpected == nil {
podAnnExpected = make(map[string]string)
}
podAnnExpected["one"] = "value-one"
podAnnExpected["two"] = "value-two"
jobExpected.Spec.Template.Annotations = podAnnExpected

// assert that the Job is as expected
assertJobCreation(t, deployment, jobExpected)
}
26 changes: 19 additions & 7 deletions api/v1/create_statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,17 @@ func TestCreateStatefulSetWithGlobalLabels(t *testing.T) {
// Create the test deployment
deployment := createTestCoherenceDeployment(spec)
// Create expected StatefulSet
jobExpected := createMinimalExpectedStatefulSet(deployment)
labelsExpected := jobExpected.Labels
stsExpected := createMinimalExpectedStatefulSet(deployment)
labelsExpected := stsExpected.Labels
labelsExpected["one"] = "value-one"
labelsExpected["two"] = "value-two"

podLabelsExpected := stsExpected.Spec.Template.Labels
podLabelsExpected["one"] = "value-one"
podLabelsExpected["two"] = "value-two"

// assert that the Job is as expected
assertStatefulSetCreation(t, deployment, jobExpected)
assertStatefulSetCreation(t, deployment, stsExpected)
}

func TestCreateStatefulSetWithGlobalAnnotations(t *testing.T) {
Expand All @@ -700,15 +704,23 @@ func TestCreateStatefulSetWithGlobalAnnotations(t *testing.T) {
// Create the test deployment
deployment := createTestCoherenceDeployment(spec)
// Create expected Job
jobExpected := createMinimalExpectedStatefulSet(deployment)
annExpected := jobExpected.Annotations
stsExpected := createMinimalExpectedStatefulSet(deployment)
annExpected := stsExpected.Annotations
if annExpected == nil {
annExpected = make(map[string]string)
}
annExpected["one"] = "value-one"
annExpected["two"] = "value-two"
jobExpected.Annotations = annExpected
stsExpected.Annotations = annExpected

podAnnExpected := stsExpected.Spec.Template.Annotations
if podAnnExpected == nil {
podAnnExpected = make(map[string]string)
}
podAnnExpected["one"] = "value-one"
podAnnExpected["two"] = "value-two"
stsExpected.Spec.Template.Annotations = podAnnExpected

// assert that the Job is as expected
assertStatefulSetCreation(t, deployment, jobExpected)
assertStatefulSetCreation(t, deployment, stsExpected)
}
4 changes: 2 additions & 2 deletions controllers/webhook/webhook_controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -119,7 +119,7 @@ func (r *CertReconciler) Reconcile(ctx context.Context, request reconcile.Reques
// It also returns the duration after which a certificate rotation should be scheduled.
func (r *CertReconciler) ReconcileResources(ctx context.Context) error {
var err error
secretName := viper.GetString(operator.FlagWebhookSecret)
secretName := operator.GetViper().GetString(operator.FlagWebhookSecret)
namespace := operator.GetNamespace()
updateSecret := true

Expand Down
5 changes: 3 additions & 2 deletions pkg/runner/cmd_initialise.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand All @@ -11,6 +11,7 @@ import (
v1 "github.com/oracle/coherence-operator/api/v1"
"github.com/oracle/coherence-operator/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

Expand Down Expand Up @@ -186,7 +187,7 @@ func initialiseWithEnv(cmd *cobra.Command, getEnv EnvFunction) (bool, error) {
}
if len(c) != 0 {
fmt.Printf("Running post initialisation command: %s\n", c)
_, err = ExecuteWithArgs(nil, c)
_, err = ExecuteWithArgsAndViper(nil, c, viper.GetViper())
return true, err
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/runner/cmd_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBasicOperator(t *testing.T) {
args := []string{"operator", "--dry-run"}
env := EnvVarsFromDeployment(d)

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())

Expand All @@ -47,7 +47,7 @@ func TestOperatorWithSingleGlobalLabel(t *testing.T) {
args := []string{"operator", "--dry-run", "--global-label", "one=value-one"}
env := EnvVarsFromDeployment(d)

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())

Expand All @@ -71,7 +71,7 @@ func TestOperatorWithMultipleGlobalLabels(t *testing.T) {
}
env := EnvVarsFromDeployment(d)

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())

Expand All @@ -93,7 +93,7 @@ func TestOperatorWithSingleGlobalAnnotation(t *testing.T) {
args := []string{"operator", "--dry-run", "--global-annotation", "one=value-one"}
env := EnvVarsFromDeployment(d)

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())

Expand All @@ -117,7 +117,7 @@ func TestOperatorWithMultipleGlobalAnnotations(t *testing.T) {
}
env := EnvVarsFromDeployment(d)

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())

Expand Down
23 changes: 15 additions & 8 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ type Execution struct {
}

// NewRootCommand builds the root cobra command that handles our command line tool.
func NewRootCommand(env map[string]string) (*cobra.Command, *viper.Viper) {
v := viper.New()
func NewRootCommand(env map[string]string, v *viper.Viper) *cobra.Command {
operator.SetViper(v)

// rootCommand is the Cobra root Command to execute
Expand Down Expand Up @@ -127,7 +126,7 @@ func NewRootCommand(env map[string]string) (*cobra.Command, *viper.Viper) {
rootCmd.AddCommand(jShellCommand())
rootCmd.AddCommand(sleepCommand())

return rootCmd, v
return rootCmd
}

func initializeConfig(cmd *cobra.Command, v *viper.Viper, env map[string]string) error {
Expand Down Expand Up @@ -174,7 +173,10 @@ func initializeConfig(cmd *cobra.Command, v *viper.Viper, env map[string]string)

// Bind the current command's flags to viper
bindFlags(cmd, v)
_ = v.BindPFlags(cmd.Parent().Flags())
parent := cmd.Parent()
if parent != nil {
_ = v.BindPFlags(cmd.Parent().Flags())
}
_ = v.BindPFlags(cmd.PersistentFlags())
return nil
}
Expand All @@ -199,12 +201,17 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) {

// Execute runs the runner with a given environment.
func Execute() (Execution, error) {
return ExecuteWithArgs(nil, nil)
return ExecuteWithArgsAndViper(nil, nil, viper.GetViper())
}

// ExecuteWithArgsAndNewViper runs the runner with a given environment and argument overrides.
func ExecuteWithArgsAndNewViper(env map[string]string, args []string) (Execution, error) {
return ExecuteWithArgsAndViper(env, args, viper.New())
}

// ExecuteWithArgs runs the runner with a given environment and argument overrides.
func ExecuteWithArgs(env map[string]string, args []string) (Execution, error) {
cmd, v := NewRootCommand(env)
// ExecuteWithArgsAndViper runs the runner with a given environment and argument overrides.
func ExecuteWithArgsAndViper(env map[string]string, args []string, v *viper.Viper) (Execution, error) {
cmd := NewRootCommand(env, v)

if len(args) > 0 {
cmd.SetArgs(args)
Expand Down
12 changes: 6 additions & 6 deletions pkg/runner/runner_application_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestApplicationArgsEmpty(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := GetMinimalExpectedArgs()

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestApplicationArgs(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := append(GetMinimalExpectedArgs(), "Foo", "Bar")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestApplicationArgsWithEnvVarExpansion(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := append(GetMinimalExpectedArgs(), "foo-value", "bar-value")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestApplicationMain(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := ReplaceArg(GetMinimalExpectedArgs(), "$DEFAULT$", "com.oracle.test.Main")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestApplicationWorkingDirectory(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := GetMinimalExpectedArgsWithoutAppClasspath()

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down
18 changes: 9 additions & 9 deletions pkg/runner/runner_application_type_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -34,7 +34,7 @@ func TestApplicationTypeNone(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := GetMinimalExpectedArgs()

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestApplicationTypeNoneWithMain(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := ReplaceArg(GetMinimalExpectedArgs(), DefaultMain, "com.foo.Bar")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestApplicationTypeCoherence(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := GetMinimalExpectedArgs()

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestApplicationTypeCoherenceWithMain(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := ReplaceArg(GetMinimalExpectedArgs(), DefaultMain, "com.foo.Bar")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestApplicationTypeJava(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := GetMinimalExpectedArgs()

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestApplicationTypeJavaWithMain(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := ReplaceArg(GetMinimalExpectedArgs(), DefaultMain, "com.foo.Bar")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestApplicationTypeHelidon(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := ReplaceArg(GetMinimalExpectedArgs(), DefaultMain, HelidonMain)

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e).NotTo(BeNil())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestApplicationTypeHelidonWithMain(t *testing.T) {
expectedCommand := GetJavaCommand()
expectedArgs := ReplaceArg(GetMinimalExpectedArgs(), DefaultMain, "com.foo.Bar")

e, err := ExecuteWithArgs(env, args)
e, err := ExecuteWithArgsAndNewViper(env, args)
g.Expect(e).NotTo(BeNil())
g.Expect(err).NotTo(HaveOccurred())
g.Expect(e.OsCmd).NotTo(BeNil())
Expand Down
Loading

0 comments on commit 4cf8459

Please sign in to comment.