This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
/
create.go
92 lines (79 loc) · 2.43 KB
/
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
package gcp
import (
"fmt"
"github.com/AlexsJones/gravitywell/kinds"
"time"
"github.com/fatih/color"
containerpb "google.golang.org/genproto/googleapis/container/v1"
)
func (g *GCPProvider) Create(clusterp kinds.ProviderCluster) error {
//Convert generic node pool type into a specific GCP resource
var convertedNodePool []*containerpb.NodePool
for _, model := range clusterp.NodePools {
nodePool := new(containerpb.NodePool)
nodePool.Name = model.NodePool.Name
nodePool.InitialNodeCount = int32(model.NodePool.Count)
nodePool.Config = &containerpb.NodeConfig{
MachineType: model.NodePool.NodeType,
OauthScopes: clusterp.OauthScopes,
}
var labels = map[string]string{}
if len(clusterp.Labels) > 0 {
for index, element := range clusterp.Labels {
labels[index] = element
}
}
if len(model.NodePool.Labels) > 0 {
for index, element := range model.NodePool.Labels {
labels[index] = element
}
}
nodePool.Config.Labels = labels
convertedNodePool = append(convertedNodePool, nodePool)
}
// ----------------------------------------------------------------
var cluster *containerpb.Cluster
if len(clusterp.NodePools) == 0 {
cluster = &containerpb.Cluster{
Name: clusterp.Name,
Locations: clusterp.Zones,
InitialNodeCount: int32(clusterp.InitialNodeCount),
NodeConfig: &containerpb.NodeConfig{
MachineType: clusterp.InitialNodeType,
OauthScopes: clusterp.OauthScopes,
},
ResourceLabels: clusterp.Labels,
}
} else {
cluster = &containerpb.Cluster{
Name: clusterp.Name,
Locations: clusterp.Zones,
NodePools: convertedNodePool,
ResourceLabels: clusterp.Labels,
}
}
clusterReq := &containerpb.CreateClusterRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", clusterp.Project,
clusterp.Region),
Cluster: cluster,
}
clusterResponse, err := g.ClusterManagerClient.CreateCluster(g.Context, clusterReq)
if err != nil {
color.Red(err.Error())
return err
}
color.Blue(fmt.Sprintf("Started cluster build at %s", clusterResponse.StartTime))
for {
clust, err :=
g.ClusterManagerClient.GetCluster(g.Context, &containerpb.GetClusterRequest{Name: fmt.Sprintf("projects/%s/locations/%s/clusters/%s", clusterp.Project,
clusterp.Region, clusterp.Name)})
if err != nil {
return err
}
if clust.GetStatus() == containerpb.Cluster_RUNNING {
color.Green("Cluster running")
return nil
}
time.Sleep(time.Second)
}
}