-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_clusters_cmd.go
235 lines (197 loc) · 6.72 KB
/
kubernetes_clusters_cmd.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2017-2021 Ingram Micro Inc.
package cmd
import (
"fmt"
"strings"
"github.com/ingrammicro/cio/api/kubernetes"
"github.com/ingrammicro/cio/api/types"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/urfave/cli"
)
// WireUpCluster prepares common resources to send request to Concerto API
func WireUpCluster(c *cli.Context) (ds *kubernetes.ClusterService, f format.Formatter) {
f = format.GetFormatter()
config, err := utils.GetConcertoConfig()
if err != nil {
f.PrintFatal("Couldn't wire up config", err)
}
hcs, err := utils.NewHTTPConcertoService(config)
if err != nil {
f.PrintFatal("Couldn't wire up concerto service", err)
}
ds, err = kubernetes.NewClusterService(hcs)
if err != nil {
f.PrintFatal("Couldn't wire up cluster service", err)
}
return ds, f
}
// ClusterList subcommand function
func ClusterList(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
clusters, err := clusterSvc.ListClusters()
if err != nil {
formatter.PrintFatal("Couldn't receive cluster data", err)
}
labelables := make([]types.Labelable, len(clusters))
for i := 0; i < len(clusters); i++ {
labelables[i] = types.Labelable(clusters[i])
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
filteredLabelables := LabelFiltering(c, labelables, labelIDsByName)
LabelAssignNamesForIDs(c, filteredLabelables, labelNamesByID)
clusters = make([]*types.Cluster, len(filteredLabelables))
for i, labelable := range filteredLabelables {
v, ok := labelable.(*types.Cluster)
if !ok {
formatter.PrintFatal(LabelFilteringUnexpected,
fmt.Errorf("expected labelable to be a *types.Cluster, got a %T", labelable))
}
clusters[i] = v
}
if err = formatter.PrintList(clusters); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ClusterShow subcommand function
func ClusterShow(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"id"}, formatter)
cluster, err := clusterSvc.GetCluster(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive cluster data", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
cluster.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*cluster); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ClusterCreate subcommand function
func ClusterCreate(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"name", "version", "cloud-account-id", "cluster-plan-id"}, formatter)
clusterIn := map[string]interface{}{
"name": c.String("name"),
"version": c.String("version"),
"cloud_account_id": c.String("cloud-account-id"),
"cluster_plan_id": c.String("cluster-plan-id"),
}
// If 'default_vpc_creation' is set as true, 'default_vpc_cidr' must be provided.
// If 'default_vpc_creation' is provided as false or not provided, 'vpc_id' must be provided.
if c.IsSet("default-vpc-creation") {
clusterIn["default_vpc_creation"] = c.Bool("default-vpc-creation")
if !c.IsSet("default-vpc-cidr") {
return fmt.Errorf("invalid parameters detected. Please provide 'default-vpc-cidr'")
}
clusterIn["default_vpc_cidr"] = c.String("default-vpc-cidr")
} else {
if !c.IsSet("vpc-id") {
return fmt.Errorf("invalid parameters detected. Please provide 'vpc-id'")
}
clusterIn["vpc_id"] = c.String("vpc-id")
}
if c.IsSet("public-access-ip-addresses") {
clusterIn["public_access_ip_addresses"] = strings.Split(c.String("public-access-ip-addresses"), ",")
}
labelIDsByName, labelNamesByID := LabelLoadsMapping(c)
if c.IsSet("labels") {
clusterIn["label_ids"] = LabelResolution(c, c.String("labels"), &labelNamesByID, &labelIDsByName)
}
cluster, err := clusterSvc.CreateCluster(&clusterIn)
if err != nil {
formatter.PrintFatal("Couldn't create cluster", err)
}
cluster.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*cluster); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ClusterUpdate subcommand function
func ClusterUpdate(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"id", "name"}, formatter)
clusterIn := map[string]interface{}{
"name": c.String("name"),
}
if c.IsSet("version") {
clusterIn["version"] = c.String("version")
}
if c.IsSet("public-access-ip-addresses") {
clusterIn["public_access_ip_addresses"] = strings.Split(c.String("public-access-ip-addresses"), ",")
}
cluster, err := clusterSvc.UpdateCluster(c.String("id"), &clusterIn)
if err != nil {
formatter.PrintFatal("Couldn't update cluster", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
cluster.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*cluster); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ClusterDelete subcommand function
func ClusterDelete(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"id"}, formatter)
cluster, err := clusterSvc.DeleteCluster(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't delete cluster", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
cluster.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*cluster); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ClusterRetry subcommand function
func ClusterRetry(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"id"}, formatter)
cluster, err := clusterSvc.RetryCluster(c.String("id"), &map[string]interface{}{})
if err != nil {
formatter.PrintFatal("Couldn't retry cluster", err)
}
_, labelNamesByID := LabelLoadsMapping(c)
cluster.FillInLabelNames(labelNamesByID)
if err = formatter.PrintItem(*cluster); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ClusterDiscard subcommand function
func ClusterDiscard(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"id"}, formatter)
err := clusterSvc.DiscardCluster(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't discard cluster", err)
}
return nil
}
// ClusterPlanShow subcommand function
func ClusterPlanShow(c *cli.Context) error {
debugCmdFuncInfo(c)
clusterSvc, formatter := WireUpCluster(c)
checkRequiredFlags(c, []string{"id"}, formatter)
clusterPlan, err := clusterSvc.GetClusterPlan(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't show cluster plan", err)
}
if err = formatter.PrintItem(*clusterPlan); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}