-
Notifications
You must be signed in to change notification settings - Fork 444
/
enterprise.go
101 lines (86 loc) · 3.33 KB
/
enterprise.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
100
101
package version
import (
"math"
"github.com/Masterminds/semver/v3"
"github.com/rotisserie/eris"
"github.com/solo-io/go-utils/githubutils"
"github.com/solo-io/go-utils/versionutils"
"github.com/solo-io/go-utils/versionutils/git"
"github.com/spf13/afero"
"helm.sh/helm/v3/pkg/repo"
)
const EnterpriseHelmRepoIndex = "https://storage.googleapis.com/gloo-ee-helm/index.yaml"
const GlooEE = "gloo-ee"
// The version of GlooE installed by the CLI.
// Calculated from the largest semver gloo-ee version in the helm repo index
func GetLatestEnterpriseVersion(stableOnly bool) (string, error) {
return GetLatestHelmChartVersionWithMaxVersion(EnterpriseHelmRepoIndex, GlooEE, stableOnly, &versionutils.Version{
Major: math.MaxInt32,
Minor: math.MaxInt32,
Patch: math.MaxInt32,
})
}
// Calculated from the largest gloo-ee version in the helm repo index with version constraints
func GetLatestHelmChartVersionWithMaxVersion(helmRepoIndex, repoName string, stableOnly bool, maxVersion *versionutils.Version) (string, error) {
fs := afero.NewOsFs()
tmpFile, err := afero.TempFile(fs, "", "")
if err != nil {
return "", err
}
if err := githubutils.DownloadFile(helmRepoIndex, tmpFile); err != nil {
return "", err
}
defer fs.Remove(tmpFile.Name())
return LatestVersionFromRepoWithMaxVersion(tmpFile.Name(), repoName, stableOnly, maxVersion)
}
func LatestVersionFromRepo(file, repoName string, stableOnly bool) (string, error) {
return LatestVersionFromRepoWithMaxVersion(file, repoName, stableOnly, &versionutils.Version{
Major: math.MaxInt32,
Minor: math.MaxInt32,
Patch: math.MaxInt32,
})
}
func LatestVersionFromRepoWithMaxVersion(file, repoName string, stableOnly bool, maxVersion *versionutils.Version) (string, error) {
ind, err := repo.LoadIndexFile(file)
if err != nil {
return "", err
}
// we can't depend on ind.SortEntries() because this doesn't properly sort rc releases
// e.g., it would sort 1.0.0-rc1, 1.0.0-rc10, 1.0.0-rc2, ... 1.0.0-rc9, which is incorrect
// instead, we use our version comparison logic to get the largest tag
zero := versionutils.Zero()
largestVersion := &zero
var largestTag string
if chartVersions, ok := ind.Entries[repoName]; ok && len(chartVersions) > 0 {
for _, chartVersion := range chartVersions {
if stableOnly {
stableOnlyConstraint, _ := semver.NewConstraint("*")
test, err := semver.NewVersion(chartVersion.Version)
if err != nil || !stableOnlyConstraint.Check(test) {
continue
}
}
version, err := versionutils.ParseVersion(git.AppendTagPrefix(chartVersion.Version))
if err != nil {
continue
}
// with the default implementation of MustIsGreaterThanOrEqualTo,
// all rc releases will be larger than equivalent beta releases. (1.0.0-rc1 > 1.0.0-beta8)
// since those are the only allowed labels in Gloo, this tiebreak is acceptable
versionConstraintSatisfied := maxVersion.MustIsGreaterThanOrEqualTo(*version)
if !versionConstraintSatisfied {
continue
}
isLargest := version.MustIsGreaterThanOrEqualTo(*largestVersion)
if isLargest {
largestVersion = version
largestTag = chartVersion.Version
}
}
}
if largestTag == "" {
return "", eris.Errorf("Couldn't find any %s versions in index file %s that satisfies constraints: [stable]: %v, [maxVersion]: %v",
repoName, file, stableOnly, maxVersion)
}
return largestTag, nil
}