Skip to content

Commit

Permalink
Adding a skaffold:true label
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed May 23, 2018
1 parent f392c62 commit 742a674
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
16 changes: 16 additions & 0 deletions pkg/skaffold/deploy/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func (l *manifestList) replaceImages(b []build.Build) (manifestList, error) {
}

recursiveReplaceImage(m, replacements)
addSkaffoldLabels(m)

updatedManifest, err := yaml.Marshal(m)
if err != nil {
Expand All @@ -318,6 +319,21 @@ func (l *manifestList) replaceImages(b []build.Build) (manifestList, error) {
return updatedManifests, nil
}

func addSkaffoldLabels(m map[interface{}]interface{}) {
metadata, ok := m["metadata"].(map[interface{}]interface{})
if !ok {
return
}

if metadata["labels"] == nil {
metadata["labels"] = make(map[interface{}]interface{})
}

if labels, ok := metadata["labels"].(map[interface{}]interface{}); ok {
labels["skaffold"] = "true"
}
}

func recursiveReplaceImage(i interface{}, replacements map[string]*replacement) {
switch t := i.(type) {
case []interface{}:
Expand Down
42 changes: 32 additions & 10 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func TestReplaceImages(t *testing.T) {
apiVersion: v1
kind: Pod
metadata:
labels:
key: value
name: getting-started
spec:
containers:
Expand All @@ -231,11 +233,18 @@ spec:
- image: gcr.io/k8s-skaffold/example:latest
name: latest
- image: gcr.io/k8s-skaffold/example:v1
name: fully-qualified
- image: skaffold/other
name: other
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883
name: digest
name: fully-qualified`), []byte(`
apiVersion: v1
kind: Deployment
metadata:
name: deployment
template:
spec:
containers:
- image: skaffold/other
name: other
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883
name: digest
`)}

builds := []build.Build{{
Expand All @@ -250,6 +259,9 @@ spec:
apiVersion: v1
kind: Pod
metadata:
labels:
key: value
skaffold: "true"
name: getting-started
spec:
containers:
Expand All @@ -258,11 +270,20 @@ spec:
- image: gcr.io/k8s-skaffold/example:TAG
name: latest
- image: gcr.io/k8s-skaffold/example:v1
name: fully-qualified
- image: skaffold/other:OTHER_TAG
name: other
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883
name: digest
name: fully-qualified`), []byte(`
apiVersion: v1
kind: Deployment
metadata:
labels:
skaffold: "true"
name: deployment
template:
spec:
containers:
- image: skaffold/other:OTHER_TAG
name: other
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883
name: digest
`)}

resultManifest, err := manifests.replaceImages(builds)
Expand Down Expand Up @@ -323,6 +344,7 @@ kind: Deployment
metadata:
labels:
run: skaffold
skaffold: "true"
name: skaffold
spec:
replicas: 1
Expand Down

0 comments on commit 742a674

Please sign in to comment.