Skip to content

Commit

Permalink
chore: retry with client-go native routine
Browse files Browse the repository at this point in the history
  • Loading branch information
nberlee committed Apr 22, 2022
1 parent f6db366 commit c2fdf81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/cluster/tolerated_failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *Cluster) updatePodDisruptionBudget(ctx context.Context) {
if c.cluster.Spec.PodDisruptionBudget {
c.logger.Infof("Updating disruption budget, amount of pods needed for a quorum changed from %d to %d", c.status.MinAvailable, currentlyMinAvailable)

err := k8sutil.UpdateOrCreatePodDisruptionBudget(ctx, c.config.KubeCli, c.cluster.Namespace, c.cluster.Name, currentlyMinAvailable, c.cluster.AsOwner(), true)
err := k8sutil.UpdateOrCreatePodDisruptionBudget(ctx, c.config.KubeCli, c.cluster.Namespace, c.cluster.Name, currentlyMinAvailable, c.cluster.AsOwner())
if err != nil {
c.logger.Errorf("failed to create/update pod disruption budget: %v", err)
return
Expand Down
33 changes: 17 additions & 16 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // for gcp auth
"k8s.io/client-go/rest"
"k8s.io/client-go/util/retry"
)

const (
Expand Down Expand Up @@ -550,26 +551,26 @@ func PatchDeployment(ctx context.Context, kubecli kubernetes.Interface, namespac
return err
}

func UpdateOrCreatePodDisruptionBudget(ctx context.Context, kubecli kubernetes.Interface, namespace, name string, minAvailable int, owner metav1.OwnerReference, retryOptimisticLockingUpdate bool) error {
func UpdateOrCreatePodDisruptionBudget(ctx context.Context, kubecli kubernetes.Interface, namespace, name string, minAvailable int, owner metav1.OwnerReference) error {
pdb := newEtcdPodDisruptionBudgetManifest(name, name, &intstr.IntOrString{IntVal: int32(minAvailable)})

addOwnerRefToObject(pdb.GetObjectMeta(), owner)

od, err := kubecli.PolicyV1beta1().PodDisruptionBudgets(namespace).Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
_, err := kubecli.PolicyV1beta1().PodDisruptionBudgets(namespace).Create(ctx, pdb, metav1.CreateOptions{})
return err
}
pdb.Status = od.Status
pdb.ObjectMeta.ResourceVersion = od.ObjectMeta.ResourceVersion // Optimistic locking
_, err = kubecli.PolicyV1beta1().PodDisruptionBudgets(namespace).Update(ctx, pdb, metav1.UpdateOptions{})
if apierrors.IsConflict(err) && retryOptimisticLockingUpdate {
return UpdateOrCreatePodDisruptionBudget(ctx, kubecli, namespace, name, minAvailable, owner, false)
}
if err != nil {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
od, err := kubecli.PolicyV1beta1().PodDisruptionBudgets(namespace).Get(ctx, name, metav1.GetOptions{})
if IsKubernetesResourceNotFoundError(err) {
_, err := kubecli.PolicyV1beta1().PodDisruptionBudgets(namespace).Create(ctx, pdb, metav1.CreateOptions{})
return err
}
if err != nil {
return err
}

pdb.Status = od.Status
pdb.ObjectMeta.ResourceVersion = od.ObjectMeta.ResourceVersion // Optimistic locking
_, err = kubecli.PolicyV1beta1().PodDisruptionBudgets(namespace).Update(ctx, pdb, metav1.UpdateOptions{})

return err
}
return nil
})
}

func CascadeDeleteOptions(gracePeriodSeconds int64) *metav1.DeleteOptions {
Expand Down

0 comments on commit c2fdf81

Please sign in to comment.