-
Notifications
You must be signed in to change notification settings - Fork 6
/
modify.go
90 lines (73 loc) · 2.52 KB
/
modify.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
package kubernetes
import (
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
// ModifyCommand creates the "kubernetes modify" command
func ModifyCommand() commands.Command {
return &modifyCommand{
BaseCommand: commands.New(
"modify",
"Modifiy an existing cluster",
"upctl cluster modify 00bb4617-c592-4b32-b869-35a60b323b18 --plan 1xCPU-1GB",
),
}
}
type modifyCommand struct {
*commands.BaseCommand
resolver.CachingKubernetes
completion.Kubernetes
controlPlaneIPFilter []string
labels []string
clearLabels config.OptionalBoolean
}
// InitCommand implements Command.InitCommand
func (c *modifyCommand) InitCommand() {
fs := &pflag.FlagSet{}
fs.StringArrayVar(
&c.controlPlaneIPFilter,
"kubernetes-api-allow-ip",
[]string{},
"Allow cluster's Kubernetes API to be accessed from an IP address or a network CIDR, multiple can be declared.",
)
fs.StringArrayVar(&c.labels, "label", nil, "Labels to describe the cluster in `key=value` format, multiple can be declared.")
config.AddToggleFlag(fs, &c.clearLabels, "clear-labels", false, "Clear all labels from to given cluster.")
c.AddFlags(fs)
c.Cobra().MarkFlagsMutuallyExclusive("label", "clear-labels")
}
// Execute implements commands.MultipleArgumentCommand
func (c *modifyCommand) Execute(exec commands.Executor, arg string) (output.Output, error) {
msg := fmt.Sprintf("Modifying Kubernetes cluster %v", arg)
exec.PushProgressStarted(msg)
req := request.ModifyKubernetesClusterRequest{
ClusterUUID: arg,
Cluster: request.ModifyKubernetesCluster{},
}
if len(c.controlPlaneIPFilter) > 0 {
req.Cluster.ControlPlaneIPFilter = &c.controlPlaneIPFilter
}
if c.clearLabels.Value() {
req.Cluster.Labels = &[]upcloud.Label{}
}
if len(c.labels) > 0 {
labelSlice, err := labels.StringsToSliceOfLabels(c.labels)
if err != nil {
return nil, err
}
req.Cluster.Labels = &labelSlice
}
res, err := exec.All().ModifyKubernetesCluster(exec.Context(), &req)
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
return output.OnlyMarshaled{Value: res}, nil
}