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 K8sManagedBy function to labeller #2270

Merged
merged 2 commits into from
Jun 19, 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
23 changes: 15 additions & 8 deletions pkg/skaffold/runner/labeller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
)

const (
K8ManagedByLabel = "app.kubernetes.io/managed-by"
UnknownVersion = "unknown"
Empty = ""
K8ManagedByLabelKey = "app.kubernetes.io/managed-by"
UnknownVersion = "unknown"
Empty = ""
)

// DefaultLabeller adds K9 style managed-by label
Expand All @@ -37,17 +37,24 @@ func NewLabeller(verStr string) *DefaultLabeller {
if verStr == Empty {
verStr = version.Get().Version
}
if verStr == Empty {
verStr = UnknownVersion
}
return &DefaultLabeller{
version: verStr,
}
}

func (d *DefaultLabeller) Labels() map[string]string {
version := d.version
if version == Empty {
version = UnknownVersion
}
return map[string]string{
K8ManagedByLabel: fmt.Sprintf("skaffold-%s", version),
K8ManagedByLabelKey: d.skaffoldVersion(),
}
}

func (d *DefaultLabeller) K8sManagedByLabelKeyValueString() string {
return fmt.Sprintf("%s=%s", K8ManagedByLabelKey, d.skaffoldVersion())
}

func (d *DefaultLabeller) skaffoldVersion() string {
return fmt.Sprintf("skaffold-%s", d.version)
}
18 changes: 13 additions & 5 deletions pkg/skaffold/runner/labeller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package runner

import (
"fmt"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
Expand All @@ -36,18 +35,27 @@ func TestDefaultLabeller(t *testing.T) {
},
{
description: "empty version should add postfix unknown",
expected: fmt.Sprintf("skaffold-unknown"),
expected: "skaffold-unknown",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
l := &DefaultLabeller{
version: test.version,
}
l := NewLabeller(test.version)
labels := l.Labels()

expected := map[string]string{"app.kubernetes.io/managed-by": test.expected}
t.CheckDeepEqual(expected, labels)
})
}
}

func TestK8sManagedByLabelKeyValueString(t *testing.T) {
defaultLabeller := &DefaultLabeller{
version: "version",
}
expected := "app.kubernetes.io/managed-by=skaffold-version"
actual := defaultLabeller.K8sManagedByLabelKeyValueString()
if actual != expected {
t.Fatalf("actual label not equal to expected label. Actual: \n %s \n Expected: \n %s", actual, expected)
}
}
2 changes: 2 additions & 0 deletions pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type SkaffoldRunner struct {
cache *cache.Cache
runCtx *runcontext.RunContext
labellers []deploy.Labeller
defaultLabeller *DefaultLabeller
builds []build.Artifact
hasBuilt bool
hasDeployed bool
Expand Down Expand Up @@ -118,6 +119,7 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldConfig) (*Sk
Syncer: kubectl.NewSyncer(runCtx.Namespaces),
Watcher: watch.NewWatcher(trigger),
labellers: labellers,
defaultLabeller: defaultLabeller,
imageList: kubernetes.NewImageList(),
cache: artifactCache,
runCtx: runCtx,
Expand Down