-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
cmd_helm.go
199 lines (179 loc) · 5.72 KB
/
cmd_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
189
190
191
192
193
194
195
196
197
198
199
package cli
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/ann"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/util"
"github.com/telepresenceio/telepresence/v2/pkg/errcat"
)
func helmCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "helm",
}
cmd.AddCommand(helmInstallCommand(), helmUpgradeCommand(), helmUninstallCommand())
return cmd
}
type HelmOpts struct {
values.Options
AllValues map[string]any
ReuseValues bool
ResetValues bool
Request *connector.ConnectRequest
cmdType connector.HelmRequest_Type
kubeFlags *pflag.FlagSet
}
var (
HelmInstallExtendFlagsFunc func(*pflag.FlagSet) //nolint:gochecknoglobals // extension point
HelmInstallPrologFunc func(context.Context, *pflag.FlagSet, *HelmOpts) error //nolint:gochecknoglobals // extension point
)
func helmInstallCommand() *cobra.Command {
var upgrade bool
ha := &HelmOpts{
cmdType: connector.HelmRequest_INSTALL,
}
cmd := &cobra.Command{
Use: "install",
Args: cobra.NoArgs,
Short: "Install telepresence traffic manager",
RunE: func(cmd *cobra.Command, args []string) error {
if upgrade {
ha.cmdType = connector.HelmRequest_UPGRADE
}
return ha.run(cmd, args)
},
Annotations: map[string]string{
ann.UserDaemon: ann.Required,
ann.VersionCheck: ann.Required,
},
}
flags := cmd.Flags()
flags.BoolVarP(&upgrade, "upgrade", "u", false, "replace the traffic manager if it already exists")
ha.addValueSettingFlags(flags)
uf := flags.Lookup("upgrade")
uf.Hidden = true
uf.Deprecated = `Use "telepresence helm upgrade" instead of "telepresence helm install --upgrade"`
ha.Request, ha.kubeFlags = initConnectRequest(cmd)
return cmd
}
func helmUpgradeCommand() *cobra.Command {
ha := &HelmOpts{
cmdType: connector.HelmRequest_UPGRADE,
}
cmd := &cobra.Command{
Use: "upgrade",
Args: cobra.NoArgs,
Short: "Upgrade telepresence traffic manager",
RunE: ha.run,
Annotations: map[string]string{
ann.UserDaemon: ann.Required,
ann.VersionCheck: ann.Required,
},
}
flags := cmd.Flags()
ha.addValueSettingFlags(flags)
flags.BoolVarP(&ha.ResetValues, "reset-values", "", false,
"when upgrading, reset the values to the ones built into the chart")
flags.BoolVarP(&ha.ReuseValues, "reuse-values", "", false,
"when upgrading, reuse the last release's values and merge in any overrides from the command line via --set and -f")
ha.Request, ha.kubeFlags = initConnectRequest(cmd)
return cmd
}
func (ha *HelmOpts) addValueSettingFlags(flags *pflag.FlagSet) {
flags.StringSliceVarP(&ha.ValueFiles, "values", "f", []string{},
"specify values in a YAML file or a URL (can specify multiple)")
flags.StringSliceVarP(&ha.Values, "set", "", []string{},
"specify a value as a.b=v (can specify multiple or separate values with commas: a.b=v1,a.c=v2)")
flags.StringSliceVarP(&ha.FileValues, "set-file", "", []string{},
"set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
flags.StringSliceVarP(&ha.JSONValues, "set-json", "", []string{},
"set JSON values on the command line (can specify multiple or separate values with commas: a.b=jsonval1,a.c=jsonval2)")
flags.StringSliceVarP(&ha.StringValues, "set-string", "", []string{},
"set STRING values on the command line (can specify multiple or separate values with commas: a.b=val1,a.c=val2)")
if HelmInstallExtendFlagsFunc != nil {
HelmInstallExtendFlagsFunc(flags)
}
}
func helmUninstallCommand() *cobra.Command {
ha := &HelmOpts{
cmdType: connector.HelmRequest_UNINSTALL,
}
cmd := &cobra.Command{
Use: "uninstall",
Args: cobra.NoArgs,
Short: "Uninstall telepresence traffic manager",
RunE: ha.run,
Annotations: map[string]string{
ann.UserDaemon: ann.Required,
ann.VersionCheck: ann.Required,
},
}
ha.Request, ha.kubeFlags = initConnectRequest(cmd)
return cmd
}
func (ha *HelmOpts) Type() connector.HelmRequest_Type {
return ha.cmdType
}
func (ha *HelmOpts) run(cmd *cobra.Command, _ []string) error {
if ha.ReuseValues && ha.ResetValues {
return errcat.User.New("--reset-values and --reuse-values are mutually exclusive")
}
var err error
if ha.AllValues, err = ha.MergeValues(getter.All(cli.New())); err != nil {
return err
}
flags := cmd.Flags()
if err = util.InitCommand(cmd); err != nil {
return err
}
ha.Request.KubeFlags = kubeFlagMap(ha.kubeFlags)
util.AddKubeconfigEnv(ha.Request)
ctx := cmd.Context()
if HelmInstallPrologFunc != nil {
if err := HelmInstallPrologFunc(ctx, flags, ha); err != nil {
return err
}
}
// always disconnect to ensure that there is no active session.
_ = util.Disconnect(ctx, false)
valuesJSON, err := json.Marshal(ha.AllValues)
if err != nil {
return err
}
doQuit := false
request := &connector.HelmRequest{
Type: ha.cmdType,
ValuesJson: valuesJSON,
ReuseValues: ha.ReuseValues,
ResetValues: ha.ResetValues,
ConnectRequest: ha.Request,
}
resp, err := util.GetUserDaemon(ctx).Helm(ctx, request)
if err != nil {
return err
}
if err = errcat.FromResult(resp); err != nil {
return err
}
var msg string
switch ha.cmdType {
case connector.HelmRequest_INSTALL:
msg = "installed"
case connector.HelmRequest_UPGRADE:
msg = "upgraded"
case connector.HelmRequest_UNINSTALL:
doQuit = true
msg = "uninstalled"
}
fmt.Fprintf(cmd.OutOrStdout(), "\nTraffic Manager %s successfully\n", msg)
if err == nil && doQuit {
err = util.Disconnect(cmd.Context(), true)
}
return err
}