From 2bc11dcf5dc46cf6ff5038e3dc10f67ebdac37c5 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Wed, 10 Jan 2024 11:20:31 -0500 Subject: [PATCH] Updates for golangci-lint (#125) * Updates for golangci-lint Switch config to be more consistent with other O-F repos. Fix lint issues. Signed-off-by: Andy Goldstein * Make operator-lib imports last Signed-off-by: Andy Goldstein --------- Signed-off-by: Andy Goldstein --- .golangci.yml | 107 +++++++++----- handler/enqueue_annotation.go | 2 +- handler/example_test.go | 7 +- handler/instrumented_enqueue_object_test.go | 4 +- handler/pause.go | 4 +- internal/annotation/filter_test.go | 146 ++++++++++---------- internal/annotation/suite_test.go | 12 +- leader/leader.go | 4 +- predicate/dependent.go | 14 +- predicate/example_test.go | 7 +- predicate/nogeneration_test.go | 34 ++--- predicate/pause.go | 4 +- predicate/predicate_suite_test.go | 12 +- proxy/proxy_test.go | 1 - prune/prune.go | 7 +- prune/prune_test.go | 4 +- prune/strategies.go | 1 - 17 files changed, 210 insertions(+), 160 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 54d05e5..cda2f0c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,36 +1,77 @@ -issues: - # don't skip warning about doc comments - # don't exclude the default set of lint - exclude-use-default: false - # restore some of the defaults - # (fill in the rest as needed) - exclude-rules: - - linters: [errcheck] - text: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked" +run: + # Default timeout is 1m, up to give more room + timeout: 4m + linters: - disable-all: true enable: - - nakedret - - interfacer - - varcheck - - deadcode - - structcheck - - misspell - - maligned - - ineffassign - - goconst - - goimports - - errcheck - - unparam - - golint - - staticcheck - - gosimple - - unused - - govet - - typecheck - - gocyclo - - gofmt - - dupl + - asciicheck + - bodyclose + - errorlint + - gofmt + - goimports + - gosec + - importas + - misspell + - nestif + - nonamedreturns + - prealloc + - revive + - stylecheck + - tparallel + - unconvert + - unparam + - unused + - whitespace -run: - deadline: 5m +linters-settings: + errorlint: + errorf: false + + importas: + alias: + - pkg: k8s.io/apimachinery/pkg/apis/meta/v1 + alias: metav1 + - pkg: k8s.io/apimachinery/pkg/api/errors + alias: apierrors + - pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1 + alias: apiextensionsv1 + - pkg: k8s.io/apimachinery/pkg/util/runtime + alias: utilruntime + - pkg: "^k8s\\.io/api/([^/]+)/(v[^/]+)$" + alias: $1$2 + - pkg: sigs.k8s.io/controller-runtime + alias: ctrl + + goimports: + local-prefixes: github.com/operator-framework/operator-lib + + revive: + ignore-generated-header: false + severity: warning + confidence: 0.8 + rules: + - name: blank-imports + - name: context-as-argument + - name: context-keys-type + - name: error-return + - name: error-strings + - name: error-naming + - name: exported + - name: increment-decrement + - name: var-naming + - name: var-declaration + - name: package-comments + - name: range + - name: receiver-naming + - name: time-naming + - name: unexported-return + - name: indent-error-flow + - name: errorf + - name: empty-block + - name: superfluous-else + - name: unused-parameter + - name: unreachable-code + - name: redefines-builtin-id + +output: + format: tab diff --git a/handler/enqueue_annotation.go b/handler/enqueue_annotation.go index b5798c3..fe882d5 100644 --- a/handler/enqueue_annotation.go +++ b/handler/enqueue_annotation.go @@ -164,7 +164,7 @@ func SetOwnerAnnotations(owner, object client.Object) error { ownerGK := owner.GetObjectKind().GroupVersionKind().GroupKind() if ownerGK.Kind == "" { - return fmt.Errorf("Owner %s Kind not found, cannot call SetOwnerAnnotations", owner.GetName()) + return fmt.Errorf("owner %s Kind not found, cannot call SetOwnerAnnotations", owner.GetName()) } annotations := object.GetAnnotations() diff --git a/handler/example_test.go b/handler/example_test.go index b888c46..a0ff195 100644 --- a/handler/example_test.go +++ b/handler/example_test.go @@ -18,14 +18,15 @@ import ( "context" "os" - "github.com/operator-framework/operator-lib/handler" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager/signals" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source" + + "github.com/operator-framework/operator-lib/handler" ) // This example applies the Pause handler to all incoming Pod events on a Pod controller. @@ -54,7 +55,7 @@ func ExampleNewPause() { if err != nil { os.Exit(1) } - if err := c.Watch(source.Kind(mgr.GetCache(), &v1.Pod{}), pause); err != nil { + if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}), pause); err != nil { os.Exit(1) } diff --git a/handler/instrumented_enqueue_object_test.go b/handler/instrumented_enqueue_object_test.go index 13a6653..60911f4 100644 --- a/handler/instrumented_enqueue_object_test.go +++ b/handler/instrumented_enqueue_object_test.go @@ -17,8 +17,6 @@ package handler import ( "context" - "github.com/operator-framework/operator-lib/handler/internal/metrics" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/prometheus/client_golang/prometheus" @@ -30,6 +28,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + "github.com/operator-framework/operator-lib/handler/internal/metrics" ) var _ = Describe("InstrumentedEnqueueRequestForObject", func() { diff --git a/handler/pause.go b/handler/pause.go index 648aaa0..9ce2b4a 100644 --- a/handler/pause.go +++ b/handler/pause.go @@ -15,9 +15,9 @@ package handler import ( - "github.com/operator-framework/operator-lib/internal/annotation" - "sigs.k8s.io/controller-runtime/pkg/handler" + + "github.com/operator-framework/operator-lib/internal/annotation" ) // NewPause returns an event handler that filters out objects with a truthy "paused" annotation. diff --git a/internal/annotation/filter_test.go b/internal/annotation/filter_test.go index 03d309c..4e470b7 100644 --- a/internal/annotation/filter_test.go +++ b/internal/annotation/filter_test.go @@ -212,60 +212,60 @@ var _ = Describe("filter", func() { Expect(pred.Update(e)).To(BeTrue()) }) It("receives neither objects having any annotations", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the new object with a registered key and false value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{annotationKey: "false"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{annotationKey: "false"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the old object with a registered key and false value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) + updated.SetLabels(map[string]string{"id": "updated"}) old.SetAnnotations(map[string]string{annotationKey: "false"}) - e = makeUpdateEventFor(old, new) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the new object with a non-registered key and true value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{"my.app/foo": "true"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{"my.app/foo": "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the new object with a non-registered key and false value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{"my.app/foo": "false"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{"my.app/foo": "false"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) }) Context("returns false", func() { It("receives the new object with a registered key", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() - new.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + old, updated := pod.DeepCopy(), pod.DeepCopy() + updated.SetAnnotations(map[string]string{annotationKey: "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) @@ -279,31 +279,31 @@ var _ = Describe("filter", func() { verifyQueueEmpty(q) }) It("receives both objects with a registered key", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() - new.SetAnnotations(map[string]string{annotationKey: "true"}) + old, updated := pod.DeepCopy(), pod.DeepCopy() + updated.SetAnnotations(map[string]string{annotationKey: "true"}) old.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) }) It("receives the old object with a registered key and false value, and new with true", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) + updated.SetLabels(map[string]string{"id": "updated"}) old.SetAnnotations(map[string]string{annotationKey: "false"}) - new.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + updated.SetAnnotations(map[string]string{annotationKey: "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) }) It("receives the old object having no annotations, and new with a registered key and true value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{annotationKey: "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) @@ -475,50 +475,50 @@ var _ = Describe("filter", func() { Expect(pred.Update(e)).To(BeFalse()) }) It("receives neither objects having any annotations", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) }) It("receives the new object with a registered key and false value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{annotationKey: "false"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{annotationKey: "false"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) }) It("receives the old object with a registered key and false value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) + updated.SetLabels(map[string]string{"id": "updated"}) old.SetAnnotations(map[string]string{annotationKey: "false"}) - e = makeUpdateEventFor(old, new) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) }) It("receives the new object with a non-registered key and true value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{"my.app/foo": "true"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{"my.app/foo": "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) }) It("receives the new object with a non-registered key and false value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{"my.app/foo": "false"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{"my.app/foo": "false"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) hdlr.Update(ctx, e, q) verifyQueueEmpty(q) @@ -526,12 +526,12 @@ var _ = Describe("filter", func() { }) Context("returns true", func() { It("receives the new object with a registered key", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() - new.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + old, updated := pod.DeepCopy(), pod.DeepCopy() + updated.SetAnnotations(map[string]string{annotationKey: "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the old object with a registered key", func() { old := pod.DeepCopy() @@ -542,34 +542,34 @@ var _ = Describe("filter", func() { verifyQueueHasPod(q, old) }) It("receives both objects with a registered key", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() - new.SetAnnotations(map[string]string{annotationKey: "true"}) + old, updated := pod.DeepCopy(), pod.DeepCopy() + updated.SetAnnotations(map[string]string{annotationKey: "true"}) old.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the old object with a registered key and false value, and new with true", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) + updated.SetLabels(map[string]string{"id": "updated"}) old.SetAnnotations(map[string]string{annotationKey: "false"}) - new.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + updated.SetAnnotations(map[string]string{annotationKey: "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) It("receives the old object having no annotations, and new with a registered key and true value", func() { - old, new := pod.DeepCopy(), pod.DeepCopy() + old, updated := pod.DeepCopy(), pod.DeepCopy() old.SetLabels(map[string]string{"id": "old"}) - new.SetLabels(map[string]string{"id": "new"}) - new.SetAnnotations(map[string]string{annotationKey: "true"}) - e = makeUpdateEventFor(old, new) + updated.SetLabels(map[string]string{"id": "updated"}) + updated.SetAnnotations(map[string]string{annotationKey: "true"}) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeTrue()) hdlr.Update(ctx, e, q) - verifyQueueHasPod(q, new) + verifyQueueHasPod(q, updated) }) }) }) diff --git a/internal/annotation/suite_test.go b/internal/annotation/suite_test.go index 0c84711..5d7877c 100644 --- a/internal/annotation/suite_test.go +++ b/internal/annotation/suite_test.go @@ -28,23 +28,27 @@ func TestAnnotation(t *testing.T) { RunSpecs(t, "Annotation Suite") } -func makeCreateEventFor(obj client.Object) (e event.CreateEvent) { +func makeCreateEventFor(obj client.Object) event.CreateEvent { + var e event.CreateEvent e.Object = obj return e } -func makeUpdateEventFor(old, new client.Object) (e event.UpdateEvent) { +func makeUpdateEventFor(old, new client.Object) event.UpdateEvent { + var e event.UpdateEvent e.ObjectOld = old e.ObjectNew = new return e } -func makeDeleteEventFor(obj client.Object) (e event.DeleteEvent) { +func makeDeleteEventFor(obj client.Object) event.DeleteEvent { + var e event.DeleteEvent e.Object = obj return e } -func makeGenericEventFor(obj client.Object) (e event.GenericEvent) { +func makeGenericEventFor(obj client.Object) event.GenericEvent { + var e event.GenericEvent e.Object = obj return e } diff --git a/leader/leader.go b/leader/leader.go index 6b9c5c7..16d497c 100644 --- a/leader/leader.go +++ b/leader/leader.go @@ -20,7 +20,6 @@ import ( "os" "time" - "github.com/operator-framework/operator-lib/internal/utils" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -28,6 +27,8 @@ import ( crclient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" logf "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/operator-framework/operator-lib/internal/utils" ) // ErrNoNamespace indicates that a namespace could not be found for the current @@ -290,7 +291,6 @@ func isNotReadyNode(ctx context.Context, client crclient.Client, nodeName string } } return false - } func deleteLeader(ctx context.Context, client crclient.Client, leaderPod *corev1.Pod, existing *corev1.ConfigMap) error { diff --git a/predicate/dependent.go b/predicate/dependent.go index bb37321..608aa45 100644 --- a/predicate/dependent.go +++ b/predicate/dependent.go @@ -85,22 +85,22 @@ func (DependentPredicate) Generic(e event.GenericEvent) bool { // resource to write to the status of one its dependent resources. func (DependentPredicate) Update(e event.UpdateEvent) bool { old := e.ObjectOld.(*unstructured.Unstructured).DeepCopy() - new := e.ObjectNew.(*unstructured.Unstructured).DeepCopy() + updated := e.ObjectNew.(*unstructured.Unstructured).DeepCopy() delete(old.Object, "status") - delete(new.Object, "status") + delete(updated.Object, "status") old.SetResourceVersion("") - new.SetResourceVersion("") + updated.SetResourceVersion("") old.SetManagedFields(removeTimeFromManagedFields(old.GetManagedFields())) - new.SetManagedFields(removeTimeFromManagedFields(new.GetManagedFields())) + updated.SetManagedFields(removeTimeFromManagedFields(updated.GetManagedFields())) - if reflect.DeepEqual(old.Object, new.Object) { + if reflect.DeepEqual(old.Object, updated.Object) { return false } log.V(1).Info("Reconciling due to dependent resource update", - "name", new.GetName(), "namespace", new.GetNamespace(), "apiVersion", - new.GroupVersionKind().GroupVersion(), "kind", new.GroupVersionKind().Kind) + "name", updated.GetName(), "namespace", updated.GetNamespace(), "apiVersion", + updated.GroupVersionKind().GroupVersion(), "kind", updated.GroupVersionKind().Kind) return true } diff --git a/predicate/example_test.go b/predicate/example_test.go index e0bac33..4ca0fab 100644 --- a/predicate/example_test.go +++ b/predicate/example_test.go @@ -18,13 +18,14 @@ import ( "context" "os" - "github.com/operator-framework/operator-lib/predicate" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager/signals" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + "github.com/operator-framework/operator-lib/predicate" ) // This example applies the Pause predicate to all incoming Pod events on a Pod controller. @@ -50,7 +51,7 @@ func ExampleNewPause() { os.Exit(1) } pred := builder.WithPredicates(pause) - if err := builder.ControllerManagedBy(mgr).For(&v1.Pod{}, pred).Complete(r); err != nil { + if err := builder.ControllerManagedBy(mgr).For(&corev1.Pod{}, pred).Complete(r); err != nil { os.Exit(1) } diff --git a/predicate/nogeneration_test.go b/predicate/nogeneration_test.go index b4ae3b1..355ce9a 100644 --- a/predicate/nogeneration_test.go +++ b/predicate/nogeneration_test.go @@ -38,50 +38,50 @@ var _ = Describe("NoGenerationPredicate", func() { It("returns false", func() { By("the new object having a non-zero generation", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} - new.SetGeneration(1) - e = makeUpdateEventFor(old, new) + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} + updated.SetGeneration(1) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) }) // The old generation will never be lower than the new, so we don't have to test that case. By("the old and new objects having equal non-zero generations", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} old.SetGeneration(1) - new.SetGeneration(1) - e = makeUpdateEventFor(old, new) + updated.SetGeneration(1) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) }) By("the old and new objects having unequal non-zero generations", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} old.SetGeneration(1) - new.SetGeneration(2) - e = makeUpdateEventFor(old, new) + updated.SetGeneration(2) + e = makeUpdateEventFor(old, updated) Expect(pred.Update(e)).To(BeFalse()) }) }) It("logs a message and returns false", func() { By("the old object being nil", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} - e = makeUpdateEventFor(old, new) + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} + e = makeUpdateEventFor(old, updated) e.ObjectOld = nil Expect(pred.Update(e)).To(BeFalse()) }) By("the old metadata being nil", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} - e = makeUpdateEventFor(old, new) + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} + e = makeUpdateEventFor(old, updated) e.ObjectOld = nil Expect(pred.Update(e)).To(BeFalse()) }) By("the new object being nil", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} - e = makeUpdateEventFor(old, new) + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} + e = makeUpdateEventFor(old, updated) e.ObjectNew = nil Expect(pred.Update(e)).To(BeFalse()) }) By("the new metadata being nil", func() { - old, new := &appsv1.Deployment{}, &appsv1.Deployment{} - e = makeUpdateEventFor(old, new) + old, updated := &appsv1.Deployment{}, &appsv1.Deployment{} + e = makeUpdateEventFor(old, updated) e.ObjectNew = nil Expect(pred.Update(e)).To(BeFalse()) }) diff --git a/predicate/pause.go b/predicate/pause.go index 9a2ea3e..b008f66 100644 --- a/predicate/pause.go +++ b/predicate/pause.go @@ -15,9 +15,9 @@ package predicate import ( - "github.com/operator-framework/operator-lib/internal/annotation" - "sigs.k8s.io/controller-runtime/pkg/predicate" + + "github.com/operator-framework/operator-lib/internal/annotation" ) // NewPause returns a predicate that filters out objects with a truthy "paused" annotation. diff --git a/predicate/predicate_suite_test.go b/predicate/predicate_suite_test.go index a96a9f5..1659c6e 100644 --- a/predicate/predicate_suite_test.go +++ b/predicate/predicate_suite_test.go @@ -28,23 +28,27 @@ func TestPredicate(t *testing.T) { RunSpecs(t, "Predicate Suite") } -func makeCreateEventFor(obj client.Object) (e event.CreateEvent) { +func makeCreateEventFor(obj client.Object) event.CreateEvent { + var e event.CreateEvent e.Object = obj return e } -func makeUpdateEventFor(old, new client.Object) (e event.UpdateEvent) { +func makeUpdateEventFor(old, new client.Object) event.UpdateEvent { + var e event.UpdateEvent e.ObjectOld = old e.ObjectNew = new return e } -func makeDeleteEventFor(obj client.Object) (e event.DeleteEvent) { +func makeDeleteEventFor(obj client.Object) event.DeleteEvent { + var e event.DeleteEvent e.Object = obj return e } -func makeGenericEventFor(obj client.Object) (e event.GenericEvent) { +func makeGenericEventFor(obj client.Object) event.GenericEvent { + var e event.GenericEvent e.Object = obj return e } diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 25052b8..ba2b331 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -31,7 +31,6 @@ func checkValueFromEnvObj(name string, envVars []corev1.EnvVar) (string, error) } } return "", errors.New("empty name") - } var _ = Describe("Retrieving", func() { diff --git a/prune/prune.go b/prune/prune.go index 460a154..10ab5c5 100644 --- a/prune/prune.go +++ b/prune/prune.go @@ -111,7 +111,6 @@ func (p Pruner) Namespace() string { // NewPruner returns a pruner that uses the given strategy to prune objects that have the given GVK func NewPruner(prunerClient client.Client, gvk schema.GroupVersionKind, strategy StrategyFunc, opts ...PrunerOption) (*Pruner, error) { - if gvk.Empty() { return nil, fmt.Errorf("error when creating a new Pruner: gvk parameter can not be empty") } @@ -132,7 +131,6 @@ func NewPruner(prunerClient client.Client, gvk schema.GroupVersionKind, strategy // Prune runs the pruner. func (p Pruner) Prune(ctx context.Context) ([]client.Object, error) { - var objs []client.Object listOpts := client.ListOptions{ LabelSelector: labels.Set(p.labels).AsSelector(), Namespace: p.namespace, @@ -144,7 +142,10 @@ func (p Pruner) Prune(ctx context.Context) ([]client.Object, error) { return nil, fmt.Errorf("error getting a list of resources: %w", err) } - for _, unsObj := range unstructuredObjs.Items { + objs := make([]client.Object, 0, len(unstructuredObjs.Items)) + + for i := range unstructuredObjs.Items { + unsObj := unstructuredObjs.Items[i] obj, err := convert(p.client, p.gvk, &unsObj) if err != nil { return nil, err diff --git a/prune/prune_test.go b/prune/prune_test.go index e610fc1..75e4893 100644 --- a/prune/prune_test.go +++ b/prune/prune_test.go @@ -687,7 +687,7 @@ func createSchemes() (*runtime.Scheme, error) { // myStrategy shows how you can write your own strategy // In this example it simply removes a resource if it has // the name 'churro1' or 'churro2' -func myStrategy(ctx context.Context, objs []client.Object) ([]client.Object, error) { +func myStrategy(_ context.Context, objs []client.Object) ([]client.Object, error) { var objsToRemove []client.Object for _, obj := range objs { @@ -710,6 +710,6 @@ func expectPanic() { // myIsPrunable shows how you can write your own IsPrunableFunc // In this example it simply removes all resources -func myIsPrunable(obj client.Object) error { +func myIsPrunable(_ client.Object) error { return nil } diff --git a/prune/strategies.go b/prune/strategies.go index 79f65be..c15ec6e 100644 --- a/prune/strategies.go +++ b/prune/strategies.go @@ -27,7 +27,6 @@ import ( // If the max count of resources is exceeded, the oldest resources are prioritized for pruning func NewPruneByCountStrategy(count int) StrategyFunc { return func(ctx context.Context, objs []client.Object) ([]client.Object, error) { - if len(objs) <= count { return nil, nil }