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

config: fix processing of reference container #257

Merged
merged 1 commit into from
Feb 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 11 additions & 5 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/data/TestLoadDefaults.expected.json
Original file line number Diff line number Diff line change
@@ -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"}}
{"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"}}
3 changes: 3 additions & 0 deletions test/data/conftree/05-full-env/_env/vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REFERENCE_NAMESPACE: humble-ns
REFERENCE_POD_NAME: fancy-pod
REFERENCE_CONTAINER_NAME: awesome-container
32 changes: 32 additions & 0 deletions test/data/conftree/05-full-env/_output/output.yaml
Original file line number Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions test/data/conftree/05-full-env/daemon/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global:
verbose: 5

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resourceMonitor:
podSetFingerprint: true
podSetFingerprintMethod: with-exclusive-resources
5 changes: 5 additions & 0 deletions test/data/conftree/05-full-env/extra/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resourceExclude:
masternode: [memory, device/exampleA]
workernode1: [memory, device/exampleB]
workernode2: [cpu]
"*": [device/exampleC]
1 change: 1 addition & 0 deletions test/data/conftree/06-full-env-args/_cmdline/flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- --reference-container=a_ns/some_pod/the_container
3 changes: 3 additions & 0 deletions test/data/conftree/06-full-env-args/_env/vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
REFERENCE_NAMESPACE: humble-ns
REFERENCE_POD_NAME: fancy-pod
REFERENCE_CONTAINER_NAME: awesome-container
32 changes: 32 additions & 0 deletions test/data/conftree/06-full-env-args/_output/output.yaml
Original file line number Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions test/data/conftree/06-full-env-args/daemon/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global:
verbose: 5

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resourceMonitor:
podSetFingerprint: true
podSetFingerprintMethod: with-exclusive-resources
5 changes: 5 additions & 0 deletions test/data/conftree/06-full-env-args/extra/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resourceExclude:
masternode: [memory, device/exampleA]
workernode1: [memory, device/exampleB]
workernode2: [cpu]
"*": [device/exampleC]
Loading