Skip to content

Commit db76a1b

Browse files
committed
Switch CRI PID namespace to an enum
1 parent b188868 commit db76a1b

File tree

7 files changed

+25
-13
lines changed

7 files changed

+25
-13
lines changed

pkg/kubelet/apis/cri/v1alpha1/runtime/api.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,18 @@ message Mount {
174174
MountPropagation propagation = 5;
175175
}
176176

177+
enum PIDNamespace {
178+
ISOLATED = 0;
179+
HOST = 1;
180+
SHARED = 2;
181+
}
182+
177183
// NamespaceOption provides options for Linux namespaces.
178184
message NamespaceOption {
179185
// If set, use the host's network namespace.
180186
bool host_network = 1;
181187
// If set, use the host's PID namespace.
182-
bool host_pid = 2;
188+
PIDNamespace pid = 2;
183189
// If set, use the host's IPC namespace.
184190
bool host_ipc = 3;
185191
}

pkg/kubelet/dockershim/docker_sandbox.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func (ds *dockerService) PodSandboxStatus(podSandboxID string) (*runtimeapi.PodS
397397
Namespaces: &runtimeapi.Namespace{
398398
Options: &runtimeapi.NamespaceOption{
399399
HostNetwork: hostNetwork,
400-
HostPid: sharesHostPid(r),
400+
Pid: pidNamespace(r),
401401
HostIpc: sharesHostIpc(r),
402402
},
403403
},
@@ -590,13 +590,12 @@ func sharesHostNetwork(container *dockertypes.ContainerJSON) bool {
590590
return false
591591
}
592592

593-
// sharesHostPid returns true if the given container is sharing the host's pid
594-
// namespace.
595-
func sharesHostPid(container *dockertypes.ContainerJSON) bool {
596-
if container != nil && container.HostConfig != nil {
597-
return string(container.HostConfig.PidMode) == namespaceModeHost
593+
// pidNamespace returns the PID Namespace mode of the given container
594+
func pidNamespace(container *dockertypes.ContainerJSON) runtimeapi.PIDNamespace {
595+
if container != nil && container.HostConfig != nil && string(container.HostConfig.PidMode) == namespaceModeHost {
596+
return runtimeapi.PIDNamespace_HOST
598597
}
599-
return false
598+
return runtimeapi.PIDNamespace_ISOLATED
600599
}
601600

602601
// sharesHostIpc returns true if the given container is sharing the host's ipc

pkg/kubelet/dockershim/security_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func modifyContainerNamespaceOptions(nsOpts *runtimeapi.NamespaceOption, podSand
144144
// modifyCommonNamespaceOptions apply common namespace options for sandbox and container
145145
func modifyCommonNamespaceOptions(nsOpts *runtimeapi.NamespaceOption, hostConfig *dockercontainer.HostConfig) {
146146
if nsOpts != nil {
147-
if nsOpts.HostPid {
147+
if nsOpts.GetPid() == runtimeapi.PIDNamespace_HOST {
148148
hostConfig.PidMode = namespaceModeHost
149149
}
150150
if nsOpts.HostIpc {

pkg/kubelet/dockershim/security_context_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func TestModifySandboxNamespaceOptions(t *testing.T) {
256256
{
257257
name: "NamespaceOption.HostPid",
258258
nsOpt: &runtimeapi.NamespaceOption{
259-
HostPid: set,
259+
Pid: runtimeapi.PIDNamespace_HOST,
260260
},
261261
expected: &dockercontainer.HostConfig{
262262
PidMode: namespaceModeHost,
@@ -306,7 +306,7 @@ func TestModifyContainerNamespaceOptions(t *testing.T) {
306306
{
307307
name: "NamespaceOption.HostPid",
308308
nsOpt: &runtimeapi.NamespaceOption{
309-
HostPid: set,
309+
Pid: runtimeapi.PIDNamespace_HOST,
310310
},
311311
expected: &dockercontainer.HostConfig{
312312
NetworkMode: dockercontainer.NetworkMode(sandboxNSMode),

pkg/kubelet/kuberuntime/helpers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,10 @@ func (m *kubeGenericRuntimeManager) getSeccompProfileFromAnnotations(annotations
283283

284284
return profile
285285
}
286+
287+
func getPIDNamespaceForPod(pod *v1.Pod) runtimeapi.PIDNamespace {
288+
if pod.Spec.HostPID {
289+
return runtimeapi.PIDNamespace_HOST
290+
}
291+
return runtimeapi.PIDNamespace_ISOLATED
292+
}

pkg/kubelet/kuberuntime/kuberuntime_sandbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxLinuxConfig(pod *v1.Pod) (
154154
lc.SecurityContext.NamespaceOptions = &runtimeapi.NamespaceOption{
155155
HostNetwork: pod.Spec.HostNetwork,
156156
HostIpc: pod.Spec.HostIPC,
157-
HostPid: pod.Spec.HostPID,
157+
Pid: getPIDNamespaceForPod(pod),
158158
}
159159

160160
if sc.FSGroup != nil {

pkg/kubelet/kuberuntime/security_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *kubeGenericRuntimeManager) determineEffectiveSecurityContext(pod *v1.Po
5151
synthesized.NamespaceOptions = &runtimeapi.NamespaceOption{
5252
HostNetwork: pod.Spec.HostNetwork,
5353
HostIpc: pod.Spec.HostIPC,
54-
HostPid: pod.Spec.HostPID,
54+
Pid: getPIDNamespaceForPod(pod),
5555
}
5656
podSc := pod.Spec.SecurityContext
5757
if podSc != nil {

0 commit comments

Comments
 (0)