Skip to content

Commit 210f129

Browse files
authored
Merge pull request kubernetes#128676 from vivzbansal/sidecar-3
Refactor: Move IsRestartableInitContainer to common utility package
2 parents e1e92bc + cf8ee42 commit 210f129

File tree

17 files changed

+62
-61
lines changed

17 files changed

+62
-61
lines changed

pkg/api/pod/util.go

+10
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,16 @@ func hasInvalidLabelValueInAffinitySelector(spec *api.PodSpec) bool {
12271227
return false
12281228
}
12291229

1230+
// IsRestartableInitContainer returns true if the container has ContainerRestartPolicyAlways.
1231+
// This function is not checking if the container passed to it is indeed an init container.
1232+
// It is just checking if the container restart policy has been set to always.
1233+
func IsRestartableInitContainer(initContainer *api.Container) bool {
1234+
if initContainer == nil || initContainer.RestartPolicy == nil {
1235+
return false
1236+
}
1237+
return *initContainer.RestartPolicy == api.ContainerRestartPolicyAlways
1238+
}
1239+
12301240
func MarkPodProposedForResize(oldPod, newPod *api.Pod) {
12311241
if len(newPod.Spec.Containers) != len(oldPod.Spec.Containers) {
12321242
// Update is invalid: ignore changes and let validation handle it

pkg/api/v1/pod/util.go

+10
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,13 @@ 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 container has ContainerRestartPolicyAlways.
411+
// This function is not checking if the container passed to it is indeed an init container.
412+
// It is just checking if the container restart policy has been set to always.
413+
func IsRestartableInitContainer(initContainer *v1.Container) bool {
414+
if initContainer == nil || initContainer.RestartPolicy == nil {
415+
return false
416+
}
417+
return *initContainer.RestartPolicy == v1.ContainerRestartPolicyAlways
418+
}

pkg/kubelet/apis/podresources/server_v1.go

+3-3
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

+2-3
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

+2-2
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import (
2929
"k8s.io/apimachinery/pkg/util/sets"
3030
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
3131
"k8s.io/klog/v2"
32+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
3233
corev1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
3334
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
3435
"k8s.io/kubernetes/pkg/kubelet/cm/containermap"
3536
"k8s.io/kubernetes/pkg/kubelet/cm/memorymanager/state"
3637
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
3738
"k8s.io/kubernetes/pkg/kubelet/config"
3839
"k8s.io/kubernetes/pkg/kubelet/status"
39-
"k8s.io/kubernetes/pkg/kubelet/types"
4040
)
4141

4242
// memoryManagerStateFileName is the file name where memory manager stores its state
@@ -228,7 +228,7 @@ func (m *manager) AddContainer(pod *v1.Pod, container *v1.Container, containerID
228228
// Since a restartable init container remains running for the full
229229
// duration of the pod's lifecycle, we should not remove it from the
230230
// memory manager state.
231-
if types.IsRestartableInitContainer(&initContainer) {
231+
if podutil.IsRestartableInitContainer(&initContainer) {
232232
continue
233233
}
234234

pkg/kubelet/cm/memorymanager/policy_static.go

+3-4
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

+2-2
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

+7-6
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"
@@ -1069,13 +1070,13 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
10691070
// If the container is previously initialized but its status is not
10701071
// found, it means its last status is removed for some reason.
10711072
// Restart it if it is a restartable init container.
1072-
if isPreviouslyInitialized && types.IsRestartableInitContainer(container) {
1073+
if isPreviouslyInitialized && podutil.IsRestartableInitContainer(container) {
10731074
changes.InitContainersToStart = append(changes.InitContainersToStart, i)
10741075
}
10751076
continue
10761077
}
10771078

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

10931094
case kubecontainer.ContainerStateRunning:
1094-
if !types.IsRestartableInitContainer(container) {
1095+
if !podutil.IsRestartableInitContainer(container) {
10951096
break
10961097
}
10971098

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

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

pkg/kubelet/kuberuntime/kuberuntime_manager.go

+2-1
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"
@@ -1327,7 +1328,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(ctx context.Context, pod *v1.Pod, po
13271328
container := &pod.Spec.InitContainers[idx]
13281329
// Start the next init container.
13291330
if err := start(ctx, "init container", metrics.InitContainer, containerStartSpec(container)); err != nil {
1330-
if types.IsRestartableInitContainer(container) {
1331+
if podutil.IsRestartableInitContainer(container) {
13311332
klog.V(4).InfoS("Failed to start the restartable init container for the pod, skipping", "initContainerName", container.Name, "pod", klog.KObj(pod))
13321333
continue
13331334
}

pkg/kubelet/kuberuntime/kuberuntime_termination_order.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
v1 "k8s.io/api/core/v1"
2424

25-
"k8s.io/kubernetes/pkg/kubelet/types"
25+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
2626
)
2727

2828
// terminationOrdering is used to enforce a termination ordering for sidecar containers. It sets up
@@ -80,7 +80,7 @@ func newTerminationOrdering(pod *v1.Pod, runningContainerNames []string) *termin
8080
close(channel)
8181
}
8282

83-
if types.IsRestartableInitContainer(&ic) {
83+
if podutil.IsRestartableInitContainer(&ic) {
8484
// sidecars need to wait for all main containers to exit
8585
to.prereqs[ic.Name] = append(to.prereqs[ic.Name], mainContainerChannels...)
8686

pkg/kubelet/lifecycle/predicate.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
utilfeature "k8s.io/apiserver/pkg/util/feature"
2525
"k8s.io/component-helpers/scheduling/corev1"
2626
"k8s.io/klog/v2"
27+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
2728
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
2829
"k8s.io/kubernetes/pkg/features"
2930
"k8s.io/kubernetes/pkg/kubelet/types"
@@ -141,7 +142,7 @@ func (w *predicateAdmitHandler) Admit(attrs *PodAdmitAttributes) PodAdmitResult
141142
// TODO: Remove this after the SidecarContainers feature gate graduates to GA.
142143
if !utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
143144
for _, c := range admitPod.Spec.InitContainers {
144-
if types.IsRestartableInitContainer(&c) {
145+
if podutil.IsRestartableInitContainer(&c) {
145146
message := fmt.Sprintf("Init container %q may not have a non-default restartPolicy", c.Name)
146147
klog.InfoS("Failed to admit pod", "pod", klog.KObj(admitPod), "message", message)
147148
return PodAdmitResult{

pkg/kubelet/prober/prober_manager.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
"k8s.io/client-go/tools/record"
2727
"k8s.io/component-base/metrics"
2828
"k8s.io/klog/v2"
29+
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
2930
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
3031
"k8s.io/kubernetes/pkg/kubelet/prober/results"
3132
"k8s.io/kubernetes/pkg/kubelet/status"
32-
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
3333
kubeutil "k8s.io/kubernetes/pkg/kubelet/util"
3434
"k8s.io/utils/clock"
3535
)
@@ -171,7 +171,7 @@ func (t probeType) String() string {
171171
func getRestartableInitContainers(pod *v1.Pod) []v1.Container {
172172
var restartableInitContainers []v1.Container
173173
for _, c := range pod.Spec.InitContainers {
174-
if kubetypes.IsRestartableInitContainer(&c) {
174+
if podutil.IsRestartableInitContainer(&c) {
175175
restartableInitContainers = append(restartableInitContainers, c)
176176
}
177177
}
@@ -325,7 +325,7 @@ func (m *manager) UpdatePodStatus(pod *v1.Pod, podStatus *v1.PodStatus) {
325325
klog.V(4).InfoS("Mismatch between pod spec and status, likely programmer error", "pod", klog.KObj(pod), "containerName", c.Name)
326326
continue
327327
}
328-
if !kubetypes.IsRestartableInitContainer(&initContainer) {
328+
if !podutil.IsRestartableInitContainer(&initContainer) {
329329
if c.State.Terminated != nil && c.State.Terminated.ExitCode == 0 {
330330
podStatus.InitContainerStatuses[i].Ready = true
331331
}

pkg/kubelet/status/generate.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
2525
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
2626
runtimeutil "k8s.io/kubernetes/pkg/kubelet/kuberuntime/util"
27-
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
2827
)
2928

3029
const (
@@ -57,7 +56,7 @@ func GenerateContainersReadyCondition(spec *v1.PodSpec, containerStatuses []v1.C
5756
unreadyContainers := []string{}
5857

5958
for _, container := range spec.InitContainers {
60-
if !kubetypes.IsRestartableInitContainer(&container) {
59+
if !podutil.IsRestartableInitContainer(&container) {
6160
continue
6261
}
6362

@@ -159,7 +158,7 @@ func GeneratePodReadyCondition(spec *v1.PodSpec, conditions []v1.PodCondition, c
159158
}
160159

161160
func isInitContainerInitialized(initContainer *v1.Container, containerStatus *v1.ContainerStatus) bool {
162-
if kubetypes.IsRestartableInitContainer(initContainer) {
161+
if podutil.IsRestartableInitContainer(initContainer) {
163162
if containerStatus.Started == nil || !*containerStatus.Started {
164163
return false
165164
}

pkg/kubelet/status/status_manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ func hasPodInitialized(pod *v1.Pod) bool {
552552
}
553553

554554
containerStatus := pod.Status.InitContainerStatuses[l-1]
555-
if kubetypes.IsRestartableInitContainer(&container) {
555+
if podutil.IsRestartableInitContainer(&container) {
556556
if containerStatus.State.Running != nil &&
557557
containerStatus.Started != nil && *containerStatus.Started {
558558
return true
@@ -616,7 +616,7 @@ func checkContainerStateTransition(oldStatuses, newStatuses *v1.PodStatus, podSp
616616
return fmt.Errorf("found mismatch between pod spec and status, container: %v", oldStatus.Name)
617617
}
618618
// Skip any restartable init container as it always is allowed to restart
619-
if kubetypes.IsRestartableInitContainer(&initContainer) {
619+
if podutil.IsRestartableInitContainer(&initContainer) {
620620
continue
621621
}
622622
// Skip any container that wasn't terminated

0 commit comments

Comments
 (0)