From 2aabd556012cd5a967ab8775a9c06d0178c29d71 Mon Sep 17 00:00:00 2001 From: Tom Hadlaw Date: Tue, 30 Jul 2024 09:35:42 -0700 Subject: [PATCH] test: add ability to override/add extra helm flags for ginkgo tests. This adds -cilium.install-helm-overrides flag which provides a way to add or override test flags without modifying the test code. This is useful for forked projects to extend testing by providing their own custom flags to the test program. For example, you could extend the tests by doing: -cilium.install-helm-overrides="foo=bar,\"zap={a,b,c}\"" which would add: --set foo=bar and --set zap={a,b,c} to the cilium install params. Signed-off-by: Tom Hadlaw --- test/config/config.go | 5 +++++ test/helpers/kubectl.go | 36 ++++++++++++++++++++++++++++++++++++ test/helpers/kubectl_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test/helpers/kubectl_test.go diff --git a/test/config/config.go b/test/config/config.go index cd1f13ce9de83..7be54f6734c0c 100644 --- a/test/config/config.go +++ b/test/config/config.go @@ -40,6 +40,8 @@ type CiliumTestConfigType struct { Kubeconfig string KubectlPath string RegistryCredentials string + InstallHelmOverrides string + // Multinode enables the running of tests that involve more than one // node. If false, some tests will silently skip multinode checks. Multinode bool @@ -97,6 +99,9 @@ func (c *CiliumTestConfigType) ParseFlags() { flagset.BoolVar(&c.RunQuarantined, "cilium.runQuarantined", false, "Run tests that are under quarantine.") flagset.BoolVar(&c.Help, "cilium.help", false, "Display this help message.") + flagset.StringVar(&c.InstallHelmOverrides, "cilium.install-helm-overrides", "", + "Comma separated list of cilium install helm --set overrides. "+ + "*note*: This will take precedence over any other value set by the tests") args := make([]string, 0, len(os.Args)) for index, flag := range os.Args { diff --git a/test/helpers/kubectl.go b/test/helpers/kubectl.go index a63b486a70e8d..c9913751acfb9 100644 --- a/test/helpers/kubectl.go +++ b/test/helpers/kubectl.go @@ -6,9 +6,11 @@ package helpers import ( "bytes" "context" + "encoding/csv" "encoding/json" "errors" "fmt" + "io" "net" "os" "path" @@ -78,6 +80,9 @@ const ( ) var ( + // cliOverrideOptions are populated from -cilium.install-helm-overrides. + cliOverrideOptions = map[string]string{} + // defaultHelmOptions are passed to helm in ciliumInstallHelm, unless // overridden by options passed in at invocation. In those cases, the test // has a specific need to override the option. @@ -280,6 +285,33 @@ func Init() { // preflight must match the cilium agent image (that's the point) defaultHelmOptions["preflight.image.repository"] = defaultHelmOptions["image.repository"] defaultHelmOptions["preflight.image.tag"] = defaultHelmOptions["image.tag"] + + if config.CiliumTestConfig.InstallHelmOverrides != "" { + parseHelmOverrides(config.CiliumTestConfig.InstallHelmOverrides, cliOverrideOptions) + } +} + +func parseHelmOverrides(overridesStr string, overrides map[string]string) { + rdr := csv.NewReader(strings.NewReader(overridesStr)) + rdr.LazyQuotes = true + for { + record, err := rdr.Read() + if err != nil { + if errors.Is(err, io.EOF) { + break + } + fmt.Fprintf(os.Stderr, "failed to parse install-helm-overrides %q: %v\n", config.CiliumTestConfig.InstallHelmOverrides, err) + os.Exit(1) + } + for _, row := range record { + toks := strings.Split(row, "=") + if len(toks) != 2 { + fmt.Fprintf(os.Stderr, "failed to parse install-helm-overrides value %q, unexpected format (should be x.y.z=foo)\n", row) + os.Exit(1) + } + overrides[toks[0]] = toks[1] + } + } } // GetCurrentK8SEnv returns the value of K8S_VERSION from the OS environment. @@ -2602,6 +2634,10 @@ func (kub *Kubectl) overwriteHelmOptions(options map[string]string) error { options["ipv6.enabled"] = "false" } + for k, v := range cliOverrideOptions { + options[k] = v + } + return nil } diff --git a/test/helpers/kubectl_test.go b/test/helpers/kubectl_test.go new file mode 100644 index 0000000000000..3ce53dbbf0e40 --- /dev/null +++ b/test/helpers/kubectl_test.go @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Authors of Cilium + +package helpers + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_parseHelmOverrides(t *testing.T) { + // This should be able to parse key value pairs, including those + // where the value has commas but is delimited by quotes. + overridesStr := "key1=value1,\"key2={a,b,c}\",\"key3={}\"" + expected := map[string]string{ + "key1": "value1", + "key2": "{a,b,c}", + "key3": "{}", + } + overrides := map[string]string{} + parseHelmOverrides(overridesStr, overrides) + assert.Equal(t, expected, overrides) +}