Skip to content

Commit 523a19a

Browse files
committed
Extract isInPlacePodVerticalScalingAllowed to shared function
1 parent 460db5c commit 523a19a

File tree

7 files changed

+94
-37
lines changed

7 files changed

+94
-37
lines changed

pkg/kubelet/kubelet.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,19 +2873,8 @@ func (kl *Kubelet) canResizePod(pod *v1.Pod) (bool, v1.PodResizeStatus, string)
28732873
// the allocation decision and pod status.
28742874
func (kl *Kubelet) handlePodResourcesResize(pod *v1.Pod, podStatus *kubecontainer.PodStatus) (*v1.Pod, error) {
28752875
allocatedPod, updated := kl.allocationManager.UpdatePodFromAllocation(pod)
2876-
// Keep this logic in sync with kuberuntime.isInPlacePodVerticalScalingAllowed
2877-
if goos == "windows" || kubetypes.IsStaticPod(pod) {
2876+
if resizable, msg := kuberuntime.IsInPlacePodVerticalScalingAllowed(pod); !resizable {
28782877
if updated {
2879-
// A resize is requested but not supported.
2880-
var msg string
2881-
switch {
2882-
case goos == "windows":
2883-
msg = "Resizing Windows pods is not supported"
2884-
case kubetypes.IsStaticPod(pod):
2885-
msg = "Resizing static pods is not supported"
2886-
default:
2887-
msg = "Resizing this pod is not supported"
2888-
}
28892878
kl.recorder.Eventf(pod, v1.EventTypeWarning, events.ResizeInfeasible, msg)
28902879
kl.statusManager.SetPodResizeStatus(pod.UID, v1.PodResizeStatusInfeasible)
28912880
} else {

pkg/kubelet/kubelet_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,7 +2711,6 @@ func TestHandlePodResourcesResize(t *testing.T) {
27112711
expectedAllocatedLims v1.ResourceList
27122712
expectedResize v1.PodResizeStatus
27132713
expectBackoffReset bool
2714-
goos string
27152714
annotations map[string]string
27162715
}{
27172716
{
@@ -2781,14 +2780,6 @@ func TestHandlePodResourcesResize(t *testing.T) {
27812780
expectedAllocatedReqs: v1.ResourceList{v1.ResourceCPU: cpu1000m, v1.ResourceMemory: mem1000M},
27822781
expectedResize: "",
27832782
},
2784-
{
2785-
name: "windows node, expect Infeasible",
2786-
originalRequests: v1.ResourceList{v1.ResourceCPU: cpu1000m, v1.ResourceMemory: mem1000M},
2787-
newRequests: v1.ResourceList{v1.ResourceCPU: cpu500m, v1.ResourceMemory: mem500M},
2788-
expectedAllocatedReqs: v1.ResourceList{v1.ResourceCPU: cpu1000m, v1.ResourceMemory: mem1000M},
2789-
expectedResize: v1.PodResizeStatusInfeasible,
2790-
goos: "windows",
2791-
},
27922783
{
27932784
name: "static pod, expect Infeasible",
27942785
originalRequests: v1.ResourceList{v1.ResourceCPU: cpu1000m, v1.ResourceMemory: mem1000M},
@@ -2882,11 +2873,6 @@ func TestHandlePodResourcesResize(t *testing.T) {
28822873
for _, tt := range tests {
28832874
for _, isSidecarContainer := range []bool{false, true} {
28842875
t.Run(tt.name, func(t *testing.T) {
2885-
oldGOOS := goos
2886-
defer func() { goos = oldGOOS }()
2887-
if tt.goos != "" {
2888-
goos = tt.goos
2889-
}
28902876
kubelet.statusManager = status.NewFakeManager()
28912877

28922878
var originalPod *v1.Pod
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build linux
2+
// +build linux
3+
4+
/*
5+
Copyright 2025 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 kuberuntime
21+
22+
import (
23+
v1 "k8s.io/api/core/v1"
24+
utilfeature "k8s.io/apiserver/pkg/util/feature"
25+
"k8s.io/kubernetes/pkg/features"
26+
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
27+
)
28+
29+
func IsInPlacePodVerticalScalingAllowed(pod *v1.Pod) (allowed bool, msg string) {
30+
if !utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) {
31+
return false, "InPlacePodVerticalScaling is disabled"
32+
}
33+
if kubetypes.IsStaticPod(pod) {
34+
return false, "In-place resize of static-pods is not supported"
35+
}
36+
return true, ""
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build !linux && !windows
2+
// +build !linux,!windows
3+
4+
/*
5+
Copyright 2025 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 kuberuntime
21+
22+
import v1 "k8s.io/api/core/v1"
23+
24+
func IsInPlacePodVerticalScalingAllowed(_ *v1.Pod) (allowed bool, msg string) {
25+
return false, "In-place pod resize is not supported on this node"
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2025 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 kuberuntime
21+
22+
import v1 "k8s.io/api/core/v1"
23+
24+
func IsInPlacePodVerticalScalingAllowed(_ *v1.Pod) (allowed bool, msg string) {
25+
return false, "In-place pod resize is not supported on Windows"
26+
}

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ func (m *kubeGenericRuntimeManager) computeInitContainerActions(pod *v1.Pod, pod
11641164
}
11651165
}
11661166

1167-
if IsInPlacePodVerticalScalingAllowed(pod) && !m.computePodResizeAction(pod, i, true, status, changes) {
1167+
if !m.computePodResizeAction(pod, i, true, status, changes) {
11681168
// computePodResizeAction updates 'changes' if resize policy requires restarting this container
11691169
break
11701170
}

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25-
"runtime"
2625
"sort"
2726
"time"
2827

@@ -551,17 +550,11 @@ func containerSucceeded(c *v1.Container, podStatus *kubecontainer.PodStatus) boo
551550
return cStatus.State == kubecontainer.ContainerStateExited && cStatus.ExitCode == 0
552551
}
553552

554-
func isInPlacePodVerticalScalingAllowed(pod *v1.Pod) bool {
555-
return utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) &&
556-
!types.IsStaticPod(pod) &&
557-
runtime.GOOS != "windows"
558-
}
559-
560553
// computePodResizeAction determines the actions required (if any) to resize the given container.
561554
// Returns whether to keep (true) or restart (false) the container.
562555
// TODO(vibansal): Make this function to be agnostic to whether it is dealing with a restartable init container or not (i.e. remove the argument `isRestartableInitContainer`).
563556
func (m *kubeGenericRuntimeManager) computePodResizeAction(pod *v1.Pod, containerIdx int, isRestartableInitContainer bool, kubeContainerStatus *kubecontainer.Status, changes *podActions) (keepContainer bool) {
564-
if !isInPlacePodVerticalScalingAllowed(pod) {
557+
if resizable, _ := IsInPlacePodVerticalScalingAllowed(pod); !resizable {
565558
return true
566559
}
567560

@@ -998,7 +991,7 @@ func (m *kubeGenericRuntimeManager) computePodActions(ctx context.Context, pod *
998991
}
999992
}
1000993

1001-
if isInPlacePodVerticalScalingAllowed(pod) {
994+
if resizable, _ := IsInPlacePodVerticalScalingAllowed(pod); resizable {
1002995
changes.ContainersToUpdate = make(map[v1.ResourceName][]containerToUpdateInfo)
1003996
}
1004997

@@ -1414,7 +1407,7 @@ func (m *kubeGenericRuntimeManager) SyncPod(ctx context.Context, pod *v1.Pod, po
14141407
}
14151408

14161409
// Step 7: For containers in podContainerChanges.ContainersToUpdate[CPU,Memory] list, invoke UpdateContainerResources
1417-
if isInPlacePodVerticalScalingAllowed(pod) {
1410+
if resizable, _ := IsInPlacePodVerticalScalingAllowed(pod); resizable {
14181411
if len(podContainerChanges.ContainersToUpdate) > 0 || podContainerChanges.UpdatePodResources {
14191412
m.doPodResizeAction(pod, podContainerChanges, &result)
14201413
}

0 commit comments

Comments
 (0)