Skip to content

Commit

Permalink
Add deleteBucket
Browse files Browse the repository at this point in the history
  • Loading branch information
amold1 committed Feb 28, 2024
1 parent fe0b237 commit d00d631
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
9 changes: 9 additions & 0 deletions cloud/scope/object_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func (s *ObjectStorageBucketScope) AddFinalizer(ctx context.Context) error {
return nil
}

// RemoveFinalizer removes a finalizer immediately patches the
// object to avoid any race conditions.
func (s *ObjectStorageBucketScope) RemoveFinalizer(ctx context.Context) error {
if controllerutil.RemoveFinalizer(s.Object, infrav1alpha1.GroupVersion.String()) {
return s.Close(ctx)
}
return nil
}

// CreateAccessKeySecret creates a Secret containing keys created for accessing the bucket.
func (s *ObjectStorageBucketScope) CreateAccessKeySecret(ctx context.Context, keys [2]linodego.ObjectStorageKey, secretName string) error {
var err error
Expand Down
9 changes: 9 additions & 0 deletions cloud/services/object_storage_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func CreateObjectStorageBucket(ctx context.Context, bucketScope *scope.ObjectSto
return bucket, nil
}

func DeleteObjectStorageBucket(ctx context.Context, bucketScope *scope.ObjectStorageBucketScope, logger logr.Logger) error {
// Delete the OBJ bucket.
if err := bucketScope.LinodeClient.DeleteObjectStorageBucket(ctx, bucketScope.Object.Spec.Cluster, bucketScope.Object.Spec.Label); err != nil {
return fmt.Errorf("delete object storage bucket: %w", err)
}

return nil
}

func CreateObjectStorageKeys(ctx context.Context, bucketScope *scope.ObjectStorageBucketScope, logger logr.Logger) ([2]linodego.ObjectStorageKey, error) {
var newKeys [2]linodego.ObjectStorageKey
var existingKeys []linodego.ObjectStorageKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: LinodeObjectStorageBucket
metadata:
Expand All @@ -9,7 +10,15 @@ metadata:
app.kubernetes.io/created-by: cluster-api-provider-linode
name: linodeobjectstoragebucket-sample
spec:
apiKeySecretRef:
credentialsRef:
name: api-key-secret
key: field-name-in-secret
namespace: default
keyGeneration: 0
cluster: us-ord-1
---
apiVersion: v1
kind: Secret
metadata:
name: api-key-secret
stringData:
apiToken: "changeme"
55 changes: 52 additions & 3 deletions controller/linodeobjectstoragebucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package controller

import (
"context"
"errors"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"

"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -40,6 +42,7 @@ import (
"github.com/linode/cluster-api-provider-linode/cloud/services"
"github.com/linode/cluster-api-provider-linode/util"
"github.com/linode/cluster-api-provider-linode/util/reconciler"
"github.com/linode/linodego"
)

// LinodeObjectStorageBucketReconciler reconciles a LinodeObjectStorageBucket object
Expand Down Expand Up @@ -156,7 +159,17 @@ func (r *LinodeObjectStorageBucketReconciler) reconcileCreate(ctx context.Contex
bucketScope.Object.Spec.Label = util.RenderObjectLabel(bucketScope.Object.UID)
}

bucket, err := services.CreateObjectStorageBucket(ctx, bucketScope, logger)
bucket := &linodego.ObjectStorageBucket{}

exists, err := r.bucketExists(ctx, logger, bucketScope)
if !exists {
bucket, err = services.CreateObjectStorageBucket(ctx, bucketScope, logger)
if err != nil {
r.setFailure(bucketScope, err)

return err
}
}
if err != nil {
r.setFailure(bucketScope, err)

Expand Down Expand Up @@ -191,11 +204,47 @@ func (r *LinodeObjectStorageBucketReconciler) reconcileCreate(ctx context.Contex
}

func (r *LinodeObjectStorageBucketReconciler) reconcileUpdate(ctx context.Context, logger logr.Logger, bucketScope *scope.ObjectStorageBucketScope) error {
panic("unimplemented")
return nil
}

func (r *LinodeObjectStorageBucketReconciler) reconcileDelete(ctx context.Context, logger logr.Logger, bucketScope *scope.ObjectStorageBucketScope) error {
panic("unimplemented")
name := types.NamespacedName{Namespace: bucketScope.Object.Namespace, Name: bucketScope.Object.Name}
bucket := &infrav1alpha1.LinodeObjectStorageBucket{}
if err := r.Client.Get(ctx, name, bucket); err != nil {
if apierrors.IsNotFound(err); err != nil {
logger.Error(err, "Failed to fetch bucket")
return err
}
return nil
}

// Delete the OBJ bucket.
if err := services.DeleteObjectStorageBucket(ctx, bucketScope, logger); err != nil {
return fmt.Errorf("delete object storage bucket: %w", err)
}

// Remove the finalizer.
// This will allow the ObjectStorageBucket object to be deleted.
if err := bucketScope.RemoveFinalizer(ctx); err != nil {
return nil
}

return nil
}

// bucketExists indicates whether or not an Object Storage Bucket exists.
// It uses the Linode API.
func (r *LinodeObjectStorageBucketReconciler) bucketExists(ctx context.Context, logger logr.Logger, bucketScope *scope.ObjectStorageBucketScope) (bool, error) {
if bucketScope.Object == nil {
return false, errors.New("nil bucket")
}

_, err := bucketScope.LinodeClient.GetObjectStorageBucket(ctx, bucketScope.Object.Spec.Cluster, bucketScope.Object.ObjectMeta.Name)
if err != nil {
return false, fmt.Errorf("get object storage bucket: %w", err)
}

return true, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down

0 comments on commit d00d631

Please sign in to comment.