Skip to content

Commit 28e041c

Browse files
committed
fix golangci reported issues
Signed-off-by: Martin Schuppert <mschuppert@redhat.com>
1 parent a89f4f0 commit 28e041c

10 files changed

Lines changed: 70 additions & 59 deletions

File tree

controllers/glance_common.go

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

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

1920
import (
2021
"context"
22+
"errors"
2123
"fmt"
2224
"time"
2325

@@ -43,10 +45,15 @@ import (
4345
"sigs.k8s.io/controller-runtime/pkg/log"
4446
)
4547

48+
// Common static errors for glance controllers
49+
var (
50+
ErrNetworkAttachmentConfig = errors.New("not all pods have interfaces with ips as configured in NetworkAttachments")
51+
)
52+
4653
// fields to index to reconcile when change
4754
const (
4855
passwordSecretField = ".spec.secret"
49-
caBundleSecretNameField = ".spec.tls.caBundleSecretName"
56+
caBundleSecretNameField = ".spec.tls.caBundleSecretName" // #nosec G101
5057
tlsAPIInternalField = ".spec.tls.api.internal.secretName"
5158
tlsAPIPublicField = ".spec.tls.api.public.secretName"
5259
topologyField = ".spec.topologyRef.Name"
@@ -279,7 +286,7 @@ func GetPvcListWithLabel(
279286
pvcList, err := h.GetKClient().CoreV1().PersistentVolumeClaims(namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelectorString})
280287

281288
if err != nil {
282-
err = fmt.Errorf("Error listing PVC for labels: %v - %w", labelSelectorMap, err)
289+
err = fmt.Errorf("error listing PVC for labels: %v - %w", labelSelectorMap, err)
283290
return nil, err
284291
}
285292
return pvcList, nil

controllers/glance_controller.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *GlanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
107107

108108
// Fetch the Glance instance
109109
instance := &glancev1.Glance{}
110-
err := r.Client.Get(ctx, req.NamespacedName, instance)
110+
err := r.Get(ctx, req.NamespacedName, instance)
111111
if err != nil {
112112
if k8s_errors.IsNotFound(err) {
113113
// Request object not found, could have been deleted after reconcile request.
@@ -244,7 +244,7 @@ func (r *GlanceReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manage
244244
listOpts := []client.ListOption{
245245
client.InNamespace(o.GetNamespace()),
246246
}
247-
if err := r.Client.List(context.Background(), glances, listOpts...); err != nil {
247+
if err := r.List(context.Background(), glances, listOpts...); err != nil {
248248
Log.Error(err, "Unable to retrieve Glance CRs %w")
249249
return nil
250250
}
@@ -274,7 +274,7 @@ func (r *GlanceReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manage
274274
listOpts := []client.ListOption{
275275
client.InNamespace(o.GetNamespace()),
276276
}
277-
if err := r.Client.List(context.Background(), glances, listOpts...); err != nil {
277+
if err := r.List(context.Background(), glances, listOpts...); err != nil {
278278
Log.Error(err, "Unable to retrieve Glance CRs %v")
279279
return nil
280280
}
@@ -795,8 +795,8 @@ func (r *GlanceReconciler) apiDeployment(
795795
// want to override "external" with "single" in case APIType == "single":
796796
// in this case we only deploy the External instance and skip the internal
797797
// one
798-
var internal string = glancev1.APIInternal
799-
var external string = glancev1.APIExternal
798+
var internal = glancev1.APIInternal
799+
var external = glancev1.APIExternal
800800
var wsgi bool
801801

802802
// We deploy:
@@ -959,8 +959,8 @@ func (r *GlanceReconciler) apiDeploymentCreateOrUpdate(
959959
MemcachedInstance: instance.Spec.MemcachedInstance,
960960
}
961961

962-
if apiSpec.GlanceAPITemplate.NodeSelector == nil {
963-
apiSpec.GlanceAPITemplate.NodeSelector = instance.Spec.NodeSelector
962+
if apiSpec.NodeSelector == nil {
963+
apiSpec.NodeSelector = instance.Spec.NodeSelector
964964
}
965965

966966
// Inherit the ImageCacheSize from the top level if not specified
@@ -969,20 +969,20 @@ func (r *GlanceReconciler) apiDeploymentCreateOrUpdate(
969969
}
970970

971971
// Inherit the values required for PVC creation from the top-level CR
972-
if apiSpec.GlanceAPITemplate.Storage.StorageRequest == "" {
973-
apiSpec.GlanceAPITemplate.Storage.StorageRequest = instance.Spec.Storage.StorageRequest
972+
if apiSpec.Storage.StorageRequest == "" {
973+
apiSpec.Storage.StorageRequest = instance.Spec.Storage.StorageRequest
974974
}
975-
if apiSpec.GlanceAPITemplate.Storage.StorageClass == "" {
976-
apiSpec.GlanceAPITemplate.Storage.StorageClass = instance.Spec.Storage.StorageClass
975+
if apiSpec.Storage.StorageClass == "" {
976+
apiSpec.Storage.StorageClass = instance.Spec.Storage.StorageClass
977977
}
978-
if !apiSpec.GlanceAPITemplate.Storage.External {
979-
apiSpec.GlanceAPITemplate.Storage.External = instance.Spec.Storage.External
978+
if !apiSpec.Storage.External {
979+
apiSpec.Storage.External = instance.Spec.Storage.External
980980
}
981981

982982
// Make sure to inject the ContainerImage passed by the OpenStackVersions
983983
// resource to all the underlying instances and rollout a new StatefulSet
984984
// if it has been changed
985-
apiSpec.GlanceAPITemplate.ContainerImage = instance.Spec.ContainerImage
985+
apiSpec.ContainerImage = instance.Spec.ContainerImage
986986

987987
// We select which glanceAPI should register the keystoneEndpoint by using
988988
// an API selector defined in the main glance CR; if it matches with the
@@ -993,8 +993,8 @@ func (r *GlanceReconciler) apiDeploymentCreateOrUpdate(
993993

994994
// If topology is not present in the underlying GlanceAPI,
995995
// inherit from the top-level CR
996-
if apiSpec.GlanceAPITemplate.TopologyRef == nil {
997-
apiSpec.GlanceAPITemplate.TopologyRef = instance.Spec.TopologyRef
996+
if apiSpec.TopologyRef == nil {
997+
apiSpec.TopologyRef = instance.Spec.TopologyRef
998998
}
999999

10001000
// Set deployment mode (proxypass vs mod_wsgi)
@@ -1014,7 +1014,7 @@ func (r *GlanceReconciler) apiDeploymentCreateOrUpdate(
10141014
op, err := controllerutil.CreateOrUpdate(ctx, r.Client, glanceStatefulset, func() error {
10151015
// Assign the created spec containing both field provided via GlanceAPITemplate
10161016
// and what is inherited from the top-level CR (ExtraMounts)
1017-
glanceStatefulset.ObjectMeta.Annotations = apiAnnotations
1017+
glanceStatefulset.Annotations = apiAnnotations
10181018
glanceStatefulset.Spec = apiSpec
10191019

10201020
// We might want to create instances pointing to different backends in
@@ -1218,7 +1218,7 @@ func (r *GlanceReconciler) glanceAPICleanup(ctx context.Context, instance *glanc
12181218
listOpts := []client.ListOption{
12191219
client.InNamespace(instance.Namespace),
12201220
}
1221-
if err := r.Client.List(ctx, apis, listOpts...); err != nil {
1221+
if err := r.List(ctx, apis, listOpts...); err != nil {
12221222
Log.Error(err, "Unable to retrieve GlanceAPI CRs %v")
12231223
return nil
12241224
}
@@ -1237,9 +1237,9 @@ func (r *GlanceReconciler) glanceAPICleanup(ctx context.Context, instance *glanc
12371237
_, exists := instance.Spec.GlanceAPIs[apiName]
12381238
// Delete the api if it's no longer in the spec
12391239
if !exists && glanceAPI.DeletionTimestamp.IsZero() {
1240-
err := r.Client.Delete(ctx, &glanceAPI)
1240+
err := r.Delete(ctx, &glanceAPI)
12411241
if err != nil && !k8s_errors.IsNotFound(err) {
1242-
err = fmt.Errorf("Error cleaning up %s: %w", glanceAPI.Name, err)
1242+
err = fmt.Errorf("error cleaning up %s: %w", glanceAPI.Name, err)
12431243
return err
12441244
}
12451245
// Update the APIEndpoints in the top-level CR
@@ -1353,7 +1353,7 @@ func (r *GlanceReconciler) checkGlanceAPIsGeneration(
13531353
listOpts := []client.ListOption{
13541354
client.InNamespace(instance.Namespace),
13551355
}
1356-
if err := r.Client.List(ctx, glances, listOpts...); err != nil {
1356+
if err := r.List(ctx, glances, listOpts...); err != nil {
13571357
Log.Error(err, "Unable to retrieve Glance CRs %w")
13581358
return false, err
13591359
}

controllers/glanceapi_controller.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
100100

101101
// Fetch the GlanceAPI instance
102102
instance := &glancev1.GlanceAPI{}
103-
err := r.Client.Get(ctx, req.NamespacedName, instance)
103+
err := r.Get(ctx, req.NamespacedName, instance)
104104
if err != nil {
105105
if k8s_errors.IsNotFound(err) {
106106
// Request object not found, could have been deleted after reconcile request.
@@ -273,16 +273,16 @@ func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
273273

274274
// Watch for changes to any CustomServiceConfigSecrets. Global secrets
275275
svcSecretFn := func(_ context.Context, o client.Object) []reconcile.Request {
276-
var namespace string = o.GetNamespace()
277-
var secretName string = o.GetName()
276+
var namespace = o.GetNamespace()
277+
var secretName = o.GetName()
278278
result := []reconcile.Request{}
279279

280280
// get all API CRs
281281
apis := &glancev1.GlanceAPIList{}
282282
listOpts := []client.ListOption{
283283
client.InNamespace(namespace),
284284
}
285-
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
285+
if err := r.List(context.Background(), apis, listOpts...); err != nil {
286286
Log.Error(err, "Unable to retrieve API CRs %v")
287287
return nil
288288
}
@@ -313,7 +313,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
313313
listOpts := []client.ListOption{
314314
client.InNamespace(o.GetNamespace()),
315315
}
316-
if err := r.Client.List(context.Background(), glanceAPIs, listOpts...); err != nil {
316+
if err := r.List(context.Background(), glanceAPIs, listOpts...); err != nil {
317317
Log.Error(err, "Unable to retrieve GlanceAPI CRs %w")
318318
return nil
319319
}
@@ -341,7 +341,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Man
341341
listOpts := []client.ListOption{
342342
client.InNamespace(o.GetNamespace()),
343343
}
344-
if err := r.Client.List(context.Background(), glanceAPIs, listOpts...); err != nil {
344+
if err := r.List(context.Background(), glanceAPIs, listOpts...); err != nil {
345345
Log.Error(err, "Unable to retrieve GlanceAPI CRs %w")
346346
return nil
347347
}
@@ -742,8 +742,8 @@ func (r *GlanceAPIReconciler) reconcileNormal(
742742
// iterate over availableBackends for backend specific cases
743743
for i := 0; i < len(availableBackends); i++ {
744744
backendToken := strings.SplitN(availableBackends[i], ":", 2)
745-
switch {
746-
case backendToken[1] == "cinder":
745+
switch backendToken[1] {
746+
case "cinder":
747747
cinderList := &cinderv1.CinderList{}
748748
err := r.List(ctx, cinderList, client.InNamespace(instance.Namespace))
749749
if err != nil {
@@ -775,7 +775,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(
775775
// We see at least a Cinder CR in the namespace, unblock glance
776776
// deployment
777777
privileged = true
778-
case backendToken[1] == "rbd":
778+
case "rbd":
779779
// enable image conversion by default
780780
Log.Info("Ceph config detected: enable image conversion by default")
781781
imageConv = true
@@ -804,7 +804,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(
804804
condition.TLSInputReadyCondition,
805805
condition.RequestedReason,
806806
condition.SeverityInfo,
807-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName)))
807+
condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName))
808808
return ctrl.Result{}, nil
809809
}
810810
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -828,7 +828,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(
828828
condition.TLSInputReadyCondition,
829829
condition.RequestedReason,
830830
condition.SeverityInfo,
831-
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, err.Error())))
831+
condition.TLSInputReadyWaitingMessage, err.Error()))
832832
return ctrl.Result{}, nil
833833
}
834834
instance.Status.Conditions.Set(condition.FalseCondition(
@@ -900,7 +900,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(
900900
return glance.ResultRequeue, err
901901
}
902902

903-
configVars[glance.KeystoneEndpoint] = env.SetValue(instance.ObjectMeta.Annotations[glance.KeystoneEndpoint])
903+
configVars[glance.KeystoneEndpoint] = env.SetValue(instance.Annotations[glance.KeystoneEndpoint])
904904
//
905905
// normal reconcile tasks
906906
//
@@ -1032,7 +1032,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(
10321032
if networkReady {
10331033
instance.Status.Conditions.MarkTrue(condition.NetworkAttachmentsReadyCondition, condition.NetworkAttachmentsReadyMessage)
10341034
} else {
1035-
err := fmt.Errorf("not all pods have interfaces with ips as configured in NetworkAttachments: %s", instance.Spec.NetworkAttachments)
1035+
err := fmt.Errorf("%w: %s", ErrNetworkAttachmentConfig, instance.Spec.NetworkAttachments)
10361036
instance.Status.Conditions.Set(condition.FalseCondition(
10371037
condition.NetworkAttachmentsReadyCondition,
10381038
condition.ErrorReason,
@@ -1134,7 +1134,7 @@ func (r *GlanceAPIReconciler) generateServiceConfig(
11341134
}
11351135

11361136
var tlsCfg *tls.Service
1137-
if instance.Spec.TLS.Ca.CaBundleSecretName != "" {
1137+
if instance.Spec.TLS.CaBundleSecretName != "" {
11381138
tlsCfg = &tls.Service{}
11391139
}
11401140
// 02-config.conf
@@ -1369,8 +1369,8 @@ func (r *GlanceAPIReconciler) ensureKeystoneEndpoints(
13691369

13701370
// If the parent controller didn't set the annotation, the current glanceAPIs
13711371
// shouldn't register the endpoints in keystone
1372-
if len(instance.ObjectMeta.Annotations) == 0 ||
1373-
instance.ObjectMeta.Annotations[glance.KeystoneEndpoint] != "true" {
1372+
if len(instance.Annotations) == 0 ||
1373+
instance.Annotations[glance.KeystoneEndpoint] != "true" {
13741374
// Mark the KeystoneEndpointReadyCondition as True because there's nothing
13751375
// to do here
13761376
instance.Status.Conditions.MarkTrue(
@@ -1461,7 +1461,7 @@ func (r *GlanceAPIReconciler) ensureImageCacheJob(
14611461
}
14621462
cachePVCs, _ := GetPvcListWithLabel(ctx, h, instance.Namespace, serviceLabels)
14631463
for _, vc := range cachePVCs.Items {
1464-
var pvcName string = vc.GetName()
1464+
var pvcName = vc.GetName()
14651465
cacheAnnotations := vc.GetAnnotations()
14661466
if _, ok := cacheAnnotations["image-cache"]; ok {
14671467
cronSpec := glance.CronJobSpec{
@@ -1516,10 +1516,10 @@ func (r *GlanceAPIReconciler) cleanupImageCacheJob(
15161516
for _, vc := range cachePVCs.Items {
15171517
cacheAnnotations := vc.GetAnnotations()
15181518
if _, ok := cacheAnnotations["image-cache"]; ok {
1519-
var pvcName string = vc.GetName()
1519+
var pvcName = vc.GetName()
15201520
// Get the pod (by name) associated to the current pvc
15211521
var pod corev1.Pod
1522-
if err := r.Client.Get(ctx, types.NamespacedName{
1522+
if err := r.Get(ctx, types.NamespacedName{
15231523
Name: strings.TrimPrefix(pvcName, "glance-cache-"),
15241524
Namespace: instance.Namespace,
15251525
}, &pod); err != nil && k8s_errors.IsNotFound(err) || instance.Spec.ImageCache.Size == "" {
@@ -1553,7 +1553,7 @@ func (r *GlanceAPIReconciler) deleteJob(
15531553
// For each imageCache we have both cleaner and pruner cronJobs to check and
15541554
// cleanup if the conditions are met
15551555
for _, cj := range []glance.CronJobType{glance.CachePruner, glance.CacheCleaner} {
1556-
if err = r.Client.Get(
1556+
if err = r.Get(
15571557
ctx,
15581558
types.NamespacedName{
15591559
Name: fmt.Sprintf("%s-%s", pvcName, cj),
@@ -1626,9 +1626,9 @@ func (r *GlanceAPIReconciler) glanceAPIRefresh(
16261626
}
16271627
return err
16281628
}
1629-
err = r.Client.Delete(ctx, sts)
1629+
err = r.Delete(ctx, sts)
16301630
if err != nil && !k8s_errors.IsNotFound(err) {
1631-
err = fmt.Errorf("Error deleting %s: %w", instance.Name, err)
1631+
err = fmt.Errorf("error deleting %s: %w", instance.Name, err)
16321632
return err
16331633
}
16341634
return nil

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package main implements the glance-operator controller manager.
1718
package main
1819

1920
import (

pkg/glance/const.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16+
// Package glance contains glance service constants and configuration.
1617
package glance
1718

1819
import (
20+
"time"
21+
1922
"github.com/openstack-k8s-operators/lib-common/modules/storage"
2023
ctrl "sigs.k8s.io/controller-runtime"
21-
"time"
2224
)
2325

2426
// CronJobType -
@@ -63,7 +65,7 @@ const (
6365
// CustomServiceConfigFileName -
6466
CustomServiceConfigFileName = "02-config.conf"
6567
// CustomServiceConfigSecretsFileName -
66-
CustomServiceConfigSecretsFileName = "03-config.conf"
68+
CustomServiceConfigSecretsFileName = "03-config.conf" // #nosec G101
6769

6870
// GlanceExtraVolTypeUndefined can be used to label an extraMount which
6971
// is not associated with a specific backend

pkg/glance/pvc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ func GetPvc(api *glancev1.GlanceAPI, labels map[string]string, pvcType PvcType)
1919
var pvcName string
2020
pvcAnnotation := map[string]string{}
2121

22-
switch {
23-
case pvcType == PvcCache:
22+
switch pvcType {
23+
case PvcCache:
2424
pvcAnnotation["image-cache"] = "true"
25-
requestSize = api.Spec.GlanceAPITemplate.ImageCache.Size
25+
requestSize = api.Spec.ImageCache.Size
2626
// append -cache to avoid confusion when listing PVCs
2727
pvcName = fmt.Sprintf("%s-cache", ServiceName)
2828
default:

pkg/glanceapi/cachejob.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
*/
1515

16+
// Package glanceapi contains glance API functionality and configuration.
1617
package glanceapi
1718

1819
import (
@@ -69,9 +70,9 @@ func ImageCacheJob(
6970
}
7071

7172
// add CA cert if defined from the first api
72-
if instance.Spec.GlanceAPITemplate.TLS.CaBundleSecretName != "" {
73-
cronJobVolume = append(cronJobVolume, instance.Spec.GlanceAPITemplate.TLS.CreateVolume())
74-
cronJobVolumeMounts = append(cronJobVolumeMounts, instance.Spec.GlanceAPITemplate.TLS.CreateVolumeMounts(nil)...)
73+
if instance.Spec.TLS.CaBundleSecretName != "" {
74+
cronJobVolume = append(cronJobVolume, instance.Spec.TLS.CreateVolume())
75+
cronJobVolumeMounts = append(cronJobVolumeMounts, instance.Spec.TLS.CreateVolumeMounts(nil)...)
7576
}
7677

7778
// The image-cache PVC should be available to the Cache CronJobs to properly

0 commit comments

Comments
 (0)