-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Config.Validate() to allow validation of configuration opti…
…ons before starting the manager (#3405) * feat: add Config.Validate() to allow validation of configuration options before starting the manager * feat: add config validation unit tests and FlagNamespacedName * lint Co-authored-by: Grzegorz Burzyński <czeslavo@gmail.com>
- Loading branch information
Showing
11 changed files
with
279 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package rootcmd | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v2/internal/manager" | ||
) | ||
|
||
func TestRootCmd(t *testing.T) { | ||
t.Run("root command succeeds by default", func(t *testing.T) { | ||
var cfg manager.Config | ||
rootCmd := GetRootCmd(&cfg) | ||
require.NoError(t, rootCmd.PersistentPreRunE(rootCmd, os.Args[:0])) | ||
}) | ||
|
||
t.Run("root command succeeds when correct flags where provided", func(t *testing.T) { | ||
var cfg manager.Config | ||
rootCmd := GetRootCmd(&cfg) | ||
require.NoError(t, rootCmd.PersistentPreRunE(rootCmd, | ||
append(os.Args[:0], | ||
"--publish-service", "namespace/servicename", | ||
), | ||
)) | ||
}) | ||
|
||
t.Run("binding environment variables succeeds when flag validation passes", func(t *testing.T) { | ||
t.Setenv("CONTROLLER_PUBLISH_SERVICE", "namespace/servicename") | ||
var cfg manager.Config | ||
rootCmd := GetRootCmd(&cfg) | ||
require.NoError(t, rootCmd.PersistentPreRunE(rootCmd, os.Args[:0])) | ||
}) | ||
|
||
t.Run("binding environment variables fails when flag validation fails", func(t *testing.T) { | ||
t.Setenv("CONTROLLER_PUBLISH_SERVICE", "servicename") | ||
var cfg manager.Config | ||
rootCmd := GetRootCmd(&cfg) | ||
require.Error(t, rootCmd.PersistentPreRunE(rootCmd, os.Args[:0]), | ||
"binding env vars should fail because a non namespaced name of publish service was provided", | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package manager | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfigValidate(t *testing.T) { | ||
t.Run("--gateway-api-controller-name", func(t *testing.T) { | ||
t.Run("valid config", func(t *testing.T) { | ||
var c Config | ||
require.NoError(t, c.FlagSet().Parse( | ||
[]string{ | ||
os.Args[0], | ||
`--gateway-api-controller-name`, `example.com/controller-name`, | ||
}, | ||
)) | ||
require.NoError(t, c.Validate()) | ||
}) | ||
|
||
t.Run("invalid config", func(t *testing.T) { | ||
var c Config | ||
require.NoError(t, c.FlagSet().Parse( | ||
[]string{ | ||
os.Args[0], | ||
`--gateway-api-controller-name`, `%invalid_controller_name$`, | ||
}, | ||
)) | ||
require.Error(t, c.Validate()) | ||
}) | ||
}) | ||
|
||
t.Run("--publish-service", func(t *testing.T) { | ||
t.Run("valid config", func(t *testing.T) { | ||
var c Config | ||
require.NoError(t, c.FlagSet().Parse( | ||
[]string{ | ||
os.Args[0], | ||
`--publish-service`, `namespace/servicename`, | ||
}, | ||
)) | ||
require.NoError(t, c.Validate()) | ||
}) | ||
|
||
t.Run("invalid config", func(t *testing.T) { | ||
var c Config | ||
require.Error(t, c.FlagSet().Parse( | ||
[]string{ | ||
os.Args[0], | ||
`--publish-service`, `servicename`, | ||
}, | ||
)) | ||
// publish service is validated through FlagNamespacedName validation logic. | ||
require.NoError(t, c.Validate()) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package manager | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// FlagNamespacedName allows parsing command line flags straight to types.NamespacedName. | ||
type FlagNamespacedName struct { | ||
NN types.NamespacedName | ||
} | ||
|
||
func (f *FlagNamespacedName) String() string { | ||
return f.NN.String() | ||
} | ||
|
||
func (f *FlagNamespacedName) Set(v string) error { | ||
s := strings.SplitN(v, "/", 3) | ||
if len(s) != 2 { | ||
return fmt.Errorf("namespaced name should be in the format: <namespace>/<name>") | ||
} | ||
f.NN = types.NamespacedName{ | ||
Namespace: s[0], | ||
Name: s[1], | ||
} | ||
return nil | ||
} | ||
|
||
func (f *FlagNamespacedName) Type() string { | ||
return "FlagNamespacedName" | ||
} |
Oops, something went wrong.