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

[WIP] Add a label to the deployments #569

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions pkg/skaffold/deploy/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func (l *manifestList) replaceImages(builds []build.Build) (manifestList, error)
}

recursiveReplaceImage(m, replacements)
addSkaffoldLabels(m)

updatedManifest, err := yaml.Marshal(m)
if err != nil {
Expand All @@ -320,6 +321,21 @@ func (l *manifestList) replaceImages(builds []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 @@ -214,6 +214,8 @@ func TestReplaceImages(t *testing.T) {
apiVersion: v1
kind: Pod
metadata:
labels:
key: value
name: getting-started
spec:
containers:
Expand All @@ -222,11 +224,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 @@ -241,6 +250,9 @@ spec:
apiVersion: v1
kind: Pod
metadata:
labels:
key: value
skaffold: "true"
name: getting-started
spec:
containers:
Expand All @@ -249,11 +261,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 @@ -312,6 +333,7 @@ kind: Deployment
metadata:
labels:
run: skaffold
skaffold: "true"
name: skaffold
spec:
replicas: 1
Expand Down