Repository navigation
Render daemonsets.podSecurityContext on GPUCluster DaemonSets #3222
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
Open
dundysm
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
dundysm:fix/gpucluster-pod-security-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+297
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,176 @@ | ||
| /** | ||
| # Copyright (c) NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # 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 controllers | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/runtime/serializer/json" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
|
|
||
| gpuv1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1" | ||
| ) | ||
|
|
||
| // namedContainerSecurity is one container securityContext, copied so later mutation | ||
| // of the DaemonSet does not change the snapshot. | ||
| type namedContainerSecurity struct { | ||
| name string | ||
| sc *corev1.SecurityContext | ||
| } | ||
|
|
||
| func snapshotContainerSecurity(spec corev1.PodSpec) []namedContainerSecurity { | ||
| out := make([]namedContainerSecurity, 0, len(spec.InitContainers)+len(spec.Containers)) | ||
| for i := range spec.InitContainers { | ||
| c := spec.InitContainers[i] | ||
| out = append(out, namedContainerSecurity{name: "init/" + c.Name, sc: c.SecurityContext.DeepCopy()}) | ||
| } | ||
| for i := range spec.Containers { | ||
| c := spec.Containers[i] | ||
| out = append(out, namedContainerSecurity{name: c.Name, sc: c.SecurityContext.DeepCopy()}) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func loadDaemonSetAsset(t *testing.T, rel string) *appsv1.DaemonSet { | ||
| t.Helper() | ||
| buf, err := os.ReadFile(filepath.Join(cfg.root, rel)) | ||
| require.NoError(t, err) | ||
| ds := &appsv1.DaemonSet{} | ||
| ser := json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme, | ||
| json.SerializerOptions{Yaml: true, Pretty: false, Strict: false}) | ||
| _, _, err = ser.Decode(buf, nil, ds) | ||
| require.NoError(t, err) | ||
| return ds | ||
| } | ||
|
|
||
| // TestClusterPolicyDaemonsetsPodSecurityContext is the control for the GPUCluster renders. | ||
| // ClusterPolicy copies daemonsets.podSecurityContext onto the pod and still forces the | ||
| // validator containers to run as root, leaving privileged and container SELinux options in place. | ||
| func TestClusterPolicyDaemonsetsPodSecurityContext(t *testing.T) { | ||
| t.Setenv("DCGM_IMAGE", "example.com/dcgm:test") | ||
| t.Setenv("DCGM_EXPORTER_IMAGE", "example.com/dcgm-exporter:test") | ||
| t.Setenv("VALIDATOR_IMAGE", "example.com/gpu-operator:test") | ||
|
|
||
| podSecurityContext := &corev1.PodSecurityContext{ | ||
| SELinuxOptions: &corev1.SELinuxOptions{ | ||
| Type: "spc_t", | ||
| Level: "s0", | ||
| }, | ||
| RunAsUser: new(int64(1000)), | ||
| } | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| asset string | ||
| runAsRoot []string | ||
| }{ | ||
| { | ||
| name: "dcgm", | ||
| asset: "assets/state-dcgm/0400_dcgm.yml", | ||
| runAsRoot: []string{"init/toolkit-validation"}, | ||
| }, | ||
| { | ||
| name: "dcgm-exporter", | ||
| asset: "assets/state-dcgm-exporter/0800_daemonset.yaml", | ||
| runAsRoot: []string{"init/toolkit-validation"}, | ||
| }, | ||
| { | ||
| name: "validator", | ||
| asset: "assets/state-operator-validation/0500_daemonset.yaml", | ||
| runAsRoot: []string{ | ||
| "init/driver-validation", | ||
| "init/toolkit-validation", | ||
| "init/cuda-validation", | ||
| "init/plugin-validation", | ||
| "nvidia-operator-validator", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ds := loadDaemonSetAsset(t, tc.asset) | ||
| before := snapshotContainerSecurity(ds.Spec.Template.Spec) | ||
|
|
||
| n := ClusterPolicyController{ | ||
| singleton: &gpuv1.ClusterPolicy{ | ||
| Spec: gpuv1.ClusterPolicySpec{ | ||
| Daemonsets: gpuv1.DaemonsetsSpec{PodSecurityContext: podSecurityContext}, | ||
| }, | ||
| }, | ||
| logger: ctrl.Log.WithName("test"), | ||
| runtime: gpuv1.Containerd, | ||
| } | ||
| require.NoError(t, preProcessDaemonSet(ds, n)) | ||
|
|
||
| require.Equal(t, podSecurityContext, ds.Spec.Template.Spec.SecurityContext) | ||
|
|
||
| after := snapshotContainerSecurity(ds.Spec.Template.Spec) | ||
| require.Equal(t, len(before), len(after)) | ||
| runAsRoot := map[string]bool{} | ||
| for _, name := range tc.runAsRoot { | ||
| runAsRoot[name] = true | ||
| } | ||
| for i := range before { | ||
| require.Equal(t, before[i].name, after[i].name) | ||
| require.Equal(t, privileged(before[i].sc), privileged(after[i].sc), before[i].name) | ||
| require.Equal(t, seLinux(before[i].sc), seLinux(after[i].sc), before[i].name) | ||
| if runAsRoot[before[i].name] { | ||
| require.NotNil(t, after[i].sc) | ||
| require.NotNil(t, after[i].sc.RunAsUser) | ||
| require.Equal(t, int64(0), *after[i].sc.RunAsUser) | ||
| require.NotNil(t, before[i].sc) | ||
| require.NotNil(t, before[i].sc.Privileged) | ||
| require.True(t, *before[i].sc.Privileged) | ||
| } else if before[i].sc != nil { | ||
| require.Equal(t, before[i].sc.RunAsUser, after[i].sc.RunAsUser) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| t.Run("unset", func(t *testing.T) { | ||
| ds := loadDaemonSetAsset(t, "assets/state-dcgm/0400_dcgm.yml") | ||
| n := ClusterPolicyController{ | ||
| singleton: &gpuv1.ClusterPolicy{}, | ||
| logger: ctrl.Log.WithName("test"), | ||
| runtime: gpuv1.Containerd, | ||
| } | ||
| require.NoError(t, preProcessDaemonSet(ds, n)) | ||
| require.Nil(t, ds.Spec.Template.Spec.SecurityContext) | ||
| }) | ||
| } | ||
|
|
||
| func privileged(sc *corev1.SecurityContext) *bool { | ||
| if sc == nil { | ||
| return nil | ||
| } | ||
| return sc.Privileged | ||
| } | ||
|
|
||
| func seLinux(sc *corev1.SecurityContext) *corev1.SELinuxOptions { | ||
| if sc == nil { | ||
| return nil | ||
| } | ||
| return sc.SELinuxOptions | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 19513
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 5891
Web query:
Kubernetes privileged container non root UID all capabilities CAP_DAC_OVERRIDE CAP_SYS_ADMIN Kubernetes security contextResult:
Web query:
github kubernetes-sigs dra-driver-nvidia-gpu v0.5.1 gpu-kubelet-plugin non root kubelet registration socket permissionsResult:
Web query:
containerd privileged non root exec ambient capabilities CAP_SYS_ADMIN bash kubernetes runc UID 1000 privileged mountResult:
Web query:
site:github.com/kubernetes-sigs/dra-driver-nvidia-gpu "runAsUser" "privileged" "non-root" kubelet pluginResult:
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 6531
Web query:
containerd CRI privileged runAsUser non root ambient capabilities 2.3 exec all capabilities KubernetesResult:
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 8306
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 30528
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 18786
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 5688
Web query:
man7 capabilities execve nonroot ambient file capabilities effective permitted Linux calculationResult:
Script executed:
Repository: NVIDIA/gpu-operator
Length of output: 642
Ensure the GPU plugin can start with a non-root pod UID.
With
runAsUser: 1000on containerd 2.3.5, the plugin can fail to create its directory under a root-owned kubelet plugins directory. Thegpuscontainer then exits and can crash-loop. Privileged mode does not preserve ambient capabilities across the non-root executable.The render test does not detect this startup failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this only changes anything when daemonsets.podSecurityContext is set, and that matches clusterpolicy today: applyCommonDaemonsetConfig copies the same field onto every operand daemonset, including the device plugin that also writes under /var/lib/kubelet. the init container here already pins runAsUser: 0, and i left the plugin containers as they are so the change stays a straight port of the clusterpolicy behavior. @rahulait if you'd prefer the kubelet plugin containers to pin runAsUser: 0 as well so a non-root pod uid can't reach them, i can add that here.