Skip to content

Add k8 style managed by label to skaffold deployed pods #2055

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

Merged
merged 2 commits into from
May 3, 2019
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
4 changes: 0 additions & 4 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ var Labels = struct {
Deployer string
Builder string
DockerAPIVersion string
DefaultLabels map[string]string
}{
DefaultLabels: map[string]string{
"deployed-with": "skaffold",
},
TagPolicy: "skaffold-tag-policy",
Deployer: "skaffold-deployer",
Builder: "skaffold-builder",
Expand Down
2 changes: 0 additions & 2 deletions pkg/skaffold/deploy/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/pkg/errors"
Expand Down Expand Up @@ -96,7 +95,6 @@ func labelDeployResults(labels map[string]string, results []Artifact) {
func addLabels(labels map[string]string, accessor metav1.Object) {
kv := make(map[string]string)

copyMap(kv, constants.Labels.DefaultLabels)
copyMap(kv, accessor.GetLabels())
copyMap(kv, labels)

Expand Down
47 changes: 47 additions & 0 deletions pkg/skaffold/runner/labeller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package runner

import (
"fmt"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
)

const (
K8ManagedByLabel = "app.kubernetes.io/managed-by"
)

// DefaultLabeller adds K9 style managed-by label
type DefaultLabeller struct {
version string
}

func NewLabeller(verStr string) *DefaultLabeller {
if verStr == "" {
verStr = version.Get().Version
}
return &DefaultLabeller{
version: verStr,
}
}

func (d *DefaultLabeller) Labels() map[string]string {
return map[string]string{
K8ManagedByLabel: fmt.Sprintf("skaffold-%s", d.version),
}
}
55 changes: 55 additions & 0 deletions pkg/skaffold/runner/labeller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package runner

import (
"fmt"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestDEfaultLabeller(t *testing.T) {
var tests = []struct {
description string
version string
expected string
}{
{
description: "version mentioned",
version: "1.0",
expected: "skaffold-1.0",
},
{
description: "no version",
expected: fmt.Sprintf("skaffold-%s", version.Get().Version),
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
l := &DefaultLabeller{
version: test.version,
}
expected := map[string]string{
"app.kubernetes.io/managed-by": test.expected,
}
testutil.CheckDeepEqual(t, expected, l.Labels())
})
}
}
3 changes: 2 additions & 1 deletion pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldConfig) (*Sk
return nil, errors.Wrap(err, "parsing deploy config")
}

labellers := []deploy.Labeller{opts, builder, deployer, tagger}
defaultLabeller := NewLabeller("")
labellers := []deploy.Labeller{opts, builder, deployer, tagger, defaultLabeller}

builder, tester, deployer = WithTimings(builder, tester, deployer, opts.CacheArtifacts)
if opts.Notification {
Expand Down
7 changes: 4 additions & 3 deletions pkg/skaffold/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
pkgkubernetes "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand Down Expand Up @@ -464,8 +463,10 @@ func fakeCmd(ctx context.Context, p v1.Pod, c v1.Container, files map[string]str

var pod = &v1.Pod{
ObjectMeta: meta_v1.ObjectMeta{
Name: "podname",
Labels: constants.Labels.DefaultLabels,
Name: "podname",
Labels: map[string]string{
"app.kubernetes.io/managed-by": "skaffold-dirty",
},
},
Status: v1.PodStatus{
Phase: v1.PodRunning,
Expand Down