Skip to content

Commit

Permalink
skaffold deploy -t flag
Browse files Browse the repository at this point in the history
  • Loading branch information
anshlykov committed Sep 13, 2020
1 parent 2582151 commit c75935e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
16 changes: 16 additions & 0 deletions cmd/skaffold/app/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags"
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/tips"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)
Expand Down Expand Up @@ -88,6 +89,21 @@ func getArtifactsToDeploy(out io.Writer, fromFile, fromCLI []build.Artifact, art
deployed = build.MergeWithPreviousBuilds(fromCLI, deployed)
deployed = build.MergeWithPreviousBuilds(fromFile, deployed)

if opts.CustomTag != "" {
for i := range deployed {
artifact := &deployed[i]
if artifact.Tag == "" {
artifact.Tag = artifact.ImageName + ":" + opts.CustomTag
} else {
newTag, err := tag.SetImageTag(artifact.Tag, opts.CustomTag)
if err != nil {
return nil, err
}
artifact.Tag = newTag
}
}
}

// Check that every image has a non empty tag
for _, d := range deployed {
if d.Tag == "" {
Expand Down
21 changes: 21 additions & 0 deletions cmd/skaffold/app/cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestGetDeployedArtifacts(t *testing.T) {
fromFile []build.Artifact
fromCLI []build.Artifact
expected []build.Artifact
customTag string
shouldErr bool
}{
{
Expand Down Expand Up @@ -82,9 +83,29 @@ func TestGetDeployedArtifacts(t *testing.T) {
fromCLI: nil,
shouldErr: true,
},
{
description: "override tag",
artifacts: []*latest.Artifact{{ImageName: "image1"}, {ImageName: "image2"}},
fromFile: []build.Artifact{{ImageName: "image1", Tag: "image1:tag"}},
fromCLI: []build.Artifact{{ImageName: "image2", Tag: "image2:tag"}},
expected: []build.Artifact{{ImageName: "image1", Tag: "image1:test"}, {ImageName: "image2", Tag: "image2:test"}},
customTag: "test",
},
{
description: "override missing tag",
artifacts: []*latest.Artifact{{ImageName: "image1"}, {ImageName: "image2"}},
fromFile: nil,
fromCLI: nil,
expected: []build.Artifact{{ImageName: "image1", Tag: "image1:test"}, {ImageName: "image2", Tag: "image2:test"}},
customTag: "test",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
if test.customTag != "" {
t.Override(&opts.CustomTag, test.customTag)
}

deployed, err := getArtifactsToDeploy(ioutil.Discard, test.fromFile, test.fromCLI, test.artifacts)

t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, deployed)
Expand Down
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var flagRegistry = []Flag{
Value: &opts.CustomTag,
DefValue: "",
FlagAddMethod: "StringVar",
DefinedOn: []string{"build", "debug", "dev", "run"},
DefinedOn: []string{"build", "debug", "dev", "run", "deploy"},
},
{
Name: "minikube-profile",
Expand Down
2 changes: 2 additions & 0 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ Options:
--rpc-port=50051: tcp port to expose event API
--skip-render=false: Don't render the manifests, just deploy them
--status-check=true: Wait for deployed resources to stabilize
-t, --tag='': The optional custom tag to use for images which overrides the current Tagger configuration
--tail=false: Stream logs from deployed objects (true by default for `skaffold dev` and `skaffold debug`)
--toot=false: Emit a terminal beep after the deploy is complete
--wait-for-deletions=true: Wait for pending deletions to complete before a deployment
Expand Down Expand Up @@ -528,6 +529,7 @@ Env vars:
* `SKAFFOLD_RPC_PORT` (same as `--rpc-port`)
* `SKAFFOLD_SKIP_RENDER` (same as `--skip-render`)
* `SKAFFOLD_STATUS_CHECK` (same as `--status-check`)
* `SKAFFOLD_TAG` (same as `--tag`)
* `SKAFFOLD_TAIL` (same as `--tail`)
* `SKAFFOLD_TOOT` (same as `--toot`)
* `SKAFFOLD_WAIT_FOR_DELETIONS` (same as `--wait-for-deletions`)
Expand Down
15 changes: 15 additions & 0 deletions pkg/skaffold/build/tag/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ func StripTags(taggedImages []string) []string {
}
return images
}

func SetImageTag(image, tag string) (string, error) {
parsed, err := docker.ParseReference(image)
if err != nil {
return "", err
}
image = parsed.BaseName
if tag != "" {
image = image + ":" + tag
}
if parsed.Digest != "" {
image = image + "@" + parsed.Digest
}
return image, nil
}
54 changes: 54 additions & 0 deletions pkg/skaffold/build/tag/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,57 @@ func TestStripTags(t *testing.T) {
})
}
}

func TestSetImageTag(t *testing.T) {
tests := []struct {
description string
image string
tag string
expectedImage string
shouldErr bool
}{
{
description: "image with tag",
image: "gcr.io/foo/bar:latest",
tag: "test-1",
expectedImage: "gcr.io/foo/bar:test-1",
},
{
description: "image with tag and digest",
image: "gcr.io/foo/bar:latest@sha256:79e160161fd8190acae2d04d8f296a27a562c8a59732c64ac71c99009a6e89bc",
tag: "test-2",
expectedImage: "gcr.io/foo/bar:test-2@sha256:79e160161fd8190acae2d04d8f296a27a562c8a59732c64ac71c99009a6e89bc",
},
{
description: "image without tag and digest",
image: "gcr.io/foo/bar",
tag: "test-3",
expectedImage: "gcr.io/foo/bar:test-3",
},
{
description: "empty tag",
image: "gcr.io/foo/bar:test-4",
expectedImage: "gcr.io/foo/bar",
},
{
description: "image with digest",
image: "gcr.io/foo/bar@sha256:79e160161fd8190acae2d04d8f296a27a562c8a59732c64ac71c99009a6e89bc",
tag: "test-5",
expectedImage: "gcr.io/foo/bar:test-5@sha256:79e160161fd8190acae2d04d8f296a27a562c8a59732c64ac71c99009a6e89bc",
},
{
description: "invalid reference",
image: "!!invalid!!",
shouldErr: true,
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Parallel()

image, err := SetImageTag(test.image, test.tag)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedImage, image)
})
}
}

0 comments on commit c75935e

Please sign in to comment.