From c3a00993d01d4aaafb297030642f836517d6037b Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Thu, 19 Oct 2017 10:45:48 +0900 Subject: [PATCH] Don't truncate and replace with 'a', update docs --- builder/googlecompute/template_funcs.go | 14 ++------------ builder/googlecompute/template_funcs_test.go | 12 ------------ website/source/docs/templates/engine.html.md | 9 ++++++--- 3 files changed, 8 insertions(+), 27 deletions(-) diff --git a/builder/googlecompute/template_funcs.go b/builder/googlecompute/template_funcs.go index f33118b5c..1e0c654c2 100644 --- a/builder/googlecompute/template_funcs.go +++ b/builder/googlecompute/template_funcs.go @@ -16,17 +16,13 @@ func isalphanumeric(b byte) bool { } // Clean up image name by replacing invalid characters with "-" -// truncate up to 63 length, convert to a lower case +// and converting upper cases to lower cases func templateCleanImageName(s string) string { if reImageFamily.MatchString(s) { return s } b := []byte(strings.ToLower(s)) - l := 63 - if len(b) < 63 { - l = len(b) - } - newb := make([]byte, l) + newb := make([]byte, len(b)) for i := range newb { if isalphanumeric(b[i]) { newb[i] = b[i] @@ -34,12 +30,6 @@ func templateCleanImageName(s string) string { newb[i] = '-' } } - if !('a' <= newb[0] && newb[0] <= 'z') { - newb[0] = 'a' - } - if newb[l-1] == '-' { - newb[l-1] = 'a' - } return string(newb) } diff --git a/builder/googlecompute/template_funcs_test.go b/builder/googlecompute/template_funcs_test.go index 887af5883..62c57309e 100644 --- a/builder/googlecompute/template_funcs_test.go +++ b/builder/googlecompute/template_funcs_test.go @@ -19,18 +19,6 @@ func Test_templateCleanImageName(t *testing.T) { origName: "abcde-012345v1.0.0", expected: "abcde-012345v1-0-0", }, - { - origName: "a123456789012345678901234567890123456789012345678901234567890123456789", - expected: "a12345678901234567890123456789012345678901234567890123456789012", - }, - { - origName: "01234567890123456789012345678901234567890123456789012345678901.", - expected: "a1234567890123456789012345678901234567890123456789012345678901a", - }, - { - origName: "01234567890123456789012345678901234567890123456789012345678901-", - expected: "a1234567890123456789012345678901234567890123456789012345678901a", - }, } for _, v := range vals { diff --git a/website/source/docs/templates/engine.html.md b/website/source/docs/templates/engine.html.md index 7dcc36b4a..c4fa72e29 100644 --- a/website/source/docs/templates/engine.html.md +++ b/website/source/docs/templates/engine.html.md @@ -53,9 +53,12 @@ Here is a full list of the available functions for reference. #### Specific to Google Compute builders: - `clean_image_name` - GCE image names can only contain certain characters and - the maximum length is 63. This function will replace illegal characters with a "-" character - and truncate a name which exceeds maximum length. - Example usage since ":" is not a legal image name is: `{{isotime | clean_image_name}}`. + the maximum length is 63. This function will convert upper cases to lower cases + and replace illegal characters with a "-" character. + Example usage since ":" is not a legal image name is: `"a-{{isotime | clean_image_name}}"` -> `a-2017-10-18t02-06-30z`. + + Note that image name must be a match of regex `(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)` + but this function won't truncate and replace with valid character such as 'a' except '-'. ## Template variables