Skip to content

Commit be1c85d

Browse files
committed
Enhance the prompt information of verifyRunAsNonRoot, add pod, container information
1 parent 66ea0f5 commit be1c85d

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

pkg/kubelet/kuberuntime/security_context_others.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323

2424
"k8s.io/api/core/v1"
25+
"k8s.io/kubernetes/pkg/kubelet/util/format"
2526
"k8s.io/kubernetes/pkg/securitycontext"
2627
)
2728

@@ -35,16 +36,16 @@ func verifyRunAsNonRoot(pod *v1.Pod, container *v1.Container, uid *int64, userna
3536

3637
if effectiveSc.RunAsUser != nil {
3738
if *effectiveSc.RunAsUser == 0 {
38-
return fmt.Errorf("container's runAsUser breaks non-root policy")
39+
return fmt.Errorf("container's runAsUser breaks non-root policy (pod: %q, container: %s)", format.Pod(pod), container.Name)
3940
}
4041
return nil
4142
}
4243

4344
switch {
4445
case uid != nil && *uid == 0:
45-
return fmt.Errorf("container has runAsNonRoot and image will run as root")
46+
return fmt.Errorf("container has runAsNonRoot and image will run as root (pod: %q, container: %s)", format.Pod(pod), container.Name)
4647
case uid == nil && len(username) > 0:
47-
return fmt.Errorf("container has runAsNonRoot and image has non-numeric user (%s), cannot verify user is non-root", username)
48+
return fmt.Errorf("container has runAsNonRoot and image has non-numeric user (%s), cannot verify user is non-root (pod: %q, container: %s)", username, format.Pod(pod), container.Name)
4849
default:
4950
return nil
5051
}

pkg/kubelet/kuberuntime/security_context_windows.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"k8s.io/api/core/v1"
2424
"k8s.io/klog/v2"
25+
"k8s.io/kubernetes/pkg/kubelet/util/format"
2526
"k8s.io/kubernetes/pkg/securitycontext"
2627
)
2728

@@ -42,24 +43,24 @@ func verifyRunAsNonRoot(pod *v1.Pod, container *v1.Container, uid *int64, userna
4243
return nil
4344
}
4445
if effectiveSc.RunAsUser != nil {
45-
klog.Warningf("Windows container does not support SecurityContext.RunAsUser, please use SecurityContext.WindowsOptions")
46+
klog.Warningf("Windows container does not support SecurityContext.RunAsUser, please use SecurityContext.WindowsOptions (pod: %q, container: %s)", format.Pod(pod), container.Name)
4647
}
4748
if effectiveSc.SELinuxOptions != nil {
48-
klog.Warningf("Windows container does not support SecurityContext.SELinuxOptions, please use SecurityContext.WindowsOptions")
49+
klog.Warningf("Windows container does not support SecurityContext.SELinuxOptions, please use SecurityContext.WindowsOptions (pod: %q, container: %s)", format.Pod(pod), container.Name)
4950
}
5051
if effectiveSc.RunAsGroup != nil {
51-
klog.Warningf("Windows container does not support SecurityContext.RunAsGroup")
52+
klog.Warningf("Windows container does not support SecurityContext.RunAsGroup (pod: %q, container: %s)", format.Pod(pod), container.Name)
5253
}
5354
if effectiveSc.WindowsOptions != nil {
5455
if effectiveSc.WindowsOptions.RunAsUserName != nil {
5556
if *effectiveSc.WindowsOptions.RunAsUserName == windowsRootUserName {
56-
return fmt.Errorf("container's runAsUser (%s) which will be regarded as root identity and will break non-root policy", username)
57+
return fmt.Errorf("container's runAsUser (%s) which will be regarded as root identity and will break non-root policy (pod: %q, container: %s)", username, format.Pod(pod), container.Name)
5758
}
5859
return nil
5960
}
6061
}
6162
if len(username) > 0 && username == windowsRootUserName {
62-
return fmt.Errorf("container's runAsUser (%s) which will be regarded as root identity and will break non-root policy", username)
63+
return fmt.Errorf("container's runAsUser (%s) which will be regarded as root identity and will break non-root policy (pod: %q, container: %s)", username, format.Pod(pod), container.Name)
6364
}
6465
return nil
6566
}

0 commit comments

Comments
 (0)