Skip to content

Commit da9c2c5

Browse files
committed
Set pod watch conditions for resize
1 parent f4d36dd commit da9c2c5

File tree

8 files changed

+36
-34
lines changed

8 files changed

+36
-34
lines changed

pkg/kubelet/container/helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ type RuntimeHelper interface {
6666

6767
// UnprepareDynamicResources unprepares resources for a a pod.
6868
UnprepareDynamicResources(ctx context.Context, pod *v1.Pod) error
69+
70+
// SetPodWatchCondition flags a pod to be inspected until the condition is met.
71+
SetPodWatchCondition(types.UID, string, func(*PodStatus) bool)
6972
}
7073

7174
// ShouldContainerBeRestarted checks whether a container needs to be restarted.

pkg/kubelet/container/testing/fake_runtime_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ func (f *FakeRuntimeHelper) PrepareDynamicResources(ctx context.Context, pod *v1
114114
func (f *FakeRuntimeHelper) UnprepareDynamicResources(ctx context.Context, pod *v1.Pod) error {
115115
return nil
116116
}
117+
118+
func (f *FakeRuntimeHelper) SetPodWatchCondition(_ kubetypes.UID, _ string, _ func(*kubecontainer.PodStatus) bool) {
119+
// Not implemented.
120+
}

pkg/kubelet/kubelet.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,17 +1980,6 @@ func (kl *Kubelet) SyncPod(ctx context.Context, updateType kubetypes.SyncPodType
19801980
return false, nil
19811981
}
19821982

1983-
if utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScaling) && isPodResizeInProgress(pod, podStatus) {
1984-
// While resize is in progress, periodically request the latest status from the runtime via
1985-
// the PLEG. This is necessary since ordinarily pod status is only fetched when a container
1986-
// undergoes a state transition.
1987-
runningPod := kubecontainer.ConvertPodStatusToRunningPod(kl.getRuntime().Type(), podStatus)
1988-
if err, _ := kl.pleg.UpdateCache(&runningPod, pod.UID); err != nil {
1989-
klog.ErrorS(err, "Failed to update pod cache", "pod", klog.KObj(pod))
1990-
return false, err
1991-
}
1992-
}
1993-
19941983
return false, nil
19951984
}
19961985

@@ -3097,3 +3086,7 @@ func (kl *Kubelet) fastStaticPodsRegistration(ctx context.Context) {
30973086
kl.tryReconcileMirrorPods(staticPod, mirrorPod)
30983087
}
30993088
}
3089+
3090+
func (kl *Kubelet) SetPodWatchCondition(podUID types.UID, conditionKey string, condition pleg.WatchCondition) {
3091+
kl.pleg.SetPodWatchCondition(podUID, conditionKey, condition)
3092+
}

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
5858
"k8s.io/kubernetes/pkg/kubelet/logs"
5959
"k8s.io/kubernetes/pkg/kubelet/metrics"
60+
"k8s.io/kubernetes/pkg/kubelet/pleg"
6061
proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results"
6162
"k8s.io/kubernetes/pkg/kubelet/runtimeclass"
6263
"k8s.io/kubernetes/pkg/kubelet/sysctl"
@@ -797,6 +798,22 @@ func (m *kubeGenericRuntimeManager) updatePodContainerResources(pod *v1.Pod, res
797798
"pod", format.Pod(pod), "resourceName", resourceName)
798799
return err
799800
}
801+
resizeKey := fmt.Sprintf("%s:resize:%s", container.Name, resourceName)
802+
resizeCondition := pleg.RunningContainerWatchCondition(container.Name, func(status *kubecontainer.Status) bool {
803+
if status.Resources == nil {
804+
return false
805+
}
806+
switch resourceName {
807+
case v1.ResourceMemory:
808+
return status.Resources.MemoryLimit.Equal(*container.Resources.Limits.Memory())
809+
case v1.ResourceCPU:
810+
return status.Resources.CPURequest.Equal(*container.Resources.Requests.Cpu()) &&
811+
status.Resources.CPULimit.Equal(*container.Resources.Limits.Cpu())
812+
default:
813+
return true // Shouldn't happen.
814+
}
815+
})
816+
m.runtimeHelper.SetPodWatchCondition(pod.UID, resizeKey, resizeCondition)
800817
// If UpdateContainerResources is error-free, it means desired values for 'resourceName' was accepted by runtime.
801818
// So we update currentContainerResources for 'resourceName', which is our view of most recently configured resources.
802819
// Note: We can't rely on GetPodStatus as runtime may lag in actuating the resource values it just accepted.

pkg/kubelet/pleg/evented.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,6 @@ func (e *EventedPLEG) updateLatencyMetric(event *runtimeapi.ContainerEventRespon
427427
metrics.EventedPLEGConnLatency.Observe(duration.Seconds())
428428
}
429429

430-
func (e *EventedPLEG) UpdateCache(pod *kubecontainer.Pod, pid types.UID) (error, bool) {
431-
return fmt.Errorf("not implemented"), false
432-
}
433-
434430
func (e *EventedPLEG) SetPodWatchCondition(podUID types.UID, conditionKey string, condition WatchCondition) {
435431
e.genericPleg.SetPodWatchCondition(podUID, conditionKey, condition)
436432
}

pkg/kubelet/pleg/generic.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,9 @@ func (g *GenericPLEG) Relist() {
305305
needsReinspection[pid] = pod
306306

307307
continue
308-
} else {
309-
if utilfeature.DefaultFeatureGate.Enabled(features.EventedPLEG) {
310-
if !updated {
311-
continue
312-
}
308+
} else if utilfeature.DefaultFeatureGate.Enabled(features.EventedPLEG) {
309+
if !updated {
310+
continue
313311
}
314312
}
315313

@@ -483,15 +481,6 @@ func (g *GenericPLEG) updateCache(ctx context.Context, pod *kubecontainer.Pod, p
483481
return status, g.cache.Set(pod.ID, status, err, timestamp), err
484482
}
485483

486-
func (g *GenericPLEG) UpdateCache(pod *kubecontainer.Pod, pid types.UID) (error, bool) {
487-
ctx := context.Background()
488-
if pod == nil {
489-
return fmt.Errorf("pod cannot be nil"), false
490-
}
491-
_, updated, err := g.updateCache(ctx, pod, pid)
492-
return err, updated
493-
}
494-
495484
func (g *GenericPLEG) SetPodWatchCondition(podUID types.UID, conditionKey string, condition WatchCondition) {
496485
g.watchConditionsLock.Lock()
497486
defer g.watchConditionsLock.Unlock()

pkg/kubelet/pleg/generic_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,8 @@ func TestWatchConditions(t *testing.T) {
766766
},
767767
},
768768
}}
769-
initialPods := append(pods, &containertest.FakePod{Pod: &kubecontainer.Pod{
769+
initialPods := pods
770+
initialPods = append(initialPods, &containertest.FakePod{Pod: &kubecontainer.Pod{
770771
Name: "terminated-pod",
771772
ID: "terminated",
772773
Sandboxes: []*kubecontainer.Container{
@@ -813,8 +814,8 @@ func TestWatchConditions(t *testing.T) {
813814
"updating": {version: 1},
814815
},
815816
}, {
816-
name: "non-existant pod",
817-
podUID: "non-existant",
817+
name: "non-existent pod",
818+
podUID: "non-existent",
818819
watchConditions: map[string]WatchCondition{
819820
"watching": neverComplete,
820821
},

pkg/kubelet/pleg/pleg.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ type PodLifecycleEventGenerator interface {
6666
Start()
6767
Watch() chan *PodLifecycleEvent
6868
Healthy() (bool, error)
69-
UpdateCache(*kubecontainer.Pod, types.UID) (error, bool)
7069
// SetPodWatchCondition flags the pod for reinspection on every Relist iteration until the watch
7170
// condition is met. The condition is keyed so it can be updated before the condition
7271
// is met.
@@ -83,7 +82,7 @@ type podLifecycleEventGeneratorHandler interface {
8382
}
8483

8584
// WatchCondition takes the latest PodStatus, and returns whether the condition is met.
86-
type WatchCondition func(*kubecontainer.PodStatus) bool
85+
type WatchCondition = func(*kubecontainer.PodStatus) bool
8786

8887
// RunningContainerWatchCondition wraps a condition on the container status to make a pod
8988
// WatchCondition. If the container is no longer running, the condition is implicitly cleared.

0 commit comments

Comments
 (0)