Skip to content

Commit

Permalink
refactor(controller): optimize PDB creation request (#12974)
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Gilgur <agilgur5@gmail.com>
  • Loading branch information
agilgur5 committed Apr 27, 2024
1 parent a2480cb commit 30ca369
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3741,38 +3741,30 @@ func (woc *wfOperationCtx) createPDBResource(ctx context.Context) error {
return nil
}

pdb, err := woc.controller.kubeclientset.PolicyV1().PodDisruptionBudgets(woc.wf.Namespace).Get(
ctx,
woc.wf.Name,
metav1.GetOptions{},
)
if err != nil && !apierr.IsNotFound(err) && !apierr.IsAlreadyExists(err) {
return err
}
if pdb != nil && pdb.Name != "" {
woc.log.Info("PDB resource already exists for workflow.")
return nil
}

labels := map[string]string{common.LabelKeyWorkflow: woc.wf.Name}
pdbSpec := *woc.execWf.Spec.PodDisruptionBudget
if pdbSpec.Selector == nil {
pdbSpec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{common.LabelKeyWorkflow: woc.wf.Name},
MatchLabels: labels,
}
}

newPDB := policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: woc.wf.Name,
Labels: map[string]string{common.LabelKeyWorkflow: woc.wf.Name},
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(woc.wf, wfv1.SchemeGroupVersion.WithKind(workflow.WorkflowKind)),
},
},
Spec: pdbSpec,
}
_, err = woc.controller.kubeclientset.PolicyV1().PodDisruptionBudgets(woc.wf.Namespace).Create(ctx, &newPDB, metav1.CreateOptions{})
_, err := woc.controller.kubeclientset.PolicyV1().PodDisruptionBudgets(woc.wf.Namespace).Create(ctx, &newPDB, metav1.CreateOptions{})
if err != nil {
if apierr.IsAlreadyExists(err) {
woc.log.Info("PDB resource already exists for workflow.")
return nil
}
return err
}
woc.log.Info("Created PDB resource for workflow.")
Expand Down

0 comments on commit 30ca369

Please sign in to comment.