diff --git a/pkg/live/helpers_test.go b/pkg/live/helpers_test.go index a62b49aa37..30262783d4 100644 --- a/pkg/live/helpers_test.go +++ b/pkg/live/helpers_test.go @@ -100,5 +100,23 @@ apiVersion: custom.io/v1 kind: Custom metadata: name: cr +` + localConfig = ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: cm + annotations: + config.kubernetes.io/local-config: "true" +data: {} +` + notLocalConfig = ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: cm + annotations: + config.kubernetes.io/local-config: "false" +data: {} ` ) diff --git a/pkg/live/rgpath.go b/pkg/live/rgpath.go index 3e439241eb..44b355d479 100644 --- a/pkg/live/rgpath.go +++ b/pkg/live/rgpath.go @@ -10,6 +10,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/kustomize/kyaml/kio" + "sigs.k8s.io/kustomize/kyaml/kio/filters" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" "sigs.k8s.io/kustomize/kyaml/yaml" ) @@ -51,7 +52,7 @@ func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured, if err != nil { return objs, err } - if fcPaths.Has(relPath) { + if fcPaths.Has(relPath) && !isExplicitNotLocalConfig(n) { continue } @@ -66,6 +67,7 @@ func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured, objs = append(objs, u) } + objs = filterLocalConfig(objs) err = manifestreader.SetNamespaces(r.Mapper, objs, r.Namespace, r.EnforceNamespace) return objs, err } @@ -100,3 +102,33 @@ func kyamlNodeToUnstructured(n *yaml.RNode) (*unstructured.Unstructured, error) Object: m, }, nil } + +const NoLocalConfigAnnoVal = "false" + +// isExplicitNotLocalConfig checks whether the resource has been explicitly +// label as NOT being local config. It checks for the config.kubernetes.io/local-config +// annotation with a value of "false". +func isExplicitNotLocalConfig(n *yaml.RNode) bool { + if val, found := n.GetAnnotations()[filters.LocalConfigAnnotation]; found { + if val == NoLocalConfigAnnoVal { + return true + } + } + return false +} + +// filterLocalConfig returns a new slice of Unstructured where all resources +// that are designated as local config have been filtered out. It does this +// by looking at the config.kubernetes.io/local-config annotation. Any value +// except "false" is considered to mean the resource is local config. +func filterLocalConfig(objs []*unstructured.Unstructured) []*unstructured.Unstructured { + var filteredObjs []*unstructured.Unstructured + for _, obj := range objs { + annoVal, found := obj.GetAnnotations()[filters.LocalConfigAnnotation] + if found && annoVal != NoLocalConfigAnnoVal { + continue + } + filteredObjs = append(filteredObjs, obj) + } + return filteredObjs +} diff --git a/pkg/live/rgpath_test.go b/pkg/live/rgpath_test.go index f7a287e5cb..2f453b7aaf 100644 --- a/pkg/live/rgpath_test.go +++ b/pkg/live/rgpath_test.go @@ -97,6 +97,32 @@ func TestPathManifestReader_Read(t *testing.T) { }, }, }, + "Function config resources which are marked as not being local config remains": { + manifests: map[string]string{ + "Kptfile": kptFileWithPipeline, + "deployment-a.yaml": deploymentA, + "cm.yaml": notLocalConfig, + }, + namespace: "test-namespace", + expectedObjs: []object.ObjMetadata{ + { + GroupKind: schema.GroupKind{ + Group: "", + Kind: "ConfigMap", + }, + Name: "cm", + Namespace: "test-namespace", + }, + { + GroupKind: schema.GroupKind{ + Group: "apps", + Kind: "Deployment", + }, + Name: "test-deployment", + Namespace: "test-namespace", + }, + }, + }, "CR and CRD in the same set is ok": { manifests: map[string]string{ "crd.yaml": crd, @@ -127,6 +153,23 @@ func TestPathManifestReader_Read(t *testing.T) { namespace: "test-namespace", expectedErrMsg: "unknown resource types: Custom.custom.io", }, + "local-config is filtered out": { + manifests: map[string]string{ + "deployment-a.yaml": deploymentA, + "lc.yaml": localConfig, + }, + namespace: "test-namespace", + expectedObjs: []object.ObjMetadata{ + { + GroupKind: schema.GroupKind{ + Group: "apps", + Kind: "Deployment", + }, + Name: "test-deployment", + Namespace: "test-namespace", + }, + }, + }, } for tn, tc := range testCases {