diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index c194a82a1..42a5f10a5 100644 --- a/cloud/services/loadbalancers.go +++ b/cloud/services/loadbalancers.go @@ -60,18 +60,12 @@ func CreateNodeBalancer(ctx context.Context, clusterScope *scope.ClusterScope, l Tags: tags, } - if linodeNB, err = clusterScope.LinodeClient.CreateNodeBalancer(ctx, createConfig); err != nil { - logger.Info("Failed to create Linode NodeBalancer", "error", err.Error()) - - // Already exists is not an error - apiErr := linodego.Error{} - if errors.As(err, &apiErr) && apiErr.Code != http.StatusFound { - return nil, err - } - - if linodeNB != nil { - logger.Info("Linode NodeBalancer already exists", "existing", linodeNB.Label) - } + linodeNB, err = clusterScope.LinodeClient.CreateNodeBalancer(ctx, createConfig) + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { + return nil, err + } + if linodeNB != nil { + logger.Info("Linode NodeBalancer already exists", "existing", linodeNB.Label) } return linodeNB, nil diff --git a/cloud/services/object_storage_buckets.go b/cloud/services/object_storage_buckets.go index cd2c327b9..8240ad232 100644 --- a/cloud/services/object_storage_buckets.go +++ b/cloud/services/object_storage_buckets.go @@ -2,7 +2,6 @@ package services import ( "context" - "errors" "fmt" "net/http" @@ -10,6 +9,7 @@ import ( utilerrors "k8s.io/apimachinery/pkg/util/errors" "github.com/linode/cluster-api-provider-linode/cloud/scope" + "github.com/linode/cluster-api-provider-linode/util" ) func EnsureObjectStorageBucket(ctx context.Context, bScope *scope.ObjectStorageBucketScope) (*linodego.ObjectStorageBucket, error) { @@ -18,8 +18,7 @@ func EnsureObjectStorageBucket(ctx context.Context, bScope *scope.ObjectStorageB bScope.Bucket.Spec.Cluster, bScope.Bucket.Name, ) - linodeErr := &linodego.Error{} - if errors.As(err, linodeErr) && linodeErr.StatusCode() != http.StatusNotFound { + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { return nil, fmt.Errorf("failed to get bucket from cluster %s: %w", bScope.Bucket.Spec.Cluster, err) } if bucket != nil { @@ -108,13 +107,10 @@ func RevokeObjectStorageKeys(ctx context.Context, bScope *scope.ObjectStorageBuc } func revokeObjectStorageKey(ctx context.Context, bScope *scope.ObjectStorageBucketScope, keyID int) error { - if err := bScope.LinodeClient.DeleteObjectStorageKey(ctx, keyID); err != nil { - linodeErr := &linodego.Error{} - if errors.As(err, linodeErr) && linodeErr.StatusCode() != http.StatusNotFound { - bScope.Logger.Error(err, "Failed to revoke access key", "id", keyID) - - return fmt.Errorf("failed to revoke access key: %w", err) - } + err := bScope.LinodeClient.DeleteObjectStorageKey(ctx, keyID) + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { + bScope.Logger.Error(err, "Failed to revoke access key", "id", keyID) + return fmt.Errorf("failed to revoke access key: %w", err) } bScope.Logger.Info("Revoked access key", "id", keyID) diff --git a/controller/linodecluster_controller.go b/controller/linodecluster_controller.go index 748aaf5ea..aa636ce6b 100644 --- a/controller/linodecluster_controller.go +++ b/controller/linodecluster_controller.go @@ -18,13 +18,11 @@ package controller import ( "context" - "errors" "fmt" "net/http" "time" "github.com/go-logr/logr" - "github.com/linode/linodego" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" utilerrors "k8s.io/apimachinery/pkg/util/errors" @@ -165,7 +163,12 @@ func (r *LinodeClusterReconciler) reconcileCreate(ctx context.Context, logger lo linodeNB, err := services.CreateNodeBalancer(ctx, clusterScope, logger) if err != nil { setFailureReason(clusterScope, cerrs.CreateClusterError, err, r) + return err + } + if linodeNB == nil { + err = fmt.Errorf("nodeBalancer created was nil") + setFailureReason(clusterScope, cerrs.CreateClusterError, err, r) return err } @@ -197,16 +200,10 @@ func (r *LinodeClusterReconciler) reconcileDelete(ctx context.Context, logger lo return nil } - if err := clusterScope.LinodeClient.DeleteNodeBalancer(ctx, *clusterScope.LinodeCluster.Spec.Network.NodeBalancerID); err != nil { - logger.Info("Failed to delete Linode NodeBalancer", "error", err.Error()) - - // Not found is not an error - apiErr := linodego.Error{} - if errors.As(err, &apiErr) && apiErr.Code != http.StatusNotFound { - setFailureReason(clusterScope, cerrs.DeleteClusterError, err, r) - - return err - } + err := clusterScope.LinodeClient.DeleteNodeBalancer(ctx, *clusterScope.LinodeCluster.Spec.Network.NodeBalancerID) + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { + setFailureReason(clusterScope, cerrs.DeleteClusterError, err, r) + return err } conditions.MarkFalse(clusterScope.LinodeCluster, clusterv1.ReadyCondition, clusterv1.DeletedReason, clusterv1.ConditionSeverityInfo, "Load balancer deleted") diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index e8957888e..125e93980 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -376,8 +376,7 @@ func (r *LinodeMachineReconciler) reconcileUpdate( } if linodeInstance, err = machineScope.LinodeClient.GetInstance(ctx, *machineScope.LinodeMachine.Spec.InstanceID); err != nil { - err = util.IgnoreLinodeAPIError(err, http.StatusNotFound) - if err != nil { + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { logger.Error(err, "Failed to get Linode machine instance") } else { logger.Info("Instance not found, let's create a new one") diff --git a/controller/linodeobjectstoragebucket_controller.go b/controller/linodeobjectstoragebucket_controller.go index 8861c3742..107dd1fb0 100644 --- a/controller/linodeobjectstoragebucket_controller.go +++ b/controller/linodeobjectstoragebucket_controller.go @@ -151,6 +151,14 @@ func (r *LinodeObjectStorageBucketReconciler) reconcileApply(ctx context.Context return err } + + if bucket == nil { + err = fmt.Errorf("bucket created is nil") + bScope.Logger.Error(err, "Failed to ensure bucket exists") + r.setFailure(bScope, err) + return err + } + bScope.Bucket.Status.Hostname = util.Pointer(bucket.Hostname) bScope.Bucket.Status.CreationTime = &metav1.Time{Time: *bucket.Created} diff --git a/devbox.lock b/devbox.lock index 41de68583..9babb9b7a 100644 --- a/devbox.lock +++ b/devbox.lock @@ -101,23 +101,23 @@ } } }, - "golangci-lint@1.56.1": { - "last_modified": "2024-02-12T13:06:46Z", - "resolved": "github:NixOS/nixpkgs/2d627a2a704708673e56346fcb13d25344b8eaf3#golangci-lint", + "golangci-lint@latest": { + "last_modified": "2024-02-24T23:06:34Z", + "resolved": "github:NixOS/nixpkgs/9a9dae8f6319600fa9aebde37f340975cab4b8c0#golangci-lint", "source": "devbox-search", - "version": "1.56.1", + "version": "1.56.2", "systems": { "aarch64-darwin": { - "store_path": "/nix/store/4f7s164lywkq0nzv2r80sj7z0kjvw7vs-golangci-lint-1.56.1" + "store_path": "/nix/store/fs44z0nysjiwl2sj2m9x70dbrx2lbyhh-golangci-lint-1.56.2" }, "aarch64-linux": { - "store_path": "/nix/store/dyv5cy8inj2f3m1ymh5jqisjrss53y7w-golangci-lint-1.56.1" + "store_path": "/nix/store/bs18c2bx56b6xnpf49wb5kzi3vynbqmx-golangci-lint-1.56.2" }, "x86_64-darwin": { - "store_path": "/nix/store/v57g47gzxgbwj6193n5zrhzrdrp53g1s-golangci-lint-1.56.1" + "store_path": "/nix/store/1xhl4b3nk3npq70d651vg4bamfg8xyga-golangci-lint-1.56.2" }, "x86_64-linux": { - "store_path": "/nix/store/5w4vqr92y2rs826z10lcnkhlv61bcxny-golangci-lint-1.56.1" + "store_path": "/nix/store/kc58bqdmjdc6mfilih5pywprs7r7lxrw-golangci-lint-1.56.2" } } },