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

Add labeling for profiles #736

Merged
merged 1 commit into from
Jun 24, 2018
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
11 changes: 8 additions & 3 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
79 changes: 79 additions & 0 deletions pkg/skaffold/config/options_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}