Created
November 19, 2020 17:13
-
-
Save didil/242744a4dbc1566b61edeeec7e4f60dc to your computer and use it in GitHub Desktop.
Bucket 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 *BucketReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | |
ctx := context.Background() | |
log := r.Log.WithValues("bucket", req.NamespacedName) | |
bucket := &abv1.Bucket{} | |
err := r.Get(ctx, req.NamespacedName, bucket) | |
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("Bucket 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 bucket") | |
return ctrl.Result{}, err | |
} | |
// CODE SKIPPED: Bucket Deletion Code | |
// check if the storage bucket has been created yet | |
if bucket.Status.CreatedAt == "" { | |
// bucket not yet created | |
log.Info("Creating Bucket", "Bucket.Cloud", bucket.Spec.Cloud, "Bucket.Name", bucket.Name) | |
switch bucket.Spec.Cloud { | |
case abv1.BucketCloudGCP: | |
err := r.createGCPBucket(ctx, bucket) | |
if err != nil { | |
log.Error(err, "Failed to create gcp Bucket", "Bucket.Name", bucket.Name) | |
return ctrl.Result{}, err | |
} | |
default: | |
log.Info("Bucket Cloud unknown.", "Bucket.Cloud", bucket.Spec.Cloud) | |
return ctrl.Result{}, nil | |
} | |
bucket.Status.CreatedAt = time.Now().Format(time.RFC3339) | |
err = r.Client.Status().Update(ctx, bucket) | |
if err != nil { | |
log.Error(err, "Failed to update bucket status") | |
return ctrl.Result{}, err | |
} | |
// Status updated - 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