-
Notifications
You must be signed in to change notification settings - Fork 82
/
kfcluster_create.go
107 lines (92 loc) · 2.51 KB
/
kfcluster_create.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
package kfcluster
import (
"fmt"
"os"
"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
)
var networkID, firewallID, size string
var kfcCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Example: "civo kfcluster create <KFCLUSTER-NAME> --network <NETWORK_ID> --size <SIZE> --firewall <FIREWALL_ID>",
Short: "Create a new kubeflow cluster",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()
check, region, err := utility.CheckAvailability("kfaas", common.RegionSet)
if err != nil {
utility.Error("Error checking availability %s", err)
os.Exit(1)
}
if !check {
utility.Error("Sorry you can't create a kubernetes cluster in the %s region", region)
os.Exit(1)
}
client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}
if common.RegionSet != "" {
client.Region = common.RegionSet
}
var network *civogo.Network
if networkID != "" {
network, err = client.FindNetwork(networkID)
if err != nil {
utility.Error("the network %s doesn't exist", networkID)
os.Exit(1)
}
}
if firewallID != "" {
_, err = client.FindFirewall(firewallID)
if err != nil {
utility.Error("the firewall %s doesn't exist", firewallID)
os.Exit(1)
}
}
sizes, err := client.ListInstanceSizes()
if err != nil {
utility.Error("Unable to list sizes %s", err)
os.Exit(1)
}
sizeIsValid := false
if size != "" {
for _, s := range sizes {
if s.Name == size && utility.SizeType(s.Name) == "KfCluster" {
sizeIsValid = true
break
}
}
if !sizeIsValid {
utility.Error("The provided size is not valid")
os.Exit(1)
}
}
kfCluster := civogo.CreateKfClusterReq{
Name: args[0],
NetworkID: network.ID,
Size: size,
FirewallID: firewallID,
Region: client.Region,
}
kfc, err := client.CreateKfCluster(kfCluster)
if err != nil {
utility.Error("Error creating kfcluster %s", err)
os.Exit(1)
}
ow := utility.NewOutputWriterWithMap(map[string]string{"id": kfc.ID, "name": kfc.Name})
switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("KfCluster (%s) with ID %s has been created\n", utility.Green(kfc.Name), kfc.ID)
}
},
}