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 labels to all k8s objects deployed by skaffold #644

Merged
merged 1 commit into from
Jun 13, 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
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewCmdBuild(out io.Writer) *cobra.Command {

// BuildOutput is the output of `skaffold build`.
type BuildOutput struct {
Builds []build.Build
Builds []build.Artifact
}

func runBuild(out io.Writer, filename string) error {
Expand Down
11 changes: 7 additions & 4 deletions cmd/skaffold/app/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/label"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ func NewCmdDeploy(out io.Writer) *cobra.Command {
func runDeploy(out io.Writer, filename string) error {
ctx := context.Background()

runner, _, err := newRunner(filename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd think runner is short enough.

r, _, err := newRunner(filename)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand All @@ -60,21 +61,23 @@ func runDeploy(out io.Writer, filename string) error {
deployOut = ioutil.Discard
}

var builds []build.Build
var builds []build.Artifact
for _, image := range images {
parsed, err := docker.ParseReference(image)
if err != nil {
return err
}
builds = append(builds, build.Build{
builds = append(builds, build.Artifact{
ImageName: parsed.BaseName,
Tag: image,
})
}

if err := runner.Deploy(ctx, deployOut, builds); err != nil {
dRes, err := r.Deploy(ctx, deployOut, builds)
if err != nil {
return errors.Wrap(err, "deploy step")
}
label.LabelDeployResults(r.Labels(), dRes)

return nil
}
8 changes: 5 additions & 3 deletions pkg/skaffold/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
)

// Build is the result corresponding to each Artifact built.
type Build struct {
// Artifact is the result corresponding to each successful build.
type Artifact struct {
ImageName string
Tag string
}
Expand All @@ -35,5 +35,7 @@ type Build struct {
// This could include pushing to a authorized repository or loading the nodes with the image.
// If artifacts is supplied, the builder should only rebuild those artifacts.
type Builder interface {
Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Build, error)
Labels() map[string]string

Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Artifact, error)
}
14 changes: 10 additions & 4 deletions pkg/skaffold/build/container_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ func NewGoogleCloudBuilder(cfg *v1alpha2.BuildConfig) (*GoogleCloudBuilder, erro
return &GoogleCloudBuilder{cfg}, nil
}

func (cb *GoogleCloudBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Build, error) {
func (cb *GoogleCloudBuilder) Labels() map[string]string {
return map[string]string{
constants.Labels.Builder: "google-cloud-builder",
}
}

func (cb *GoogleCloudBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Artifact, error) {
client, err := google.DefaultClient(ctx, cloudbuild.CloudPlatformScope)
if err != nil {
return nil, errors.Wrap(err, "getting google client")
Expand All @@ -93,7 +99,7 @@ func (cb *GoogleCloudBuilder) Build(ctx context.Context, out io.Writer, tagger t
}
defer c.Close()

var builds []Build
var builds []Artifact
for _, artifact := range artifacts {
build, err := cb.buildArtifact(ctx, out, tagger, cbclient, c, artifact)
if err != nil {
Expand All @@ -105,7 +111,7 @@ func (cb *GoogleCloudBuilder) Build(ctx context.Context, out io.Writer, tagger t
return builds, nil
}

func (cb *GoogleCloudBuilder) buildArtifact(ctx context.Context, out io.Writer, tagger tag.Tagger, cbclient *cloudbuild.Service, c *cstorage.Client, artifact *v1alpha2.Artifact) (*Build, error) {
func (cb *GoogleCloudBuilder) buildArtifact(ctx context.Context, out io.Writer, tagger tag.Tagger, cbclient *cloudbuild.Service, c *cstorage.Client, artifact *v1alpha2.Artifact) (*Artifact, error) {
logrus.Infof("Building artifact: %+v", artifact)

// need to format build args as strings to pass to container builder docker
Expand Down Expand Up @@ -220,7 +226,7 @@ watch:
return nil, errors.Wrap(err, "tagging image")
}

return &Build{
return &Artifact{
ImageName: artifact.ImageName,
Tag: newTag,
}, nil
Expand Down
13 changes: 10 additions & 3 deletions pkg/skaffold/build/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kaniko"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
Expand All @@ -42,7 +43,13 @@ func NewKanikoBuilder(cfg *v1alpha2.BuildConfig) (*KanikoBuilder, error) {
}, nil
}

func (k *KanikoBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Build, error) {
func (k *KanikoBuilder) Labels() map[string]string {
return map[string]string{
constants.Labels.Builder: "kaniko",
}
}

func (k *KanikoBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Artifact, error) {
client, err := kubernetes.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "getting kubernetes client")
Expand Down Expand Up @@ -72,7 +79,7 @@ func (k *KanikoBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tag
}()

// TODO(r2d4): parallel builds
var builds []Build
var builds []Artifact

for _, artifact := range artifacts {
initialTag, err := kaniko.RunKanikoBuild(ctx, out, artifact, k.KanikoBuild)
Expand All @@ -97,7 +104,7 @@ func (k *KanikoBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tag
return nil, errors.Wrap(err, "tagging image")
}

builds = append(builds, Build{
builds = append(builds, Artifact{
ImageName: artifact.ImageName,
Tag: tag,
})
Expand Down
17 changes: 14 additions & 3 deletions pkg/skaffold/build/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func NewLocalBuilder(cfg *v1alpha2.BuildConfig, kubeContext string) (*LocalBuild
return l, nil
}

func (l *LocalBuilder) Labels() map[string]string {
labels := map[string]string{
constants.Labels.Builder: "local",
}
v, err := l.api.ServerVersion(context.Background())
if err == nil {
labels[constants.Labels.DockerAPIVersion] = fmt.Sprintf("%v", v.APIVersion)
}
return labels
}

func (l *LocalBuilder) runBuildForArtifact(ctx context.Context, out io.Writer, artifact *v1alpha2.Artifact) (string, error) {
if artifact.DockerArtifact != nil {
return l.buildDocker(ctx, out, artifact)
Expand All @@ -77,15 +88,15 @@ func (l *LocalBuilder) runBuildForArtifact(ctx context.Context, out io.Writer, a

// Build runs a docker build on the host and tags the resulting image with
// its checksum. It streams build progress to the writer argument.
func (l *LocalBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Build, error) {
func (l *LocalBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Artifact, error) {
if l.localCluster {
if _, err := fmt.Fprintf(out, "Found [%s] context, using local docker daemon.\n", l.kubeContext); err != nil {
return nil, errors.Wrap(err, "writing status")
}
}
defer l.api.Close()

var builds []Build
var builds []Artifact

for _, artifact := range artifacts {
initialTag, err := l.runBuildForArtifact(ctx, out, artifact)
Expand Down Expand Up @@ -119,7 +130,7 @@ func (l *LocalBuilder) Build(ctx context.Context, out io.Writer, tagger tag.Tagg
}
}

builds = append(builds, Build{
builds = append(builds, Artifact{
ImageName: artifact.ImageName,
Tag: tag,
})
Expand Down
10 changes: 7 additions & 3 deletions pkg/skaffold/build/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (f *FakeTagger) GenerateFullyQualifiedImageName(workingDir string, tagOpts
return f.Out, f.Err
}

func (f *FakeTagger) Labels() map[string]string {
return map[string]string{}
}

type testAuthHelper struct{}

func (t testAuthHelper) GetAuthConfig(string) (types.AuthConfig, error) {
Expand Down Expand Up @@ -70,7 +74,7 @@ func TestLocalRun(t *testing.T) {
api docker.APIClient
tagger tag.Tagger
artifacts []*v1alpha2.Artifact
expected []Build
expected []Artifact
localCluster bool
shouldErr bool
}{
Expand All @@ -95,7 +99,7 @@ func TestLocalRun(t *testing.T) {
},
tagger: &tag.ChecksumTagger{},
api: testutil.NewFakeImageAPIClient(map[string]string{}, &testutil.FakeImageAPIOptions{}),
expected: []Build{
expected: []Artifact{
{
ImageName: "gcr.io/test/image",
Tag: "gcr.io/test/image:imageid",
Expand Down Expand Up @@ -139,7 +143,7 @@ func TestLocalRun(t *testing.T) {
},
},
api: testutil.NewFakeImageAPIClient(map[string]string{}, &testutil.FakeImageAPIOptions{}),
expected: []Build{
expected: []Artifact{
{
ImageName: "gcr.io/test/image",
Tag: "gcr.io/test/image:imageid",
Expand Down
8 changes: 8 additions & 0 deletions pkg/skaffold/build/tag/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ package tag

import (
"fmt"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
)

type CustomTag struct {
Tag string
}

func (c *CustomTag) Labels() map[string]string {
return map[string]string{
constants.Labels.TagPolicy: "custom",
}
}

// GenerateFullyQualifiedImageName tags an image with the custom tag
func (c *CustomTag) GenerateFullyQualifiedImageName(workingDir string, opts *Options) (string, error) {
if opts == nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/skaffold/build/tag/date_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package tag
import (
"fmt"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
)

const tagTime = "2006-01-02_15-04-05.999_MST"
Expand All @@ -40,6 +42,12 @@ func NewDateTimeTagger(format, timezone string) Tagger {
}
}

func (tagger *dateTimeTagger) Labels() map[string]string {
return map[string]string{
constants.Labels.TagPolicy: "dateTimeTagger",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be just dateTime to be consistent

}
}

// GenerateFullyQualifiedImageName tags an image with the supplied image name and the current timestamp
func (tagger *dateTimeTagger) GenerateFullyQualifiedImageName(workingDir string, opts *Options) (string, error) {
if opts == nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/skaffold/build/tag/env_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"
"text/template"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
)
Expand All @@ -40,8 +41,14 @@ func NewEnvTemplateTagger(t string) (Tagger, error) {
}, nil
}

func (t *envTemplateTagger) Labels() map[string]string {
return map[string]string{
constants.Labels.TagPolicy: "envTemplateTagger",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be just envTemplate to be consistent

}
}

// GenerateFullyQualifiedImageName tags an image with the custom tag
func (c *envTemplateTagger) GenerateFullyQualifiedImageName(workingDir string, opts *Options) (string, error) {
func (t *envTemplateTagger) GenerateFullyQualifiedImageName(workingDir string, opts *Options) (string, error) {
customMap := map[string]string{}

customMap["IMAGE_NAME"] = opts.ImageName
Expand All @@ -55,5 +62,5 @@ func (c *envTemplateTagger) GenerateFullyQualifiedImageName(workingDir string, o
}
}

return util.ExecuteEnvTemplate(c.Template, customMap)
return util.ExecuteEnvTemplate(t.Template, customMap)
}
8 changes: 8 additions & 0 deletions pkg/skaffold/build/tag/git_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"io"
"sort"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"

"github.com/pkg/errors"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
Expand All @@ -32,6 +34,12 @@ import (
type GitCommit struct {
}

func (c *GitCommit) Labels() map[string]string {
return map[string]string{
constants.Labels.TagPolicy: "git-commit",
}
}

// GenerateFullyQualifiedImageName tags an image with the supplied image name and the git commit.
func (c *GitCommit) GenerateFullyQualifiedImageName(workingDir string, opts *Options) (string, error) {
repo, err := git.PlainOpenWithOptions(workingDir, &git.PlainOpenOptions{DetectDotGit: true})
Expand Down
8 changes: 8 additions & 0 deletions pkg/skaffold/build/tag/sha256.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ package tag
import (
"fmt"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
)

// ChecksumTagger tags an image by the sha256 of the image tarball
type ChecksumTagger struct {
}

func (c *ChecksumTagger) Labels() map[string]string {
return map[string]string{
constants.Labels.TagPolicy: "sha256",
}
}

// GenerateFullyQualifiedImageName tags an image with the supplied image name and the sha256 checksum of the image
func (c *ChecksumTagger) GenerateFullyQualifiedImageName(workingDir string, opts *Options) (string, error) {
if opts == nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/build/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tag
// Tagger is an interface for tag strategies to be implemented against
type Tagger interface {
GenerateFullyQualifiedImageName(workingDir string, tagOpts *Options) (string, error)
Labels() map[string]string
}

type Options struct {
Expand Down
16 changes: 16 additions & 0 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.

package config

import (
"fmt"
)

// SkaffoldOptions are options that are set by command line arguments not included
// in the config file itself
type SkaffoldOptions struct {
Expand All @@ -25,3 +29,15 @@ type SkaffoldOptions struct {
CustomTag string
Namespace string
}

// 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),
}
if opts.Namespace != "" {
labels["namespace"] = opts.Namespace
}
return labels
}
Loading