From 652481cdf30544bbe9c3d39313855cbc2d24b30c Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 26 Apr 2019 08:12:20 -0400 Subject: [PATCH 1/2] Always refresh partial versions They currently follow normal refresh rules which can be too long in some cases --- config.go | 5 +++++ main.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 11e09e2f..3370daa8 100644 --- a/config.go +++ b/config.go @@ -284,6 +284,11 @@ func (config *TGFConfig) Validate() (errors []error) { return } +// IsPartialVersion returns true if the given version is partial (x.x instead of semver's x.x.x) +func (config *TGFConfig) IsPartialVersion() bool { + return config.ImageVersion != nil && reVersion.MatchString(*config.ImageVersion) && strings.Count(*config.ImageVersion, ".") == 1 +} + // GetImageName returns the actual image name func (config *TGFConfig) GetImageName() string { var suffix string diff --git a/main.go b/main.go index 5ef0a395..da0b2939 100644 --- a/main.go +++ b/main.go @@ -244,7 +244,7 @@ func main() { } imageName := config.GetImageName() - if lastRefresh(imageName) > config.Refresh || !checkImage(imageName) || refresh { + if lastRefresh(imageName) > config.Refresh || config.IsPartialVersion() || !checkImage(imageName) || refresh { refreshImage(imageName) } From da8ba9839eb8e9e09df54327405588edaf7d38b5 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 26 Apr 2019 09:19:01 -0400 Subject: [PATCH 2/2] Exclude bad versions like `hello 2.1` from the IsPartialVersion test --- config.go | 5 ++++- config_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 3370daa8..5e827252 100644 --- a/config.go +++ b/config.go @@ -237,6 +237,7 @@ func (config *TGFConfig) SetDefaultValues(ssmParameterFolder, location, files st } var reVersion = regexp.MustCompile(`(?P\d+\.\d+(?:\.\d+){0,1})`) +var reVersionWithEndMarkers = regexp.MustCompile(`^` + reVersion.String() + `$`) // https://regex101.com/r/ZKt4OP/5 var reImage = regexp.MustCompile(`^(?P.*?)(?::(?:` + reVersion.String() + `(?:(?P[\.-])(?P.+))?|(?P.+)))?$`) @@ -286,7 +287,9 @@ func (config *TGFConfig) Validate() (errors []error) { // IsPartialVersion returns true if the given version is partial (x.x instead of semver's x.x.x) func (config *TGFConfig) IsPartialVersion() bool { - return config.ImageVersion != nil && reVersion.MatchString(*config.ImageVersion) && strings.Count(*config.ImageVersion, ".") == 1 + return config.ImageVersion != nil && + reVersionWithEndMarkers.MatchString(*config.ImageVersion) && + strings.Count(*config.ImageVersion, ".") == 1 } // GetImageName returns the actual image name diff --git a/config_test.go b/config_test.go index e978c011..058bdceb 100644 --- a/config_test.go +++ b/config_test.go @@ -207,6 +207,56 @@ func TestParseAliases(t *testing.T) { } } +func TestIsPartialVersion(t *testing.T) { + tests := []struct { + name string + version *string + isPartial bool + }{ + { + "nil version", + nil, + false, + }, + { + "partial", + aws.String("2.1"), + true, + }, + { + "full", + aws.String("2.1.2"), + false, + }, + { + "non-semver", + aws.String("stuff"), + false, + }, + { + "partial-letters", + aws.String("a.b"), + false, + }, + { + "partial with tag (this is not a real version, TGF would give a warning)", + aws.String("2.1-k8s"), + false, + }, + { + "partial with non-semver word", + aws.String("hello 2.1"), + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + config := &TGFConfig{ImageVersion: tt.version} + assert.Equal(t, tt.isPartial, config.IsPartialVersion()) + }) + } +} + func writeSSMConfig(parameterFolder, parameterKey, parameterValue string) { fullParameterKey := fmt.Sprintf("%s/%s", parameterFolder, parameterKey) client := getSSMClient()