Home > Mobile >  How to trigger a rollout restart on deployment resource from controller-runtime
How to trigger a rollout restart on deployment resource from controller-runtime

Time:02-15

I have been using kubebuilder for writing custom controller, and aware of Get(), Update(), Delete() methods that it provides. But Now I am looking for a method which mimic the behaviour of kubectl rollout restart deployment. If there is no such direct method then I am looking for correct way to mimic the same.

type CustomReconciler struct {
    client.Client
    Log    logr.Logger
    Scheme *runtime.Scheme
}

func (r *CustomReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    configMap := &v1.ConfigMap{}
    err = r.Get(ctx, req.namespacedName, configMap)
    if err != nil {
        logger.Error(err, "Failed to GET configMap")
        return ctrl.Result{}, err
}

Say in above code I read a deployment name from the configmap and rollout restart the same as follows:

    val := configMap.Data["config.yml"]
    config := Config{}
    if err := yaml.Unmarshal([]byte(val), &config); err != nil {
        logger.Error(err, "failed to unmarshal config data")
        return ctrl.Result{}, err
    }

    // Need equivalent of following
    // r.RolloutRestart(config.DeploymentName)

CodePudding user response:

In all cases where you wish to replicate kubectl behavior, the answer is always to increase its verbosity and it'll show you exactly -- sometimes down to the wire payloads -- what it is doing.

For rollout restart, one will find that it just bumps an annotation on the Deployment/StatefulSet/whatever and that causes the outer object to be "different," and triggering a reconciliation run

You can squat on their annotation, or you can make up your own, or you can use a label change -- practically any "meaningless" change will do

  • Related