Skip to content

Commit

Permalink
Add labeling for profiles
Browse files Browse the repository at this point in the history
Fix GoogleContainerTools#679

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jun 24, 2018
1 parent a808d19 commit ccd0e86
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
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)
}
})
}
}

0 comments on commit ccd0e86

Please sign in to comment.