From 635f9c0bce96f0101e6cdceb98ba011bd8b40022 Mon Sep 17 00:00:00 2001 From: Olivier Michallat Date: Wed, 28 Aug 2024 10:10:16 -0700 Subject: [PATCH 1/2] Upgrade golint and fix errors (fixes #692) --- Makefile | 2 +- apis/cassandra/v1beta1/cassandradatacenter_webhook.go | 4 ++-- pkg/httphelper/client.go | 2 +- pkg/reconciliation/construct_service.go | 4 ++-- pkg/reconciliation/decommission_node.go | 4 ++-- pkg/reconciliation/reconcile_racks.go | 9 +++++---- pkg/reconciliation/reconcile_racks_test.go | 3 ++- tests/util/ginkgo/lib.go | 3 ++- tests/util/kubectl/kubectl.go | 3 ++- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 5e72ec45..e603f4a0 100644 --- a/Makefile +++ b/Makefile @@ -245,7 +245,7 @@ CONTROLLER_TOOLS_VERSION ?= v0.15.0 OPERATOR_SDK_VERSION ?= 1.35.0 HELM_VERSION ?= 3.14.2 OPM_VERSION ?= 1.38.0 -GOLINT_VERSION ?= 1.59.1 +GOLINT_VERSION ?= 1.60.3 .PHONY: cert-manager cert-manager: ## Install cert-manager to the cluster diff --git a/apis/cassandra/v1beta1/cassandradatacenter_webhook.go b/apis/cassandra/v1beta1/cassandradatacenter_webhook.go index ca1b0af0..20b16292 100644 --- a/apis/cassandra/v1beta1/cassandradatacenter_webhook.go +++ b/apis/cassandra/v1beta1/cassandradatacenter_webhook.go @@ -212,10 +212,10 @@ func ValidateDatacenterFieldChanges(oldDc CassandraDatacenter, newDc CassandraDa if int(newSizeDifference) < minSizeAdjustment { return attemptedTo( - fmt.Sprintf("add racks without increasing size enough to prevent existing"+ + "add racks without increasing size enough to prevent existing"+ " nodes from moving to new racks to maintain balance.\n"+ "New racks added: %d, size increased by: %d. Expected size increase to be at least %d", - newRackCount, newSizeDifference, minSizeAdjustment)) + newRackCount, newSizeDifference, minSizeAdjustment) } } diff --git a/pkg/httphelper/client.go b/pkg/httphelper/client.go index bfece9f2..f2b9603c 100644 --- a/pkg/httphelper/client.go +++ b/pkg/httphelper/client.go @@ -318,7 +318,7 @@ func (client *NodeMgmtClient) CallCreateRoleEndpoint(pod *corev1.Pod, username s if _, err = callNodeMgmtEndpoint(client, request, ""); err != nil { // The error could include a password, strip it strippedErrMsg := strings.ReplaceAll(err.Error(), password, "******") - return fmt.Errorf(strippedErrMsg) + return errors.New(strippedErrMsg) } return nil } diff --git a/pkg/reconciliation/construct_service.go b/pkg/reconciliation/construct_service.go index 5724bd69..6ed990a6 100644 --- a/pkg/reconciliation/construct_service.go +++ b/pkg/reconciliation/construct_service.go @@ -79,7 +79,7 @@ func newServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Servi } func addAdditionalOptions(service *corev1.Service, serviceConfig *api.ServiceConfigAdditions) { - if serviceConfig.Labels != nil && len(serviceConfig.Labels) > 0 { + if len(serviceConfig.Labels) > 0 { if service.Labels == nil { service.Labels = make(map[string]string, len(serviceConfig.Labels)) } @@ -88,7 +88,7 @@ func addAdditionalOptions(service *corev1.Service, serviceConfig *api.ServiceCon } } - if serviceConfig.Annotations != nil && len(serviceConfig.Annotations) > 0 { + if len(serviceConfig.Annotations) > 0 { if service.Annotations == nil { service.Annotations = make(map[string]string, len(serviceConfig.Annotations)) } diff --git a/pkg/reconciliation/decommission_node.go b/pkg/reconciliation/decommission_node.go index e83db46d..4cef983e 100644 --- a/pkg/reconciliation/decommission_node.go +++ b/pkg/reconciliation/decommission_node.go @@ -398,7 +398,7 @@ func (rc *ReconciliationContext) EnsurePodsCanAbsorbDecommData(decommPod *corev1 msg := fmt.Sprintf("Not enough free space available to decommission. %s has %d free space, but %d is needed.", pod.Name, free, int64(spaceUsedByDecommPod), ) - rc.ReqLogger.Error(fmt.Errorf(msg), msg) + rc.ReqLogger.Error(errors.New(msg), msg) rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeWarning, events.InvalidDatacenterSpec, msg) if err := rc.setCondition( @@ -409,7 +409,7 @@ func (rc *ReconciliationContext) EnsurePodsCanAbsorbDecommData(decommPod *corev1 return errors.Wrap(err, msg) } - return fmt.Errorf(msg) + return errors.New(msg) } } diff --git a/pkg/reconciliation/reconcile_racks.go b/pkg/reconciliation/reconcile_racks.go index 952dce60..2d79cb1e 100644 --- a/pkg/reconciliation/reconcile_racks.go +++ b/pkg/reconciliation/reconcile_racks.go @@ -30,6 +30,7 @@ import ( "github.com/k8ssandra/cass-operator/pkg/monitoring" "github.com/k8ssandra/cass-operator/pkg/oplabels" "github.com/k8ssandra/cass-operator/pkg/utils" + pkgerrors "github.com/pkg/errors" ) var ( @@ -230,7 +231,7 @@ func (rc *ReconciliationContext) CheckVolumeClaimSizes(statefulSet, desiredSts * } rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeWarning, events.InvalidDatacenterSpec, "Shrinking CassandraDatacenter PVCs is not supported") - return result.Error(fmt.Errorf(msg)) + return result.Error(pkgerrors.New(msg)) } if currentSize.Cmp(createdSize) < 0 { @@ -238,7 +239,7 @@ func (rc *ReconciliationContext) CheckVolumeClaimSizes(statefulSet, desiredSts * if !metav1.HasAnnotation(rc.Datacenter.ObjectMeta, api.AllowStorageChangesAnnotation) || rc.Datacenter.Annotations[api.AllowStorageChangesAnnotation] != "true" { msg := fmt.Sprintf("PVC resize requested, but %s annotation is not set to 'true'", api.AllowStorageChangesAnnotation) rc.Recorder.Eventf(rc.Datacenter, corev1.EventTypeWarning, events.InvalidDatacenterSpec, msg) - return result.Error(fmt.Errorf(msg)) + return result.Error(pkgerrors.New(msg)) } supportsExpansion, err := rc.storageExpansion() @@ -255,7 +256,7 @@ func (rc *ReconciliationContext) CheckVolumeClaimSizes(statefulSet, desiredSts * )); err != nil { return result.Error(err) } - return result.Error(fmt.Errorf(msg)) + return result.Error(pkgerrors.New(msg)) } if err := rc.setConditionStatus(api.DatacenterResizingVolumes, corev1.ConditionTrue); err != nil { @@ -1688,7 +1689,7 @@ func (rc *ReconciliationContext) ReconcilePods(statefulSet *appsv1.StatefulSet) "Update rack labels for Pod %s", podName) } - if pod.Spec.Volumes == nil || len(pod.Spec.Volumes) == 0 || pod.Spec.Volumes[0].PersistentVolumeClaim == nil { + if len(pod.Spec.Volumes) == 0 || pod.Spec.Volumes[0].PersistentVolumeClaim == nil { continue } diff --git a/pkg/reconciliation/reconcile_racks_test.go b/pkg/reconciliation/reconcile_racks_test.go index e19c6b82..5b8083d8 100644 --- a/pkg/reconciliation/reconcile_racks_test.go +++ b/pkg/reconciliation/reconcile_racks_test.go @@ -6,6 +6,7 @@ package reconciliation import ( "context" "fmt" + "github.com/pkg/errors" "io" "net/http" "reflect" @@ -1643,7 +1644,7 @@ func TestStripPassword(t *testing.T) { func(req *http.Request) bool { return req != nil })). - Return(nil, fmt.Errorf(password)). + Return(nil, errors.New(password)). Once() client := httphelper.NodeMgmtClient{ diff --git a/tests/util/ginkgo/lib.go b/tests/util/ginkgo/lib.go index 7a083b30..a05e5d86 100644 --- a/tests/util/ginkgo/lib.go +++ b/tests/util/ginkgo/lib.go @@ -6,6 +6,7 @@ package ginkgo_util import ( "encoding/base64" "fmt" + "github.com/pkg/errors" "os" "path/filepath" "regexp" @@ -567,7 +568,7 @@ func (ns NsWrapper) ExpectKeyValue(m map[string]interface{}, key string, expecte tryFloat64, ok := m[key].(float64) if !ok { msg := fmt.Sprintf("Actual value for key %s is not expected type", key) - err := fmt.Errorf(msg) + err := errors.New(msg) Expect(err).ToNot(HaveOccurred()) } actualValue = fmt.Sprintf("%f", tryFloat64) diff --git a/tests/util/kubectl/kubectl.go b/tests/util/kubectl/kubectl.go index 5f6f661d..33327d26 100644 --- a/tests/util/kubectl/kubectl.go +++ b/tests/util/kubectl/kubectl.go @@ -5,6 +5,7 @@ package kubectl import ( "fmt" + "github.com/pkg/errors" "os" "os/user" "regexp" @@ -321,7 +322,7 @@ func waitForOutputPattern(k KCmd, pattern string, seconds int) error { if err != nil { msg = fmt.Sprintf("%s\nThe following error occurred while querying k8s: %v", msg, err) } - e := fmt.Errorf(msg) + e := errors.New(msg) return e case <-c: return nil From 237da852b3897bb5cc64174bf3aeb1a3c9178931 Mon Sep 17 00:00:00 2001 From: Olivier Michallat Date: Wed, 28 Aug 2024 10:35:37 -0700 Subject: [PATCH 2/2] Fix failing test --- pkg/reconciliation/reconcile_racks_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/reconciliation/reconcile_racks_test.go b/pkg/reconciliation/reconcile_racks_test.go index 5b8083d8..bb53bd67 100644 --- a/pkg/reconciliation/reconcile_racks_test.go +++ b/pkg/reconciliation/reconcile_racks_test.go @@ -2473,13 +2473,15 @@ func TestCheckVolumeClaimSizesValidation(t *testing.T) { require.NoErrorf(err, "error occurred creating statefulset") res = rc.CheckVolumeClaimSizes(originalStatefulSet, desiredStatefulSet) - require.Equal(result.Error(fmt.Errorf("PVC resize requested, but cassandra.datastax.com/allow-storage-changes annotation is not set to 'true'")), res, "We should have an error, feature flag is not set") + _, err = res.Output() + require.EqualError(err, "PVC resize requested, but cassandra.datastax.com/allow-storage-changes annotation is not set to 'true'", "We should have an error, feature flag is not set") metav1.SetMetaDataAnnotation(&rc.Datacenter.ObjectMeta, api.AllowStorageChangesAnnotation, "true") require.NoError(rc.Client.Update(rc.Ctx, rc.Datacenter)) res = rc.CheckVolumeClaimSizes(originalStatefulSet, desiredStatefulSet) - require.Equal(result.Error(fmt.Errorf("PVC resize requested, but StorageClass standard does not support expansion")), res, "We should have an error, StorageClass does not allow expansion") + _, err = res.Output() + require.EqualError(err, "PVC resize requested, but StorageClass standard does not support expansion", "We should have an error, StorageClass does not allow expansion") cond, found := rc.Datacenter.GetCondition(api.DatacenterValid) require.True(found) require.Equal(corev1.ConditionFalse, cond.Status) @@ -2495,7 +2497,8 @@ func TestCheckVolumeClaimSizesValidation(t *testing.T) { desiredStatefulSet, err = newStatefulSetForCassandraDatacenter(nil, "default", rc.Datacenter, 2) require.NoErrorf(err, "error occurred creating statefulset") res = rc.CheckVolumeClaimSizes(originalStatefulSet, desiredStatefulSet) - require.Equal(result.Error(fmt.Errorf("shrinking PVC %s is not supported", originalStatefulSet.Spec.VolumeClaimTemplates[0].Name)), res, "We should have an error, shrinking is disabled") + _, err = res.Output() + require.EqualError(err, fmt.Sprintf("shrinking PVC %s is not supported", originalStatefulSet.Spec.VolumeClaimTemplates[0].Name), "We should have an error, shrinking is disabled") cond, found = rc.Datacenter.GetCondition(api.DatacenterValid) require.True(found) require.Equal(corev1.ConditionFalse, cond.Status) @@ -2715,7 +2718,8 @@ func TestCheckRackPodTemplateWithVolumeExpansion(t *testing.T) { rc.Datacenter.Spec.StorageConfig.CassandraDataVolumeClaimSpec.Resources.Requests = map[corev1.ResourceName]resource.Quantity{corev1.ResourceStorage: resource.MustParse("2Gi")} require.NoError(rc.Client.Update(rc.Ctx, rc.Datacenter)) res = rc.CheckRackPodTemplate() - require.Equal(result.Error(fmt.Errorf("PVC resize requested, but StorageClass standard does not support expansion")), res, "We should have an error, storageClass does not support expansion") + _, err := res.Output() + require.EqualError(err, "PVC resize requested, but StorageClass standard does not support expansion", "We should have an error, storageClass does not support expansion") // Mark the StorageClass as allowing expansion storageClass := &storagev1.StorageClass{}