Skip to content

Commit a91ffd8

Browse files
authored
feat(CollaSet): Add pod labels to indicate PodOpsLifecycle phase (#297)
* add pod label to indicate PodOpsLifecycle phase
1 parent 1384acc commit a91ffd8

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ require (
9494
k8s.io/apiserver v0.22.6 // indirect
9595
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
9696
k8s.io/kubectl v0.29.0
97-
kusionstack.io/kube-api v0.6.2
97+
kusionstack.io/kube-api v0.6.3
9898
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
9999
sigs.k8s.io/yaml v1.3.0 // indirect
100100
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,8 +1139,8 @@ k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
11391139
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
11401140
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
11411141
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
1142-
kusionstack.io/kube-api v0.6.2 h1:OCfDPXoXRvM8wB6muGcEBBYjfV6ivlOf1R3NSXU+QqA=
1143-
kusionstack.io/kube-api v0.6.2/go.mod h1:J0+EHiroG/88X904Y9TV9iMRcoEuD5tXMTLMBDSwM+Y=
1142+
kusionstack.io/kube-api v0.6.3 h1:HKgB6E95DVRGkZ2OjXkuV4YE327gGt24sBFJRB+tc6A=
1143+
kusionstack.io/kube-api v0.6.3/go.mod h1:J0+EHiroG/88X904Y9TV9iMRcoEuD5tXMTLMBDSwM+Y=
11441144
kusionstack.io/resourceconsist v0.0.1 h1:+k/jriq5Ld7fQUYfWSMGynz/FesHtl3Rk2fmQPjBe0g=
11451145
kusionstack.io/resourceconsist v0.0.1/go.mod h1:816xS/fY6EOUbPFjXIWW/TGs8/YE46qP4ElKeIiwFdU=
11461146
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=

pkg/controllers/collaset/collaset_controller_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ var _ = Describe("collaset controller", func() {
272272
}, 5*time.Second, 1*time.Second).Should(BeTrue())
273273
Expect(c.Get(context.TODO(), types.NamespacedName{Namespace: cs.Namespace, Name: cs.Name}, cs)).Should(BeNil())
274274
Expect(expectedStatusReplicas(c, cs, 0, 0, 0, 2, 2, 0, 0, 0)).Should(BeNil())
275+
for _, pod := range podList.Items {
276+
Expect(pod.Labels[appsv1alpha1.PodCreatingLabel]).ShouldNot(BeEquivalentTo(""))
277+
}
275278

276279
// pod replica will be kept, after deleted
277280
podToDelete := &podList.Items[0]
@@ -987,6 +990,8 @@ var _ = Describe("collaset controller", func() {
987990
// manual changes should have lower priority than PodTemplate changes
988991
Expect(pod.Labels["test1"]).Should(BeEquivalentTo("v4"))
989992
Expect(pod.Labels["test2"]).Should(BeEquivalentTo("v1"))
993+
Expect(pod.Labels[appsv1alpha1.PodCompletingLabel]).ShouldNot(BeEquivalentTo(""))
994+
Expect(pod.Labels[appsv1alpha1.PodCreatingLabel]).Should(BeEquivalentTo(""))
990995

991996
_, exist = pod.Labels["manual-added"]
992997
Expect(exist).Should(BeFalse())

pkg/controllers/collaset/synccontrol/sync_control.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ func (r *RealSyncControl) Scale(
343343
revision,
344344
func(in *corev1.Pod) (localErr error) {
345345
in.Labels[appsv1alpha1.PodInstanceIDLabelKey] = fmt.Sprintf("%d", availableIDContext.ID)
346+
if availableIDContext.Data[podcontext.JustCreateContextDataKey] == "true" {
347+
in.Labels[appsv1alpha1.PodCreatingLabel] = strconv.FormatInt(time.Now().UnixNano(), 10)
348+
} else {
349+
in.Labels[appsv1alpha1.PodCompletingLabel] = strconv.FormatInt(time.Now().UnixNano(), 10)
350+
}
346351
revisionsInfo, ok := availableIDContext.Get(podcontext.PodDecorationRevisionKey)
347352
var pds map[string]*appsv1alpha1.PodDecoration
348353
if !ok {

pkg/controllers/podopslifecycle/podopslifecycle_controller.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,19 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
185185
v1alpha1.PodPreparingLabelPrefix: false, // Set readiness gate to false
186186
v1alpha1.PodCompletingLabelPrefix: true, // Set readiness gate to true
187187
}
188-
for _, k := range []string{v1alpha1.PodPreparingLabelPrefix, v1alpha1.PodCompletingLabelPrefix} {
189-
v := expected[k]
188+
for phaseLabel, expectedReadinessGateStatus := range expected {
190189
keeped := false
191190
for _, labels := range idToLabelsMap {
192-
if _, ok := labels[k]; !ok {
191+
if _, ok := labels[phaseLabel]; !ok {
193192
continue
194193
}
195194

196-
updated, err := r.updateServiceReadiness(ctx, pod, v)
195+
updated, err := r.updateServiceReadiness(ctx, pod, expectedReadinessGateStatus)
197196
if err != nil {
198197
return reconcile.Result{}, err // Only need set once
199198
}
200199
if updated {
201-
logger.Info("update readiness gate", "status", v)
200+
logger.Info("update readiness gate", "status", expectedReadinessGateStatus)
202201
}
203202
keeped = true
204203
}
@@ -221,7 +220,7 @@ func (r *ReconcilePodOpsLifecycle) Reconcile(ctx context.Context, request reconc
221220
// addServiceAvailable try to add service available label to pod
222221
func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, error) {
223222
if pod.Labels == nil {
224-
return false, nil
223+
pod.Labels = map[string]string{}
225224
}
226225
if _, ok := pod.Labels[v1alpha1.PodServiceAvailableLabel]; ok {
227226
return false, nil
@@ -248,10 +247,29 @@ func (r *ReconcilePodOpsLifecycle) addServiceAvailable(pod *corev1.Pod) (bool, e
248247
return false, nil
249248
}
250249

251-
podLabelsToAdd := map[string]string{
252-
v1alpha1.PodServiceAvailableLabel: strconv.FormatInt(time.Now().UnixNano(), 10),
250+
key := controllerKey(pod)
251+
r.expectation.ExpectUpdate(key, pod.ResourceVersion)
252+
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
253+
newPod := &corev1.Pod{}
254+
err := r.Client.Get(context.Background(), types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name}, newPod)
255+
if err != nil {
256+
return err
257+
}
258+
if newPod.Labels == nil {
259+
newPod.Labels = map[string]string{}
260+
}
261+
newPod.Labels[v1alpha1.PodServiceAvailableLabel] = strconv.FormatInt(time.Now().UnixNano(), 10)
262+
delete(newPod.Labels, v1alpha1.PodCreatingLabel)
263+
delete(newPod.Labels, v1alpha1.PodCompletingLabel)
264+
265+
return r.Client.Update(context.Background(), newPod)
266+
})
267+
if err != nil {
268+
r.Logger.Error(err, "failed to set Pod as service available", "pod", utils.ObjectKeyString(pod))
269+
r.expectation.DeleteExpectations(key)
253270
}
254-
return true, r.addLabels(context.Background(), pod, podLabelsToAdd)
271+
272+
return true, err
255273
}
256274

257275
func (r *ReconcilePodOpsLifecycle) removeDirtyExpectedFinalizer(pod *corev1.Pod, notSatisfiedFinalizers map[string]string) (bool, error) {

0 commit comments

Comments
 (0)