forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmesh_upgrade.go
149 lines (120 loc) · 4.18 KB
/
mesh_upgrade.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
package main
import (
"bytes"
"fmt"
"io"
"time"
"github.com/spf13/cobra"
helm "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
)
const (
defaultContainerRegistry = "openservicemesh"
defaultOsmImageTag = "v0.9.0"
)
const upgradeDesc = `
This command upgrades an OSM control plane by upgrading the
underlying Helm release.
The mesh to upgrade is identified by its mesh name and namespace. If either were
overridden from the default for the "osm install" command, the --mesh-name and
--osm-namespace flags need to be specified.
Values from the current Helm release will be carried over to the new release
with the exception of OpenServiceMesh.image.registry (--container-registry) and
OpenServiceMesh.image.tag (--osm-image-tag), which will be overridden from the
old release by default.
Note: edits to resources NOT made by Helm or the OSM CLI may not persist after
"osm mesh upgrade" is run.
Note: edits made to chart values that impact the preset-mesh-config will not
apply to the osm-mesh-config, when "osm mesh upgrade" is run. This means configuration
changes made to the osm-mesh-config resource will persist through an upgrade
and any configuration changes needed can be done by patching this resource prior or
post an upgrade.
If any CustomResourceDefinitions (CRDs) are different between the installed
chart and the upgraded chart, the CRDs (and any corresponding custom resources)
need to be deleted and recreated using the CRDs in the new chart prior to
updating the mesh to ensure compatibility.
`
const meshUpgradeExample = `
# Upgrade the mesh with the default name in the osm-system namespace, setting
# the image registry and tag to the defaults, and leaving all other values unchanged.
osm mesh upgrade --osm-namespace osm-system
`
type meshUpgradeCmd struct {
out io.Writer
meshName string
chart *chart.Chart
containerRegistry string
osmImageTag string
}
func newMeshUpgradeCmd(config *helm.Configuration, out io.Writer) *cobra.Command {
upg := &meshUpgradeCmd{
out: out,
}
var chartPath string
cmd := &cobra.Command{
Use: "upgrade",
Short: "upgrade osm control plane",
Long: upgradeDesc,
Example: meshUpgradeExample,
RunE: func(cmd *cobra.Command, _ []string) error {
if chartPath != "" {
var err error
upg.chart, err = loader.Load(chartPath)
if err != nil {
return err
}
}
return upg.run(config)
},
}
f := cmd.Flags()
f.StringVar(&upg.meshName, "mesh-name", defaultMeshName, "Name of the mesh to upgrade")
f.StringVar(&chartPath, "osm-chart-path", "", "path to osm chart to override default chart")
f.StringVar(&upg.containerRegistry, "container-registry", defaultContainerRegistry, "container registry that hosts control plane component images")
f.StringVar(&upg.osmImageTag, "osm-image-tag", defaultOsmImageTag, "osm image tag")
return cmd
}
func (u *meshUpgradeCmd) run(config *helm.Configuration) error {
if u.chart == nil {
var err error
u.chart, err = loader.LoadArchive(bytes.NewReader(chartTGZSource))
if err != nil {
return err
}
}
// Add the overlay values to be updated to the current release's values map
values, err := u.resolveValues(config)
if err != nil {
return err
}
upgradeClient := helm.NewUpgrade(config)
upgradeClient.Wait = true
upgradeClient.Timeout = 5 * time.Minute
upgradeClient.ResetValues = true
if _, err = upgradeClient.Run(u.meshName, u.chart, values); err != nil {
return err
}
fmt.Fprintf(u.out, "OSM successfully upgraded mesh [%s] in namespace [%s]\n", u.meshName, settings.Namespace())
return nil
}
func (u *meshUpgradeCmd) resolveValues(config *helm.Configuration) (map[string]interface{}, error) {
vals := map[string]interface{}{
"image": map[string]interface{}{
"tag": u.osmImageTag,
"registry": u.containerRegistry,
},
}
vals = map[string]interface{}{
"OpenServiceMesh": vals,
}
oldRelease, err := config.Releases.Deployed(u.meshName)
if err != nil {
return nil, err
}
// The final merged values from the previous release
oldVals := chartutil.CoalesceTables(oldRelease.Config, oldRelease.Chart.Values)
vals = chartutil.CoalesceTables(vals, oldVals)
return vals, nil
}