Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation of agent flag values for ConfigMap #16014

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion daemon/cmd/daemon_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func init() {
return
}

cobra.OnInitialize(option.InitConfig("Cilium", "ciliumd"))
cobra.OnInitialize(option.InitConfig(RootCmd, "Cilium", "ciliumd"))

// Reset the help function to also exit, as we block elsewhere in interrupts
// and would not exit when called with -h.
Expand Down
2 changes: 1 addition & 1 deletion operator/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func init() {
cobra.OnInitialize(option.InitConfig("Cilium-Operator", "cilium-operators"))
cobra.OnInitialize(option.InitConfig(rootCmd, "Cilium-Operator", "cilium-operators"))

flags := rootCmd.Flags()

Expand Down
31 changes: 28 additions & 3 deletions pkg/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/shirou/gopsutil/v3/mem"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -3164,8 +3165,27 @@ func sanitizeIntParam(paramName string, paramDefault int) int {
return intParam
}

// validateConfigmap checks whether the flag exists and validate the value of flag
func validateConfigmap(cmd *cobra.Command, m map[string]interface{}) (error, string) {
// validate the config-map
for key, value := range m {
if val := fmt.Sprintf("%v", value); val != "" {
flags := cmd.Flags()
// check whether the flag exists
if flag := flags.Lookup(key); flag != nil {
// validate the value of flag
if err := flag.Value.Set(val); err != nil {
pchaigno marked this conversation as resolved.
Show resolved Hide resolved
return err, key
}
}
}
}

pchaigno marked this conversation as resolved.
Show resolved Hide resolved
return nil, ""
}

// InitConfig reads in config file and ENV variables if set.
func InitConfig(programName, configName string) func() {
func InitConfig(cmd *cobra.Command, programName, configName string) func() {
return func() {
if viper.GetBool("version") {
fmt.Printf("%s %s\n", programName, version.Version)
Expand All @@ -3190,8 +3210,13 @@ func InitConfig(programName, configName string) func() {
} else {
// replace deprecated fields with new fields
ReplaceDeprecatedFields(m)
err := MergeConfig(m)
if err != nil {

// validate the config-map
if err, flag := validateConfigmap(cmd, m); err != nil {
log.WithError(err).Fatal("Incorrect config-map flag " + flag)
}

if err := MergeConfig(m); err != nil {
log.WithError(err).Fatal("Unable to merge configuration")
}
}
Expand Down