Created
November 19, 2020 17:09
-
-
Save didil/ebb1f3aa98b9757b26bab3762eeed201 to your computer and use it in GitHub Desktop.
Deployment Reconcile Loop
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
func (r *DeploymentReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | |
ctx := context.Background() | |
log := r.Log.WithValues("deployment", req.NamespacedName) | |
dep := &appsv1.Deployment{} | |
err := r.Get(ctx, req.NamespacedName, dep) | |
if err != nil { | |
if errors.IsNotFound(err) { | |
// Request object not found, could have been deleted after reconcile request. | |
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. | |
// Return and don't requeue | |
log.Info("Deployment resource not found. Ignoring since object must be deleted") | |
return ctrl.Result{}, nil | |
} | |
// Error reading the object - requeue the request. | |
log.Error(err, "Failed to get deployment") | |
return ctrl.Result{}, err | |
} | |
bucketCloud := abv1.BucketCloud(dep.Annotations[bucketCloudKey]) | |
if bucketCloud == "" { | |
// no autobucket annotation | |
return ctrl.Result{}, nil | |
} | |
// Check if the bucket object already exists, if not create a new one | |
bucket := &abv1.Bucket{} | |
err = r.Get(ctx, types.NamespacedName{Name: dep.Name, Namespace: dep.Namespace}, bucket) | |
if err != nil && errors.IsNotFound(err) { | |
// Define new | |
bucket, err := r.bucketForDeployment(dep) | |
if err != nil { | |
log.Error(err, "Failed to build new Bucket", "Bucket.Name", dep.Name) | |
return ctrl.Result{}, err | |
} | |
log.Info("Creating a new Bucket", "Bucket.Name", bucket.Name) | |
err = r.Create(ctx, bucket) | |
if err != nil { | |
log.Error(err, "Failed to create new Bucket", "Bucket.Name", bucket.Name) | |
return ctrl.Result{}, err | |
} | |
// created successfully - return and requeue | |
return ctrl.Result{Requeue: true}, nil | |
} else if err != nil { | |
log.Error(err, "Failed to get Bucket") | |
return ctrl.Result{}, err | |
} | |
// check if bucket ondelete policy must be updated | |
bucketOnDeletePolicy := abv1.BucketOnDeletePolicy(dep.Annotations[bucketOnDeletePolicyKey]) | |
if bucketOnDeletePolicy != bucket.Spec.OnDeletePolicy { | |
bucket.Spec.OnDeletePolicy = bucketOnDeletePolicy | |
log.Info("Updating Bucket OnDeletePolicy", "Bucket.Name", bucket.Name, "Bucket.OnDeletePolicy", bucketOnDeletePolicy) | |
if err := r.Update(context.Background(), bucket); err != nil { | |
log.Error(err, "Failed to update bucket") | |
return ctrl.Result{}, err | |
} | |
// updated successfully - return and requeue | |
return ctrl.Result{Requeue: true}, nil | |
} | |
return ctrl.Result{}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment