-
Notifications
You must be signed in to change notification settings - Fork 23
/
helm.go
162 lines (137 loc) · 5.08 KB
/
helm.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright 2020 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 (
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"github.com/blang/semver/v4"
log "github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/repo"
)
// Helm upstream representation.
type Helm struct {
Base `mapstructure:",squash"`
// Helm repository URL, e.g. https://grafana.github.io/helm-charts
Repo string
// Helm chart name in this repository
Chart 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 non-draft, non-prerelease Helm Release
// for the given repository (depending on the Constraints if set).
func (upstream Helm) LatestVersion() (string, error) {
log.Debug("Using Helm flavour")
return latestChartVersion(upstream)
}
func latestChartVersion(upstream Helm) (string, error) {
// Sanity checking
if upstream.Repo == "" {
return "", errors.New("invalid helm upstream: missing repo argument")
}
if upstream.Chart == "" {
return "", errors.New("invalid helm upstream: missing chart argument")
}
parsedRepo, err := url.Parse(upstream.Repo)
if err != nil {
return "", fmt.Errorf("invalid helm repo url: %s: %w", upstream.Repo, err)
}
s := parsedRepo.Scheme
if s != "http" && s != "https" && s != "oci" {
// We currently only support http-based and oci repos (Helm defaults)
// Helm allows custom handlers via plugins, but I've never seen it in practice - could be added later if needed
return "", fmt.Errorf("invalid helm repo: %s, only http, https and oci are supported", upstream.Repo)
}
var useSemverConstraints bool
var expectedRange semver.Range
semverConstraints := upstream.Constraints
if semverConstraints == "" {
useSemverConstraints = false
} else {
useSemverConstraints = true
validatedExpectedRange, err := semver.ParseRange(semverConstraints)
if err != nil {
return "", fmt.Errorf("invalid semver constraints range: %#v: %w", upstream.Constraints, err)
}
expectedRange = validatedExpectedRange
}
// First, get the repo index
// Helm expects a cache directory, so we create a temporary one
cacheDir, err := os.MkdirTemp("", "zeitgeist-helm-cache")
if err != nil {
log.Errorf("failed to create temporary directory for Helm cache")
return "", err
}
defer os.RemoveAll(cacheDir)
cfg := repo.Entry{
Name: "zeitgeist",
URL: upstream.Repo,
}
settings := cli.EnvSettings{
PluginsDirectory: "",
RepositoryCache: cacheDir,
}
re, err := repo.NewChartRepository(&cfg, getter.All(&settings))
if err != nil {
log.Errorf("failed to instantiate the Helm Chart Repository")
return "", err
}
log.Debugf("Downloading repo index for %s...", upstream.Repo)
indexFile, err := re.DownloadIndexFile()
if err != nil {
log.Errorf("failed to download index file for repo %s", upstream.Repo)
return "", err
}
log.Debugf("Loading repo index for %s...", upstream.Repo)
index, err := repo.LoadIndexFile(indexFile)
if err != nil {
log.Errorf("failed to load index file for repo %s", upstream.Repo)
return "", err
}
chartVersions := index.Entries[upstream.Chart]
if chartVersions == nil {
return "", fmt.Errorf("no chart for %s found in repository %s", upstream.Chart, upstream.Repo)
}
// Iterate over versions and get the first newer version
// (Or the first version that matches our semver constraints, if defined)
// Versions are already ordered, cf https://github.com/helm/helm/blob/6a3daaa7aa5b89a150042cadcbe869b477bb62a1/pkg/repo/index.go#L344
for _, chartVersion := range chartVersions {
chartVersionStr := strings.TrimPrefix(chartVersion.Version, "v")
prerelease, err := strconv.ParseBool(chartVersion.Annotations["artifacthub.io/prerelease"])
if err == nil && prerelease {
log.Debugf("Skipping annotated prerelease: %s\n", chartVersionStr)
continue
}
version, err := semver.Parse(chartVersionStr)
if err != nil { //nolint:gocritic
log.Debugf("Error parsing version %s (%#v) as semver, cannot validate semver constraints", chartVersionStr, err)
} else if len(version.Pre) > 0 {
log.Debugf("Skipping semver prerelease: %s\n", chartVersionStr)
continue
} else if useSemverConstraints && !expectedRange(version) {
log.Debugf("Skipping release not matching range constraints (%s): %s\n", upstream.Constraints, chartVersionStr)
continue
}
log.Debugf("Found latest matching release: %s\n", chartVersionStr)
return chartVersionStr, nil
}
// No latest version found – no versions? Only prereleases?
return "", errors.New("no potential version found")
}