Skip to content

Commit

Permalink
fix, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shaofan-hs committed Aug 25, 2023
1 parent fd34779 commit db21af8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
6 changes: 3 additions & 3 deletions pkg/controllers/collaset/collaset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func expectedStatusReplicas(c client.Client, cls *appsv1alpha1.CollaSet, schedul
}

func updateCollaSetWithRetry(c client.Client, namespace, name string, updateFn func(cls *appsv1alpha1.CollaSet) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
cls := &appsv1alpha1.CollaSet{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, cls); err != nil {
return err
Expand All @@ -650,7 +650,7 @@ func updateCollaSetWithRetry(c client.Client, namespace, name string, updateFn f
}

func updatePodWithRetry(c client.Client, namespace, name string, updateFn func(pod *corev1.Pod) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
pod := &corev1.Pod{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, pod); err != nil {
return err
Expand All @@ -665,7 +665,7 @@ func updatePodWithRetry(c client.Client, namespace, name string, updateFn func(p
}

func updatePodStatusWithRetry(c client.Client, namespace, name string, updateFn func(pod *corev1.Pod) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
pod := &corev1.Pod{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, pod); err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions pkg/controllers/collaset/synccontrol/sync_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package synccontrol

import (
"fmt"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (sc *RealSyncControl) SyncPods(instance *appsv1alpha1.CollaSet, updatedRevi

// get owned IDs
var ownedIDs map[int]*appsv1alpha1.ContextDetail
if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
ownedIDs, err = podcontext.AllocateID(sc.client, instance, updatedRevision.Name, int(replicasRealValue(instance.Spec.Replicas)))
return err
}); err != nil {
Expand Down Expand Up @@ -126,7 +127,7 @@ func (sc *RealSyncControl) SyncPods(instance *appsv1alpha1.CollaSet, updatedRevi

if needUpdateContext {
klog.V(1).Infof("try to update ResourceContext for CollaSet %s/%s when sync", instance.Namespace, instance.Name)
if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return podcontext.UpdateToPodContext(sc.client, instance, ownedIDs)
}); err != nil {
return false, nil, ownedIDs, fmt.Errorf("fail to update ResourceContext when reclaiming IDs: %s", err)
Expand Down Expand Up @@ -248,7 +249,7 @@ func (sc *RealSyncControl) Scale(set *appsv1alpha1.CollaSet, podWrappers []*coll
// mark these Pods to scalingIn
if needUpdateContext {
klog.V(1).Infof("try to update ResourceContext for CollaSet %s/%s when scaling in Pod", set.Namespace, set.Name)
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
return podcontext.UpdateToPodContext(sc.client, set, ownedIDs)
})

Expand Down Expand Up @@ -303,7 +304,7 @@ func (sc *RealSyncControl) Scale(set *appsv1alpha1.CollaSet, podWrappers []*coll

if needUpdatePodContext {
klog.V(1).Infof("try to update ResourceContext for CollaSet %s/%s after scaling", set.Namespace, set.Name)
if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return podcontext.UpdateToPodContext(sc.client, set, ownedIDs)
}); err != nil {
return scaling, fmt.Errorf("fail to reset ResourceContext: %s", err)
Expand Down Expand Up @@ -401,7 +402,7 @@ func (sc *RealSyncControl) Update(set *appsv1alpha1.CollaSet, podWrapers []*coll
// 5. mark Pod to use updated revision before updating it.
if needUpdateContext {
klog.V(1).Infof("try to update ResourceContext for CollaSet %s/%s", set.Namespace, set.Name)
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
return podcontext.UpdateToPodContext(sc.client, set, ownedIDs)
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/poddeletion/poddeletion_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var _ = Describe("Pod Deletion controller", func() {
})

func updatePodWithRetry(c client.Client, namespace, name string, updateFn func(pod *corev1.Pod) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
pod := &corev1.Pod{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, pod); err != nil {
return err
Expand All @@ -125,7 +125,7 @@ func updatePodWithRetry(c client.Client, namespace, name string, updateFn func(p
}

func updatePodStatusWithRetry(c client.Client, namespace, name string, updateFn func(pod *corev1.Pod) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
pod := &corev1.Pod{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, pod); err != nil {
return err
Expand Down
24 changes: 15 additions & 9 deletions pkg/controllers/podopslifecycle/podopslifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -213,7 +214,7 @@ func (r *ReconcilePodOpsLifecycle) updateServiceReadiness(ctx context.Context, p

key := controllerKey(pod)
r.expectation.ExpectUpdate(key, pod.ResourceVersion)
if err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return r.Client.Status().Update(ctx, pod)
}); err != nil {
klog.Errorf("failed to update pod status %s: %s", key, err)
Expand Down Expand Up @@ -311,17 +312,22 @@ func (r *ReconcilePodOpsLifecycle) addLabels(ctx context.Context, pod *corev1.Po
if len(labels) == 0 {
return nil
}
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
for k, v := range labels {
pod.Labels[k] = v
}

key := controllerKey(pod)
expectation.ExpectUpdate(key, pod.ResourceVersion)
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return r.Client.Update(ctx, pod)
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
newPod := &corev1.Pod{}
err := r.Client.Get(ctx, types.NamespacedName{Namespace: pod.Namespace, Name: pod.Name}, newPod)
if err != nil {
return err
}
if newPod.Labels == nil {
newPod.Labels = map[string]string{}
}
for k, v := range labels {
newPod.Labels[k] = v
}
return r.Client.Update(ctx, newPod)
})
if err != nil {
klog.Errorf("failed to update pod %s with labels: %v: %s", key, labels, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func expectedStatusReplicas(c client.Client, cls *appsv1alpha1.CollaSet, schedul
}

func updateCollaSetWithRetry(c client.Client, namespace, name string, updateFn func(cls *appsv1alpha1.CollaSet) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
cls := &appsv1alpha1.CollaSet{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, cls); err != nil {
return err
Expand All @@ -289,7 +289,7 @@ func updateCollaSetWithRetry(c client.Client, namespace, name string, updateFn f
}

func updatePodWithRetry(c client.Client, namespace, name string, updateFn func(pod *corev1.Pod) bool) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
pod := &corev1.Pod{}
if err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, pod); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/ruleset/ruleset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (r *RuleSetReconciler) updatePodDetail(ctx context.Context, pod *corev1.Pod
return nil
}
patch := client.RawPatch(types.MergePatchType, controllerutils.GetLabelAnnoPatchBytes(nil, nil, nil, map[string]string{detailAnno: newDetail}))
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
return r.Patch(ctx, pod, patch)
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func CreateTestingNS(baseName string, c clientset.Interface, labels map[string]s
return nil, err
}

err = retry.OnError(retry.DefaultBackoff, apierrors.IsNotFound, func() error {
err = retry.OnError(retry.DefaultRetry, apierrors.IsNotFound, func() error {
time.Sleep(1 * time.Second)
Logf("waiting get namespace for: %s", got.Name)
getNs, err := c.CoreV1().Namespaces().Get(context.TODO(), got.Name, metav1.GetOptions{})
Expand Down

0 comments on commit db21af8

Please sign in to comment.