Skip to content

Commit 11aadf6

Browse files
committed
fix golangci reported issues
Signed-off-by: Martin Schuppert <mschuppert@redhat.com>
1 parent 9c2a8bc commit 11aadf6

17 files changed

Lines changed: 83 additions & 64 deletions

controllers/cinder_common.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package controllers implements the cinder-operator Kubernetes controllers.
1718
package controllers
1819

1920
import (
2021
"context"
22+
"errors"
2123
"fmt"
24+
"time"
25+
2226
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
2327
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
2428
"k8s.io/apimachinery/pkg/types"
25-
"time"
2629

2730
"github.com/openstack-k8s-operators/cinder-operator/pkg/cinder"
2831
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
@@ -35,6 +38,14 @@ import (
3538
"sigs.k8s.io/controller-runtime/pkg/log"
3639
)
3740

41+
// Common static errors for all cinder controllers
42+
var (
43+
ErrNetworkAttachmentWaiting = errors.New("network attachment waiting")
44+
ErrNetworkAttachmentNotFound = errors.New("network-attachment-definition not found")
45+
ErrStatefulSetWaiting = errors.New("waiting for StatefulSet to start reconciling")
46+
ErrNetworkAttachmentConfig = errors.New("not all pods have interfaces with ips as configured in NetworkAttachments")
47+
)
48+
3849
type conditionUpdater interface {
3950
Set(c *condition.Condition)
4051
MarkTrue(t condition.Type, messageFormat string, messageArgs ...interface{})

controllers/cinder_controller.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (r *CinderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
130130

131131
// Fetch the Cinder instance
132132
instance := &cinderv1beta1.Cinder{}
133-
err := r.Client.Get(ctx, req.NamespacedName, instance)
133+
err := r.Get(ctx, req.NamespacedName, instance)
134134
if err != nil {
135135
if k8s_errors.IsNotFound(err) {
136136
// Request object not found, could have been deleted after reconcile request.
@@ -247,7 +247,7 @@ func (r *CinderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
247247
// fields to index to reconcile when change
248248
const (
249249
passwordSecretField = ".spec.secret"
250-
caBundleSecretNameField = ".spec.tls.caBundleSecretName"
250+
caBundleSecretNameField = ".spec.tls.caBundleSecretName" // #nosec G101
251251
tlsAPIInternalField = ".spec.tls.api.internal.secretName"
252252
tlsAPIPublicField = ".spec.tls.api.public.secretName"
253253
topologyField = ".spec.topologyRef.Name"
@@ -291,7 +291,7 @@ func (r *CinderReconciler) SetupWithManager(mgr ctrl.Manager) error {
291291
listOpts := []client.ListOption{
292292
client.InNamespace(o.GetNamespace()),
293293
}
294-
if err := r.Client.List(ctx, cinders, listOpts...); err != nil {
294+
if err := r.List(ctx, cinders, listOpts...); err != nil {
295295
Log.Error(err, "Unable to retrieve Cinder CRs %v")
296296
return nil
297297
}
@@ -327,7 +327,7 @@ func (r *CinderReconciler) SetupWithManager(mgr ctrl.Manager) error {
327327
listOpts := []client.ListOption{
328328
client.InNamespace(o.GetNamespace()),
329329
}
330-
if err := r.Client.List(ctx, cinders, listOpts...); err != nil {
330+
if err := r.List(ctx, cinders, listOpts...); err != nil {
331331
Log.Error(err, "Unable to retrieve Cinder CRs %w")
332332
return nil
333333
}
@@ -383,7 +383,7 @@ func (r *CinderReconciler) findObjectForSrc(ctx context.Context, src client.Obje
383383
listOps := &client.ListOptions{
384384
Namespace: src.GetNamespace(),
385385
}
386-
err := r.Client.List(ctx, crList, listOps)
386+
err := r.List(ctx, crList, listOps)
387387
if err != nil {
388388
Log.Error(err, fmt.Sprintf("listing %s for namespace: %s", crList.GroupVersionKind().Kind, src.GetNamespace()))
389389
return requests
@@ -736,7 +736,7 @@ func (r *CinderReconciler) reconcileNormal(ctx context.Context, instance *cinder
736736
condition.SeverityInfo,
737737
condition.NetworkAttachmentsReadyWaitingMessage,
738738
netAtt))
739-
return cinder.ResultRequeue, fmt.Errorf(condition.NetworkAttachmentsReadyWaitingMessage, netAtt)
739+
return cinder.ResultRequeue, fmt.Errorf("%w: %s", ErrNetworkAttachmentWaiting, netAtt)
740740
}
741741
instance.Status.Conditions.Set(condition.FalseCondition(
742742
condition.NetworkAttachmentsReadyCondition,
@@ -981,7 +981,7 @@ func (r *CinderReconciler) generateServiceConfigs(
981981
labels := labels.GetLabels(instance, labels.GetGroupLabel(cinder.ServiceName), serviceLabels)
982982

983983
var tlsCfg *tls.Service
984-
if instance.Spec.CinderAPI.TLS.Ca.CaBundleSecretName != "" {
984+
if instance.Spec.CinderAPI.TLS.CaBundleSecretName != "" {
985985
tlsCfg = &tls.Service{}
986986
}
987987

@@ -1295,20 +1295,20 @@ func (r *CinderReconciler) backupCleanupDeployment(ctx context.Context, instance
12951295
},
12961296
}
12971297
key := client.ObjectKeyFromObject(deployment)
1298-
err := r.Client.Get(ctx, key, deployment)
1298+
err := r.Get(ctx, key, deployment)
12991299

13001300
if k8s_errors.IsNotFound(err) {
13011301
// Nothing to clean up
13021302
return nil
13031303
}
13041304

13051305
if err != nil {
1306-
return fmt.Errorf("Error looking for '%s' deployment in '%s' namespace: %w", deployment.Name, instance.Namespace, err)
1306+
return fmt.Errorf("error looking for '%s' deployment in '%s' namespace: %w", deployment.Name, instance.Namespace, err)
13071307
}
13081308

1309-
err = r.Client.Delete(ctx, deployment)
1309+
err = r.Delete(ctx, deployment)
13101310
if err != nil && !k8s_errors.IsNotFound(err) {
1311-
return fmt.Errorf("Error cleaning up %s: %w", deployment.Name, err)
1311+
return fmt.Errorf("error cleaning up %s: %w", deployment.Name, err)
13121312
}
13131313

13141314
return nil
@@ -1326,12 +1326,12 @@ func (r *CinderReconciler) volumeDeploymentCreateOrUpdate(ctx context.Context, i
13261326
MemcachedInstance: &instance.Spec.MemcachedInstance,
13271327
}
13281328

1329-
if cinderVolumeSpec.CinderVolumeTemplate.NodeSelector == nil {
1330-
cinderVolumeSpec.CinderVolumeTemplate.NodeSelector = instance.Spec.NodeSelector
1329+
if cinderVolumeSpec.NodeSelector == nil {
1330+
cinderVolumeSpec.NodeSelector = instance.Spec.NodeSelector
13311331
}
13321332

1333-
if cinderVolumeSpec.CinderVolumeTemplate.TopologyRef == nil {
1334-
cinderVolumeSpec.CinderVolumeTemplate.TopologyRef = instance.Spec.TopologyRef
1333+
if cinderVolumeSpec.TopologyRef == nil {
1334+
cinderVolumeSpec.TopologyRef = instance.Spec.TopologyRef
13351335
}
13361336

13371337
deployment := &cinderv1beta1.CinderVolume{
@@ -1371,7 +1371,7 @@ func (r *CinderReconciler) volumeCleanupDeployments(ctx context.Context, instanc
13711371
listOpts := []client.ListOption{
13721372
client.InNamespace(instance.Namespace),
13731373
}
1374-
if err := r.Client.List(ctx, volumes, listOpts...); err != nil {
1374+
if err := r.List(ctx, volumes, listOpts...); err != nil {
13751375
Log.Error(err, "Unable to retrieve volume CRs %v")
13761376
return nil
13771377
}
@@ -1385,9 +1385,9 @@ func (r *CinderReconciler) volumeCleanupDeployments(ctx context.Context, instanc
13851385
// Delete the volume if it's no longer in the spec
13861386
_, exists := instance.Spec.CinderVolumes[volume.BackendName()]
13871387
if !exists && volume.DeletionTimestamp.IsZero() {
1388-
err := r.Client.Delete(ctx, &volume)
1388+
err := r.Delete(ctx, &volume)
13891389
if err != nil && !k8s_errors.IsNotFound(err) {
1390-
err = fmt.Errorf("Error cleaning up %s: %w", volume.Name, err)
1390+
err = fmt.Errorf("error cleaning up %s: %w", volume.Name, err)
13911391
return err
13921392
}
13931393
}

controllers/cinderapi_controller.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (r *CinderAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
114114

115115
// Fetch the CinderAPI instance
116116
instance := &cinderv1beta1.CinderAPI{}
117-
err := r.Client.Get(ctx, req.NamespacedName, instance)
117+
err := r.Get(ctx, req.NamespacedName, instance)
118118
if err != nil {
119119
if k8s_errors.IsNotFound(err) {
120120
// Request object not found, could have been deleted after reconcile request.
@@ -226,16 +226,16 @@ func (r *CinderAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
226226
// Watch for changes to secrets we don't own. Global secrets
227227
// (e.g. TransportURLSecret) are handled by the main cinder controller.
228228
secretFn := func(_ context.Context, o client.Object) []reconcile.Request {
229-
var namespace string = o.GetNamespace()
230-
var secretName string = o.GetName()
229+
var namespace = o.GetNamespace()
230+
var secretName = o.GetName()
231231
result := []reconcile.Request{}
232232

233233
// get all API CRs
234234
apis := &cinderv1beta1.CinderAPIList{}
235235
listOpts := []client.ListOption{
236236
client.InNamespace(namespace),
237237
}
238-
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
238+
if err := r.List(context.Background(), apis, listOpts...); err != nil {
239239
Log.Error(err, "Unable to retrieve API CRs %v")
240240
return nil
241241
}
@@ -750,7 +750,7 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
750750
condition.TLSInputReadyCondition,
751751
condition.RequestedReason,
752752
condition.SeverityInfo,
753-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName)))
753+
condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName))
754754
return ctrl.Result{}, nil
755755
}
756756
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -775,7 +775,7 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
775775
condition.TLSInputReadyCondition,
776776
condition.RequestedReason,
777777
condition.SeverityInfo,
778-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, err.Error())))
778+
condition.TLSInputReadyWaitingMessage, err.Error()))
779779
return ctrl.Result{}, nil
780780
}
781781
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -832,7 +832,7 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
832832
condition.SeverityInfo,
833833
condition.NetworkAttachmentsReadyWaitingMessage,
834834
netAtt))
835-
return cinder.ResultRequeue, fmt.Errorf("network-attachment-definition %s not found", netAtt)
835+
return cinder.ResultRequeue, fmt.Errorf("%w: %s", ErrNetworkAttachmentNotFound, netAtt)
836836
}
837837
instance.Status.Conditions.Set(condition.FalseCondition(
838838
condition.NetworkAttachmentsReadyCondition,
@@ -952,7 +952,7 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
952952
ssData = ss.GetStatefulSet()
953953
if ssData.Generation != ssData.Status.ObservedGeneration {
954954
ctrlResult = cinder.ResultRequeue
955-
err = fmt.Errorf("waiting for Statefulset %s to start reconciling", ssData.Name)
955+
err = fmt.Errorf("%w: %s", ErrStatefulSetWaiting, ssData.Name)
956956
}
957957
}
958958

@@ -1002,7 +1002,7 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
10021002
if networkReady {
10031003
instance.Status.Conditions.MarkTrue(condition.NetworkAttachmentsReadyCondition, condition.NetworkAttachmentsReadyMessage)
10041004
} else {
1005-
err := fmt.Errorf("not all pods have interfaces with ips as configured in NetworkAttachments: %s", instance.Spec.NetworkAttachments)
1005+
err := fmt.Errorf("%w: %s", ErrNetworkAttachmentConfig, instance.Spec.NetworkAttachments)
10061006
instance.Status.Conditions.Set(condition.FalseCondition(
10071007
condition.NetworkAttachmentsReadyCondition,
10081008
condition.ErrorReason,

controllers/cinderbackup_controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (r *CinderBackupReconciler) Reconcile(ctx context.Context, req ctrl.Request
9898

9999
// Fetch the CinderBackup instance
100100
instance := &cinderv1beta1.CinderBackup{}
101-
err := r.Client.Get(ctx, req.NamespacedName, instance)
101+
err := r.Get(ctx, req.NamespacedName, instance)
102102
if err != nil {
103103
if k8s_errors.IsNotFound(err) {
104104
// Request object not found, could have been deleted after reconcile request.
@@ -200,16 +200,16 @@ func (r *CinderBackupReconciler) SetupWithManager(ctx context.Context, mgr ctrl.
200200
secretFn := func(ctx context.Context, o client.Object) []reconcile.Request {
201201
Log := r.GetLogger(ctx)
202202

203-
var namespace string = o.GetNamespace()
204-
var secretName string = o.GetName()
203+
var namespace = o.GetNamespace()
204+
var secretName = o.GetName()
205205
result := []reconcile.Request{}
206206

207207
// get all backup CRs
208208
backups := &cinderv1beta1.CinderBackupList{}
209209
listOpts := []client.ListOption{
210210
client.InNamespace(namespace),
211211
}
212-
if err := r.Client.List(ctx, backups, listOpts...); err != nil {
212+
if err := r.List(ctx, backups, listOpts...); err != nil {
213213
Log.Error(err, "Unable to retrieve backups CRs %v")
214214
return nil
215215
}
@@ -439,7 +439,7 @@ func (r *CinderBackupReconciler) reconcileNormal(ctx context.Context, instance *
439439
condition.TLSInputReadyCondition,
440440
condition.RequestedReason,
441441
condition.SeverityInfo,
442-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName)))
442+
condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName))
443443
return ctrl.Result{}, nil
444444
}
445445
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -523,7 +523,7 @@ func (r *CinderBackupReconciler) reconcileNormal(ctx context.Context, instance *
523523
condition.SeverityInfo,
524524
condition.NetworkAttachmentsReadyWaitingMessage,
525525
netAtt))
526-
return cinder.ResultRequeue, fmt.Errorf("network-attachment-definition %s not found", netAtt)
526+
return cinder.ResultRequeue, fmt.Errorf("%w: %s", ErrNetworkAttachmentNotFound, netAtt)
527527
}
528528
instance.Status.Conditions.Set(condition.FalseCondition(
529529
condition.NetworkAttachmentsReadyCondition,
@@ -601,7 +601,7 @@ func (r *CinderBackupReconciler) reconcileNormal(ctx context.Context, instance *
601601
ssData = ss.GetStatefulSet()
602602
if ssData.Generation != ssData.Status.ObservedGeneration {
603603
ctrlResult = cinder.ResultRequeue
604-
err = fmt.Errorf("waiting for Statefulset %s to start reconciling", ssData.Name)
604+
err = fmt.Errorf("%w: %s", ErrStatefulSetWaiting, ssData.Name)
605605
}
606606
}
607607

@@ -651,7 +651,7 @@ func (r *CinderBackupReconciler) reconcileNormal(ctx context.Context, instance *
651651
if networkReady {
652652
instance.Status.Conditions.MarkTrue(condition.NetworkAttachmentsReadyCondition, condition.NetworkAttachmentsReadyMessage)
653653
} else {
654-
err := fmt.Errorf("not all pods have interfaces with ips as configured in NetworkAttachments: %s", instance.Spec.NetworkAttachments)
654+
err := fmt.Errorf("%w: %s", ErrNetworkAttachmentConfig, instance.Spec.NetworkAttachments)
655655
instance.Status.Conditions.Set(condition.FalseCondition(
656656
condition.NetworkAttachmentsReadyCondition,
657657
condition.ErrorReason,

controllers/cinderscheduler_controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (r *CinderSchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
9898

9999
// Fetch the CinderScheduler instance
100100
instance := &cinderv1beta1.CinderScheduler{}
101-
err := r.Client.Get(ctx, req.NamespacedName, instance)
101+
err := r.Get(ctx, req.NamespacedName, instance)
102102
if err != nil {
103103
if k8s_errors.IsNotFound(err) {
104104
// Request object not found, could have been deleted after reconcile request.
@@ -200,16 +200,16 @@ func (r *CinderSchedulerReconciler) SetupWithManager(ctx context.Context, mgr ct
200200
// Watch for changes to secrets we don't own. Global secrets
201201
// (e.g. TransportURLSecret) are handled by the main cinder controller.
202202
secretFn := func(_ context.Context, o client.Object) []reconcile.Request {
203-
var namespace string = o.GetNamespace()
204-
var secretName string = o.GetName()
203+
var namespace = o.GetNamespace()
204+
var secretName = o.GetName()
205205
result := []reconcile.Request{}
206206

207207
// get all scheduler CRs
208208
schedulers := &cinderv1beta1.CinderSchedulerList{}
209209
listOpts := []client.ListOption{
210210
client.InNamespace(namespace),
211211
}
212-
if err := r.Client.List(context.Background(), schedulers, listOpts...); err != nil {
212+
if err := r.List(context.Background(), schedulers, listOpts...); err != nil {
213213
Log.Error(err, "Unable to retrieve scheduler CRs %v")
214214
return nil
215215
}
@@ -437,7 +437,7 @@ func (r *CinderSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
437437
condition.TLSInputReadyCondition,
438438
condition.RequestedReason,
439439
condition.SeverityInfo,
440-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName)))
440+
condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName))
441441
return ctrl.Result{}, nil
442442
}
443443
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -521,7 +521,7 @@ func (r *CinderSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
521521
condition.SeverityInfo,
522522
condition.NetworkAttachmentsReadyWaitingMessage,
523523
netAtt))
524-
return cinder.ResultRequeue, fmt.Errorf("network-attachment-definition %s not found", netAtt)
524+
return cinder.ResultRequeue, fmt.Errorf("%w: %s", ErrNetworkAttachmentNotFound, netAtt)
525525
}
526526
instance.Status.Conditions.Set(condition.FalseCondition(
527527
condition.NetworkAttachmentsReadyCondition,
@@ -599,7 +599,7 @@ func (r *CinderSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
599599
ssData = ss.GetStatefulSet()
600600
if ssData.Generation != ssData.Status.ObservedGeneration {
601601
ctrlResult = cinder.ResultRequeue
602-
err = fmt.Errorf("waiting for Statefulset %s to start reconciling", ssData.Name)
602+
err = fmt.Errorf("%w: %s", ErrStatefulSetWaiting, ssData.Name)
603603
}
604604
}
605605

@@ -649,7 +649,7 @@ func (r *CinderSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
649649
if networkReady {
650650
instance.Status.Conditions.MarkTrue(condition.NetworkAttachmentsReadyCondition, condition.NetworkAttachmentsReadyMessage)
651651
} else {
652-
err := fmt.Errorf("not all pods have interfaces with ips as configured in NetworkAttachments: %s", instance.Spec.NetworkAttachments)
652+
err := fmt.Errorf("%w: %s", ErrNetworkAttachmentConfig, instance.Spec.NetworkAttachments)
653653
instance.Status.Conditions.Set(condition.FalseCondition(
654654
condition.NetworkAttachmentsReadyCondition,
655655
condition.ErrorReason,

0 commit comments

Comments
 (0)