From fb64b601130bfab5b35ca018595d74de4f446b9c Mon Sep 17 00:00:00 2001 From: haramjeong <04harams77@gmail.com> Date: Sat, 24 Oct 2026 01:40:48 +0900 Subject: [PATCH 1/2] Run go fix as part of the test workflow Run go fix in the root and API modules before tests so the CI dirty check also enforces source modernization. Apply the resulting fixes. Set rendered artifact names on the local OutputArtifact copy before embedding it to avoid conflicting promoted-field initializers. Signed-off-by: haramjeong <04harams77@gmail.com> Assisted-by: Codex/GPT-6 --- Makefile | 8 +- cmd/main.go | 3 +- go.mod | 2 +- .../artifactgenerator_authorization_test.go | 7 +- .../artifactgenerator_controller.go | 23 ++-- .../artifactgenerator_controller_test.go | 102 ++++++------------ ...ifactgenerator_direct_source_fetch_test.go | 13 +-- .../artifactgenerator_drift_test.go | 70 +++++------- ...rtifactgenerator_externalfinalizer_test.go | 5 +- .../controller/artifactgenerator_finalize.go | 7 +- .../artifactgenerator_finalize_test.go | 5 +- .../artifactgenerator_inputs_test.go | 3 +- .../artifactgenerator_namespace_test.go | 80 ++++++-------- .../artifactgenerator_pathpattern.go | 4 +- .../artifactgenerator_validation_test.go | 7 +- .../artifactgenerator_watch_test.go | 28 ++--- 16 files changed, 138 insertions(+), 229 deletions(-) diff --git a/Makefile b/Makefile index ff87c0b..e81a7a8 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ all: manager # Run tests KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)" -test: tidy generate fmt vet manifests install-envtest download-crd-deps +test: tidy generate fix fmt vet manifests install-envtest download-crd-deps KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... $(GO_TEST_ARGS) -coverprofile cover.out # Build manager binary @@ -115,6 +115,12 @@ tidy: cd api; rm -f go.sum; go mod tidy -compat=1.27 rm -f go.sum; go mod tidy -compat=1.27 +# Run go fix against code +.PHONY: fix +fix: + cd api; go fix ./... + go fix ./... + # Run go fmt against code fmt: go fmt ./... diff --git a/cmd/main.go b/cmd/main.go index 22efe54..8f5840c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -29,7 +29,6 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth" - "k8s.io/utils/ptr" ctrlruntime "sigs.k8s.io/controller-runtime" ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache" ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -177,7 +176,7 @@ func main() { Logger: ctrlruntime.Log, Controller: ctrlcfg.Controller{ MaxConcurrentReconciles: concurrent, - RecoverPanic: ptr.To(true), + RecoverPanic: new(true), ReconciliationTimeout: reconciliationTimeout, }, Client: ctrlclient.Options{ diff --git a/go.mod b/go.mod index 39d4eb5..743d07e 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,6 @@ require ( k8s.io/apiextensions-apiserver v0.37.1 k8s.io/apimachinery v0.37.1 k8s.io/client-go v0.37.1 - k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3 sigs.k8s.io/controller-runtime v0.25.2 sigs.k8s.io/yaml v1.6.0 ) @@ -144,6 +143,7 @@ require ( k8s.io/klog/v2 v2.140.0 // indirect k8s.io/kube-openapi v0.0.0-20260821135717-be32def86098 // indirect k8s.io/kubectl v0.37.1 // indirect + k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3 // indirect sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect sigs.k8s.io/kustomize/api v0.21.2 // indirect sigs.k8s.io/kustomize/kyaml v0.21.2 // indirect diff --git a/internal/controller/artifactgenerator_authorization_test.go b/internal/controller/artifactgenerator_authorization_test.go index 4b4c591..8dd5a76 100644 --- a/internal/controller/artifactgenerator_authorization_test.go +++ b/internal/controller/artifactgenerator_authorization_test.go @@ -23,7 +23,6 @@ import ( . "github.com/onsi/gomega" authzv1 "k8s.io/api/authorization/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/controller-runtime/pkg/client/interceptor" @@ -60,7 +59,7 @@ func TestAccessCheckerDeduplicates(t *testing.T) { }).Build() obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"}, + Name: "test", Namespace: "flux-system", } checker := newAccessChecker(kubeClient, obj, "default-sa") @@ -94,7 +93,7 @@ func TestAccessCheckerSameNamespaceUsesControllerCredentials(t *testing.T) { }).Build() obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"}, + Name: "test", Namespace: "flux-system", } checker := newAccessChecker(kubeClient, obj, "default-sa") @@ -118,7 +117,7 @@ func TestAccessCheckerDenied(t *testing.T) { }).Build() obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"}, + Name: "test", Namespace: "flux-system", } checker := newAccessChecker(kubeClient, obj, "default-sa") diff --git a/internal/controller/artifactgenerator_controller.go b/internal/controller/artifactgenerator_controller.go index b0b9717..19b189e 100644 --- a/internal/controller/artifactgenerator_controller.go +++ b/internal/controller/artifactgenerator_controller.go @@ -100,8 +100,7 @@ func (r *ArtifactGeneratorReconciler) Reconcile(ctx context.Context, req ctrl.Re // are retryable, as the RBAC bindings may be fixed without changing the // ArtifactGenerator. defer func() { - var accessErr *accessDeniedError - if errors.As(retErr, &accessErr) { + if accessErr, ok := errors.AsType[*accessDeniedError](retErr); ok { gotkconditions.MarkFalse(obj, gotkmeta.ReadyCondition, swapi.AccessDeniedReason, @@ -763,16 +762,12 @@ func (r *ArtifactGeneratorReconciler) reconcileExternalArtifact(ctx context.Cont // Create the ExternalArtifact object. ea := &sourcev1.ExternalArtifact{ - TypeMeta: metav1.TypeMeta{ - APIVersion: sourcev1.GroupVersion.String(), - Kind: sourcev1.ExternalArtifactKind, - }, - ObjectMeta: metav1.ObjectMeta{ - Name: outputArtifact.Name, - Namespace: obj.GetArtifactNamespace(outputArtifact), - Labels: labels, - Annotations: annotations, - }, + APIVersion: sourcev1.GroupVersion.String(), + Kind: sourcev1.ExternalArtifactKind, + Name: outputArtifact.Name, + Namespace: obj.GetArtifactNamespace(outputArtifact), + Labels: labels, + Annotations: annotations, Spec: sourcev1.ExternalArtifactSpec{ SourceRef: &gotkmeta.NamespacedObjectKindReference{ APIVersion: swapi.GroupVersion.String(), @@ -824,9 +819,7 @@ func (r *ArtifactGeneratorReconciler) reconcileExternalArtifact(ctx context.Cont ExportedInputs: exportedInputs, } statusOpts := &client.SubResourcePatchOptions{ - PatchOptions: client.PatchOptions{ - FieldManager: r.ControllerName, - }, + FieldManager: r.ControllerName, } if err := auth.ensureExternalArtifactVerb(ctx, namespace, "patch", "status"); err != nil { return nil, nil, err diff --git a/internal/controller/artifactgenerator_controller_test.go b/internal/controller/artifactgenerator_controller_test.go index beac218..b965ea8 100644 --- a/internal/controller/artifactgenerator_controller_test.go +++ b/internal/controller/artifactgenerator_controller_test.go @@ -324,14 +324,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { } generator := &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { @@ -350,10 +346,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { cleanup := func() { testClient.Delete(context.Background(), &sourcev1.GitRepository{ - ObjectMeta: metav1.ObjectMeta{Name: gitKey.Name, Namespace: gitKey.Namespace}, + Name: gitKey.Name, Namespace: gitKey.Namespace, }) testClient.Delete(context.Background(), &sourcev1.OCIRepository{ - ObjectMeta: metav1.ObjectMeta{Name: ociKey.Name, Namespace: ociKey.Namespace}, + Name: ociKey.Name, Namespace: ociKey.Namespace, }) } @@ -370,14 +366,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { objKey := client.ObjectKey{Name: "test-generator", Namespace: "default"} generator := &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { @@ -413,14 +405,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { } generator := &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { @@ -434,7 +422,7 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { cleanup := func() { testClient.Delete(context.Background(), &sourcev1.GitRepository{ - ObjectMeta: metav1.ObjectMeta{Name: gitKey.Name, Namespace: gitKey.Namespace}, + Name: gitKey.Name, Namespace: gitKey.Namespace, }) } @@ -458,14 +446,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { } generator := &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { @@ -480,7 +464,7 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) { cleanup := func() { testClient.Delete(context.Background(), &sourcev1.GitRepository{ - ObjectMeta: metav1.ObjectMeta{Name: gitKey.Name, Namespace: gitKey.Namespace}, + Name: gitKey.Name, Namespace: gitKey.Namespace, }) } @@ -713,14 +697,10 @@ func getArtifactGeneratorReconciler() *ArtifactGeneratorReconciler { func getArtifactGenerator(objectKey client.ObjectKey) *swapi.ArtifactGenerator { return &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objectKey.Name, - Namespace: objectKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objectKey.Name, + Namespace: objectKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { @@ -766,14 +746,10 @@ func applyGitRepository(objKey client.ObjectKey, revision string, files []gotkte } repo := &sourcev1.GitRepository{ - TypeMeta: metav1.TypeMeta{ - Kind: sourcev1.GitRepositoryKind, - APIVersion: sourcev1.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: sourcev1.GitRepositoryKind, + APIVersion: sourcev1.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: sourcev1.GitRepositorySpec{ URL: "https://github.com/test/repository", Interval: metav1.Duration{Duration: time.Minute}, @@ -816,9 +792,7 @@ func applyGitRepository(objKey client.ObjectKey, revision string, files []gotkte repo.Status = status statusOpts := &client.SubResourcePatchOptions{ - PatchOptions: client.PatchOptions{ - FieldManager: "source-controller", - }, + FieldManager: "source-controller", } return testClient.Status().Patch(context.Background(), repo, client.Apply, statusOpts) @@ -831,14 +805,10 @@ func applyOCIRepository(objKey client.ObjectKey, revision string, files []gotkte } repo := &sourcev1.OCIRepository{ - TypeMeta: metav1.TypeMeta{ - Kind: sourcev1.OCIRepositoryKind, - APIVersion: sourcev1.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: sourcev1.OCIRepositoryKind, + APIVersion: sourcev1.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: sourcev1.OCIRepositorySpec{ URL: "oci://ghcr.io/test/repository", Interval: &metav1.Duration{Duration: time.Minute}, @@ -883,9 +853,7 @@ func applyOCIRepository(objKey client.ObjectKey, revision string, files []gotkte repo.Status = status statusOpts := &client.SubResourcePatchOptions{ - PatchOptions: client.PatchOptions{ - FieldManager: "source-controller", - }, + FieldManager: "source-controller", } return testClient.Status().Patch(context.Background(), repo, client.Apply, statusOpts) @@ -1102,7 +1070,7 @@ func TestAccessCheckerServiceAccountSelection(t *testing.T) { g := NewWithT(t) obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"}, + Name: "test", Namespace: "flux-system", } checker := newAccessChecker(nil, obj, "default-sa") diff --git a/internal/controller/artifactgenerator_direct_source_fetch_test.go b/internal/controller/artifactgenerator_direct_source_fetch_test.go index b1df84f..0765c54 100644 --- a/internal/controller/artifactgenerator_direct_source_fetch_test.go +++ b/internal/controller/artifactgenerator_direct_source_fetch_test.go @@ -23,7 +23,6 @@ import ( "time" . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -67,14 +66,10 @@ func TestArtifactGeneratorReconciler_DirectSourceFetch(t *testing.T) { Namespace: ns.Name, } obj := &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { diff --git a/internal/controller/artifactgenerator_drift_test.go b/internal/controller/artifactgenerator_drift_test.go index 2baab3d..054e343 100644 --- a/internal/controller/artifactgenerator_drift_test.go +++ b/internal/controller/artifactgenerator_drift_test.go @@ -75,10 +75,8 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { // Generate the ExternalArtifact in cluster obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - }, + Name: "test-generator", + Namespace: ns.Name, } auth := newAccessChecker(nil, obj, "") _, _, err = reconciler.reconcileExternalArtifact(ctx, obj, outputArtifact, artifact, nil, nil, auth) @@ -95,11 +93,9 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { { name: "no drift when everything matches", obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - Generation: 1, - }, + Name: "test-generator", + Namespace: ns.Name, + Generation: 1, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ {Alias: "test", Kind: sourcev1.GitRepositoryKind, Name: "test-repo"}, @@ -133,11 +129,9 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { { name: "drift detected when object is not ready", obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - Generation: 1, - }, + Name: "test-generator", + Namespace: ns.Name, + Generation: 1, Status: swapi.ArtifactGeneratorStatus{ Conditions: []metav1.Condition{ { @@ -181,11 +175,9 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { { name: "drift detected when sources digest changed", obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - Generation: 1, - }, + Name: "test-generator", + Namespace: ns.Name, + Generation: 1, Status: swapi.ArtifactGeneratorStatus{ Conditions: []metav1.Condition{ { @@ -205,11 +197,9 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { { name: "drift detected when number of output artifacts changed", obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - Generation: 1, - }, + Name: "test-generator", + Namespace: ns.Name, + Generation: 1, Spec: swapi.ArtifactGeneratorSpec{ OutputArtifacts: []swapi.OutputArtifact{ {Name: "artifact-1"}, @@ -244,11 +234,9 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { { name: "drift detected when artifact missing from storage", obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - Generation: 1, - }, + Name: "test-generator", + Namespace: ns.Name, + Generation: 1, Spec: swapi.ArtifactGeneratorSpec{ OutputArtifacts: []swapi.OutputArtifact{*outputArtifact}, }, @@ -281,19 +269,15 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { setupFunc: func() { // Delete the in cluster artifact err = testEnv.Cleanup(ctx, &sourcev1.ExternalArtifact{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-artifact", - Namespace: ns.Name, - }, + Name: "test-artifact", + Namespace: ns.Name, }) g.Expect(err).ToNot(HaveOccurred()) }, obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-generator", - Namespace: ns.Name, - Generation: 1, - }, + Name: "test-generator", + Namespace: ns.Name, + Generation: 1, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ {Alias: "test", Kind: sourcev1.GitRepositoryKind, Name: "test-repo"}, @@ -327,12 +311,10 @@ func TestArtifactGeneratorReconciler_DetectDrift(t *testing.T) { { name: "no drift when pathPattern inventory count differs from templates", obj: &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: "path-pattern-generator", - Namespace: ns.Name, - UID: "path-pattern-generator", - Generation: 1, - }, + Name: "path-pattern-generator", + Namespace: ns.Name, + UID: "path-pattern-generator", + Generation: 1, Spec: swapi.ArtifactGeneratorSpec{ PathPattern: "@test/apps/{app}/envs/{env}", OutputArtifacts: []swapi.OutputArtifact{ diff --git a/internal/controller/artifactgenerator_externalfinalizer_test.go b/internal/controller/artifactgenerator_externalfinalizer_test.go index 3141204..0634046 100644 --- a/internal/controller/artifactgenerator_externalfinalizer_test.go +++ b/internal/controller/artifactgenerator_externalfinalizer_test.go @@ -24,7 +24,6 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/rand" "sigs.k8s.io/controller-runtime/pkg/client" @@ -104,7 +103,7 @@ func TestNamespaceExternallyFinalized(t *testing.T) { g := NewWithT(t) ctx := context.Background() - ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "tenant"}} + ns := &corev1.Namespace{Name: "tenant"} kubeClient := fake.NewClientBuilder().WithScheme(NewTestScheme()).WithObjects(ns).Build() r := &ArtifactGeneratorReconciler{ Client: kubeClient, @@ -192,7 +191,7 @@ func TestArtifactGeneratorReconciler_ManagedNamespacesExternalFinalizer(t *testi g.Expect(err).ToNot(HaveOccurred()) // The external finalizer object, standing in for the ResourceSet. - fin := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test-finalizer", Namespace: srcNS.Name}} + fin := &corev1.ConfigMap{Name: "test-finalizer", Namespace: srcNS.Name} g.Expect(testClient.Create(ctx, fin)).To(Succeed()) g.Expect(testClient.Get(ctx, client.ObjectKeyFromObject(fin), fin)).To(Succeed()) diff --git a/internal/controller/artifactgenerator_finalize.go b/internal/controller/artifactgenerator_finalize.go index 2a47735..e15661e 100644 --- a/internal/controller/artifactgenerator_finalize.go +++ b/internal/controller/artifactgenerator_finalize.go @@ -24,7 +24,6 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -138,10 +137,8 @@ func (r *ArtifactGeneratorReconciler) finalizeExternalArtifact(ctx context.Conte // Delete from cluster. ea := &sourcev1.ExternalArtifact{ - ObjectMeta: metav1.ObjectMeta{ - Name: ref.Name, - Namespace: ref.Namespace, - }, + Name: ref.Name, + Namespace: ref.Namespace, } if err := r.Client.Delete(ctx, ea); err != nil && !apierrors.IsNotFound(err) { retErr = errors.Join(retErr, fmt.Errorf("failed to delete ExternalArtifact: %w", err)) diff --git a/internal/controller/artifactgenerator_finalize_test.go b/internal/controller/artifactgenerator_finalize_test.go index 4e1f14e..15fe972 100644 --- a/internal/controller/artifactgenerator_finalize_test.go +++ b/internal/controller/artifactgenerator_finalize_test.go @@ -25,7 +25,6 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -102,7 +101,7 @@ func TestArtifactGeneratorReconciler_FinalizeReferencesSurvivors(t *testing.T) { // already deleted. forbidden := apierrors.NewForbidden(schema.GroupResource{Resource: "namespaces"}, "tenant", errors.New("forbidden")) kubeClient := fake.NewClientBuilder(). - WithObjects(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "tenant"}}). + WithObjects(&corev1.Namespace{Name: "tenant"}). WithInterceptorFuncs(interceptor.Funcs{ Delete: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.DeleteOption) error { return forbidden @@ -116,7 +115,7 @@ func TestArtifactGeneratorReconciler_FinalizeReferencesSurvivors(t *testing.T) { } obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "src", UID: "uid"}, + Name: "test", Namespace: "src", UID: "uid", Spec: swapi.ArtifactGeneratorSpec{ ServiceAccountName: "sa", ManagedNamespaces: &swapi.ManagedNamespaces{ diff --git a/internal/controller/artifactgenerator_inputs_test.go b/internal/controller/artifactgenerator_inputs_test.go index 9b4c0f4..a937d93 100644 --- a/internal/controller/artifactgenerator_inputs_test.go +++ b/internal/controller/artifactgenerator_inputs_test.go @@ -24,7 +24,6 @@ import ( "testing" . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -151,7 +150,7 @@ metadata: r := &ArtifactGeneratorReconciler{ControllerName: controllerName} obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Namespace: "flux-system", UID: "uid"}, + Namespace: "flux-system", UID: "uid", Spec: swapi.ArtifactGeneratorSpec{ CommonMetadata: &swapi.CommonMetadata{ Labels: map[string]string{"common": "base"}, diff --git a/internal/controller/artifactgenerator_namespace_test.go b/internal/controller/artifactgenerator_namespace_test.go index 47ee575..434c1a0 100644 --- a/internal/controller/artifactgenerator_namespace_test.go +++ b/internal/controller/artifactgenerator_namespace_test.go @@ -77,7 +77,7 @@ func TestNamespaceMetadata(t *testing.T) { r := &ArtifactGeneratorReconciler{ControllerName: "source-watcher"} obj := &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Namespace: "flux-system", UID: "uid"}, + Namespace: "flux-system", UID: "uid", Spec: swapi.ArtifactGeneratorSpec{ CommonMetadata: &swapi.CommonMetadata{ Labels: map[string]string{"common": "base", "team": "platform"}, @@ -442,7 +442,7 @@ func TestArtifactGeneratorReconciler_ServiceAccountSameNamespaceDenied(t *testin // The ServiceAccount exists but has no RBAC bindings. saName := "test-agns-same-deny-sa" g.Expect(testClient.Create(ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: saName, Namespace: srcNS.Name}, + Name: saName, Namespace: srcNS.Name, })).To(Succeed()) objKey := client.ObjectKey{Name: "test-agns-same-deny", Namespace: srcNS.Name} @@ -624,16 +624,12 @@ func TestArtifactGeneratorReconciler_OwnershipConflict(t *testing.T) { // different ArtifactGenerator. for _, name := range eaNames { otherEA := &sourcev1.ExternalArtifact{ - TypeMeta: metav1.TypeMeta{ - APIVersion: sourcev1.GroupVersion.String(), - Kind: sourcev1.ExternalArtifactKind, - }, - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: tgtNS.Name, - Labels: map[string]string{ - swapi.ArtifactGeneratorLabel: "11111111-1111-1111-1111-111111111111", - }, + APIVersion: sourcev1.GroupVersion.String(), + Kind: sourcev1.ExternalArtifactKind, + Name: name, + Namespace: tgtNS.Name, + Labels: map[string]string{ + swapi.ArtifactGeneratorLabel: "11111111-1111-1111-1111-111111111111", }, Spec: sourcev1.ExternalArtifactSpec{ SourceRef: &gotkmeta.NamespacedObjectKindReference{ @@ -708,7 +704,7 @@ func TestArtifactGeneratorReconciler_CrossNamespaceAccessDenied(t *testing.T) { // The ServiceAccount exists but has no RBAC bindings in the target namespace. saName := "artifact-generator-denied" g.Expect(testClient.Create(ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: saName, Namespace: srcNS.Name}, + Name: saName, Namespace: srcNS.Name, })).To(Succeed()) objKey := client.ObjectKey{Name: "test-agns-deny", Namespace: srcNS.Name} @@ -1015,11 +1011,9 @@ func TestArtifactGeneratorReconciler_ManagedNamespacesTakeover(t *testing.T) { // Pre-create the namespace owned by another ArtifactGenerator. other := &corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: tgtName, - Labels: map[string]string{ - swapi.ArtifactGeneratorLabel: "11111111-1111-1111-1111-111111111111", - }, + Name: tgtName, + Labels: map[string]string{ + swapi.ArtifactGeneratorLabel: "11111111-1111-1111-1111-111111111111", }, } g.Expect(testClient.Create(ctx, other)).To(Succeed()) @@ -1716,7 +1710,7 @@ func TestArtifactGeneratorReconciler_FinalizeServiceAccountMissing(t *testing.T) // Remove the ServiceAccount to simulate a tenant offboarded before its // ArtifactGenerator. g.Expect(testClient.Delete(ctx, &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{Name: saName, Namespace: srcNS.Name}, + Name: saName, Namespace: srcNS.Name, })).To(Succeed()) // The ArtifactGenerator is deleted even though the managed objects cannot @@ -1739,7 +1733,7 @@ func grantNamespaceManagement(ctx context.Context, saName, saNamespace string, v verbs = []string{"get", "list", "watch", "create", "update", "patch", "delete"} } role := &rbacv1.ClusterRole{ - ObjectMeta: metav1.ObjectMeta{Name: saName}, + Name: saName, Rules: []rbacv1.PolicyRule{ { APIGroups: []string{""}, @@ -1763,7 +1757,7 @@ func grantNamespaceManagement(ctx context.Context, saName, saNamespace string, v } binding := &rbacv1.ClusterRoleBinding{ - ObjectMeta: metav1.ObjectMeta{Name: saName}, + Name: saName, Subjects: []rbacv1.Subject{ { Kind: rbacv1.ServiceAccountKind, @@ -1784,10 +1778,8 @@ func grantNamespaceManagement(ctx context.Context, saName, saNamespace string, v // it permission to manage ExternalArtifacts in the given target namespaces. func createServiceAccountRBAC(ctx context.Context, saName, saNamespace string, targetNamespaces ...string) error { sa := &corev1.ServiceAccount{ - ObjectMeta: metav1.ObjectMeta{ - Name: saName, - Namespace: saNamespace, - }, + Name: saName, + Namespace: saNamespace, } if err := testClient.Create(ctx, sa); err != nil { return err @@ -1805,10 +1797,8 @@ func createServiceAccountRBAC(ctx context.Context, saName, saNamespace string, t // permission to manage ExternalArtifacts in the target namespace. func grantExternalArtifactAccessInNamespace(ctx context.Context, saName, saNamespace, targetNamespace string) error { role := &rbacv1.Role{ - ObjectMeta: metav1.ObjectMeta{ - Name: saName, - Namespace: targetNamespace, - }, + Name: saName, + Namespace: targetNamespace, Rules: []rbacv1.PolicyRule{ { APIGroups: []string{sourcev1.GroupVersion.Group}, @@ -1827,10 +1817,8 @@ func grantExternalArtifactAccessInNamespace(ctx context.Context, saName, saNames } binding := &rbacv1.RoleBinding{ - ObjectMeta: metav1.ObjectMeta{ - Name: saName, - Namespace: targetNamespace, - }, + Name: saName, + Namespace: targetNamespace, Subjects: []rbacv1.Subject{ { Kind: rbacv1.ServiceAccountKind, @@ -1928,7 +1916,7 @@ func TestValidateManagedNamespaces(t *testing.T) { reqs := func(namespaces ...string) []artifactRequest { out := make([]artifactRequest, 0, len(namespaces)) for _, ns := range namespaces { - out = append(out, artifactRequest{OutputArtifact: swapi.OutputArtifact{Name: "app", Namespace: ns}}) + out = append(out, artifactRequest{Name: "app", Namespace: ns}) } return out } @@ -1936,7 +1924,7 @@ func TestValidateManagedNamespaces(t *testing.T) { t.Run("allows a matching namespace", func(t *testing.T) { g := NewWithT(t) r := &ArtifactGeneratorReconciler{} - obj := &swapi.ArtifactGenerator{ObjectMeta: metav1.ObjectMeta{Namespace: "gen"}} + obj := &swapi.ArtifactGenerator{Namespace: "gen"} err := r.validateManagedNamespaces(ctx, obj, reqs("tenant-a"), newPolicies(t, "ns.startsWith('tenant-')")) g.Expect(err).ToNot(HaveOccurred()) }) @@ -1944,7 +1932,7 @@ func TestValidateManagedNamespaces(t *testing.T) { t.Run("rejects a namespace that does not match", func(t *testing.T) { g := NewWithT(t) r := &ArtifactGeneratorReconciler{} - obj := &swapi.ArtifactGenerator{ObjectMeta: metav1.ObjectMeta{Namespace: "gen"}} + obj := &swapi.ArtifactGenerator{Namespace: "gen"} err := r.validateManagedNamespaces(ctx, obj, reqs("othera"), newPolicies(t, "ns.startsWith('tenant-')")) g.Expect(err).To(HaveOccurred()) g.Expect(err.Error()).To(ContainSubstring("allowedNames")) @@ -1954,7 +1942,7 @@ func TestValidateManagedNamespaces(t *testing.T) { for _, denied := range []string{"default", "kube-system", "kube-public", "flux-system"} { g := NewWithT(t) r := &ArtifactGeneratorReconciler{} - obj := &swapi.ArtifactGenerator{ObjectMeta: metav1.ObjectMeta{Namespace: "gen"}} + obj := &swapi.ArtifactGenerator{Namespace: "gen"} err := r.validateManagedNamespaces(ctx, obj, reqs(denied), newPolicies(t, "true")) g.Expect(err).To(HaveOccurred(), denied) } @@ -1963,7 +1951,7 @@ func TestValidateManagedNamespaces(t *testing.T) { t.Run("skips empty and generator namespace", func(t *testing.T) { g := NewWithT(t) r := &ArtifactGeneratorReconciler{} - obj := &swapi.ArtifactGenerator{ObjectMeta: metav1.ObjectMeta{Namespace: "gen"}} + obj := &swapi.ArtifactGenerator{Namespace: "gen"} err := r.validateManagedNamespaces(ctx, obj, reqs("", "gen"), newPolicies(t, "false")) g.Expect(err).ToNot(HaveOccurred()) }) @@ -1972,7 +1960,7 @@ func TestValidateManagedNamespaces(t *testing.T) { func TestValidateNamespaceMetadataPaths(t *testing.T) { newObj := func(path string) *swapi.ArtifactGenerator { return &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{Namespace: "gen"}, + Namespace: "gen", Spec: swapi.ArtifactGeneratorSpec{ ManagedNamespaces: &swapi.ManagedNamespaces{ AllowedNames: []string{"true"}, @@ -1985,14 +1973,14 @@ func TestValidateNamespaceMetadataPaths(t *testing.T) { } req := func(namespace string, captures map[string]string) artifactRequest { return artifactRequest{ - OutputArtifact: swapi.OutputArtifact{Name: "app", Namespace: namespace}, - Captures: captures, + Name: "app", Namespace: namespace, + Captures: captures, } } t.Run("ignores artifacts without sourced metadata", func(t *testing.T) { g := NewWithT(t) - obj := &swapi.ArtifactGenerator{ObjectMeta: metav1.ObjectMeta{Namespace: "gen"}} + obj := &swapi.ArtifactGenerator{Namespace: "gen"} err := validateNamespaceMetadataPaths(obj, []artifactRequest{ req("tenant-a", map[string]string{"module": "a"}), req("tenant-a", map[string]string{"module": "b"}), @@ -2295,11 +2283,9 @@ func TestArtifactGeneratorReconciler_ExternalArtifactAdopted(t *testing.T) { // Pre-create the ExternalArtifact without an owner. unmanagedEA := &sourcev1.ExternalArtifact{ - TypeMeta: metav1.TypeMeta{ - APIVersion: sourcev1.GroupVersion.String(), - Kind: sourcev1.ExternalArtifactKind, - }, - ObjectMeta: metav1.ObjectMeta{Name: eaName, Namespace: tgtNS.Name}, + APIVersion: sourcev1.GroupVersion.String(), + Kind: sourcev1.ExternalArtifactKind, + Name: eaName, Namespace: tgtNS.Name, } g.Expect(testClient.Create(ctx, unmanagedEA)).To(Succeed()) diff --git a/internal/controller/artifactgenerator_pathpattern.go b/internal/controller/artifactgenerator_pathpattern.go index a9c94ec..eb49329 100644 --- a/internal/controller/artifactgenerator_pathpattern.go +++ b/internal/controller/artifactgenerator_pathpattern.go @@ -258,13 +258,13 @@ func renderArtifactRequest(oa swapi.OutputArtifact, normalizedCaptures, rawCaptu return artifactRequest{}, err } + oa.Name = name + oa.Namespace = namespace req := artifactRequest{ OutputArtifact: oa, Labels: normalizedCaptures, Captures: rawCaptures, } - req.Name = name - req.Namespace = namespace // Substitute capture placeholders in the inputs source path. if oa.InputsFrom != "" { diff --git a/internal/controller_test/artifactgenerator_validation_test.go b/internal/controller_test/artifactgenerator_validation_test.go index 51e625e..3129298 100644 --- a/internal/controller_test/artifactgenerator_validation_test.go +++ b/internal/controller_test/artifactgenerator_validation_test.go @@ -22,7 +22,6 @@ import ( "testing" . "github.com/onsi/gomega" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" swapi "github.com/fluxcd/source-watcher/api/v2/v1beta1" ) @@ -37,10 +36,8 @@ func TestArtifactGenerator_CommonMetadataAnnotations(t *testing.T) { newObj := func(name string) *swapi.ArtifactGenerator { return &swapi.ArtifactGenerator{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: ns.Name, - }, + Name: name, + Namespace: ns.Name, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { diff --git a/internal/controller_test/artifactgenerator_watch_test.go b/internal/controller_test/artifactgenerator_watch_test.go index 1943188..c711d09 100644 --- a/internal/controller_test/artifactgenerator_watch_test.go +++ b/internal/controller_test/artifactgenerator_watch_test.go @@ -61,14 +61,10 @@ func TestArtifactGenerator_Watch(t *testing.T) { g.Expect(err).ToNot(HaveOccurred()) obj := &swapi.ArtifactGenerator{ - TypeMeta: metav1.TypeMeta{ - Kind: swapi.ArtifactGeneratorKind, - APIVersion: swapi.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: swapi.ArtifactGeneratorKind, + APIVersion: swapi.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: swapi.ArtifactGeneratorSpec{ Sources: []swapi.SourceReference{ { @@ -183,14 +179,10 @@ func applyOCIRepository(objKey client.ObjectKey, revision string, files []gotkte } repo := &sourcev1.OCIRepository{ - TypeMeta: metav1.TypeMeta{ - Kind: sourcev1.OCIRepositoryKind, - APIVersion: sourcev1.GroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: objKey.Name, - Namespace: objKey.Namespace, - }, + Kind: sourcev1.OCIRepositoryKind, + APIVersion: sourcev1.GroupVersion.String(), + Name: objKey.Name, + Namespace: objKey.Namespace, Spec: sourcev1.OCIRepositorySpec{ URL: "oci://ghcr.io/test/repository", Interval: &metav1.Duration{Duration: time.Minute}, @@ -232,9 +224,7 @@ func applyOCIRepository(objKey client.ObjectKey, revision string, files []gotkte repo.Status = status statusOpts := &client.SubResourcePatchOptions{ - PatchOptions: client.PatchOptions{ - FieldManager: "source-controller", - }, + FieldManager: "source-controller", } return testClient.Status().Patch(context.Background(), repo, client.Apply, statusOpts) From 5dfc8f669aa0863a19d114977dfa30eeae2b4dec Mon Sep 17 00:00:00 2001 From: Matheus Pimenta Date: Sat, 14 Nov 2026 15:16:49 +0000 Subject: [PATCH 2/2] Run go fix on a loop until convergence Signed-off-by: Matheus Pimenta --- Makefile | 28 ++++++++++++++++--- .../artifactgenerator_controller.go | 2 +- .../artifactgenerator_externalfinalizer.go | 2 +- ...rtifactgenerator_externalfinalizer_test.go | 12 +++----- .../controller/artifactgenerator_finalize.go | 9 ++---- .../controller/artifactgenerator_inputs.go | 6 +--- .../artifactgenerator_inputs_test.go | 10 ++----- .../controller/artifactgenerator_namespace.go | 7 ++--- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index e81a7a8..c3ab1cb 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ ENVTEST_ARCH ?= $(LOCALARCH) # API generation utilities CONTROLLER_GEN_VERSION ?= v0.21.0 +GOPLS_VERSION ?= v0.24.0 # Download source-controller CRDs SOURCE_VER ?= $(shell go list -m all | grep github.com/fluxcd/source-controller/api | awk '{print $$2}') @@ -44,7 +45,7 @@ all: manager # Run tests KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)" -test: tidy generate fix fmt vet manifests install-envtest download-crd-deps +test: tidy generate fix fmt vet unused manifests install-envtest download-crd-deps KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... $(GO_TEST_ARGS) -coverprofile cover.out # Build manager binary @@ -115,11 +116,16 @@ tidy: cd api; rm -f go.sum; go mod tidy -compat=1.27 rm -f go.sum; go mod tidy -compat=1.27 -# Run go fix against code +# Run go fix against code until it makes no changes .PHONY: fix fix: - cd api; go fix ./... - go fix ./... + @set -e; while :; do \ + before=$$(git diff --no-ext-diff --no-color HEAD -- '*.go' | cksum); \ + go fix ./...; \ + ( cd api && go fix ./... ); \ + after=$$(git diff --no-ext-diff --no-color HEAD -- '*.go' | cksum); \ + if [ "$$before" = "$$after" ]; then break; fi; \ + done # Run go fmt against code fmt: @@ -129,6 +135,14 @@ fmt: vet: go vet ./... +# Check for unused code and parameters +unused: gopls + @out=$$($(GOPLS) check -severity=info $$(git ls-files '*.go') | grep -E 'unused parameter:| is unused$$' || true); \ + if [ -n "$$out" ]; then \ + printf '%s\n' "$$out"; \ + exit 1; \ + fi + # Generate code generate: generate-api generate-json-schemas @@ -168,6 +182,12 @@ manifests-release: kustomize build config/release-tmp > ./build/source-watcher.deployment.yaml rm -rf config/release-tmp +# Find or download gopls +GOPLS = $(shell pwd)/bin/gopls +.PHONY: gopls +gopls: ## Download gopls locally if necessary. + $(call go-install-tool,$(GOPLS),golang.org/x/tools/gopls@$(GOPLS_VERSION)) + # Find or download controller-gen CONTROLLER_GEN = $(shell pwd)/bin/controller-gen .PHONY: controller-gen diff --git a/internal/controller/artifactgenerator_controller.go b/internal/controller/artifactgenerator_controller.go index 19b189e..c11493f 100644 --- a/internal/controller/artifactgenerator_controller.go +++ b/internal/controller/artifactgenerator_controller.go @@ -353,7 +353,7 @@ func (r *ArtifactGeneratorReconciler) reconcile(ctx context.Context, // Load and parse the exported inputs. The staging directory is // namespaced to match the artifact builder. artifactDir := filepath.Join(tmpDir, namespace, oa.Name) - exportedInputs, err := r.loadExportedInputs(ctx, &oa, localSources, artifactDir) + exportedInputs, err := r.loadExportedInputs(&oa, localSources, artifactDir) if err != nil { msg := fmt.Sprintf("%s inputs failed: %s", oa.Name, err.Error()) gotkconditions.MarkFalse(obj, diff --git a/internal/controller/artifactgenerator_externalfinalizer.go b/internal/controller/artifactgenerator_externalfinalizer.go index 1de21be..f513bdb 100644 --- a/internal/controller/artifactgenerator_externalfinalizer.go +++ b/internal/controller/artifactgenerator_externalfinalizer.go @@ -157,7 +157,7 @@ func (r *ArtifactGeneratorReconciler) namespaceExternallyFinalized(ctx context.C // managed namespace. The annotation must be set by an external actor, so that // the controller cannot be tricked into handing over a namespace it is still // accountable for. -func validateNamespaceMetadataExternalFinalizer(namespace string, labels, annotations map[string]string) error { +func validateNamespaceMetadataExternalFinalizer(namespace string, annotations map[string]string) error { if annotations[swapi.ExternalFinalizerAnnotation] == "" { return nil } diff --git a/internal/controller/artifactgenerator_externalfinalizer_test.go b/internal/controller/artifactgenerator_externalfinalizer_test.go index 0634046..35ae25e 100644 --- a/internal/controller/artifactgenerator_externalfinalizer_test.go +++ b/internal/controller/artifactgenerator_externalfinalizer_test.go @@ -84,19 +84,15 @@ func TestParseExternalFinalizerRef(t *testing.T) { func TestValidateNamespaceMetadataExternalFinalizer(t *testing.T) { g := NewWithT(t) - g.Expect(validateNamespaceMetadataExternalFinalizer("tenant", nil, nil)).To(Succeed()) - g.Expect(validateNamespaceMetadataExternalFinalizer("tenant", nil, map[string]string{"owner": "platform"})).To(Succeed()) + g.Expect(validateNamespaceMetadataExternalFinalizer("tenant", nil)).To(Succeed()) + g.Expect(validateNamespaceMetadataExternalFinalizer("tenant", map[string]string{"owner": "platform"})).To(Succeed()) - err := validateNamespaceMetadataExternalFinalizer("tenant", nil, map[string]string{ - swapi.ExternalFinalizerAnnotation: "fluxcd.controlplane.io/v1/ResourceSet/flux-system/tenants/uid", - }) + err := validateNamespaceMetadataExternalFinalizer("tenant", map[string]string{swapi.ExternalFinalizerAnnotation: "fluxcd.controlplane.io/v1/ResourceSet/flux-system/tenants/uid"}) g.Expect(err).To(HaveOccurred()) g.Expect(err.Error()).To(ContainSubstring(swapi.ExternalFinalizerAnnotation)) // An empty value does not hand over the finalization and is ignored. - g.Expect(validateNamespaceMetadataExternalFinalizer("tenant", nil, map[string]string{ - swapi.ExternalFinalizerAnnotation: "", - })).To(Succeed()) + g.Expect(validateNamespaceMetadataExternalFinalizer("tenant", map[string]string{swapi.ExternalFinalizerAnnotation: ""})).To(Succeed()) } func TestNamespaceExternallyFinalized(t *testing.T) { diff --git a/internal/controller/artifactgenerator_finalize.go b/internal/controller/artifactgenerator_finalize.go index e15661e..62a0399 100644 --- a/internal/controller/artifactgenerator_finalize.go +++ b/internal/controller/artifactgenerator_finalize.go @@ -97,14 +97,14 @@ func (r *ArtifactGeneratorReconciler) finalizeReferences(ctx context.Context, log.Info("Skipping managed namespace deletion, no ServiceAccount configured", "namespace", ref.Name) continue } - if err := r.deleteNamespace(ctx, obj, ref.Name, auth); err != nil { + if err := r.deleteNamespace(ctx, ref.Name, auth); err != nil { log.Error(err, "Failed to delete Namespace, will retry", "namespace", ref.Name) survivors = append(survivors, ref) } continue } - if err := r.finalizeExternalArtifact(ctx, obj, ref, auth); err != nil { + if err := r.finalizeExternalArtifact(ctx, ref, auth); err != nil { log.Error(err, "Failed to delete ExternalArtifact, will retry", "artifact", fmt.Sprintf("%s/%s/%s", sourcev1.ExternalArtifactKind, ref.Namespace, ref.Name)) survivors = append(survivors, ref) @@ -116,10 +116,7 @@ func (r *ArtifactGeneratorReconciler) finalizeReferences(ctx context.Context, // finalizeExternalArtifact deletes the ExternalArtifact referenced in the // provided entry, along with its associated artifact in the storage backend. // It returns an error when the deletion was not confirmed by the API server. -func (r *ArtifactGeneratorReconciler) finalizeExternalArtifact(ctx context.Context, - obj *swapi.ArtifactGenerator, - ref swapi.InventoryEntry, - auth *accessChecker) error { +func (r *ArtifactGeneratorReconciler) finalizeExternalArtifact(ctx context.Context, ref swapi.InventoryEntry, auth *accessChecker) error { log := ctrl.LoggerFrom(ctx) if err := auth.ensureExternalArtifactVerb(ctx, ref.Namespace, "delete", ""); err != nil { diff --git a/internal/controller/artifactgenerator_inputs.go b/internal/controller/artifactgenerator_inputs.go index ed92f34..51bfcce 100644 --- a/internal/controller/artifactgenerator_inputs.go +++ b/internal/controller/artifactgenerator_inputs.go @@ -17,7 +17,6 @@ limitations under the License. package controller import ( - "context" "encoding/json" "errors" "fmt" @@ -44,10 +43,7 @@ var errFileTooLarge = errors.New("file exceeds the maximum size") // loadExportedInputs reads, parses and returns the structured inputs // referenced by .spec.artifacts[].inputsFrom, for publication in the // ExternalArtifact .status.exportedInputs field. -func (r *ArtifactGeneratorReconciler) loadExportedInputs(ctx context.Context, - oa *swapi.OutputArtifact, - sources map[string]string, - artifactDir string) (map[string]*apiextensionsv1.JSON, error) { +func (r *ArtifactGeneratorReconciler) loadExportedInputs(oa *swapi.OutputArtifact, sources map[string]string, artifactDir string) (map[string]*apiextensionsv1.JSON, error) { if oa.InputsFrom == "" { return nil, nil } diff --git a/internal/controller/artifactgenerator_inputs_test.go b/internal/controller/artifactgenerator_inputs_test.go index a937d93..8f19ae9 100644 --- a/internal/controller/artifactgenerator_inputs_test.go +++ b/internal/controller/artifactgenerator_inputs_test.go @@ -72,10 +72,7 @@ func TestLoadExportedInputs(t *testing.T) { t.Run("exports inputs", func(t *testing.T) { g := NewWithT(t) - inputs, err := r.loadExportedInputs(context.Background(), &swapi.OutputArtifact{ - Name: "test", - InputsFrom: "@repo/inputs.yaml", - }, sources, "") + inputs, err := r.loadExportedInputs(&swapi.OutputArtifact{Name: "test", InputsFrom: "@repo/inputs.yaml"}, sources, "") g.Expect(err).ToNot(HaveOccurred()) g.Expect(inputs).To(HaveKey("replicas")) g.Expect(inputs).To(HaveKey("name")) @@ -85,10 +82,7 @@ func TestLoadExportedInputs(t *testing.T) { t.Run("missing file", func(t *testing.T) { g := NewWithT(t) - _, err := r.loadExportedInputs(context.Background(), &swapi.OutputArtifact{ - Name: "test", - InputsFrom: "@repo/missing.yaml", - }, sources, "") + _, err := r.loadExportedInputs(&swapi.OutputArtifact{Name: "test", InputsFrom: "@repo/missing.yaml"}, sources, "") g.Expect(err).To(HaveOccurred()) }) } diff --git a/internal/controller/artifactgenerator_namespace.go b/internal/controller/artifactgenerator_namespace.go index 8b91dba..b2ccd0f 100644 --- a/internal/controller/artifactgenerator_namespace.go +++ b/internal/controller/artifactgenerator_namespace.go @@ -526,7 +526,7 @@ func (r *ArtifactGeneratorReconciler) namespaceMetadata( } labels, annotations := r.namespaceMetadataFor(obj, namespace, sourcedLabels, sourcedAnnotations) - if err := validateNamespaceMetadataExternalFinalizer(namespace, labels, annotations); err != nil { + if err := validateNamespaceMetadataExternalFinalizer(namespace, annotations); err != nil { return nil, nil, newTerminalNamespaceMetadataError("%w", err) } // Validate the resolved metadata before the server-side apply, so that an @@ -840,10 +840,7 @@ func namespaceIfNotPresent(expectedLabels, expectedAnnotations map[string]string // delete exclusion annotations ('prune=disabled', 'reconcile=disabled' or // 'ssa=Ignore'). It returns an error when the deletion was not confirmed by // the API server. -func (r *ArtifactGeneratorReconciler) deleteNamespace(ctx context.Context, - obj *swapi.ArtifactGenerator, - name string, - auth *accessChecker) error { +func (r *ArtifactGeneratorReconciler) deleteNamespace(ctx context.Context, name string, auth *accessChecker) error { log := ctrl.LoggerFrom(ctx) if err := auth.ensureNamespaceVerb(ctx, "get"); err != nil {