Last active
December 2, 2021 22:08
-
-
Save gallettilance/f842411c90f548023414d0029c5c892d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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