Skip to content

Commit 763e810

Browse files
committed
refactor code to add sidecar container support in IPPR
1 parent 6e25c2a commit 763e810

File tree

17 files changed

+58
-59
lines changed

17 files changed

+58
-59
lines changed

pkg/api/pod/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,15 @@ func hasInvalidLabelValueInAffinitySelector(spec *api.PodSpec) bool {
12271227
return false
12281228
}
12291229

1230+
// IsRestartableInitContainer returns true if the initContainer has
1231+
// ContainerRestartPolicyAlways.
1232+
func IsRestartableInitContainer(initContainer *api.Container) bool {
1233+
if initContainer == nil || initContainer.RestartPolicy == nil {
1234+
return false
1235+
}
1236+
return *initContainer.RestartPolicy == api.ContainerRestartPolicyAlways
1237+
}
1238+
12301239
func MarkPodProposedForResize(oldPod, newPod *api.Pod) {
12311240
if len(newPod.Spec.Containers) != len(oldPod.Spec.Containers) {
12321241
// Update is invalid: ignore changes and let validation handle it

pkg/api/v1/pod/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,12 @@ func UpdatePodCondition(status *v1.PodStatus, condition *v1.PodCondition) bool {
406406
// Return true if one of the fields have changed.
407407
return !isEqual
408408
}
409+
410+
// IsRestartableInitContainer returns true if the initContainer has
411+
// ContainerRestartPolicyAlways.
412+
func IsRestartableInitContainer(initContainer *v1.Container) bool {
413+
if initContainer == nil || initContainer.RestartPolicy == nil {
414+
return false
415+
}
416+
return *initContainer.RestartPolicy == v1.ContainerRestartPolicyAlways
417+
}

pkg/kubelet/apis/podresources/server_v1.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222

2323
v1 "k8s.io/api/core/v1"
2424
utilfeature "k8s.io/apiserver/pkg/util/feature"
25+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
2526
kubefeatures "k8s.io/kubernetes/pkg/features"
2627
"k8s.io/kubernetes/pkg/kubelet/metrics"
27-
"k8s.io/kubernetes/pkg/kubelet/types"
2828

2929
podresourcesv1 "k8s.io/kubelet/pkg/apis/podresources/v1"
3030
)
@@ -70,7 +70,7 @@ func (p *v1PodResourcesServer) List(ctx context.Context, req *podresourcesv1.Lis
7070
pRes.Containers = make([]*podresourcesv1.ContainerResources, 0, len(pod.Spec.InitContainers)+len(pod.Spec.Containers))
7171

7272
for _, container := range pod.Spec.InitContainers {
73-
if !types.IsRestartableInitContainer(&container) {
73+
if !podutil.IsRestartableInitContainer(&container) {
7474
continue
7575
}
7676

@@ -130,7 +130,7 @@ func (p *v1PodResourcesServer) Get(ctx context.Context, req *podresourcesv1.GetP
130130
podResources.Containers = make([]*podresourcesv1.ContainerResources, 0, len(pod.Spec.InitContainers)+len(pod.Spec.Containers))
131131

132132
for _, container := range pod.Spec.InitContainers {
133-
if !types.IsRestartableInitContainer(&container) {
133+
if !podutil.IsRestartableInitContainer(&container) {
134134
continue
135135
}
136136

pkg/kubelet/cm/cpumanager/policy_static.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
3131
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
3232
"k8s.io/kubernetes/pkg/kubelet/metrics"
33-
"k8s.io/kubernetes/pkg/kubelet/types"
3433
"k8s.io/utils/cpuset"
3534
)
3635

@@ -298,7 +297,7 @@ func (p *staticPolicy) updateCPUsToReuse(pod *v1.Pod, container *v1.Container, c
298297
// If so, add its cpuset to the cpuset of reusable CPUs for any new allocations.
299298
for _, initContainer := range pod.Spec.InitContainers {
300299
if container.Name == initContainer.Name {
301-
if types.IsRestartableInitContainer(&initContainer) {
300+
if podutil.IsRestartableInitContainer(&initContainer) {
302301
// If the container is a restartable init container, we should not
303302
// reuse its cpuset, as a restartable init container can run with
304303
// regular containers.
@@ -489,7 +488,7 @@ func (p *staticPolicy) podGuaranteedCPUs(pod *v1.Pod) int {
489488
requestedCPU := p.guaranteedCPUs(pod, &container)
490489
// See https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/753-sidecar-containers#resources-calculation-for-scheduling-and-pod-admission
491490
// for the detail.
492-
if types.IsRestartableInitContainer(&container) {
491+
if podutil.IsRestartableInitContainer(&container) {
493492
requestedByRestartableInitContainers += requestedCPU
494493
} else if requestedByRestartableInitContainers+requestedCPU > requestedByInitContainers {
495494
requestedByInitContainers = requestedByRestartableInitContainers + requestedCPU

pkg/kubelet/cm/devicemanager/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"k8s.io/apiserver/pkg/server/healthz"
3838
utilfeature "k8s.io/apiserver/pkg/util/feature"
3939
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
40+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
4041
"k8s.io/kubernetes/pkg/features"
4142
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
4243
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
@@ -49,7 +50,6 @@ import (
4950
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
5051
"k8s.io/kubernetes/pkg/kubelet/metrics"
5152
"k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
52-
"k8s.io/kubernetes/pkg/kubelet/types"
5353
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework"
5454
)
5555

@@ -384,7 +384,7 @@ func (m *ManagerImpl) Allocate(pod *v1.Pod, container *v1.Container) error {
384384
if err := m.allocateContainerResources(pod, container, m.devicesToReuse[string(pod.UID)]); err != nil {
385385
return err
386386
}
387-
if !types.IsRestartableInitContainer(&initContainer) {
387+
if !podutil.IsRestartableInitContainer(&initContainer) {
388388
m.podDevices.addContainerAllocatedResources(string(pod.UID), container.Name, m.devicesToReuse[string(pod.UID)])
389389
} else {
390390
// If the init container is restartable, we need to keep the

pkg/kubelet/cm/memorymanager/memory_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import (
2828
"k8s.io/apimachinery/pkg/util/sets"
2929
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
3030
"k8s.io/klog/v2"
31+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
3132
corev1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
3233
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
3334
"k8s.io/kubernetes/pkg/kubelet/cm/containermap"
3435
"k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state"
3536
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
3637
"k8s.io/kubernetes/pkg/kubelet/config"
3738
"k8s.io/kubernetes/pkg/kubelet/status"
38-
"k8s.io/kubernetes/pkg/kubelet/types"
3939
)
4040

4141
// memoryManagerStateFileName is the file name where memory manager stores its state
@@ -209,7 +209,7 @@ func (m *manager) AddContainer(pod *v1.Pod, container *v1.Container, containerID
209209
// Since a restartable init container remains running for the full
210210
// duration of the pod's lifecycle, we should not remove it from the
211211
// memory manager state.
212-
if types.IsRestartableInitContainer(&initContainer) {
212+
if podutil.IsRestartableInitContainer(&initContainer) {
213213
continue
214214
}
215215

pkg/kubelet/cm/memorymanager/policy_static.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
3535
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
3636
"k8s.io/kubernetes/pkg/kubelet/metrics"
37-
"k8s.io/kubernetes/pkg/kubelet/types"
3837
)
3938

4039
const policyTypeStatic policyType = "Static"
@@ -353,7 +352,7 @@ func getPodRequestedResources(pod *v1.Pod) (map[v1.ResourceName]uint64, error) {
353352

354353
// See https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/753-sidecar-containers#resources-calculation-for-scheduling-and-pod-admission
355354
// for the detail.
356-
if types.IsRestartableInitContainer(&ctr) {
355+
if podutil.IsRestartableInitContainer(&ctr) {
357356
reqRsrcsByRestartableInitCtrs[rsrcName] += qty
358357
} else if reqRsrcsByRestartableInitCtrs[rsrcName]+qty > reqRsrcsByInitCtrs[rsrcName] {
359358
reqRsrcsByInitCtrs[rsrcName] = reqRsrcsByRestartableInitCtrs[rsrcName] + qty
@@ -969,7 +968,7 @@ func (p *staticPolicy) updateInitContainersMemoryBlocks(s state.State, pod *v1.P
969968
break
970969
}
971970

972-
if types.IsRestartableInitContainer(&initContainer) {
971+
if podutil.IsRestartableInitContainer(&initContainer) {
973972
// we should not reuse the resource from any restartable init
974973
// container
975974
continue
@@ -1011,7 +1010,7 @@ func (p *staticPolicy) updateInitContainersMemoryBlocks(s state.State, pod *v1.P
10111010
func isRegularInitContainer(pod *v1.Pod, container *v1.Container) bool {
10121011
for _, initContainer := range pod.Spec.InitContainers {
10131012
if initContainer.Name == container.Name {
1014-
return !types.IsRestartableInitContainer(&initContainer)
1013+
return !podutil.IsRestartableInitContainer(&initContainer)
10151014
}
10161015
}
10171016

pkg/kubelet/kubelet_pods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ func getPhase(pod *v1.Pod, info []v1.ContainerStatus, podIsTerminal bool) v1.Pod
15851585

15861586
// regular init containers
15871587
for _, container := range spec.InitContainers {
1588-
if kubetypes.IsRestartableInitContainer(&container) {
1588+
if podutil.IsRestartableInitContainer(&container) {
15891589
// Skip the restartable init containers here to handle them separately as
15901590
// they are slightly different from the init containers in terms of the
15911591
// pod phase.
@@ -1628,7 +1628,7 @@ func getPhase(pod *v1.Pod, info []v1.ContainerStatus, podIsTerminal bool) v1.Pod
16281628
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
16291629
// restartable init containers
16301630
for _, container := range spec.InitContainers {
1631-
if !kubetypes.IsRestartableInitContainer(&container) {
1631+
if !podutil.IsRestartableInitContainer(&container) {
16321632
// Skip the regular init containers, as they have been handled above.
16331633
continue
16341634
}

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
5151
remote "k8s.io/cri-client/pkg"
5252
kubelettypes "k8s.io/kubelet/pkg/types"
53+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
5354
"k8s.io/kubernetes/pkg/features"
5455
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
5556
"k8s.io/kubernetes/pkg/kubelet/events"
@@ -1070,13 +1071,13 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
10701071
// If the container is previously initialized but its status is not
10711072
// found, it means its last status is removed for some reason.
10721073
// Restart it if it is a restartable init container.
1073-
if isPreviouslyInitialized && types.IsRestartableInitContainer(container) {
1074+
if isPreviouslyInitialized && podutil.IsRestartableInitContainer(container) {
10741075
changes.InitContainersToStart = append(changes.InitContainersToStart, i)
10751076
}
10761077
continue
10771078
}
10781079

1079-
if isPreviouslyInitialized && !types.IsRestartableInitContainer(container) {
1080+
if isPreviouslyInitialized && !podutil.IsRestartableInitContainer(container) {
10801081
// after initialization, only restartable init containers need to be kept
10811082
// running
10821083
continue
@@ -1092,11 +1093,11 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
10921093
changes.InitContainersToStart = append(changes.InitContainersToStart, i)
10931094

10941095
case kubecontainer.ContainerStateRunning:
1095-
if !types.IsRestartableInitContainer(container) {
1096+
if !podutil.IsRestartableInitContainer(container) {
10961097
break
10971098
}
10981099

1099-
if types.IsRestartableInitContainer(container) {
1100+
if podutil.IsRestartableInitContainer(container) {
11001101
if container.StartupProbe != nil {
11011102
startup, found := m.startupManager.Get(status.ID)
11021103
if !found {
@@ -1167,7 +1168,7 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
11671168
// If the init container failed and the restart policy is Never, the pod is terminal.
11681169
// Otherwise, restart the init container.
11691170
case kubecontainer.ContainerStateExited:
1170-
if types.IsRestartableInitContainer(container) {
1171+
if podutil.IsRestartableInitContainer(container) {
11711172
changes.InitContainersToStart = append(changes.InitContainersToStart, i)
11721173
} else { // init container
11731174
if isInitContainerFailed(status) {
@@ -1190,7 +1191,7 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
11901191
}
11911192

11921193
default: // kubecontainer.ContainerStatusUnknown or other unknown states
1193-
if types.IsRestartableInitContainer(container) {
1194+
if podutil.IsRestartableInitContainer(container) {
11941195
// If the restartable init container is in unknown state, restart it.
11951196
changes.ContainersToKill[status.ID] = containerToKillInfo{
11961197
name: container.Name,

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
4747

4848
"k8s.io/kubernetes/pkg/api/legacyscheme"
49+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
4950
"k8s.io/kubernetes/pkg/credentialprovider"
5051
"k8s.io/kubernetes/pkg/credentialprovider/plugin"
5152
"k8s.io/kubernetes/pkg/features"
@@ -1321,7 +1322,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(ctx context.Context, pod *v1.Pod, po
13211322
container := &pod.Spec.InitContainers[idx]
13221323
// Start the next init container.
13231324
if err := start(ctx, "init container", metrics.InitContainer, containerStartSpec(container)); err != nil {
1324-
if types.IsRestartableInitContainer(container) {
1325+
if podutil.IsRestartableInitContainer(container) {
13251326
klog.V(4).InfoS("Failed to start the restartable init container for the pod, skipping", "initContainerName", container.Name, "pod", klog.KObj(pod))
13261327
continue
13271328
}

0 commit comments

Comments
 (0)