From 4beacbb95d7489531e39bd936086f63140f81e92 Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Tue, 6 Feb 2024 11:30:43 +0100 Subject: [PATCH] config: fix processing of reference container because of a bug, a missing --reference-container flag used to clear the value set through env vars. Now we preserve the value unless explicitely overriden, as per intention Take the chance to keep improving the tests and the coverage. Signed-off-by: Francesco Romani --- pkg/config/config_test.go | 108 ++++++++++++++++++ pkg/config/flags.go | 16 ++- test/data/TestLoadDefaults.expected.json | 2 +- test/data/conftree/05-full-env/_env/vars.yaml | 3 + .../conftree/05-full-env/_output/output.yaml | 32 ++++++ .../conftree/05-full-env/daemon/config.yaml | 14 +++ .../daemon/config.yaml.d/00-verbose.yaml | 3 + .../daemon/config.yaml.d/30-pfp.yaml | 3 + .../conftree/05-full-env/extra/config.yaml | 5 + .../06-full-env-args/_cmdline/flags.yaml | 1 + .../conftree/06-full-env-args/_env/vars.yaml | 3 + .../06-full-env-args/_output/output.yaml | 32 ++++++ .../06-full-env-args/daemon/config.yaml | 14 +++ .../daemon/config.yaml.d/00-verbose.yaml | 3 + .../daemon/config.yaml.d/30-pfp.yaml | 3 + .../06-full-env-args/extra/config.yaml | 5 + 16 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 pkg/config/config_test.go create mode 100644 test/data/conftree/05-full-env/_env/vars.yaml create mode 100644 test/data/conftree/05-full-env/_output/output.yaml create mode 100644 test/data/conftree/05-full-env/daemon/config.yaml create mode 100644 test/data/conftree/05-full-env/daemon/config.yaml.d/00-verbose.yaml create mode 100644 test/data/conftree/05-full-env/daemon/config.yaml.d/30-pfp.yaml create mode 100644 test/data/conftree/05-full-env/extra/config.yaml create mode 100644 test/data/conftree/06-full-env-args/_cmdline/flags.yaml create mode 100644 test/data/conftree/06-full-env-args/_env/vars.yaml create mode 100644 test/data/conftree/06-full-env-args/_output/output.yaml create mode 100644 test/data/conftree/06-full-env-args/daemon/config.yaml create mode 100644 test/data/conftree/06-full-env-args/daemon/config.yaml.d/00-verbose.yaml create mode 100644 test/data/conftree/06-full-env-args/daemon/config.yaml.d/30-pfp.yaml create mode 100644 test/data/conftree/06-full-env-args/extra/config.yaml diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 000000000..cbaa957db --- /dev/null +++ b/pkg/config/config_test.go @@ -0,0 +1,108 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "sigs.k8s.io/yaml" +) + +func TestLoadArgs(t *testing.T) { + type testCase struct { + name string + } + + for _, tcase := range []testCase{ + { + name: "05-full-env", + }, + { + name: "06-full-env-args", + }, + } { + t.Run(tcase.name, func(t *testing.T) { + confRoot := filepath.Join(testDataDir, "conftree", tcase.name) + + environ := filepath.Join(confRoot, "_env", "vars.yaml") + setupEnviron(t, environ) + + cmdline := filepath.Join(confRoot, "_cmdline", "flags.yaml") + args := setupCmdline(t, cmdline) + args = append(args, confRoot) // always last + + pArgs, err := LoadArgs(args...) + if err != nil { + t.Fatalf("LoadArgs(%s) failed: %v", confRoot, err) + } + + golden := filepath.Join(confRoot, "_output", "output.yaml") + + expectedRaw, err := os.ReadFile(golden) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + got := strings.TrimSpace(string(pArgs.ToYAMLString())) + expected := strings.TrimSpace(string(expectedRaw)) + if got != expected { + t.Errorf("invalid defaults.\n>>> got:\n{%s}\n>>> expected:\n{%s}", got, expected) + } + }) + } +} + +func setupEnviron(t *testing.T, envDefsPath string) { + t.Helper() + data, err := os.ReadFile(envDefsPath) + if err != nil { + // intentionally swallow + return + } + var envVars map[string]string + err = yaml.Unmarshal(data, &envVars) + if err != nil { + t.Logf("error getting environment definitions from %q: %v", envDefsPath, err) + // intentionally swallow + return + } + for key, val := range envVars { + t.Logf("setup environ: %q -> %q", key, val) + t.Setenv(key, val) + } +} + +func setupCmdline(t *testing.T, cmdlineDefsPath string) []string { + t.Helper() + var flags []string + data, err := os.ReadFile(cmdlineDefsPath) + if err != nil { + // intentionally swallow + return flags + } + err = yaml.Unmarshal(data, &flags) + if err != nil { + t.Logf("error getting commandline flags from %q: %v", cmdlineDefsPath, err) + // intentionally swallow + return flags + } + t.Logf("using command line: %q", flags) + return flags +} diff --git a/pkg/config/flags.go b/pkg/config/flags.go index 413a22758..fad65eb47 100644 --- a/pkg/config/flags.go +++ b/pkg/config/flags.go @@ -29,6 +29,7 @@ import ( ) func FromFlags(pArgs *ProgArgs, args ...string) (string, string, error) { + var refCnt string var configPath string flags := flag.NewFlagSet(version.ProgramName, flag.ExitOnError) @@ -62,7 +63,7 @@ func FromFlags(pArgs *ProgArgs, args ...string) (string, string, error) { flags.BoolVar(&pArgs.RTE.AddNRTOwnerEnable, "add-nrt-owner", pArgs.RTE.AddNRTOwnerEnable, "RTE will inject NRT's related node as OwnerReference to ensure cleanup if the node is deleted.") flags.StringVar(&pArgs.RTE.MetricsMode, "metrics-mode", pArgs.RTE.MetricsMode, fmt.Sprintf("Select the mode to expose metrics endpoint. Valid options: %s", metricssrv.ServingModeSupported())) - refCnt := flags.String("reference-container", "", "Reference container, used to learn about the shared cpu pool\n See: https://github.com/kubernetes/kubernetes/issues/102190\n format of spec is namespace/podname/containername.\n Alternatively, you can use the env vars REFERENCE_NAMESPACE, REFERENCE_POD_NAME, REFERENCE_CONTAINER_NAME.") + flags.StringVar(&refCnt, "reference-container", pArgs.RTE.ReferenceContainer.String(), "Reference container, used to learn about the shared cpu pool\n See: https://github.com/kubernetes/kubernetes/issues/102190\n format of spec is namespace/podname/containername.\n Alternatively, you can use the env vars REFERENCE_NAMESPACE, REFERENCE_POD_NAME, REFERENCE_CONTAINER_NAME.") flags.StringVar(&pArgs.RTE.NotifyFilePath, "notify-file", pArgs.RTE.NotifyFilePath, "Notification file path.") // Lets keep it simple by now and expose only "events-per-second" @@ -87,12 +88,17 @@ Special targets: return DefaultConfigRoot, LegacyExtraConfigPath, err } - pArgs.RTE.ReferenceContainer, err = sharedcpuspool.ContainerIdentFromString(*refCnt) - if err != nil { - return DefaultConfigRoot, LegacyExtraConfigPath, err + if pArgs.Global.Debug { + klog.Infof("using reference container: %q", refCnt) + } + if refCnt != "" { + pArgs.RTE.ReferenceContainer, err = sharedcpuspool.ContainerIdentFromString(refCnt) + if err != nil { + return DefaultConfigRoot, LegacyExtraConfigPath, err + } } if pArgs.Global.Debug { - klog.Infof("reference container: %+v", pArgs.RTE.ReferenceContainer) + klog.Infof("reference container: %q", pArgs.RTE.ReferenceContainer.String()) } params := flags.Args() diff --git a/test/data/TestLoadDefaults.expected.json b/test/data/TestLoadDefaults.expected.json index b24e47e85..47cfe5418 100644 --- a/test/data/TestLoadDefaults.expected.json +++ b/test/data/TestLoadDefaults.expected.json @@ -1 +1 @@ -{"global":{"verbose":2},"nrtUpdater":{"hostname":"TEST_NODE"},"resourceMonitor":{"sysfsRoot":"/sys","podSetFingerprint":true,"podSetFingerprintMethod":"with-exclusive-resources"},"topologyExporter":{"kubeletConfigFile":"/podresources/config.yaml","podResourcesSocketPath":"unix:///podresources/kubelet.sock","sleepInterval":60000000000,"podReadinessEnable":true,"maxEventPerTimeUnit":1,"timeUnitToLimitEvents":1000000000,"addNRTOwnerEnable":true,"metricsMode":"disabled"}} \ No newline at end of file +{"global":{"verbose":2},"nrtUpdater":{"hostname":"TEST_NODE"},"resourceMonitor":{"sysfsRoot":"/sys","podSetFingerprint":true,"podSetFingerprintMethod":"with-exclusive-resources"},"topologyExporter":{"referenceContainer":{"namespace":"TEST_NS","podName":"TEST_POD","containerName":"TEST_CONT"},"kubeletConfigFile":"/podresources/config.yaml","podResourcesSocketPath":"unix:///podresources/kubelet.sock","sleepInterval":60000000000,"podReadinessEnable":true,"maxEventPerTimeUnit":1,"timeUnitToLimitEvents":1000000000,"addNRTOwnerEnable":true,"metricsMode":"disabled"}} \ No newline at end of file diff --git a/test/data/conftree/05-full-env/_env/vars.yaml b/test/data/conftree/05-full-env/_env/vars.yaml new file mode 100644 index 000000000..6d4dc81e4 --- /dev/null +++ b/test/data/conftree/05-full-env/_env/vars.yaml @@ -0,0 +1,3 @@ +REFERENCE_NAMESPACE: humble-ns +REFERENCE_POD_NAME: fancy-pod +REFERENCE_CONTAINER_NAME: awesome-container diff --git a/test/data/conftree/05-full-env/_output/output.yaml b/test/data/conftree/05-full-env/_output/output.yaml new file mode 100644 index 000000000..9ac196b78 --- /dev/null +++ b/test/data/conftree/05-full-env/_output/output.yaml @@ -0,0 +1,32 @@ +global: + verbose: 5 +nrtUpdater: + hostname: node.kubelab.io +resourceMonitor: + podSetFingerprint: true + podSetFingerprintMethod: with-exclusive-resources + resourceExclude: + '*': + - device/exampleC + masternode: + - memory + - device/exampleA + workernode1: + - memory + - device/exampleB + workernode2: + - cpu + sysfsRoot: /sys +topologyExporter: + kubeletConfigFile: /podresources/config.yaml + maxEventPerTimeUnit: 1 + metricsMode: disabled + podReadinessEnable: true + podResourcesSocketPath: unix:///podresources/kubelet.sock + referenceContainer: + containerName: awesome-container + namespace: humble-ns + podName: fancy-pod + sleepInterval: 60000000000 + timeUnitToLimitEvents: 1000000000 + diff --git a/test/data/conftree/05-full-env/daemon/config.yaml b/test/data/conftree/05-full-env/daemon/config.yaml new file mode 100644 index 000000000..9453cac1d --- /dev/null +++ b/test/data/conftree/05-full-env/daemon/config.yaml @@ -0,0 +1,14 @@ +global: + verbose: 2 +nrtUpdater: + hostname: node.kubelab.io +resourceMonitor: + sysfsRoot: /sys +topologyExporter: + addNRTOwnerEnable: false + maxEventPerTimeUnit: 1 + metricsMode: disabled + podReadinessEnable: true + podResourcesSocketPath: unix:///podresources/kubelet.sock + sleepInterval: 60000000000 + timeUnitToLimitEvents: 1000000000 diff --git a/test/data/conftree/05-full-env/daemon/config.yaml.d/00-verbose.yaml b/test/data/conftree/05-full-env/daemon/config.yaml.d/00-verbose.yaml new file mode 100644 index 000000000..94ce1a29e --- /dev/null +++ b/test/data/conftree/05-full-env/daemon/config.yaml.d/00-verbose.yaml @@ -0,0 +1,3 @@ +global: + verbose: 5 + diff --git a/test/data/conftree/05-full-env/daemon/config.yaml.d/30-pfp.yaml b/test/data/conftree/05-full-env/daemon/config.yaml.d/30-pfp.yaml new file mode 100644 index 000000000..487426a60 --- /dev/null +++ b/test/data/conftree/05-full-env/daemon/config.yaml.d/30-pfp.yaml @@ -0,0 +1,3 @@ +resourceMonitor: + podSetFingerprint: true + podSetFingerprintMethod: with-exclusive-resources diff --git a/test/data/conftree/05-full-env/extra/config.yaml b/test/data/conftree/05-full-env/extra/config.yaml new file mode 100644 index 000000000..27f6cbf56 --- /dev/null +++ b/test/data/conftree/05-full-env/extra/config.yaml @@ -0,0 +1,5 @@ +resourceExclude: + masternode: [memory, device/exampleA] + workernode1: [memory, device/exampleB] + workernode2: [cpu] + "*": [device/exampleC] diff --git a/test/data/conftree/06-full-env-args/_cmdline/flags.yaml b/test/data/conftree/06-full-env-args/_cmdline/flags.yaml new file mode 100644 index 000000000..0ff3f7719 --- /dev/null +++ b/test/data/conftree/06-full-env-args/_cmdline/flags.yaml @@ -0,0 +1 @@ +- --reference-container=a_ns/some_pod/the_container diff --git a/test/data/conftree/06-full-env-args/_env/vars.yaml b/test/data/conftree/06-full-env-args/_env/vars.yaml new file mode 100644 index 000000000..6d4dc81e4 --- /dev/null +++ b/test/data/conftree/06-full-env-args/_env/vars.yaml @@ -0,0 +1,3 @@ +REFERENCE_NAMESPACE: humble-ns +REFERENCE_POD_NAME: fancy-pod +REFERENCE_CONTAINER_NAME: awesome-container diff --git a/test/data/conftree/06-full-env-args/_output/output.yaml b/test/data/conftree/06-full-env-args/_output/output.yaml new file mode 100644 index 000000000..33fa2b6ef --- /dev/null +++ b/test/data/conftree/06-full-env-args/_output/output.yaml @@ -0,0 +1,32 @@ +global: + verbose: 5 +nrtUpdater: + hostname: node.kubelab.io +resourceMonitor: + podSetFingerprint: true + podSetFingerprintMethod: with-exclusive-resources + resourceExclude: + '*': + - device/exampleC + masternode: + - memory + - device/exampleA + workernode1: + - memory + - device/exampleB + workernode2: + - cpu + sysfsRoot: /sys +topologyExporter: + kubeletConfigFile: /podresources/config.yaml + maxEventPerTimeUnit: 1 + metricsMode: disabled + podReadinessEnable: true + podResourcesSocketPath: unix:///podresources/kubelet.sock + referenceContainer: + containerName: the_container + namespace: a_ns + podName: some_pod + sleepInterval: 60000000000 + timeUnitToLimitEvents: 1000000000 + diff --git a/test/data/conftree/06-full-env-args/daemon/config.yaml b/test/data/conftree/06-full-env-args/daemon/config.yaml new file mode 100644 index 000000000..9453cac1d --- /dev/null +++ b/test/data/conftree/06-full-env-args/daemon/config.yaml @@ -0,0 +1,14 @@ +global: + verbose: 2 +nrtUpdater: + hostname: node.kubelab.io +resourceMonitor: + sysfsRoot: /sys +topologyExporter: + addNRTOwnerEnable: false + maxEventPerTimeUnit: 1 + metricsMode: disabled + podReadinessEnable: true + podResourcesSocketPath: unix:///podresources/kubelet.sock + sleepInterval: 60000000000 + timeUnitToLimitEvents: 1000000000 diff --git a/test/data/conftree/06-full-env-args/daemon/config.yaml.d/00-verbose.yaml b/test/data/conftree/06-full-env-args/daemon/config.yaml.d/00-verbose.yaml new file mode 100644 index 000000000..94ce1a29e --- /dev/null +++ b/test/data/conftree/06-full-env-args/daemon/config.yaml.d/00-verbose.yaml @@ -0,0 +1,3 @@ +global: + verbose: 5 + diff --git a/test/data/conftree/06-full-env-args/daemon/config.yaml.d/30-pfp.yaml b/test/data/conftree/06-full-env-args/daemon/config.yaml.d/30-pfp.yaml new file mode 100644 index 000000000..487426a60 --- /dev/null +++ b/test/data/conftree/06-full-env-args/daemon/config.yaml.d/30-pfp.yaml @@ -0,0 +1,3 @@ +resourceMonitor: + podSetFingerprint: true + podSetFingerprintMethod: with-exclusive-resources diff --git a/test/data/conftree/06-full-env-args/extra/config.yaml b/test/data/conftree/06-full-env-args/extra/config.yaml new file mode 100644 index 000000000..27f6cbf56 --- /dev/null +++ b/test/data/conftree/06-full-env-args/extra/config.yaml @@ -0,0 +1,5 @@ +resourceExclude: + masternode: [memory, device/exampleA] + workernode1: [memory, device/exampleB] + workernode2: [cpu] + "*": [device/exampleC]