Skip to content

Commit 1e90c82

Browse files
authored
Fix support for local-config annotation (#2275)
1 parent 49b7ce2 commit 1e90c82

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

pkg/live/helpers_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,23 @@ apiVersion: custom.io/v1
100100
kind: Custom
101101
metadata:
102102
name: cr
103+
`
104+
localConfig = `
105+
apiVersion: v1
106+
kind: ConfigMap
107+
metadata:
108+
name: cm
109+
annotations:
110+
config.kubernetes.io/local-config: "true"
111+
data: {}
112+
`
113+
notLocalConfig = `
114+
apiVersion: v1
115+
kind: ConfigMap
116+
metadata:
117+
name: cm
118+
annotations:
119+
config.kubernetes.io/local-config: "false"
120+
data: {}
103121
`
104122
)

pkg/live/rgpath.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1111
"sigs.k8s.io/cli-utils/pkg/manifestreader"
1212
"sigs.k8s.io/kustomize/kyaml/kio"
13+
"sigs.k8s.io/kustomize/kyaml/kio/filters"
1314
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
1415
"sigs.k8s.io/kustomize/kyaml/yaml"
1516
)
@@ -51,7 +52,7 @@ func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured,
5152
if err != nil {
5253
return objs, err
5354
}
54-
if fcPaths.Has(relPath) {
55+
if fcPaths.Has(relPath) && !isExplicitNotLocalConfig(n) {
5556
continue
5657
}
5758

@@ -66,6 +67,7 @@ func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured,
6667
objs = append(objs, u)
6768
}
6869

70+
objs = filterLocalConfig(objs)
6971
err = manifestreader.SetNamespaces(r.Mapper, objs, r.Namespace, r.EnforceNamespace)
7072
return objs, err
7173
}
@@ -100,3 +102,33 @@ func kyamlNodeToUnstructured(n *yaml.RNode) (*unstructured.Unstructured, error)
100102
Object: m,
101103
}, nil
102104
}
105+
106+
const NoLocalConfigAnnoVal = "false"
107+
108+
// isExplicitNotLocalConfig checks whether the resource has been explicitly
109+
// label as NOT being local config. It checks for the config.kubernetes.io/local-config
110+
// annotation with a value of "false".
111+
func isExplicitNotLocalConfig(n *yaml.RNode) bool {
112+
if val, found := n.GetAnnotations()[filters.LocalConfigAnnotation]; found {
113+
if val == NoLocalConfigAnnoVal {
114+
return true
115+
}
116+
}
117+
return false
118+
}
119+
120+
// filterLocalConfig returns a new slice of Unstructured where all resources
121+
// that are designated as local config have been filtered out. It does this
122+
// by looking at the config.kubernetes.io/local-config annotation. Any value
123+
// except "false" is considered to mean the resource is local config.
124+
func filterLocalConfig(objs []*unstructured.Unstructured) []*unstructured.Unstructured {
125+
var filteredObjs []*unstructured.Unstructured
126+
for _, obj := range objs {
127+
annoVal, found := obj.GetAnnotations()[filters.LocalConfigAnnotation]
128+
if found && annoVal != NoLocalConfigAnnoVal {
129+
continue
130+
}
131+
filteredObjs = append(filteredObjs, obj)
132+
}
133+
return filteredObjs
134+
}

pkg/live/rgpath_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ func TestPathManifestReader_Read(t *testing.T) {
9797
},
9898
},
9999
},
100+
"Function config resources which are marked as not being local config remains": {
101+
manifests: map[string]string{
102+
"Kptfile": kptFileWithPipeline,
103+
"deployment-a.yaml": deploymentA,
104+
"cm.yaml": notLocalConfig,
105+
},
106+
namespace: "test-namespace",
107+
expectedObjs: []object.ObjMetadata{
108+
{
109+
GroupKind: schema.GroupKind{
110+
Group: "",
111+
Kind: "ConfigMap",
112+
},
113+
Name: "cm",
114+
Namespace: "test-namespace",
115+
},
116+
{
117+
GroupKind: schema.GroupKind{
118+
Group: "apps",
119+
Kind: "Deployment",
120+
},
121+
Name: "test-deployment",
122+
Namespace: "test-namespace",
123+
},
124+
},
125+
},
100126
"CR and CRD in the same set is ok": {
101127
manifests: map[string]string{
102128
"crd.yaml": crd,
@@ -127,6 +153,23 @@ func TestPathManifestReader_Read(t *testing.T) {
127153
namespace: "test-namespace",
128154
expectedErrMsg: "unknown resource types: Custom.custom.io",
129155
},
156+
"local-config is filtered out": {
157+
manifests: map[string]string{
158+
"deployment-a.yaml": deploymentA,
159+
"lc.yaml": localConfig,
160+
},
161+
namespace: "test-namespace",
162+
expectedObjs: []object.ObjMetadata{
163+
{
164+
GroupKind: schema.GroupKind{
165+
Group: "apps",
166+
Kind: "Deployment",
167+
},
168+
Name: "test-deployment",
169+
Namespace: "test-namespace",
170+
},
171+
},
172+
},
130173
}
131174

132175
for tn, tc := range testCases {

0 commit comments

Comments
 (0)