Skip to content

Instantly share code, notes, and snippets.

@gallettilance
Last active December 2, 2021 22:08
Show Gist options
  • Select an option

  • Save gallettilance/f842411c90f548023414d0029c5c892d to your computer and use it in GitHub Desktop.

Select an option

Save gallettilance/f842411c90f548023414d0029c5c892d to your computer and use it in GitHub Desktop.
func (r *MykindReconciler) CreateOrUpdateReconciler(ctx context.Context, req ctrl.Request, crInstance *grpv1alpha1.Mykind) (ctrl.Result, error) {
desiredNumPods := crInstance.Spec.NumPods
actualNumPods := 0
if crInstance.Status.Pods == nil {
crInstance.Status.Pods = make(map[string]string)
} else {
podList, err = h.getRunningPodsOwnedBy(ctx, crInstance)
if err != nil {
return ctrl.Result{}, err
}
actualNumPods = len(podList.Items)
}
switch {
case desiredNumPods > actualNumPods:
for i := 0; i < desiredNumPods; i++ {
name := fmt.Sprintf("mypod.%s.%d", crInstance.Name, i)
pod := makePod(name, req.Namespace)
err := r.AddPodIfNotExist(ctx, req, crInstance, pod)
if err != nil {
return ctrl.Result{}, err
}
}
case desiredNumPods < actualNumPods:
for i := desiredNumPods; i < actualNumPods; i++ {
name := fmt.Sprintf("mypod.%s.%d", crInstance.Name, i)
err := r.DeletePodIfExist(ctx, req, crInstance, name)
if err != nil {
return ctrl.Result{}, err
}
}
default:
// nothing to do
return ctrl.Result{}, nil
}
err := r.Status().Update(ctx, crInstance)
if err != nil {
log.Error(err, "Error when updating status")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment