Skip to content

Commit 69e160a

Browse files
committed
Emit event when pod is updated in-place
1 parent 9befb31 commit 69e160a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

vertical-pod-autoscaler/pkg/updater/restriction/pods_inplace_restriction.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ func (ip *PodsInPlaceRestrictionImpl) InPlaceUpdate(podToUpdate *apiv1.Pod, vpa
155155
return fmt.Errorf("no resource patches were calculated to apply")
156156
}
157157

158-
// TODO(maxcao13): If this keeps getting called on the same object with the same reason, it is considered a patch request.
159-
// And we fail to have the corresponding rbac for it. So figure out if we need this later.
160-
// Do we even need to emit an event? The node might reject the resize request. If so, should we rename this to InPlaceResizeAttempted?
161-
// eventRecorder.Event(podToUpdate, apiv1.EventTypeNormal, "InPlaceResizedByVPA", "Pod was resized in place by VPA Updater.")
158+
eventRecorder.Event(podToUpdate, apiv1.EventTypeNormal, "InPlaceResizedByVPA", "Pod was resized in place by VPA Updater.")
162159

163160
singleGroupStats, present := ip.creatorToSingleGroupStatsMap[cr]
164161
if !present {

vertical-pod-autoscaler/pkg/updater/restriction/pods_inplace_restriction_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/stretchr/testify/assert"
2525
apiv1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/client-go/tools/record"
2728
featuregatetesting "k8s.io/component-base/featuregate/testing"
2829
baseclocktest "k8s.io/utils/clock/testing"
2930

@@ -379,3 +380,48 @@ func TestInPlaceAtLeastOne(t *testing.T) {
379380
assert.Error(t, err, "Error expected")
380381
}
381382
}
383+
384+
func TestInPlaceUpdate_EventEmission(t *testing.T) {
385+
featuregatetesting.SetFeatureGateDuringTest(t, features.MutableFeatureGate, features.InPlaceOrRecreate, true)
386+
387+
replicas := int32(5)
388+
livePods := 5
389+
tolerance := 0.1
390+
391+
rc := apiv1.ReplicationController{
392+
ObjectMeta: metav1.ObjectMeta{
393+
Name: "rc",
394+
Namespace: "default",
395+
},
396+
TypeMeta: metav1.TypeMeta{
397+
Kind: "ReplicationController",
398+
},
399+
Spec: apiv1.ReplicationControllerSpec{
400+
Replicas: &replicas,
401+
},
402+
}
403+
404+
pods := make([]*apiv1.Pod, livePods)
405+
for i := range pods {
406+
pods[i] = test.Pod().WithName(getTestPodName(i)).WithCreator(&rc.ObjectMeta, &rc.TypeMeta).Get()
407+
}
408+
409+
basicVpa := getBasicVpa()
410+
factory, err := getRestrictionFactory(&rc, nil, nil, nil, 2, tolerance, nil, nil, GetFakeCalculatorsWithFakeResourceCalc())
411+
assert.NoError(t, err)
412+
creatorToSingleGroupStatsMap, podToReplicaCreatorMap, err := factory.GetCreatorMaps(pods, basicVpa)
413+
assert.NoError(t, err)
414+
inplace := factory.NewPodsInPlaceRestriction(creatorToSingleGroupStatsMap, podToReplicaCreatorMap)
415+
416+
eventRecorder := record.NewFakeRecorder(10)
417+
418+
err = inplace.InPlaceUpdate(pods[0], basicVpa, eventRecorder)
419+
assert.NoError(t, err)
420+
421+
select {
422+
case event := <-eventRecorder.Events:
423+
assert.Contains(t, event, "InPlaceResizedByVPA")
424+
case <-time.After(1 * time.Second):
425+
assert.Fail(t, "timeout waiting for event")
426+
}
427+
}

0 commit comments

Comments
 (0)