Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for local-config annotation #2275

Merged
merged 1 commit into from
Jun 17, 2021
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
18 changes: 18 additions & 0 deletions pkg/live/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
`
)
34 changes: 33 additions & 1 deletion pkg/live/rgpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
43 changes: 43 additions & 0 deletions pkg/live/rgpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": {
Copy link
Contributor

@frankfarzan frankfarzan Jun 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orthogonal to this: I'm pretty much convinced that declaring packages in Go like this is an anti-pattern. At some point, need to move away from this.

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 {
Expand Down