From ccd0e86f9247c7f6aefe77b5035e188ee0cef514 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Sun, 24 Jun 2018 15:52:43 +0200 Subject: [PATCH] Add labeling for profiles Fix #679 Signed-off-by: David Gageot --- pkg/skaffold/config/options.go | 11 ++-- pkg/skaffold/config/options_test.go | 79 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 pkg/skaffold/config/options_test.go diff --git a/pkg/skaffold/config/options.go b/pkg/skaffold/config/options.go index 5556042e120..5b7d3e8462d 100644 --- a/pkg/skaffold/config/options.go +++ b/pkg/skaffold/config/options.go @@ -17,7 +17,7 @@ limitations under the License. package config import ( - "fmt" + "strings" ) // SkaffoldOptions are options that are set by command line arguments not included @@ -33,11 +33,16 @@ type SkaffoldOptions struct { // Labels returns a map of labels to be applied to all deployed // k8s objects during the duration of the run func (opts *SkaffoldOptions) Labels() map[string]string { - labels := map[string]string{ - "cleanup": fmt.Sprintf("%t", opts.Cleanup), + labels := map[string]string{} + + if opts.Cleanup { + labels["cleanup"] = "true" } if opts.Namespace != "" { labels["namespace"] = opts.Namespace } + if len(opts.Profiles) > 0 { + labels["profiles"] = strings.Join(opts.Profiles, ",") + } return labels } diff --git a/pkg/skaffold/config/options_test.go b/pkg/skaffold/config/options_test.go new file mode 100644 index 00000000000..6f9e98d6c83 --- /dev/null +++ b/pkg/skaffold/config/options_test.go @@ -0,0 +1,79 @@ +/* +Copyright 2018 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 config + +import ( + "reflect" + "testing" +) + +func TestLabels(t *testing.T) { + tests := []struct { + description string + options SkaffoldOptions + expectedLabels map[string]string + }{ + { + description: "empty", + options: SkaffoldOptions{}, + expectedLabels: map[string]string{}, + }, + { + description: "cleanup", + options: SkaffoldOptions{Cleanup: true}, + expectedLabels: map[string]string{"cleanup": "true"}, + }, + { + description: "namespace", + options: SkaffoldOptions{Namespace: "NS"}, + expectedLabels: map[string]string{"namespace": "NS"}, + }, + { + description: "profile", + options: SkaffoldOptions{Profiles: []string{"profile"}}, + expectedLabels: map[string]string{"profiles": "profile"}, + }, + { + description: "profiles", + options: SkaffoldOptions{Profiles: []string{"profile1", "profile2"}}, + expectedLabels: map[string]string{"profiles": "profile1,profile2"}, + }, + { + description: "all labels", + options: SkaffoldOptions{ + Cleanup: true, + Namespace: "namespace", + Profiles: []string{"p1", "p2"}, + }, + expectedLabels: map[string]string{ + "cleanup": "true", + "namespace": "namespace", + "profiles": "p1,p2", + }, + }, + } + + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + labels := test.options.Labels() + + if !reflect.DeepEqual(test.expectedLabels, labels) { + t.Errorf("Wrong labels. Expected %v. Got %v", test.expectedLabels, labels) + } + }) + } +}