-
Notifications
You must be signed in to change notification settings - Fork 785
/
upgrade_addons.go
247 lines (219 loc) · 6.63 KB
/
upgrade_addons.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
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
package upgrade
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/create"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/addon"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
upgradeAddonsLong = templates.LongDesc(`
Upgrades any Addons added to Jenkins X if there are any new releases available
`)
upgradeAddonsExample = templates.Examples(`
# Upgrades any Addons added to Jenkins X
jx upgrade addons
`)
)
// UpgradeAddonsOptions the options for the create spring command
type UpgradeAddonsOptions struct {
options.CreateOptions
Namespace string
Set string
VersionsDir string
InstallFlags create.InstallFlags
}
// NewCmdUpgradeAddons defines the command
func NewCmdUpgradeAddons(commonOpts *opts.CommonOptions) *cobra.Command {
options := &UpgradeAddonsOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "addons",
Short: "Upgrades any Addons added to Jenkins X if there are any new releases available",
Aliases: []string{"addon"},
Long: upgradeAddonsLong,
Example: upgradeAddonsExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.addFlags(cmd)
options.InstallFlags.AddCloudEnvOptions(cmd)
cmd.AddCommand(NewCmdUpgradeAddonProw(commonOpts))
return cmd
}
func (options *UpgradeAddonsOptions) addFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The Namespace to upgrade")
cmd.Flags().StringVarP(&options.Set, "set", "s", "", "The Helm parameters to pass in while upgrading")
cmd.Flags().StringVarP(&options.VersionsDir, "versions-dir", "", "", "The directory containing the versions repo")
}
// Run implements the command
func (o *UpgradeAddonsOptions) Run() error {
err := o.Helm().UpdateRepo()
if err != nil {
return err
}
_, _, err = o.KubeClientAndDevNamespace()
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
_, ns, err = o.JXClientAndDevNamespace()
if err != nil {
return err
}
}
addonConfig, err := addon.LoadAddonsConfig()
if err != nil {
return err
}
addonEnabled := map[string]bool{}
for _, addon := range addonConfig.Addons {
if addon.Enabled {
addonEnabled[addon.Name] = true
}
}
releases, _, err := o.Helm().ListReleases(ns)
if err != nil {
log.Logger().Warnf("Failed to find Helm installs: %s", err)
}
charts := kube.AddonCharts
keys := []string{}
if len(o.Args) > 0 {
for _, k := range o.Args {
chart := charts[k]
if chart == "" {
return errors.New(fmt.Sprintf("Could not find addon %s in the list of addons", k))
}
status := releases[k].Status
if status == "" {
return errors.New(fmt.Sprintf("Could not find %s chart %s installed in namespace %s\n", util.ColorInfo(k), util.ColorInfo(chart), util.ColorInfo(ns)))
}
keys = append(keys, k)
}
} else {
keys = util.SortedMapKeys(charts)
}
for _, k := range keys {
if k == "jx-prow" {
//installing prow
log.Logger().Infof("Upgrading prow")
upgradeAddonProwOptions := &UpgradeAddonProwOptions{
UpgradeAddonsOptions: *o,
}
err = upgradeAddonProwOptions.Upgrade()
if err != nil {
return err
}
continue
}
chart := charts[k]
status := releases[k].Status
name := k
if status != "" {
log.Logger().Infof("Upgrading %s chart %s...", util.ColorInfo(name), util.ColorInfo(chart))
valueFiles := []string{}
valueFiles, err = helm.AppendMyValues(valueFiles)
if err != nil {
return errors.Wrap(err, "failed to append the myvalues.yaml file")
}
values := []string{}
if o.Set != "" {
values = append(values, o.Set)
}
config := &v1.ConfigMap{}
plugins := &v1.ConfigMap{}
if k == kube.DefaultProwReleaseName {
// lets backup any Prow config as we should never replace this, eventually we'll move config to a git repo so this is temporary
config, plugins, err = o.backupConfigs()
if err != nil {
return errors.Wrap(err, "backing up the prow config")
}
}
helmOptions := helm.InstallChartOptions{
Chart: chart,
ReleaseName: k,
Ns: ns,
NoForce: true,
ValueFiles: valueFiles,
SetValues: values,
UpgradeOnly: true,
VersionsDir: o.VersionsDir,
VersionsGitURL: o.InstallFlags.VersionsRepository,
VersionsGitRef: o.InstallFlags.VersionsGitRef,
}
// TODO this will fail to upgrade file system charts like Istio
err = o.InstallChartWithOptions(helmOptions)
if err != nil {
return errors.Wrapf(err, "Failed to upgrade %s chart %s: %v\n", name, chart, err)
}
if k == kube.DefaultProwReleaseName {
err = o.restoreConfigs(config, plugins)
if err != nil {
return err
}
}
log.Logger().Infof("Upgraded %s chart %s", util.ColorInfo(name), util.ColorInfo(chart))
}
}
return nil
}
func (o *UpgradeAddonsOptions) restoreConfigs(config *v1.ConfigMap, plugins *v1.ConfigMap) error {
client, devNamespace, err := o.KubeClientAndDevNamespace()
if err != nil {
return err
}
var err1 error
if config != nil {
_, err = client.CoreV1().ConfigMaps(devNamespace).Get("config", metav1.GetOptions{})
if err != nil {
_, err = client.CoreV1().ConfigMaps(devNamespace).Create(config)
if err != nil {
b, _ := yaml.Marshal(config)
err1 = fmt.Errorf("error restoring config %s\n", string(b))
}
}
}
if plugins != nil {
_, err = client.CoreV1().ConfigMaps(devNamespace).Get("plugins", metav1.GetOptions{})
if err != nil {
_, err = client.CoreV1().ConfigMaps(devNamespace).Create(plugins)
if err != nil {
b, _ := yaml.Marshal(plugins)
err = fmt.Errorf("%v/nerror restoring plugins %s\n", err1, string(b))
}
}
}
return err
}
func (o *UpgradeAddonsOptions) backupConfigs() (*v1.ConfigMap, *v1.ConfigMap, error) {
client, devNamespace, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, nil, err
}
config, _ := client.CoreV1().ConfigMaps(devNamespace).Get("config", metav1.GetOptions{})
plugins, _ := client.CoreV1().ConfigMaps(devNamespace).Get("plugins", metav1.GetOptions{})
config = config.DeepCopy()
config.ResourceVersion = ""
plugins = plugins.DeepCopy()
plugins.ResourceVersion = ""
return config, plugins, nil
}