/
rollout_stop_k8s.go
148 lines (117 loc) · 3.22 KB
/
rollout_stop_k8s.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
package ocean
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spotinst/spotctl/internal/errors"
"github.com/spotinst/spotctl/internal/flags"
"github.com/spotinst/spotctl/internal/spot"
)
type (
CmdRolloutStopKubernetes struct {
cmd *cobra.Command
opts CmdRolloutStopKubernetesOptions
}
CmdRolloutStopKubernetesOptions struct {
*CmdRolloutStopOptions
spot.OceanRolloutOptions
}
)
func NewCmdRolloutStopKubernetes(opts *CmdRolloutStopOptions) *cobra.Command {
return newCmdRolloutStopKubernetes(opts).cmd
}
func newCmdRolloutStopKubernetes(opts *CmdRolloutStopOptions) *CmdRolloutStopKubernetes {
var cmd CmdRolloutStopKubernetes
cmd.cmd = &cobra.Command{
Use: "kubernetes",
Short: "Stop an in-progress rollout of a Kubernetes cluster",
SilenceErrors: true,
SilenceUsage: true,
Aliases: []string{"k8s", "kube", "k"},
RunE: func(*cobra.Command, []string) error {
return cmd.Run(context.Background())
},
}
cmd.opts.Init(cmd.cmd.Flags(), opts)
return &cmd
}
func (x *CmdRolloutStopKubernetes) Run(ctx context.Context) error {
steps := []func(context.Context) error{
x.survey,
x.log,
x.validate,
x.run,
}
for _, step := range steps {
if err := step(ctx); err != nil {
return err
}
}
return nil
}
func (x *CmdRolloutStopKubernetes) survey(ctx context.Context) error {
if x.opts.Noninteractive {
return nil
}
return nil
}
func (x *CmdRolloutStopKubernetes) log(ctx context.Context) error {
flags.Log(x.cmd)
return nil
}
func (x *CmdRolloutStopKubernetes) validate(ctx context.Context) error {
return x.opts.Validate()
}
func (x *CmdRolloutStopKubernetes) run(ctx context.Context) error {
spotClientOpts := []spot.ClientOption{
spot.WithCredentialsProfile(x.opts.Profile),
spot.WithDryRun(x.opts.DryRun),
}
spotClient, err := x.opts.Clientset.NewSpotClient(spotClientOpts...)
if err != nil {
return err
}
oceanClient, err := spotClient.Services().Ocean(x.opts.CloudProvider, spot.OrchestratorKubernetes)
if err != nil {
return err
}
oceanRollout, err := oceanClient.NewRolloutBuilder(x.cmd.Flags(), &x.opts.OceanRolloutOptions).Build()
if err != nil {
return err
}
rollout, err := oceanClient.UpdateRollout(ctx, oceanRollout)
if err != nil {
return err
}
fmt.Fprintln(x.opts.Out, fmt.Sprintf("Stopped (%q).", rollout.ID))
return nil
}
func (x *CmdRolloutStopKubernetesOptions) Init(fs *pflag.FlagSet, opts *CmdRolloutStopOptions) {
x.initDefaults(opts)
x.initFlags(fs)
}
func (x *CmdRolloutStopKubernetesOptions) initDefaults(opts *CmdRolloutStopOptions) {
x.CmdRolloutStopOptions = opts
x.OceanRolloutOptions.Status = "STOPPED"
}
func (x *CmdRolloutStopKubernetesOptions) initFlags(fs *pflag.FlagSet) {
fs.StringVar(&x.ClusterID, flags.FlagOceanClusterID, x.ClusterID, "")
fs.StringVar(&x.RolloutID, flags.FlagOceanRolloutID, x.RolloutID, "")
}
func (x *CmdRolloutStopKubernetesOptions) Validate() error {
errg := errors.NewErrorGroup()
if err := x.CmdRolloutStopOptions.Validate(); err != nil {
errg.Add(err)
}
if x.ClusterID == "" {
errg.Add(errors.Required("ClusterID"))
}
if x.RolloutID == "" {
errg.Add(errors.Required("RolloutID"))
}
if errg.Len() > 0 {
return errg
}
return nil
}