/
describe_cluster_k8s.go
142 lines (111 loc) · 3.08 KB
/
describe_cluster_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
package ocean
import (
"context"
"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"
"github.com/spotinst/spotctl/internal/writer/writers/json"
)
type (
CmdDescribeClusterKubernetes struct {
cmd *cobra.Command
opts CmdDescribeClusterKubernetesOptions
}
CmdDescribeClusterKubernetesOptions struct {
*CmdDescribeClusterOptions
ClusterID string
}
)
func NewCmdDescribeClusterKubernetes(opts *CmdDescribeClusterOptions) *cobra.Command {
return newCmdDescribeClusterKubernetes(opts).cmd
}
func newCmdDescribeClusterKubernetes(opts *CmdDescribeClusterOptions) *CmdDescribeClusterKubernetes {
var cmd CmdDescribeClusterKubernetes
cmd.cmd = &cobra.Command{
Use: "kubernetes",
Short: "Describe 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 *CmdDescribeClusterKubernetes) 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 *CmdDescribeClusterKubernetes) survey(ctx context.Context) error {
if x.opts.Noninteractive {
return nil
}
return nil
}
func (x *CmdDescribeClusterKubernetes) log(ctx context.Context) error {
flags.Log(x.cmd)
return nil
}
func (x *CmdDescribeClusterKubernetes) validate(ctx context.Context) error {
return x.opts.Validate()
}
func (x *CmdDescribeClusterKubernetes) 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
}
cluster, err := oceanClient.GetCluster(ctx, x.opts.ClusterID)
if err != nil {
return err
}
w, err := x.opts.Clientset.NewWriter(json.WriterFormat)
if err != nil {
return err
}
return w.Write(cluster.Obj)
}
func (x *CmdDescribeClusterKubernetesOptions) Init(fs *pflag.FlagSet, opts *CmdDescribeClusterOptions) {
x.initDefaults(opts)
x.initFlags(fs)
}
func (x *CmdDescribeClusterKubernetesOptions) initDefaults(opts *CmdDescribeClusterOptions) {
x.CmdDescribeClusterOptions = opts
}
func (x *CmdDescribeClusterKubernetesOptions) initFlags(fs *pflag.FlagSet) {
fs.StringVar(&x.ClusterID, flags.FlagOceanClusterID, x.ClusterID, "id of the cluster")
}
func (x *CmdDescribeClusterKubernetesOptions) Validate() error {
errg := errors.NewErrorGroup()
if err := x.CmdDescribeClusterOptions.Validate(); err != nil {
errg.Add(err)
}
if x.ClusterID == "" {
errg.Add(errors.Required("ClusterID"))
}
if errg.Len() > 0 {
return errg
}
return nil
}