This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
/
vmware.go
206 lines (190 loc) · 5.49 KB
/
vmware.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
package cmavmware
import (
"context"
"crypto/tls"
pb "github.com/samsung-cnct/cma-vmware/pkg/generated/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func CreateNewClient(hostname string, insecure bool) (ClientInterface, error) {
output := Client{}
err := output.CreateNewClient(hostname, insecure)
if err != nil {
return nil, err
}
return &output, err
}
func (a *Client) CreateNewClient(hostname string, insecure bool) error {
var err error
if insecure {
// This is for non TLS traffic
a.conn, err = grpc.Dial(hostname, grpc.WithInsecure())
if err != nil {
return err
}
} else {
// If TLS is enabled, we're going to create credentials, also using built in certificates
var tlsConf tls.Config
creds := credentials.NewTLS(&tlsConf)
a.conn, err = grpc.Dial(hostname, grpc.WithTransportCredentials(creds))
if err != nil {
return err
}
}
a.client = pb.NewClusterClient(a.conn)
return nil
}
func (a *Client) Close() error {
return a.conn.Close()
}
func (a *Client) SetClient(client pb.ClusterClient) {
a.client = client
}
func (a *Client) CreateCluster(input CreateClusterInput) (CreateClusterOutput, error) {
var workerNodes []*pb.VMWareMachineSpec
var controlPlaneNodes []*pb.VMWareMachineSpec
for _, j := range input.ControlPlaneNodes {
var labels []*pb.KubernetesLabel
for _, k := range j.Labels {
labels = append(labels, &pb.KubernetesLabel{Name: k.Name, Value: k.Value})
}
controlPlaneNodes = append(controlPlaneNodes, &pb.VMWareMachineSpec{
Host: j.Host,
Port: int32(j.Port),
Username: j.Username,
Password: j.Password,
Labels: labels,
})
}
for _, j := range input.WorkerNodes {
var labels []*pb.KubernetesLabel
for _, k := range j.Labels {
labels = append(labels, &pb.KubernetesLabel{Name: k.Name, Value: k.Value})
}
workerNodes = append(workerNodes, &pb.VMWareMachineSpec{
Host: j.Host,
Port: int32(j.Port),
Username: j.Username,
Password: j.Password,
Labels: labels,
})
}
result, err := a.client.CreateCluster(context.Background(), &pb.CreateClusterMsg{
Name: input.Name,
K8SVersion: input.K8SVersion,
ControlPlaneNodes: controlPlaneNodes,
WorkerNodes: workerNodes,
ApiEndpoint: input.APIEndpoint,
HighAvailability: input.HighAvailability,
NetworkFabric: input.NetworkFabric,
})
if err != nil {
return CreateClusterOutput{}, err
}
output := CreateClusterOutput{
Cluster: ClusterItem{
ID: result.Cluster.Id,
Name: result.Cluster.Name,
Status: result.Cluster.Status.String(),
},
}
return output, nil
}
func (a *Client) GetCluster(input GetClusterInput) (GetClusterOutput, error) {
result, err := a.client.GetCluster(context.Background(), &pb.GetClusterMsg{
Name: input.Name,
})
if err != nil {
return GetClusterOutput{}, err
}
output := GetClusterOutput{
Cluster: ClusterDetailItem{
ID: result.Cluster.Id,
Name: result.Cluster.Name,
Status: result.Cluster.Status.String(),
Kubeconfig: result.Cluster.Kubeconfig,
},
}
return output, nil
}
func (a *Client) DeleteCluster(input DeleteClusterInput) (DeleteClusterOutput, error) {
result, err := a.client.DeleteCluster(context.Background(), &pb.DeleteClusterMsg{
Name: input.Name,
})
if err != nil {
return DeleteClusterOutput{}, err
}
output := DeleteClusterOutput{
Status: result.Status,
}
return output, nil
}
func (a *Client) ListClusters(input ListClusterInput) (ListClusterOutput, error) {
var clusters []ClusterItem
result, err := a.client.GetClusterList(context.Background(), &pb.GetClusterListMsg{})
if err != nil {
return ListClusterOutput{}, err
}
for _, j := range result.Clusters {
clusters = append(clusters, ClusterItem{
ID: j.Id,
Name: j.Name,
Status: j.Status.String(),
})
}
output := ListClusterOutput{
Clusters: clusters,
}
return output, nil
}
func (a *Client) AdjustCluster(input AdjustClusterInput) (AdjustClusterOutput, error) {
var addNodes []*pb.VMWareMachineSpec
var removeNodes []*pb.AdjustClusterMsg_VMWareRemoveMachineSpec
for _, j := range input.AddNodes {
var labels []*pb.KubernetesLabel
for _, k := range j.Labels {
labels = append(labels, &pb.KubernetesLabel{Name: k.Name, Value: k.Value})
}
addNodes = append(addNodes, &pb.VMWareMachineSpec{
Host: j.Host,
Port: int32(j.Port),
Username: j.Username,
Password: j.Password,
Labels: labels,
})
}
for _, j := range input.RemoveNodes {
removeNodes = append(removeNodes, &pb.AdjustClusterMsg_VMWareRemoveMachineSpec{
Host: j.Host,
})
}
_, err := a.client.AdjustClusterNodes(context.Background(), &pb.AdjustClusterMsg{
Name: input.Name,
AddNodes: addNodes,
RemoveNodes: removeNodes,
})
if err != nil {
return AdjustClusterOutput{}, err
}
return AdjustClusterOutput{}, nil
}
func (a *Client) GetClusterUpgrades(input GetClusterUpgradesInput) (GetClusterUpgradesOutput, error) {
result, err := a.client.GetUpgradeClusterInformation(context.Background(), &pb.GetUpgradeClusterInformationMsg{
Name: input.Name,
})
if err != nil {
return GetClusterUpgradesOutput{}, err
}
output := GetClusterUpgradesOutput{}
for _, j := range result.Versions {
output.Versions = append(output.Versions, j)
}
return output, nil
}
func (a *Client) ClusterUpgrade(input ClusterUpgradeInput) (ClusterUpgradeOutput, error) {
_, err := a.client.UpgradeCluster(context.Background(), &pb.UpgradeClusterMsg{
Name: input.Name,
Version: input.Version,
})
return ClusterUpgradeOutput{}, err
}