Skip to content

Commit

Permalink
update equalResourcesInPlaceFields logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-yzj committed Aug 12, 2024
1 parent f90137e commit 4168425
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/controller/instanceset/in_place_update_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ func equalBasicInPlaceFields(old, new *corev1.Pod) bool {

func equalResourcesInPlaceFields(old, new *corev1.Pod) bool {
if len(old.Spec.Containers) != len(new.Spec.Containers) {
return false
if len(old.Spec.Containers) < len(new.Spec.Containers) {
return false
}
return isContainerInjected(old.Spec.Containers, new.Spec.Containers)
}
for _, nc := range new.Spec.Containers {
index := slices.IndexFunc(old.Spec.Containers, func(oc corev1.Container) bool {
Expand Down Expand Up @@ -336,6 +339,21 @@ func getPodUpdatePolicy(its *workloads.InstanceSet, pod *corev1.Pod) (PodUpdateP
return NoOpsPolicy, nil
}

func isContainerInjected(ocs, ncs []corev1.Container) bool {
for _, nc := range ncs {
index := slices.IndexFunc(ocs, func(oc corev1.Container) bool {
return nc.Name == oc.Name
})
if index < 0 {
return false
}
if nc.Image != ocs[index].Image {
return false
}
}
return true
}

// IsPodUpdated tells whether the pod's spec is as expected in the InstanceSet.
// This function is meant to replace the old fashion `GetPodRevision(pod) == updateRevision`,
// as the pod template revision has been redefined in instanceset.
Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/instanceset/in_place_update_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ var _ = Describe("instance util test", func() {
policy, err = getPodUpdatePolicy(its, pod5)
Expect(err).Should(BeNil())
Expect(policy).Should(Equal(NoOpsPolicy))

By("build a pod without revision updated, with IgnorePodVerticalScaling disabled")
pod6 := pod1.DeepCopy()
pod6.Spec.Containers = append(pod6.Spec.Containers, corev1.Container{
Name: "sidecar1",
Image: "bar2",
Ports: []corev1.ContainerPort{
{
Name: "my-svc",
Protocol: corev1.ProtocolTCP,
ContainerPort: 54321,
},
},
})
randStr = rand.String(16)
mergeMap(&map[string]string{key: randStr}, &pod6.Annotations)
ignorePodVerticalScaling = viper.GetBool(FeatureGateIgnorePodVerticalScaling)
defer viper.Set(FeatureGateIgnorePodVerticalScaling, ignorePodVerticalScaling)
viper.Set(FeatureGateIgnorePodVerticalScaling, false)
policy, err = getPodUpdatePolicy(its, pod6)
Expect(err).Should(BeNil())
Expect(policy).Should(Equal(InPlaceUpdatePolicy))
})
})
})

0 comments on commit 4168425

Please sign in to comment.