Skip to content

Commit 25f1b66

Browse files
authored
Merge pull request #2055 from tejal29/add_labels
Add k8 style managed by label to skaffold deployed pods
2 parents 2e65f9e + d0aed16 commit 25f1b66

File tree

6 files changed

+108
-10
lines changed

6 files changed

+108
-10
lines changed

pkg/skaffold/constants/constants.go

-4
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ var Labels = struct {
8484
Deployer string
8585
Builder string
8686
DockerAPIVersion string
87-
DefaultLabels map[string]string
8887
}{
89-
DefaultLabels: map[string]string{
90-
"deployed-with": "skaffold",
91-
},
9288
TagPolicy: "skaffold-tag-policy",
9389
Deployer: "skaffold-deployer",
9490
Builder: "skaffold-builder",

pkg/skaffold/deploy/labels.go

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"strings"
2323
"time"
2424

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

99-
copyMap(kv, constants.Labels.DefaultLabels)
10098
copyMap(kv, accessor.GetLabels())
10199
copyMap(kv, labels)
102100

pkg/skaffold/runner/labeller.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2019 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package runner
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
23+
)
24+
25+
const (
26+
K8ManagedByLabel = "app.kubernetes.io/managed-by"
27+
)
28+
29+
// DefaultLabeller adds K9 style managed-by label
30+
type DefaultLabeller struct {
31+
version string
32+
}
33+
34+
func NewLabeller(verStr string) *DefaultLabeller {
35+
if verStr == "" {
36+
verStr = version.Get().Version
37+
}
38+
return &DefaultLabeller{
39+
version: verStr,
40+
}
41+
}
42+
43+
func (d *DefaultLabeller) Labels() map[string]string {
44+
return map[string]string{
45+
K8ManagedByLabel: fmt.Sprintf("skaffold-%s", d.version),
46+
}
47+
}

pkg/skaffold/runner/labeller_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2019 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package runner
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
24+
"github.com/GoogleContainerTools/skaffold/testutil"
25+
)
26+
27+
func TestDEfaultLabeller(t *testing.T) {
28+
var tests = []struct {
29+
description string
30+
version string
31+
expected string
32+
}{
33+
{
34+
description: "version mentioned",
35+
version: "1.0",
36+
expected: "skaffold-1.0",
37+
},
38+
{
39+
description: "no version",
40+
expected: fmt.Sprintf("skaffold-%s", version.Get().Version),
41+
},
42+
}
43+
44+
for _, test := range tests {
45+
t.Run(test.description, func(t *testing.T) {
46+
l := &DefaultLabeller{
47+
version: test.version,
48+
}
49+
expected := map[string]string{
50+
"app.kubernetes.io/managed-by": test.expected,
51+
}
52+
testutil.CheckDeepEqual(t, expected, l.Labels())
53+
})
54+
}
55+
}

pkg/skaffold/runner/runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldConfig) (*Sk
9191
return nil, errors.Wrap(err, "parsing deploy config")
9292
}
9393

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

9697
builder, tester, deployer = WithTimings(builder, tester, deployer, opts.CacheArtifacts)
9798
if opts.Notification {

pkg/skaffold/sync/sync_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"testing"
2525

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

465464
var pod = &v1.Pod{
466465
ObjectMeta: meta_v1.ObjectMeta{
467-
Name: "podname",
468-
Labels: constants.Labels.DefaultLabels,
466+
Name: "podname",
467+
Labels: map[string]string{
468+
"app.kubernetes.io/managed-by": "skaffold-dirty",
469+
},
469470
},
470471
Status: v1.PodStatus{
471472
Phase: v1.PodRunning,

0 commit comments

Comments
 (0)