diff --git a/util/version/version.go b/util/version/version.go index 6e9e110b..0c4dd9ed 100644 --- a/util/version/version.go +++ b/util/version/version.go @@ -3,10 +3,13 @@ package version import ( "errors" "fmt" + "sort" "strings" "github.com/Masterminds/semver" "github.com/rusenask/keel/types" + + log "github.com/Sirupsen/logrus" ) // ErrVersionTagMissing - tag missing error @@ -60,6 +63,47 @@ func GetImageNameAndVersion(name string) (string, *types.Version, error) { return "", nil, ErrVersionTagMissing } +// NewAvailable - takes version and current tags. Checks whether there is a new version in the list of tags +// and returns it as well as newAvailable bool +func NewAvailable(current string, tags []string) (newVersion string, newAvailable bool, err error) { + + currentVersion, err := semver.NewVersion(current) + if err != nil { + return "", false, err + } + + if len(tags) == 0 { + return "", false, nil + } + + var vs []*semver.Version + for _, r := range tags { + v, err := semver.NewVersion(r) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "tag": r, + }).Error("failed to parse tag") + continue + + } + + vs = append(vs, v) + } + + if len(vs) == 0 { + log.Error("no versions available") + return "", false, nil + } + + sort.Sort(sort.Reverse(semver.Collection(vs))) + + if currentVersion.LessThan(vs[0]) { + return vs[0].String(), true, nil + } + return "", false, nil +} + // ShouldUpdate - checks whether update is needed func ShouldUpdate(current *types.Version, new *types.Version, policy types.PolicyType) (bool, error) { if policy == types.PolicyTypeForce {