-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate.go
More file actions
267 lines (224 loc) · 7.49 KB
/
update.go
File metadata and controls
267 lines (224 loc) · 7.49 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*******************************************************************************
*
* Copyright 2019 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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 cmd
import (
"fmt"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/uniknow/helm-outdated/pkg/git"
"github.com/uniknow/helm-outdated/pkg/helm"
"helm.sh/helm/v3/pkg/cli"
)
type updateCmd struct {
chartPath string
maxColumnWidth uint
indent int
isIncrementChartVersion bool
dependencyFilter *helm.Filter
git *git.Git
hub *git.Hub
// **Experimental**
// isAutoUpdate updates the dependencies, increments version of the chart with the dependency and (git) commits the changes.
isAutoUpdate,
isOnlyPullRequest bool
authorName,
authorEmail string
}
var updateLongUsage = `
Update outdated dependencies of a given chart to their latest version.
Examples:
# Update dependencies of the given chart.
$ helm outdated update <chartPath>
# Only update specific dependencies of the given chart.
$ helm outdated update <chartPath> --dependencies kube-state-metrics,prometheus-operator
`
func newUpdateOutdatedDependenciesCmd() *cobra.Command {
u := &updateCmd{
dependencyFilter: &helm.Filter{},
maxColumnWidth: 60,
}
cmd := &cobra.Command{
Use: "update",
Long: updateLongUsage,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if debug, err := cmd.Flags().GetBool("debug"); err == nil {
if debug == true {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
}
if maxColumnWidth, err := cmd.Flags().GetUint("max-column-width"); err == nil {
u.maxColumnWidth = maxColumnWidth
}
if repositories, err := cmd.Flags().GetStringSlice("repositories"); err == nil {
u.dependencyFilter.Repositories = repositories
}
if deps, err := cmd.Flags().GetStringSlice("dependencies"); err == nil {
u.dependencyFilter.DependencyNames = deps
}
path := "."
if len(args) > 0 {
path = args[0]
}
path, err := filepath.Abs(path)
if err != nil {
return err
}
u.chartPath = path
return u.update()
},
}
addCommonFlags(cmd)
cmd.Flags().BoolVarP(&u.isIncrementChartVersion, "increment-chart-version", "", false, "Increment the version of the Helm chart if requirements are updated.")
cmd.Flags().IntVarP(&u.indent, "indent", "", 4, "Indent to use when writing the requirements.yaml .")
// **Experimental** Update dependencies of the given chart, commit and push to upstream using git.
cmd.Flags().BoolVar(&u.isAutoUpdate, "auto-update", false, "**Experimental** Update dependencies of the given chart, commit and push to upstream using git.")
cmd.Flags().StringVar(&u.authorName, "author-name", "", "The name of the author and committer to be used when auto update is enabled.")
cmd.Flags().StringVar(&u.authorEmail, "author-email", "", "The email of the author and committer to be used when auto update is enabled.")
cmd.Flags().BoolVar(&u.isOnlyPullRequest, "only-pull-requests", false, "Only use pull requests. Do not commit minor changes to master branch.")
return cmd
}
func (u *updateCmd) update() error {
outdatedDeps, err := helm.ListOutdatedDependencies(u.chartPath, cli.New(), u.dependencyFilter)
if err != nil {
return err
}
if len(outdatedDeps) == 0 {
fmt.Println("All charts up-to-date.")
return nil
}
fmt.Println(u.formatResults(outdatedDeps))
if u.isIncrementChartVersion || u.isAutoUpdate {
fmt.Println("UPDATING CHART VERSION")
if err = helm.IncrementChartVersion(u.chartPath, helm.IncTypes.Patch); err != nil {
fmt.Println("ERROR OCCURRED WHILE UPDATING CHART VERSION")
return err
}
}
fmt.Println("UPDATING DEPENDENCIES")
if err := helm.UpdateDependencies(u.chartPath, outdatedDeps, u.indent); err != nil {
fmt.Println("ERROR OCCURRED WHILE UPDATING DEPENDENCIES")
return err
}
// Return here if the auto update is not enabled.
if !u.isAutoUpdate {
return nil
}
// maxIncType is used to keep track of the version changes when updating dependencies.
maxIncType := helm.IncTypes.Patch
depNames := make([]string, len(outdatedDeps))
for idx, dep := range outdatedDeps {
if i := helm.GetIncType(dep.CurrentVersion, dep.LatestVersion); maxIncType.IsGreater(i) {
maxIncType = i
}
depName := dep.Alias
if depName == "" {
depName = dep.Name
}
depNames[idx] = fmt.Sprintf("%s@%s", depName, dep.LatestVersion)
}
chartName, err := helm.GetChartName(u.chartPath)
if err != nil {
return err
}
commitMessage := fmt.Sprintf("[%s] updated dependency to %s", chartName, strings.Join(depNames, ", "))
// If potential breaking changes are expected, use a pull request.
if u.isOnlyPullRequest || maxIncType == helm.IncTypes.Major || maxIncType == helm.IncTypes.Minor {
return u.upstreamMajorChanges(commitMessage, chartName)
}
return u.upstreamMinorChanges(commitMessage)
}
// upstreamMinorChanges commits the changes to the master branch of the upstream github repository.
func (u *updateCmd) upstreamMinorChanges(commitMessage string) error {
g, err := git.NewGit(u.chartPath, u.authorName, u.authorEmail)
if err != nil {
return err
}
res, err := g.Diff()
if err != nil {
return err
}
fmt.Println(res)
res, err = g.Commit(commitMessage)
if err != nil {
return err
}
fmt.Println(res)
res, err = g.RebaseAndPushToMaster()
fmt.Println(res)
return err
}
// upstreamMajorChanges same as upstreamMinorChanges but via github.com pull request.
func (u *updateCmd) upstreamMajorChanges(commitMessage, chartName string) error {
g, err := git.NewGit(u.chartPath, u.authorName, u.authorEmail)
if err != nil {
return err
}
branchName := fmt.Sprintf("%s-%d", chartName, time.Now().UTC().Unix())
res, err := g.CreateAndCheckoutBranch(branchName)
if err != nil {
return err
}
defer g.CheckoutBranch("master")
res, err = g.Diff()
if err != nil {
return err
}
fmt.Println(res)
res, err = g.Commit(commitMessage)
if err != nil {
return err
}
fmt.Println(res)
res, err = g.Push(branchName)
if err != nil {
return err
}
fmt.Println(res)
hub, err := git.NewHub(u.chartPath)
if err != nil {
return err
}
res, err = hub.OpenPullRequestToMaster(branchName, fmt.Sprintf("[%s] updating dependencies", chartName), commitMessage)
fmt.Println(res)
return err
}
func (u *updateCmd) formatResults(results []*helm.Result) string {
if len(results) == 0 {
return "All charts up to date."
}
table := uitable.New()
table.MaxColWidth = u.maxColumnWidth
table.AddRow("Updating the following dependencies to their latest version:")
table.AddRow("ALIAS", "VERSION", "LATEST_VERSION", "REPOSITORY")
for _, r := range results {
name := r.Alias
if name == "" {
name = r.Name
}
table.AddRow(name, r.Version, r.LatestVersion, r.Repository)
}
return table.String()
}