From 106728bcd3963bb99ea796c65ddffd77d22bce21 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Thu, 30 May 2024 11:49:21 +0200 Subject: [PATCH] fix(cmd): fail when resource file is not referenced a warning message is not enough, the GH action would succeed and we would miss the problem Signed-off-by: Xavier Coulon --- pkg/validation/components_test.go | 29 ++++++++++++++++------------- pkg/validation/resources.go | 3 ++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pkg/validation/components_test.go b/pkg/validation/components_test.go index 9649352..a2baa53 100644 --- a/pkg/validation/components_test.go +++ b/pkg/validation/components_test.go @@ -304,13 +304,24 @@ setRoleBindingSubjects: defaultOnly err := afs.Mkdir("/path/to/components", os.ModeDir) require.NoError(t, err) err = addFile(afs, "/path/to/components/kustomization.yaml", `kind: Kustomization -apiVersion: kustomize.config.k8s.io/v1beta1`) +apiVersion: kustomize.config.k8s.io/v1beta1 + +resources: + - configmap1.yaml`) require.NoError(t, err) - err = addFile(afs, "/path/to/components/configmap.yaml", `apiVersion: v1 + err = addFile(afs, "/path/to/components/configmap1.yaml", `apiVersion: v1 kind: ConfigMap metadata: namespace: test - name: secret + name: config1 +data: + cookie: yummy`) + require.NoError(t, err) + err = addFile(afs, "/path/to/components/configmap2.yaml", `apiVersion: v1 +kind: ConfigMap +metadata: + namespace: test + name: config2 data: cookie: yummy`) require.NoError(t, err) @@ -319,17 +330,9 @@ data: err = validation.CheckComponents(logger, afs, "/path/to", "components") // then - require.Error(t, err, "invalid resources at /path/to/components: kustomization.yaml is empty") + require.Error(t, err, "resource is not referenced: /path/to/components/configmap2.yaml") assert.Empty(t, logger.Errors()) - assert.Contains(t, logger.Warnings(), LogRecord{ - Msg: "resource is not referenced", - KeyVals: []interface{}{ - "path", - "/path/to/components/kustomization.yaml", - "resource", - "configmap.yaml", - }, - }) + assert.Empty(t, logger.Warnings()) }) }) } diff --git a/pkg/validation/resources.go b/pkg/validation/resources.go index d552e09..7666fae 100644 --- a/pkg/validation/resources.go +++ b/pkg/validation/resources.go @@ -1,6 +1,7 @@ package validation import ( + "fmt" "path/filepath" "strings" @@ -85,7 +86,7 @@ entries: continue entries } } - logger.Warn("resource is not referenced", "path", path, "resource", e.Name()) + return fmt.Errorf("resource is not referenced: %s", filepath.Join(path, e.Name())) } return nil }