Skip to content

Commit ce4de97

Browse files
committed
make kubelet call updatePodSandboxResources on PodResizeAction
1 parent b27c303 commit ce4de97

File tree

6 files changed

+910
-546
lines changed

6 files changed

+910
-546
lines changed

pkg/kubelet/kuberuntime/instrumented_services.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ func (in instrumentedRuntimeService) PortForward(ctx context.Context, req *runti
272272
return resp, err
273273
}
274274

275+
func (in instrumentedRuntimeService) UpdatePodSandboxResources(ctx context.Context, req *runtimeapi.UpdatePodSandboxResourcesRequest) (*runtimeapi.UpdatePodSandboxResourcesResponse, error) {
276+
const operation = "update_podsandbox"
277+
defer recordOperation(operation, time.Now())
278+
279+
resp, err := in.service.UpdatePodSandboxResources(ctx, req)
280+
recordError(operation, err)
281+
return resp, err
282+
}
283+
275284
func (in instrumentedRuntimeService) UpdateRuntimeConfig(ctx context.Context, runtimeConfig *runtimeapi.RuntimeConfig) error {
276285
const operation = "update_runtime_config"
277286
defer recordOperation(operation, time.Now())

pkg/kubelet/kuberuntime/kuberuntime_container.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"sync"
3434
"time"
3535

36+
codes "google.golang.org/grpc/codes"
3637
crierror "k8s.io/cri-api/pkg/errors"
3738

3839
"github.com/opencontainers/selinux/go-selinux"
@@ -411,6 +412,25 @@ func (m *kubeGenericRuntimeManager) updateContainerResources(pod *v1.Pod, contai
411412
return err
412413
}
413414

415+
func (m *kubeGenericRuntimeManager) updatePodSandboxResources(sandboxID string, pod *v1.Pod) error {
416+
podResourcesRequest := &runtimeapi.UpdatePodSandboxResourcesRequest{
417+
PodSandboxId: sandboxID,
418+
Overhead: m.convertOverheadToLinuxResources(pod),
419+
Resources: m.calculateSandboxResources(pod),
420+
}
421+
422+
ctx := context.Background()
423+
_, err := m.runtimeService.UpdatePodSandboxResources(ctx, podResourcesRequest)
424+
if err != nil {
425+
stat, _ := grpcstatus.FromError(err)
426+
if stat.Code() == codes.Unimplemented {
427+
klog.ErrorS(err, "updatePodSandboxResources failed: method unimplemented on runtime service", "sandboxID", sandboxID)
428+
return nil
429+
}
430+
}
431+
return err
432+
}
433+
414434
// makeDevices generates container devices for kubelet runtime v1.
415435
func makeDevices(opts *kubecontainer.RunContainerOptions) []*runtimeapi.Device {
416436
devices := make([]*runtimeapi.Device, len(opts.Devices))

pkg/kubelet/kuberuntime/kuberuntime_container_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,3 +969,39 @@ func TestUpdateContainerResources(t *testing.T) {
969969
// Verify container is updated
970970
assert.Contains(t, fakeRuntime.Called, "UpdateContainerResources")
971971
}
972+
973+
// TestUpdatePodSandboxResources tests updating a podSandBox resources.
974+
func TestUpdatePodSandboxResources(t *testing.T) {
975+
fakeRuntime, _, m, errCreate := createTestRuntimeManager()
976+
require.NoError(t, errCreate)
977+
pod := &v1.Pod{
978+
ObjectMeta: metav1.ObjectMeta{
979+
UID: "12345678",
980+
Name: "bar",
981+
Namespace: "new",
982+
},
983+
Spec: v1.PodSpec{
984+
Containers: []v1.Container{
985+
{
986+
Name: "foo",
987+
Image: "busybox",
988+
ImagePullPolicy: v1.PullIfNotPresent,
989+
},
990+
},
991+
},
992+
}
993+
994+
// Create fake sandbox and container
995+
fakeSandbox, fakeContainers := makeAndSetFakePod(t, m, fakeRuntime, pod)
996+
assert.Len(t, fakeContainers, 1)
997+
998+
ctx := context.Background()
999+
_, err := m.getPodContainerStatuses(ctx, pod.UID, pod.Name, pod.Namespace)
1000+
require.NoError(t, err)
1001+
1002+
err = m.updatePodSandboxResources(fakeSandbox.Id, pod)
1003+
require.NoError(t, err)
1004+
1005+
// Verify sandbox is updated
1006+
assert.Contains(t, fakeRuntime.Called, "UpdatePodSandboxResources")
1007+
}

pkg/kubelet/kuberuntime/kuberuntime_manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ func (m *kubeGenericRuntimeManager) doPodResizeAction(pod *v1.Pod, podContainerC
775775
result.Fail(errResize)
776776
return
777777
}
778+
if errUpdate := m.updatePodSandboxResources(podContainerChanges.SandboxID, pod); errUpdate != nil {
779+
klog.ErrorS(err, "updatePodSandboxResources failed", "pod", pod.Name)
780+
}
778781
}
779782
if len(podContainerChanges.ContainersToUpdate[v1.ResourceCPU]) > 0 || podContainerChanges.UpdatePodResources {
780783
if podResources.CPUShares == nil {
@@ -801,6 +804,9 @@ func (m *kubeGenericRuntimeManager) doPodResizeAction(pod *v1.Pod, podContainerC
801804
result.Fail(errResize)
802805
return
803806
}
807+
if errUpdate := m.updatePodSandboxResources(podContainerChanges.SandboxID, pod); errUpdate != nil {
808+
klog.ErrorS(err, "updatePodSandboxResources failed", "pod", pod.Name)
809+
}
804810
}
805811
}
806812

0 commit comments

Comments
 (0)