Skip to content

Commit 3812fa1

Browse files
carloryrphillips
andcommitted
Fix kubelet on Windows fails if a pod has SecurityContext with RunAsUser.
Co-authored-by: rphillips <[email protected]>
1 parent 5732a8b commit 3812fa1

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

pkg/volume/util/atomic_writer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
444444
if fileProjection.FsUser == nil {
445445
continue
446446
}
447-
if err := os.Chown(fullPath, int(*fileProjection.FsUser), -1); err != nil {
447+
448+
if err := w.chown(fullPath, int(*fileProjection.FsUser), -1); err != nil {
448449
klog.Errorf("%s: unable to change file %s with owner %v: %v", w.logContext, fullPath, int(*fileProjection.FsUser), err)
449450
return err
450451
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build linux
2+
// +build linux
3+
4+
/*
5+
Copyright 2024 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package util
21+
22+
import "os"
23+
24+
// chown changes the numeric uid and gid of the named file.
25+
func (w *AtomicWriter) chown(name string, uid, gid int) error {
26+
return os.Chown(name, uid, gid)
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !linux
2+
// +build !linux
3+
4+
/*
5+
Copyright 2024 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package util
21+
22+
import (
23+
"runtime"
24+
25+
"k8s.io/klog/v2"
26+
)
27+
28+
// chown changes the numeric uid and gid of the named file.
29+
// This is a no-op on unsupported platforms.
30+
func (w *AtomicWriter) chown(name string, uid, _ /* gid */ int) error {
31+
klog.Warningf("%s: skipping change of Linux owner %v for file %s; unsupported on %s", w.logContext, uid, name, runtime.GOOS)
32+
return nil
33+
}

test/e2e/windows/security_context.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/fields"
3030
"k8s.io/apimachinery/pkg/util/uuid"
31+
"k8s.io/apimachinery/pkg/util/wait"
3132
clientset "k8s.io/client-go/kubernetes"
33+
"k8s.io/client-go/kubernetes/scheme"
3234
"k8s.io/kubernetes/pkg/kubelet/events"
3335
"k8s.io/kubernetes/test/e2e/feature"
3436
"k8s.io/kubernetes/test/e2e/framework"
3537
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
3638
e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
3739
testutils "k8s.io/kubernetes/test/utils"
40+
"k8s.io/kubernetes/test/utils/format"
3841
imageutils "k8s.io/kubernetes/test/utils/image"
3942
admissionapi "k8s.io/pod-security-admission/api"
43+
"k8s.io/utils/ptr"
4044
)
4145

4246
const runAsUserNameContainerName = "run-as-username-container"
@@ -193,6 +197,67 @@ var _ = sigDescribe(feature.Windows, "SecurityContext", skipUnlessWindows(func()
193197
})
194198
}))
195199

200+
var _ = sigDescribe(feature.Windows, "SecurityContext", skipUnlessWindows(func() {
201+
f := framework.NewDefaultFramework("windows-with-unsupported-fields")
202+
f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
203+
204+
ginkgo.It("should be able to create pod and run containers", func(ctx context.Context) {
205+
ginkgo.By("Creating 1 pods: run with unsupported fields")
206+
207+
pod := &v1.Pod{
208+
ObjectMeta: metav1.ObjectMeta{
209+
Name: "run-ignore-unsupported-fields",
210+
Namespace: f.Namespace.Name,
211+
},
212+
Spec: v1.PodSpec{
213+
NodeSelector: map[string]string{"kubernetes.io/os": "windows"},
214+
Containers: []v1.Container{
215+
{
216+
Name: "test-container",
217+
Image: imageutils.GetE2EImage(imageutils.Pause),
218+
},
219+
},
220+
SecurityContext: &v1.PodSecurityContext{
221+
RunAsUser: ptr.To[int64](999), // windows does not support
222+
RunAsGroup: ptr.To[int64](999), // windows does not support
223+
RunAsNonRoot: ptr.To(true),
224+
},
225+
RestartPolicy: v1.RestartPolicyNever,
226+
},
227+
}
228+
229+
pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod, metav1.CreateOptions{})
230+
framework.ExpectNoError(err, "Error creating pod")
231+
232+
podErr := e2epod.WaitForPodRunningInNamespace(ctx, f.ClientSet, pod)
233+
234+
// Get the logs and events before calling ExpectNoError, so we can debug any errors.
235+
var logs string
236+
var events *v1.EventList
237+
if err := wait.PollUntilContextTimeout(ctx, 30*time.Second, 2*time.Minute, true, func(ctx context.Context) (done bool, err error) {
238+
framework.Logf("polling logs")
239+
logs, err = e2epod.GetPodLogs(ctx, f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name)
240+
if err != nil {
241+
framework.Logf("Error pulling logs: %v", err)
242+
return false, nil
243+
}
244+
245+
events, err = f.ClientSet.CoreV1().Events(pod.Namespace).Search(scheme.Scheme, pod)
246+
if err != nil {
247+
return false, fmt.Errorf("error in listing events: %w", err)
248+
}
249+
return true, nil
250+
}); err != nil {
251+
framework.Failf("Unexpected error getting pod logs/events: %v", err)
252+
} else {
253+
framework.Logf("Pod logs: \n%v", logs)
254+
framework.Logf("Pod events: \n%v", format.Object(events, 1))
255+
}
256+
257+
framework.ExpectNoError(podErr)
258+
})
259+
}))
260+
196261
func runAsUserNamePod(username *string) *v1.Pod {
197262
podName := "run-as-username-" + string(uuid.NewUUID())
198263
return &v1.Pod{

0 commit comments

Comments
 (0)