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

Whitelist recursively transformable kinds. #3833

Merged
merged 1 commit into from
Apr 15, 2020
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
20 changes: 19 additions & 1 deletion pkg/skaffold/deploy/kubectl/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCollectNamespaces(t *testing.T) {
expected []string
}{
{
description: "single manifest in the list",
description: "single Pod manifest in the list",
manifests: ManifestList{[]byte(`
apiVersion: v1
kind: Pod
Expand All @@ -40,6 +40,24 @@ spec:
containers:
- image: gcr.io/k8s-skaffold/example
name: example
`)},
expected: []string{"test"},
}, {
description: "single Service manifest in the list",
manifests: ManifestList{[]byte(`
apiVersion: v1
kind: Service
metadata:
name: getting-started
namespace: test
spec:
type: ClusterIP
ports:
- port: 443
targetPort: 8443
protocol: TCP
selector:
app: getting-started
`)},
expected: []string{"test"},
}, {
Expand Down
13 changes: 12 additions & 1 deletion pkg/skaffold/deploy/kubectl/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import (
"gopkg.in/yaml.v2"
)

// recursivelyTransformableKinds whitelists kinds that can be transformed recursively.
var recursivelyTransformableKinds = map[string]bool{
"Pod": true,
"ReplicaSet": true,
"StatefulSet": true,
"Deployment": true,
"DaemonSet": true,
"Job": true,
"CronJob": true,
}

// FieldVisitor represents the aggregation/transformation that should be performed on each traversed field.
type FieldVisitor interface {
// Visit is called for each transformable key contained in the object and may apply transformations/aggregations on it.
Expand Down Expand Up @@ -58,7 +69,7 @@ func (l *ManifestList) Visit(visitor FieldVisitor) (ManifestList, error) {
// traverseManifest traverses all transformable fields contained within the manifest.
func traverseManifestFields(manifest map[interface{}]interface{}, visitor FieldVisitor) {
kind := manifest["kind"]
if kind != "CustomResourceDefinition" {
if k, ok := kind.(string); ok && recursivelyTransformableKinds[k] {
visitor = &recursiveVisitorDecorator{visitor}
}
visitFields(manifest, visitor)
Expand Down
99 changes: 70 additions & 29 deletions pkg/skaffold/deploy/kubectl/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,47 +69,88 @@ func TestVisit(t *testing.T) {
expected: []string{"test=bar"},
},
{
description: "nested map",
description: "skip nested map",
manifests: ManifestList{[]byte(`nested:
prop: x
test: foo`)},
expected: []string{"test=foo", "nested=map[...", "prop=x"},
expected: []string{"test=foo", "nested=map[..."},
},
{
description: "skip nested map in Role",
manifests: ManifestList{[]byte(`apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: myrole
rules:
- apiGroups:
- ""
resources:
- configmaps
verbs:
- list
- get`)},
expected: []string{"apiVersion=rbac...", "kind=Role", "metadata=map[...", "rules=[map..."},
},
{
description: "nested map in Pod",
manifests: ManifestList{[]byte(`kind: Pod
metadata:
name: mpod
spec:
restartPolicy: Always`)},
expected: []string{"kind=Pod", "metadata=map[...", "name=mpod", "spec=map[...", "restartPolicy=Alwa..."},
},
{
description: "skip recursion at key",
pivotKey: "nested",
manifests: ManifestList{[]byte(`nested:
prop: x
test: foo`)},
expected: []string{"test=foo", "nested=map[..."},
pivotKey: "metadata",
manifests: ManifestList{[]byte(`kind: Pod
metadata:
name: mpod
spec:
restartPolicy: Always`)},
expected: []string{"kind=Pod", "metadata=map[...", "spec=map[...", "restartPolicy=Alwa..."},
},
{
description: "nested array and map",
manifests: ManifestList{[]byte(`items:
- a
- 3
- name: item
value: data
- c
test: foo`)},
expected: []string{"test=foo", "items=[a 3...", "name=item", "value=data"},
description: "nested array and map in Pod",
manifests: ManifestList{[]byte(`kind: Pod
metadata:
name: mpod
spec:
containers:
- env:
name: k
value: v
name: c1
- name: c2
restartPolicy: Always`)},
expected: []string{"kind=Pod", "metadata=map[...", "name=mpod",
"spec=map[...", "containers=[map...",
"name=c1", "env=map[...", "name=k", "value=v",
"name=c2", "restartPolicy=Alwa...",
},
},
{
description: "replace key",
pivotKey: "test",
pivotKey: "name",
replaceWith: "repl",
manifests: ManifestList{[]byte(`nested:
name: item
test:
sub:
name: asdf
test: foo`), []byte(`test: bar`)},
expectedManifests: ManifestList{[]byte(`
nested:
name: item
test: repl
test: repl`), []byte(`test: repl`)},
expected: []string{"nested=map[...", "name=item", "test=map[...", "test=foo", "test=bar"},
manifests: ManifestList{[]byte(`kind: Deployment
metadata:
labels:
name: x
name: app
spec:
replicas: 0`), []byte(`name: foo`)},
// This behaviour is questionable but implemented like this for simplicity.
// In practice this is not a problem (currently) since only the fields
// "metadata" and "image" are matched in known kinds without ambiguous field names.
expectedManifests: ManifestList{[]byte(`kind: Deployment
metadata:
labels:
name: repl
name: repl
spec:
replicas: 0`), []byte(`name: repl`)},
expected: []string{"kind=Depl...", "metadata=map[...", "name=app", "labels=map[...", "name=x", "spec=map[...", "replicas=0", "name=foo"},
},
{
description: "invalid input",
Expand Down