Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

fix(cmd): fail when resource file is not referenced #15

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions pkg/validation/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
})
})
}
3 changes: 2 additions & 1 deletion pkg/validation/resources.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validation

import (
"fmt"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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
}
Loading