Skip to content

Commit

Permalink
Ignore proper usage of {{.DIGEST}} and print warning
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Feb 5, 2019
1 parent 6ebfacc commit 0eb8ae0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 35 deletions.
51 changes: 39 additions & 12 deletions pkg/skaffold/build/tag/env_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,30 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// for testing
var warner Warner = &logrusWarner{}

// Warner shows warnings.
type Warner interface {
Warnf(format string, args ...interface{})
}

type logrusWarner struct{}

func (l *logrusWarner) Warnf(format string, args ...interface{}) {
logrus.Warnf(format, args...)
}

// envTemplateTagger implements Tagger
type envTemplateTagger struct {
Template *template.Template
}

// NewEnvTemplateTagger creates a new envTemplateTagger
func NewEnvTemplateTagger(t string) (Tagger, error) {
if strings.Contains(t, "{{.DIGEST}}") {
return nil, errors.New("{{.DIGEST}} is deprecated, image digest will automatically be apppended to image tags")
}
if strings.Contains(t, "{{.DIGEST_ALGO}}") {
return nil, errors.New("{{.DIGEST_ALGO}} is deprecated, image digest will automatically be apppended to image tags")
}
if strings.Contains(t, "{{.DIGEST_HEX}}") {
return nil, errors.New("{{.DIGEST_HEX}} is deprecated, image digest will automatically be apppended to image tags")
}

tmpl, err := util.ParseEnvTemplate(t)
if err != nil {
return nil, errors.Wrap(err, "parsing template")
Expand All @@ -60,7 +65,29 @@ func (t *envTemplateTagger) Labels() map[string]string {

// GenerateFullyQualifiedImageName tags an image with the custom tag
func (t *envTemplateTagger) GenerateFullyQualifiedImageName(workingDir, imageName string) (string, error) {
return util.ExecuteEnvTemplate(t.Template, map[string]string{
"IMAGE_NAME": imageName,
tag, err := util.ExecuteEnvTemplate(t.Template, map[string]string{
"IMAGE_NAME": imageName,
"DIGEST": "[DEPRECATED_DIGEST]",
"DIGEST_ALGO": "[DEPRECATED_DIGEST_ALGO]",
"DIGEST_HEX": "[DEPRECATED_DIGEST_HEX]",
})
if err != nil {
return "", err
}

if strings.Contains(tag, "[DEPRECATED_DIGEST]") ||
strings.Contains(tag, "[DEPRECATED_DIGEST_ALGO]") ||
strings.Contains(tag, "[DEPRECATED_DIGEST_HEX]") {
warner.Warnf("{{.DIGEST}}, {{.DIGEST_ALGO}} and {{.DIGEST_HEX}} are deprecated, image digest will now automatically be apppended to image tags")

switch {
case strings.HasSuffix(tag, "@[DEPRECATED_DIGEST]"):
tag = strings.TrimSuffix(tag, "@[DEPRECATED_DIGEST]")

case strings.HasSuffix(tag, "@[DEPRECATED_DIGEST_ALGO]:[DEPRECATED_DIGEST_HEX]"):
tag = strings.TrimSuffix(tag, "@[DEPRECATED_DIGEST_ALGO]:[DEPRECATED_DIGEST_HEX]")
}
}

return tag, nil
}
77 changes: 54 additions & 23 deletions pkg/skaffold/build/tag/env_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ limitations under the License.
package tag

import (
"fmt"
"sort"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
)

type fakeWarner struct {
warnings []string
}

func (l *fakeWarner) Warnf(format string, args ...interface{}) {
l.warnings = append(l.warnings, fmt.Sprintf(format, args...))
sort.Strings(l.warnings)
}

func TestEnvTemplateTagger_GenerateFullyQualifiedImageName(t *testing.T) {
tests := []struct {
name string
template string
imageName string
env []string
expected string
shoudErr bool
name string
template string
imageName string
env []string
expected string
expectedWarnings []string
}{
{
name: "empty env",
Expand All @@ -53,22 +64,39 @@ func TestEnvTemplateTagger_GenerateFullyQualifiedImageName(t *testing.T) {
expected: "image_name-FOO:latest",
},
{
name: "digest is not supported anymore",
template: "{{.IMAGE_NAME}}@{{.DIGEST}}",
imageName: "foo",
shoudErr: true,
name: "ignore @{{.DIGEST}} suffix",
template: "{{.IMAGE_NAME}}:tag@{{.DIGEST}}",
imageName: "foo",
expected: "foo:tag",
expectedWarnings: []string{"{{.DIGEST}}, {{.DIGEST_ALGO}} and {{.DIGEST_HEX}} are deprecated, image digest will now automatically be apppended to image tags"},
},
{
name: "digest algo is not supported anymore",
template: "{{.IMAGE_NAME}}@{{.DIGEST_ALGO}}",
imageName: "foo",
shoudErr: true,
name: "ignore @{{.DIGEST_ALGO}}:{{.DIGEST_HEX}} suffix",
template: "{{.IMAGE_NAME}}:tag@{{.DIGEST_ALGO}}:{{.DIGEST_HEX}}",
imageName: "image_name",
expected: "image_name:tag",
expectedWarnings: []string{"{{.DIGEST}}, {{.DIGEST_ALGO}} and {{.DIGEST_HEX}} are deprecated, image digest will now automatically be apppended to image tags"},
},
{
name: "digest hex is not supported anymore",
template: "{{.IMAGE_NAME}}@{{.DIGEST_HEX}}",
imageName: "foo",
shoudErr: true,
name: "digest is deprecated",
template: "{{.IMAGE_NAME}}:{{.DIGEST}}",
imageName: "foo",
expected: "foo:[DEPRECATED_DIGEST]",
expectedWarnings: []string{"{{.DIGEST}}, {{.DIGEST_ALGO}} and {{.DIGEST_HEX}} are deprecated, image digest will now automatically be apppended to image tags"},
},
{
name: "digest algo is deprecated",
template: "{{.IMAGE_NAME}}:{{.DIGEST_ALGO}}",
imageName: "foo",
expected: "foo:[DEPRECATED_DIGEST_ALGO]",
expectedWarnings: []string{"{{.DIGEST}}, {{.DIGEST_ALGO}} and {{.DIGEST_HEX}} are deprecated, image digest will now automatically be apppended to image tags"},
},
{
name: "digest hex is deprecated",
template: "{{.IMAGE_NAME}}:{{.DIGEST_HEX}}",
imageName: "foo",
expected: "foo:[DEPRECATED_DIGEST_HEX]",
expectedWarnings: []string{"{{.DIGEST}}, {{.DIGEST_ALGO}} and {{.DIGEST_HEX}} are deprecated, image digest will now automatically be apppended to image tags"},
},
}
for _, test := range tests {
Expand All @@ -77,14 +105,17 @@ func TestEnvTemplateTagger_GenerateFullyQualifiedImageName(t *testing.T) {
return test.env
}

defer func(w Warner) { warner = w }(warner)
fakeWarner := &fakeWarner{}
warner = fakeWarner

c, err := NewEnvTemplateTagger(test.template)
testutil.CheckError(t, test.shoudErr, err)
testutil.CheckError(t, false, err)

if !test.shoudErr {
got, err := c.GenerateFullyQualifiedImageName("", test.imageName)
got, err := c.GenerateFullyQualifiedImageName("", test.imageName)

testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, got)
}
testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, got)
testutil.CheckDeepEqual(t, test.expectedWarnings, fakeWarner.warnings)
})
}
}
Expand Down

0 comments on commit 0eb8ae0

Please sign in to comment.