forked from kubernetes-sigs/zeitgeist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
99 lines (84 loc) · 2.97 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package upstream
import (
"sort"
"github.com/blang/semver"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/zeitgeist/pkg/container"
)
// Container upstream representation
type Container struct {
Base `mapstructure:",squash"`
// Registry URL, e.g. gcr.io/k8s-staging-kubernetes/conformance
Registry string
// Optional: semver constraints, e.g. < 2.0.0
// Will have no effect if the dependency does not follow Semver
Constraints string
}
// LatestVersion returns the latest tag for the given repository
// (depending on the Constraints if set).
func (upstream Container) LatestVersion() (string, error) { // nolint:gocritic
log.Debugf("Using Container flavour")
return highestSemanticImageTag(&upstream)
}
func highestSemanticImageTag(upstream *Container) (string, error) {
client := container.New()
semverConstraints := upstream.Constraints
if semverConstraints == "" {
// If no range is passed, just use the broadest possible range
semverConstraints = DefaultSemVerConstraints
}
expectedRange, err := semver.ParseRange(semverConstraints)
if err != nil {
return "", errors.Errorf("invalid semver constraints range: %v", upstream.Constraints)
}
log.Debugf("Retrieving tags for %s...", upstream.Registry)
tags, err := client.ListTags(upstream.Registry)
if err != nil {
return "", errors.Wrap(err, "retrieving Container tags")
}
log.Debugf("Found %d tags for %s...", len(tags), upstream.Registry)
// parse semvers first so we can safely sort
type semverWithOrig struct {
orig string // original tag string
parsed semver.Version // parsed semver
}
versions := make([]semverWithOrig, 0, len(tags))
for _, tag := range tags {
parsed, err := semver.ParseTolerant(tag)
if err != nil {
log.Debugf("Error parsing version %s (%v) as semver", tag, err)
continue
}
versions = append(versions, semverWithOrig{
orig: tag,
parsed: parsed,
})
}
// reverse sort, highest first
sort.Slice(versions, func(i, j int) bool {
return versions[j].parsed.LT(versions[i].parsed)
})
// find first version matching constraints
for _, version := range versions {
if !expectedRange(version.parsed) {
log.Debugf("Skipping release not matching range constraints (%s): %s", upstream.Constraints, version.parsed.String())
continue
}
log.Debugf("Found latest matching tag: %s", version.orig)
return version.orig, nil
}
return "", errors.Errorf("no potential tag found")
}