Last active
December 3, 2021 18:14
-
-
Save gallettilance/c258f719be37433069c44cbb76d39438 to your computer and use it in GitHub Desktop.
This file contains 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
var log = ctrl.Log.WithName("controllers").WithName("mykind") | |
type MykindReconciler struct { | |
client.Client | |
Scheme *runtime.Scheme | |
} | |
func (r *MykindReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | |
log.WithValues(req.Name, req.Namespace) | |
cr := &grpv1alpha1.Mykind{} | |
if err := r.Get(ctx, req.NamespacedName, cr); err != nil { | |
if errors.IsNotFound(err) { | |
// Request object not found, could have been deleted | |
return reconcile.Result{}, nil | |
} | |
return ctrl.Result{}, err | |
} | |
// Check if the resource is being deleted | |
if !cr.ObjectMeta.DeletionTimestamp.IsZero() { | |
return r.DeletionReconciler(ctx, cr) | |
} | |
// Initialize resource | |
log.Info("Initializing") | |
if res, err := r.initialize(ctx, cr); err != nil { | |
return res, err | |
} | |
// The resource is being created or updated | |
return r.CreateOrUpdateReconciler(ctx, req, cr) | |
} | |
func (r *MykindReconciler) initialize(ctx context.Context, cr *grpv1alpha1.Mykind) (ctrl.Result, error) { | |
// Mutate Instance in case mutating webhook isn't enabled | |
oldSpec := cr.DeepCopy().Spec | |
crInstance.Default() | |
// check if mutation changed anything | |
if !reflect.DeepEqual(oldSpec, cr.Spec) { | |
log.Info("mutating") | |
if err := r.Update(ctx, cr); err != nil { | |
return r.manageFailure(ctx, cr, err) | |
} | |
} | |
// Validate Instance in case validating webhook isn't enabled | |
// validate creation | |
if err := cr.ValidateCreate(); err != nil { | |
return r.manageFailure(ctx, cr, err) | |
} | |
return ctrl.Result{}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment