Skip to content

Commit

Permalink
Merge branch 'main' into fixup-obj-bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mendoza committed Mar 12, 2024
2 parents 3850ea1 + 5639638 commit 76d6756
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 55 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build_test_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ jobs:
e2e-test:
needs: [go-build-test, docker-build]
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- name: Harden Runner
uses: step-security/harden-runner@v2
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ IMAGE_NAME ?= cluster-api-provider-linode
CONTROLLER_IMAGE ?= $(REGISTRY)/$(IMAGE_NAME)
TAG ?= dev
ENVTEST_K8S_VERSION := 1.28.0
VERSION ?= $(shell git describe --always --tag --dirty=-dev)
BUILD_ARGS := --build-arg VERSION=$(VERSION)
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
CONTAINER_TOOL ?= docker
MDBOOK_DEV_HOST = 0.0.0.0
MDBOOK_DEV_PORT = 3000
VERSION ?= $(shell git describe --always --tag --dirty=-dev)

# ENVTEST_K8S_VERSION
# - refers to the version of kubebuilder assets to be downloaded by envtest binary.
Expand Down
17 changes: 17 additions & 0 deletions cloud/scope/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ package scope
import (
"context"

"github.com/linode/linodego"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// LinodeObjectStorageClient defines functions suitable for provisioning object storage buckets and keys.
type LinodeObjectStorageClient interface {
GetObjectStorageBucket(ctx context.Context, cluster, label string) (*linodego.ObjectStorageBucket, error)
CreateObjectStorageBucket(ctx context.Context, opts linodego.ObjectStorageBucketCreateOptions) (*linodego.ObjectStorageBucket, error)
CreateObjectStorageKey(ctx context.Context, opts linodego.ObjectStorageKeyCreateOptions) (*linodego.ObjectStorageKey, error)
DeleteObjectStorageKey(ctx context.Context, keyID int) error
}

// LinodeObjectStorageClientBuilder is a function that returns a LinodeObjectStorageClient.
type LinodeObjectStorageClientBuilder func(apiKey string) (LinodeObjectStorageClient, error)

// CreateLinodeObjectStorageClient is the main implementation of LinodeObjectStorageClientBuilder.
func CreateLinodeObjectStorageClient(apiKey string) (LinodeObjectStorageClient, error) {
return CreateLinodeClient(apiKey)
}

type k8sClient interface {
client.Client
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopePara
}
apiKey = string(data)
}
linodeClient, err := createLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestClusterScopeMethods(t *testing.T) {
mockPatchHelper := mock.NewMockPatchHelper(ctrl)
mockK8sClient := mock.NewMockk8sClient(ctrl)

lClient, err := createLinodeClient("test-key")
lClient, err := CreateLinodeClient("test-key")
if err != nil {
t.Errorf("createLinodeClient() error = %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type patchNewHelper func(obj client.Object, crClient client.Client) (*patch.Helper, error)

func createLinodeClient(apiKey string) (*linodego.Client, error) {
func CreateLinodeClient(apiKey string) (*linodego.Client, error) {
if apiKey == "" {
return nil, errors.New("missing Linode API key")
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCreateLinodeClient(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

got, err := createLinodeClient(testCase.apiKey)
got, err := CreateLinodeClient(testCase.apiKey)

if testCase.expectedErr != nil {
assert.EqualError(t, err, testCase.expectedErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewMachineScope(ctx context.Context, apiKey string, params MachineScopePara
}
apiKey = string(data)
}
linodeClient, err := createLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
14 changes: 9 additions & 5 deletions cloud/scope/object_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import (
)

type ObjectStorageBucketScopeParams struct {
Client client.Client
Bucket *infrav1alpha1.LinodeObjectStorageBucket
Logger *logr.Logger
Client client.Client
LinodeClientBuilder LinodeObjectStorageClientBuilder
Bucket *infrav1alpha1.LinodeObjectStorageBucket
Logger *logr.Logger
}

type ObjectStorageBucketScope struct {
client client.Client
Bucket *infrav1alpha1.LinodeObjectStorageBucket
Logger logr.Logger
LinodeClient *linodego.Client
LinodeClient LinodeObjectStorageClient
BucketPatchHelper *patch.Helper
}

Expand All @@ -40,6 +41,9 @@ func validateObjectStorageBucketScopeParams(params ObjectStorageBucketScopeParam
if params.Logger == nil {
return errors.New("logger is required when creating an ObjectStorageBucketScope")
}
if params.LinodeClientBuilder == nil {
return errors.New("LinodeClientBuilder is required when creating an ObjectStorageBucketScope")
}

return nil
}
Expand All @@ -57,7 +61,7 @@ func NewObjectStorageBucketScope(ctx context.Context, apiKey string, params Obje
}
apiKey = string(data)
}
linodeClient, err := createLinodeClient(apiKey)
linodeClient, err := params.LinodeClientBuilder(apiKey)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewVPCScope(ctx context.Context, apiKey string, params VPCScopeParams) (*VP
}
apiKey = string(data)
}
linodeClient, err := createLinodeClient(apiKey)
linodeClient, err := CreateLinodeClient(apiKey)
if err != nil {
return nil, fmt.Errorf("failed to create linode client: %w", err)
}
Expand Down
14 changes: 8 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

infrastructurev1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
"github.com/linode/cluster-api-provider-linode/cloud/scope"
controller2 "github.com/linode/cluster-api-provider-linode/controller"
"github.com/linode/cluster-api-provider-linode/version"

Expand Down Expand Up @@ -140,12 +141,13 @@ func main() {
os.Exit(1)
}
if err = (&controller2.LinodeObjectStorageBucketReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Logger: ctrl.Log.WithName("LinodeObjectStorageBucketReconciler"),
Recorder: mgr.GetEventRecorderFor("LinodeObjectStorageBucketReconciler"),
WatchFilterValue: objectStorageBucketWatchFilter,
LinodeApiKey: linodeToken,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Logger: ctrl.Log.WithName("LinodeObjectStorageBucketReconciler"),
Recorder: mgr.GetEventRecorderFor("LinodeObjectStorageBucketReconciler"),
WatchFilterValue: objectStorageBucketWatchFilter,
LinodeApiKey: linodeToken,
LinodeClientBuilder: scope.CreateLinodeObjectStorageClient,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "LinodeObjectStorageBucket")
os.Exit(1)
Expand Down
24 changes: 14 additions & 10 deletions controller/linodeobjectstoragebucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ import (
// LinodeObjectStorageBucketReconciler reconciles a LinodeObjectStorageBucket object
type LinodeObjectStorageBucketReconciler struct {
client.Client
Scheme *runtime.Scheme
Logger logr.Logger
Recorder record.EventRecorder
LinodeApiKey string
WatchFilterValue string
ReconcileTimeout time.Duration
Scheme *runtime.Scheme
Logger logr.Logger
Recorder record.EventRecorder
LinodeApiKey string
LinodeClientBuilder scope.LinodeObjectStorageClientBuilder
WatchFilterValue string
ReconcileTimeout time.Duration
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragebuckets,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -90,9 +91,10 @@ func (r *LinodeObjectStorageBucketReconciler) Reconcile(ctx context.Context, req
ctx,
r.LinodeApiKey,
scope.ObjectStorageBucketScopeParams{
Client: r.Client,
Bucket: objectStorageBucket,
Logger: &logger,
Client: r.Client,
LinodeClientBuilder: r.LinodeClientBuilder,
Bucket: objectStorageBucket,
Logger: &logger,
},
)
if err != nil {
Expand Down Expand Up @@ -173,7 +175,7 @@ func (r *LinodeObjectStorageBucketReconciler) reconcileApply(ctx context.Context
bScope.Bucket.Status.LastKeyGeneration = bScope.Bucket.Spec.KeyGeneration
}

r.Recorder.Event(bScope.Bucket, corev1.EventTypeNormal, "Ready", "Object storage bucket configuration applied")
r.Recorder.Event(bScope.Bucket, corev1.EventTypeNormal, "Ready", "Object storage bucket applied")

bScope.Bucket.Status.Ready = true
conditions.MarkTrue(bScope.Bucket, clusterv1.ReadyCondition)
Expand All @@ -199,6 +201,8 @@ func (r *LinodeObjectStorageBucketReconciler) reconcileDelete(ctx context.Contex
return err
}

r.Recorder.Event(bScope.Bucket, clusterv1.DeletedReason, "Ready", "Object storage bucket deleted")

return nil
}

Expand Down
Loading

0 comments on commit 76d6756

Please sign in to comment.