diff --git a/config.go b/config.go index 11e09e2f..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.+)))?$`) @@ -284,6 +285,13 @@ 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 && + reVersionWithEndMarkers.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/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() 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) }