-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathokectl.go
More file actions
623 lines (529 loc) · 21.8 KB
/
okectl.go
File metadata and controls
623 lines (529 loc) · 21.8 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package main
// import libraries..
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/Jeffail/gabs"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/oracle/oci-go-sdk/common"
"github.com/oracle/oci-go-sdk/containerengine"
"github.com/oracle/oci-go-sdk/example/helpers"
)
// variables..
var (
// general..
cleanUp = false // func configureFileSystem
nodeLifeCycleState = ("nil") // func getNodeLifeCycleState
// app..
app = kingpin.New("okectl", "A command-line application for configuring Oracle OKE (Container Engine for Kubernetes.)")
configDir = app.Flag("configDir", "Path where output files are created or referenced - e.g. kubeconfig file. Specify as absolute path.").Default(".okectl").String()
// (c1) :: create cluster..
c1 = app.Command("createOkeCluster", "Create new OKE Kubernetes cluster.")
c1VcnId = c1.Flag("vcnId", "OCI VCN Id where cluster will be created.").Required().String()
c1CompartmentId = c1.Flag("compartmentId", "OCI Compartment-Id where cluster will be created.").Required().String()
c1Subnet1Id = c1.Flag("subnet1Id", "Cluster Control Plane LB Subnet 1.").Required().String()
c1Subnet2Id = c1.Flag("subnet2Id", "Cluster Control Plane LB Subnet 2.").Required().String()
c1Subnet3Id = c1.Flag("subnet3Id", "Worker Node Subnet 1.").Required().String()
c1Subnet4Id = c1.Flag("subnet4Id", "Worker Node Subnet 2.").String()
c1Subnet5Id = c1.Flag("subnet5Id", "Worker Node Subnet 3.").String()
c1ClusterName = c1.Flag("clusterName", "Kubernetes cluster name.").Default("dev-oke-001").String()
c1KubeVersion = c1.Flag("kubeVersion", "Kubernetes cluster version.").Default("v1.10.3").String()
c1NodeImageName = c1.Flag("nodeImageName", "OS image used for Worker Node(s).").Default("Oracle-Linux-7.4").String()
c1NodeShape = c1.Flag("nodeShape", "CPU/RAM allocated to Worker Node(s).").Default("VM.Standard1.1").String()
c1NodeSshKey = c1.Flag("nodeSshKey", "SSH key to provision to Worker Node(s) for remote access.").String()
c1QuantityWkrSubnets = c1.Flag("quantityWkrSubnets", "Number of subnets used to host Worker Node(s).").Default("1").Int()
c1QuantityPerSubnet = c1.Flag("quantityPerSubnet", "Number of Worker Nodes per subnet.").Default("1").Int()
c1WaitNodesActive = c1.Flag("waitNodesActive", "If waitNodesActive=all, wait & return when all nodes in the pool are active. " +
"If waitNodesActive=any, wait & return when any of the nodes in the pool are active. " +
"If waitNodesActive=false, no wait & return when the node pool is active.").Default("false").String()
// (d1) :: delete cluster..
d1 = app.Command("deleteOkeCluster", "Delete OKE Kubernetes cluster.")
d1ClusterId = d1.Flag("clusterId", "OKE Kubernetes cluster Id. If not specified, clusterId contained in nodepool.json will be used.").String()
// (c2) :: create kubeconfig.. //update to read clusterId from file..
c2 = app.Command("createOkeKubeconfig", "Create kubeconfig autentication artefact for kubectl.")
c2ClusterId = c2.Flag("clusterId", "OKE Kubernetes cluster ID. If not specified, clusterId contained in nodepool.json will be used.").String()
// (c3) :: create nodepool..
// (g3) :: get nodepool..
g3 = app.Command("getOkeNodePool", "Get cluster, node pool, and node details for a specified node pool.")
g3NodePoolId = g3.Flag("nodePoolId", "OKE Node Pool Id. If not specified, Id contained in nodepool.json will be used.").String()
g3TfExternalDs = g3.Flag("tfExternalDs", "Run as a Terraform external data source, & provide json only response data for Terraform.").Default("false").String()
g3WaitNodesActive = g3.Flag("waitNodesActive", "If waitNodesActive=all, wait & return when all nodes in the pool are active. " +
"If waitNodesActive=any, wait & return when any of the nodes in the pool are active. " +
"If waitNodesActive=false, no wait & return when the node pool is active.").Default("false").String()
// (d3) :: delete nodepool..
)
// oke crud..
func main() {
ctx := context.Background()
c, clerr := containerengine.NewContainerEngineClientWithConfigurationProvider(common.DefaultConfigProvider())
helpers.FatalIfError(clerr)
// command-line args & flags..
app.Version("0.0.3")
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
// create cluster..
case c1.FullCommand():
fmt.Println("")
fmt.Println("OKECTL :: Create Cluster :: Request Parameters ...")
fmt.Println("-------------------------------------------------------")
fmt.Println("configDir:", *configDir)
fmt.Println("clusterName:", *c1ClusterName)
fmt.Println("kubeVersion:", *c1KubeVersion)
fmt.Println("vcnId:", *c1VcnId)
fmt.Println("compartmentId:", *c1CompartmentId)
fmt.Println("subnet1Id:", *c1Subnet1Id)
fmt.Println("subnet2Id:", *c1Subnet2Id)
fmt.Println("subnet3Id:", *c1Subnet3Id)
fmt.Println("subnet4Id:", *c1Subnet4Id)
fmt.Println("subnet5Id:", *c1Subnet5Id)
fmt.Println("nodeImageName:", *c1NodeImageName)
fmt.Println("nodeShape:", *c1NodeShape)
fmt.Println("nodeSshKey:", *c1NodeSshKey)
fmt.Println("quantityWkrSubnets:", *c1QuantityWkrSubnets)
fmt.Println("quantityPerSubnet:", *c1QuantityPerSubnet)
fmt.Println("waitNodesActive:", *c1WaitNodesActive)
fmt.Println("")
// brief pause..
time.Sleep(5 * time.Second)
// configure file system..
cleanUp = true
configDirPath := configureFileSystem(*configDir, cleanUp)
// create cluster..
createClusterResp := createCluster(ctx, c, *c1ClusterName, *c1VcnId, *c1CompartmentId, *c1KubeVersion, *c1Subnet1Id, *c1Subnet2Id)
// wait for create cluster completion..
workReqRespCls := waitUntilWorkRequestComplete(c, createClusterResp.OpcWorkRequestId)
fmt.Println("OKECTL :: Create Cluster :: Complete ...")
clusterId := getResourceID(workReqRespCls.Resources, containerengine.WorkRequestResourceActionTypeCreated, "CLUSTER")
// create nodepool..
createNodePoolResp := createNodePool(ctx, c, *c1CompartmentId, *c1ClusterName, *clusterId, *c1KubeVersion, *c1NodeImageName, *c1NodeShape, *c1NodeSshKey, *c1Subnet3Id, *c1Subnet4Id, *c1Subnet5Id, *c1QuantityWkrSubnets, *c1QuantityPerSubnet)
// wait for create nodepool completion..
workReqRespNpl := waitUntilWorkRequestComplete(c, createNodePoolResp.OpcWorkRequestId)
fmt.Println("OKECTL :: Create NodePool :: Complete ...")
nodePoolId := getResourceID(workReqRespNpl.Resources, containerengine.WorkRequestResourceActionTypeCreated, "NODEPOOL")
// wait for create node completion..
if *c1WaitNodesActive != "false" {
nodeProvisioning := true
// This loop continues while "nodeProvisioning" is true..
for nodeProvisioning {
getNodeLifeCycleState(ctx, c, *nodePoolId)
// if we are waiting for all nodes to be active..
if *c1WaitNodesActive == "all" {
boolLcs := (strings.Contains(nodeLifeCycleState, "ATING"))
if nodeLifeCycleState != "{}" {
if boolLcs != true {
nodeProvisioning = false
}
}
}
// if we are waiting for any nodes to be active..
if *c1WaitNodesActive == "any" {
boolLcs := (strings.Contains(nodeLifeCycleState, "ACTIVE"))
if nodeLifeCycleState != "{}" {
if boolLcs == true {
nodeProvisioning = false
}
}
}
time.Sleep(15 * time.Second)
}
}
fmt.Println("OKECTL :: Create Node(s) :: Complete ...")
// get nodepool details & create nodepool.json..
getNodePool(ctx, c, *nodePoolId, configDirPath)
// create kubeconfig file..
getKubeConfig(ctx, c, *clusterId, configDirPath)
// done, output config data..
fmt.Println("")
fmt.Println("OKECTL :: Create Cluster :: Complete ...")
fmt.Println("-------------------------------------------------------")
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println(err)
}
strNodePool := string(content)
fmt.Println(strNodePool)
// delete cluster..
case d1.FullCommand():
var clusterId(string)
// no --clusterId flag provided, reading nodepool.json..
if *d1ClusterId == "" {
// configure file system..
cleanUp = false
configDirPath := configureFileSystem(*configDir, cleanUp)
// read nodepool.json..
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println("OKECTL :: No --clusterId flag provided, error reading nodepool.json at specified path :: Exiting..")
fmt.Println(err)
os.Exit(3)
}
// get clusterId from nodepool.json..
jsonParsed, err := gabs.ParseJSON(content)
clusterId = (jsonParsed.Path("clusterId").String())
*d1ClusterId = clusterId[1 : len(clusterId)-1]
}
fmt.Println("")
fmt.Println("OKECTL :: Delete Cluster :: Request Parameters ...")
fmt.Println("-------------------------------------------------------")
fmt.Println("clusterId:", *d1ClusterId)
fmt.Println("")
// brief pause..
time.Sleep(5 * time.Second)
// delete cluster..
deleteClusterResp := deleteCluster(ctx, c, *d1ClusterId)
// wait for delete cluster completion..
waitUntilWorkRequestComplete(c, deleteClusterResp.OpcWorkRequestId)
// done..
fmt.Println("")
fmt.Println("OKECTL :: Delete Cluster :: Complete ...")
// create kubeconfig..
case c2.FullCommand():
var clusterId(string)
// no --clusterId flag provided, reading nodepool.json..
if *c2ClusterId == "" {
// configure file system..
cleanUp = false
configDirPath := configureFileSystem(*configDir, cleanUp)
// read nodepool.json..
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println("OKECTL :: No --clusterId flag provided, error reading nodepool.json at specified path :: Exiting..")
fmt.Println(err)
os.Exit(3)
}
// get clusterId from nodepool.json..
jsonParsed, err := gabs.ParseJSON(content)
clusterId = (jsonParsed.Path("clusterId").String())
*c2ClusterId = clusterId[1 : len(clusterId)-1]
}
fmt.Println("")
fmt.Println("OKECTL :: Create kubeconfig :: Request Parameters ...")
fmt.Println("-------------------------------------------------------")
fmt.Println("configDir:", *configDir)
fmt.Println("clusterId:", *c2ClusterId)
fmt.Println("")
// brief pause..
time.Sleep(5 * time.Second)
// configure file system..
cleanUp = false
configDirPath := configureFileSystem(*configDir, cleanUp)
// create kubeconfig file..
getKubeConfig(ctx, c, *c2ClusterId, configDirPath)
// done..
fmt.Println("")
fmt.Println("OKECTL :: Create kubeconfig :: Complete ...")
// get node pool..
case g3.FullCommand():
var nodePoolId(string)
// configure file system..
cleanUp = false
configDirPath := configureFileSystem(*configDir, cleanUp)
// no --nodePoolId flag provided, reading nodepool.json..
if *g3NodePoolId == "" {
// read nodepool.json..
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println("OKECTL :: No --nodePoolId flag provided, error reading nodepool.json at specified path :: Exiting..")
fmt.Println(err)
os.Exit(3)
}
// get node pool id from nodepool.json..
jsonParsed, err := gabs.ParseJSON(content)
nodePoolId = (jsonParsed.Path("id").String())
*g3NodePoolId = nodePoolId[1 : len(nodePoolId)-1]
}
if *g3TfExternalDs == "false" {
fmt.Println("")
fmt.Println("OKECTL :: Get NodePool :: Request Parameters ...")
fmt.Println("-------------------------------------------------------")
fmt.Println("nodePoolId:", *g3NodePoolId)
fmt.Println("waitNodesActive:", *g3WaitNodesActive)
fmt.Println("tfExternalDs:", *g3TfExternalDs)
fmt.Println("")
}
// brief pause..
time.Sleep(5 * time.Second)
// wait for create node completion - all nodes..
if *g3WaitNodesActive != "false" {
nodeProvisioning := true
// This loop continues while "nodeProvisioning" is true..
for nodeProvisioning {
getNodeLifeCycleState(ctx, c, *g3NodePoolId)
// if we are waiting for all nodes to be active..
if *g3WaitNodesActive == "all" {
boolLcs := (strings.Contains(nodeLifeCycleState, "ATING"))
if nodeLifeCycleState != "{}" {
if boolLcs != true {
nodeProvisioning = false
}
}
}
// if we are waiting for any nodes to be active..
if *g3WaitNodesActive == "any" {
boolLcs := (strings.Contains(nodeLifeCycleState, "ACTIVE"))
if nodeLifeCycleState != "{}" {
if boolLcs == true {
nodeProvisioning = false
}
}
}
time.Sleep(15 * time.Second)
}
}
// get nodepool details & create nodepool.json..
getNodePool(ctx, c, *g3NodePoolId, configDirPath)
// done, output config data..
// if we are running as a terraform external data source, return only json data..
if *g3TfExternalDs == "true" {
// read nodepool.json..
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println("OKECTL :: No --nodePoolId flag provided, error reading nodepool.json at specified path :: Exiting..")
fmt.Println(err)
os.Exit(3)
}
// get worker node publicip from nodepool.json..
jsonParsed, err := gabs.ParseJSON(content)
nodeIps := (jsonParsed.Path("nodes.publicIp").String())
nodeIp := findIP(nodeIps)
jsonObj := gabs.New()
jsonObj.Set(nodeIp, "workerNodeIp")
fmt.Println(jsonObj.String())
} else {
// not running as terraform external data source, return verbose output..
fmt.Println("")
fmt.Println("OKECTL :: Get NodePool :: Complete ...")
fmt.Println("-------------------------------------------------------")
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
content, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println(err)
}
strNodePool := string(content)
fmt.Println(strNodePool)
}
}
}
// functions..
// configure local file system..
func configureFileSystem(configDir string, cleanUp bool) (configDirPath string) {
// if default configDir..
if configDir == ".okectl" {
// find our okectl binary path..
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println(err)
}
// clean-up & create configDir..
configDirPath = (dir + string(os.PathSeparator) + configDir)
if cleanUp == true {
err = os.RemoveAll(configDirPath)
if err != nil {
fmt.Println(err)
}
}
if _, err := os.Stat(configDir); err != nil {
err = os.MkdirAll(configDirPath, 0777)
if err != nil {
fmt.Println(err)
}
}
}
// if custom configDir..
if configDir != ".okectl" {
if _, err := os.Stat(configDir); err == nil {
// specified configDir exists..
configDirPath = configDir
} else {
// specified configDir does not exist..
fmt.Println("OKECTL :: Directory --configDir not found :: Exiting ...")
os.Exit(3)
}
}
return configDirPath
}
// create cluster..
func createCluster(
ctx context.Context,
client containerengine.ContainerEngineClient,
clusterName, vcnId, compartmentId, kubeVersion, subnet1Id, subnet2Id string) containerengine.CreateClusterResponse {
req := containerengine.CreateClusterRequest{}
req.Name = common.String(clusterName)
req.CompartmentId = common.String(compartmentId)
req.VcnId = common.String(vcnId)
req.KubernetesVersion = common.String(kubeVersion)
req.Options = &containerengine.ClusterCreateOptions{
ServiceLbSubnetIds: []string{subnet1Id, subnet2Id},
AddOns: &containerengine.AddOnOptions{
IsKubernetesDashboardEnabled: common.Bool(true),
IsTillerEnabled: common.Bool(true),
},
}
fmt.Println("OKECTL :: Create Cluster :: Submitted ...")
resp, err := client.CreateCluster(ctx, req)
helpers.FatalIfError(err)
return resp
}
// delete cluster..
func deleteCluster(ctx context.Context, client containerengine.ContainerEngineClient, clusterId string) containerengine.DeleteClusterResponse {
req := containerengine.DeleteClusterRequest{
ClusterId: common.String(clusterId),
}
fmt.Println("OKECTL :: Delete Cluster :: Submitted ...")
resp, err := client.DeleteCluster(ctx, req)
helpers.FatalIfError(err)
return resp
}
// create nodepool..
func createNodePool(
ctx context.Context,
client containerengine.ContainerEngineClient,
compartmentId, clusterName, clusterId, kubeVersion, nodeImageName, nodeShape, nodeSshKey, subnet3Id, subnet4Id, subnet5Id string, quantityWkrSubnets, quantityPerSubnet int) containerengine.CreateNodePoolResponse {
req := containerengine.CreateNodePoolRequest{}
req.CompartmentId = common.String(compartmentId)
req.Name = common.String(clusterName)
req.ClusterId = common.String(clusterId)
req.KubernetesVersion = common.String(kubeVersion)
req.NodeImageName = common.String(nodeImageName)
req.NodeShape = common.String(nodeShape)
// worker node ssh key..
if nodeSshKey != "Null" {
req.SshPublicKey = common.String(nodeSshKey)
}
// worker subnets..
if quantityWkrSubnets == 1 {
req.SubnetIds = []string{subnet3Id}
}
if quantityWkrSubnets == 2 {
req.SubnetIds = []string{subnet3Id, subnet4Id}
}
if quantityWkrSubnets == 3 {
req.SubnetIds = []string{subnet3Id, subnet4Id, subnet5Id}
}
req.QuantityPerSubnet = common.Int(quantityPerSubnet)
fmt.Println("OKECTL :: Create NodePool :: Submitted ...")
resp, err := client.CreateNodePool(ctx, req)
helpers.FatalIfError(err)
return resp
}
// delete nodepool
func deleteNodePool(ctx context.Context, client containerengine.ContainerEngineClient, nodePoolID *string) {
deleteReq := containerengine.DeleteNodePoolRequest{
NodePoolId: nodePoolID,
}
client.DeleteNodePool(ctx, deleteReq)
fmt.Println("deleting nodepool")
}
// get worker node lifecycle status..
func getNodeLifeCycleState(
ctx context.Context,
client containerengine.ContainerEngineClient,
nodePoolId string) containerengine.GetNodePoolResponse {
req := containerengine.GetNodePoolRequest{}
req.NodePoolId = common.String(nodePoolId)
resp, err := client.GetNodePool(ctx, req)
helpers.FatalIfError(err)
// marshal & parse json..
nodePoolResp := resp.NodePool
nodesJson, _ := json.Marshal(nodePoolResp)
jsonParsed, _ := gabs.ParseJSON(nodesJson)
nodeLifeCycleState = (jsonParsed.Path("nodes.lifecycleState").String())
return resp
}
// get nodepool details & create nodepool.json..
func getNodePool(
ctx context.Context,
client containerengine.ContainerEngineClient,
nodePoolId, configDirPath string) containerengine.GetNodePoolResponse {
req := containerengine.GetNodePoolRequest{}
req.NodePoolId = common.String(nodePoolId)
if *g3TfExternalDs == "false" {
fmt.Println("OKECTL :: Getting NodePool Data ...")
}
resp, err := client.GetNodePool(ctx, req)
helpers.FatalIfError(err)
// create output file..
configFilePath := configDirPath + string(os.PathSeparator) + "nodepool.json"
file, err := os.Create(configFilePath)
if err != nil {
fmt.Println("OKECTL :: Error Creating nodepool.json File:", err)
}
defer file.Close()
// populate nodepool.json file..
nodePoolResp := resp.NodePool
nodesJsonIndent, _ := json.MarshalIndent(nodePoolResp, "", "\t")
err = ioutil.WriteFile(configFilePath, nodesJsonIndent, 0666)
if err != nil {
fmt.Println("OKECTL :: Error Writing nodepool.json File:", err)
}
helpers.FatalIfError(err)
return resp
}
// create kubeconfig..
func getKubeConfig(
ctx context.Context,
client containerengine.ContainerEngineClient,
clusterId, configDirPath string) containerengine.CreateKubeconfigResponse {
req := containerengine.CreateKubeconfigRequest{}
req.ClusterId = common.String(clusterId)
req.Expiration = common.Int(360)
fmt.Println("OKECTL :: Getting kubeconfig Data ...")
// create output file..
configFilePath := configDirPath + string(os.PathSeparator) + "kubeconfig"
file, err := os.Create(configFilePath)
if err != nil {
fmt.Println("Cannot create kubeconfig file", err)
}
defer file.Close()
// populate output file..
resp, err := client.CreateKubeconfig(ctx, req)
_, err = io.Copy(file, resp.Content)
if err != nil {
fmt.Println("OKECTL :: Error Writing kubeconfig File:", err)
}
helpers.FatalIfError(err)
return resp
}
// locate ip address..
func findIP(input string) string {
numBlock := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
regexPattern := numBlock + "\\." + numBlock + "\\." + numBlock + "\\." + numBlock
regEx := regexp.MustCompile(regexPattern)
return regEx.FindString(input)
}
// wait until work request finishes..
func waitUntilWorkRequestComplete(client containerengine.ContainerEngineClient, workReuqestID *string) containerengine.GetWorkRequestResponse {
// retry GetWorkRequest call until TimeFinished is set..
shouldRetryFunc := func(r common.OCIOperationResponse) bool {
return r.Response.(containerengine.GetWorkRequestResponse).TimeFinished == nil
}
getWorkReq := containerengine.GetWorkRequestRequest{
WorkRequestId: workReuqestID,
RequestMetadata: helpers.GetRequestMetadataWithCustomizedRetryPolicy(shouldRetryFunc),
}
getResp, err := client.GetWorkRequest(context.Background(), getWorkReq)
helpers.FatalIfError(err)
return getResp
}
// getResourceID return a resource ID based on the filter of resource actionType and entityType..
func getResourceID(resources []containerengine.WorkRequestResource, actionType containerengine.WorkRequestResourceActionTypeEnum, entityType string) *string {
for _, resource := range resources {
if resource.ActionType == actionType && strings.ToUpper(*resource.EntityType) == entityType {
return resource.Identifier
}
}
fmt.Println("OKECTL :: Unable to obtain Resource ID ...")
return nil
}