-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
helm.go
188 lines (151 loc) · 5.48 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Copyright 2023 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 steps
import (
"bytes"
"fmt"
"os"
"strings"
semver "github.com/blang/semver/v4"
"github.com/helm/helm/pkg/chartutil"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
yamlpath "github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
utils "k8s.io/ingress-nginx/magefiles/utils"
)
const (
HelmChartPath = "charts/ingress-nginx/Chart.yaml"
HelmChartValues = "charts/ingress-nginx/values.yaml"
)
type Helm mg.Namespace
// UpdateVersion Update Helm Version of the Chart
func (Helm) UpdateVersion(version string) {
updateVersion(version)
}
func currentChartVersion() string {
chart, err := chartutil.LoadChartfile(HelmChartPath)
utils.CheckIfError(err, "HELM Could not Load Chart")
return chart.Version
}
func updateVersion(version string) {
utils.Info("HELM Reading File %v", HelmChartPath)
chart, err := chartutil.LoadChartfile(HelmChartPath)
utils.CheckIfError(err, "HELM Could not Load Chart")
// Get the current tag
// appVersionV, err := getIngressNGINXVersion()
// utils.CheckIfError(err, "HELM Issue Retrieving the Current Ingress Nginx Version")
// remove the v from TAG
appVersion := version
utils.Info("HELM Ingress-Nginx App Version: %s Chart AppVersion: %s", appVersion, chart.AppVersion)
if appVersion == chart.AppVersion {
utils.Warning("HELM Ingress NGINX Version didnt change Ingress-Nginx App Version: %s Chart AppVersion: %s", appVersion, chart.AppVersion)
return
}
controllerSemVer, err := semver.Parse(version)
utils.CheckIfError(err, "error parsing semver of new app")
isPreRelease := len(controllerSemVer.Pre) > 0
oldControllerSemVer, err := semver.Parse(chart.AppVersion)
utils.CheckIfError(err, "error parsing semver of old chart")
isBreakingChange := controllerSemVer.Major > oldControllerSemVer.Major || controllerSemVer.Minor > oldControllerSemVer.Minor
// Update the helm chart
chart.AppVersion = appVersion
cTag, err := semver.Make(chart.Version)
utils.CheckIfError(err, "HELM Creating Chart Version: %v", err)
incrFunc := cTag.IncrementPatch
if isBreakingChange {
cTag.Patch = 0
incrFunc = cTag.IncrementMinor
}
if isPreRelease {
chart.Annotations["artifacthub.io/prerelease"] = "true"
cTag.Pre = controllerSemVer.Pre
}
if err = incrFunc(); err != nil {
utils.ErrorF("HELM Incrementing Chart Version: %v", err)
os.Exit(1)
}
chart.Version = cTag.String()
utils.Debug("HELM Updated Chart Version: %v", chart.Version)
err = chartutil.SaveChartfile(HelmChartPath, chart)
utils.CheckIfError(err, "HELM Saving new Chart")
}
func updateChartReleaseNotes(releasesNotes []string) {
utils.Info("HELM Updating the Chart Release notes")
chart, err := chartutil.LoadChartfile(HelmChartPath)
utils.CheckIfError(err, "HELM Could not Load Chart to update release notes %s", HelmChartPath)
for i := range releasesNotes {
releasesNotes[i] = fmt.Sprintf("- %q", releasesNotes[i])
}
releaseNoteString := strings.Join(releasesNotes, "\n")
utils.Info("HELM Release note string %s", releaseNoteString)
chart.Annotations["artifacthub.io/changes"] = releaseNoteString
err = chartutil.SaveChartfile(HelmChartPath, chart)
utils.CheckIfError(err, "HELM Saving updated release notes for Chart")
}
// UpdateChartValue Updates the Helm ChartValue
func (Helm) UpdateChartValue(key, value string) {
updateChartValue(key, value)
}
func updateChartValue(key, value string) {
utils.Info("HELM Updating Chart %s %s:%s", HelmChartValues, key, value)
// read current values.yaml
data, err := os.ReadFile(HelmChartValues)
utils.CheckIfError(err, "HELM Could not Load Helm Chart Values files %s", HelmChartValues)
// var valuesStruct IngressChartValue
var n yaml.Node
utils.CheckIfError(yaml.Unmarshal(data, &n), "HELM Could not Unmarshal %s", HelmChartValues)
// update value
// keyParse := parsePath(key)
p, err := yamlpath.NewPath(key)
utils.CheckIfError(err, "HELM cannot create path")
q, err := p.Find(&n)
utils.CheckIfError(err, "HELM unexpected error finding path")
for _, i := range q {
utils.Info("HELM Found %s at %s", i.Value, key)
i.Value = value
utils.Info("HELM Updated %s at %s", i.Value, key)
}
//// write to file
var b bytes.Buffer
yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(2)
err = yamlEncoder.Encode(&n)
utils.CheckIfError(err, "HELM Could not Marshal new Values file")
err = os.WriteFile(HelmChartValues, b.Bytes(), 0o644)
utils.CheckIfError(err, "HELM Could not write new Values file to %s", HelmChartValues)
utils.Info("HELM Ingress Nginx Helm Chart update %s %s", key, value)
}
func (Helm) Helmdocs() error {
return runHelmDocs()
}
func runHelmDocs() error {
err := installHelmDocs()
if err != nil {
return err
}
err = sh.RunV("helm-docs", "--chart-search-root=${PWD}/charts")
if err != nil {
return err
}
return nil
}
func installHelmDocs() error {
utils.Info("HELM Install HelmDocs")
g0 := sh.RunCmd("go")
err := g0("install", "github.com/norwoodj/helm-docs/cmd/helm-docs@v1.11.0")
if err != nil {
return err
}
return nil
}