From 0eb8ae0cace824e8436b44bf29af4606f4d771a5 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 4 Feb 2019 11:20:56 +0100 Subject: [PATCH] Ignore proper usage of {{.DIGEST}} and print warning Signed-off-by: David Gageot --- pkg/skaffold/build/tag/env_template.go | 51 ++++++++++---- pkg/skaffold/build/tag/env_template_test.go | 77 +++++++++++++++------ 2 files changed, 93 insertions(+), 35 deletions(-) diff --git a/pkg/skaffold/build/tag/env_template.go b/pkg/skaffold/build/tag/env_template.go index 6b9053e03c9..e6b0656eb6f 100644 --- a/pkg/skaffold/build/tag/env_template.go +++ b/pkg/skaffold/build/tag/env_template.go @@ -23,8 +23,23 @@ 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 @@ -32,16 +47,6 @@ type envTemplateTagger struct { // 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") @@ -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 } diff --git a/pkg/skaffold/build/tag/env_template_test.go b/pkg/skaffold/build/tag/env_template_test.go index a48e7c1b66e..fbeac1b1c01 100644 --- a/pkg/skaffold/build/tag/env_template_test.go +++ b/pkg/skaffold/build/tag/env_template_test.go @@ -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", @@ -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 { @@ -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) }) } }