forked from vmware/cloud-provider-for-cloud-director
/
capiyaml_utils.go
213 lines (189 loc) · 7.14 KB
/
capiyaml_utils.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
package testingsdk
import (
"bufio"
"bytes"
"context"
"fmt"
"github.com/vmware/cloud-provider-for-cloud-director/pkg/vcdsdk"
swagger "github.com/vmware/cloud-provider-for-cloud-director/pkg/vcdswaggerclient"
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)
const (
MachineDeployment = "MachineDeployment"
KubeadmConfigTemplate = "KubeadmConfigTemplate"
VCDMachineTemplate = "VCDMachineTemplate"
)
func GetCapvcdYamlFromRde(capvcdRDE swagger.DefinedEntity) (string, error) {
specIf, ok := capvcdRDE.Entity["spec"]
if !ok {
return "", fmt.Errorf("unable to get spec field from capvcd RDE [%s]", capvcdRDE.Id)
}
spec, ok := specIf.(map[string]interface{})
if !ok {
return "", fmt.Errorf("unable to convert spec of capvcd rde [%s] from type [%T] to map[string]interface{}",
capvcdRDE.Id, specIf)
}
capiYamlIf, ok := spec["capiYaml"]
if !ok {
return "", fmt.Errorf("`capiYaml` field not found in spec for capvcd RDE [%s]", capvcdRDE.Id)
}
capiYaml, ok := capiYamlIf.(string)
if !ok {
return "", fmt.Errorf("unable to convert `capiYaml` of capvcd rde [%s] from type [%T] to string",
capvcdRDE.Id, specIf)
}
return capiYaml, nil
}
func GetMapBySpecName(specMap map[string]interface{}, specName string, sectionName string) (map[string]interface{}, error) {
entity, ok := specMap[specName]
if !ok {
return nil, fmt.Errorf("unable to get map: [%s] from %s in capiYaml String\n", specName, sectionName)
}
updatedSpecMap, ok := entity.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert [%T] to map[string]interface{}", entity)
}
return updatedSpecMap, nil
}
func getWorkerNodePoolObjectBase(ctx context.Context, client *vcdsdk.Client, clusterId string, objectType string) (map[string]interface{}, error) {
rde, err := getRdeById(ctx, client, clusterId)
if err != nil {
return nil, fmt.Errorf("unable to get defined entity by clusterId [%s]: [%v]", clusterId, err)
}
capiYaml, err := GetCapvcdYamlFromRde(*rde)
if err != nil {
return nil, fmt.Errorf("unable to get capiyaml from RDE: [%v]", err)
}
hundredKB := 100 * 1024
err = nil
yamlReader := k8syaml.NewYAMLReader(bufio.NewReader(bytes.NewReader([]byte(capiYaml))))
for err == nil {
yamlBytes, err := yamlReader.Read()
if err == io.EOF {
break
}
yamlDecoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(yamlBytes), hundredKB)
unstructuredObj := unstructured.Unstructured{}
err = yamlDecoder.Decode(&unstructuredObj)
if err != nil {
return nil, fmt.Errorf("unable to parse yaml segment: [%v]\n", err)
}
kind := unstructuredObj.GetKind()
switch kind {
case objectType:
return unstructuredObj.Object, nil
}
}
return nil, fmt.Errorf("unable to find any [%s] entities in capiyaml", objectType)
}
func CreateNewVCDMachineTemplate(ctx context.Context, client *vcdsdk.Client, clusterId string, nodePoolName string) (map[string]interface{}, error) {
newVcdMachineTemplate, err := getWorkerNodePoolObjectBase(ctx, client, clusterId, VCDMachineTemplate)
if err != nil {
return nil, fmt.Errorf("unable to get VCDMachineTemplateBase: [%v]", err)
}
metadataIf, ok := newVcdMachineTemplate["metadata"]
if !ok {
return nil, fmt.Errorf("VcdMachineTemplate base has no metadata attribute")
}
metadata, ok := metadataIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("could not convert %s.metadata to map", VCDMachineTemplate)
}
metadata["name"] = nodePoolName
newVcdMachineTemplate["metadata"] = metadata
return newVcdMachineTemplate, nil
}
func CreateNewKubeadmConfigTemplate(ctx context.Context, client *vcdsdk.Client, clusterId string, nodePoolName string) (map[string]interface{}, error) {
newKubeadmConfigTemplate, err := getWorkerNodePoolObjectBase(ctx, client, clusterId, KubeadmConfigTemplate)
if err != nil {
return nil, fmt.Errorf("unable to get KubeadmConfigTemplate: [%v]", err)
}
metadataIf, ok := newKubeadmConfigTemplate["metadata"]
if !ok {
return nil, fmt.Errorf("KubeadmConfigTemplate base has no metadata attribute")
}
metadata, ok := metadataIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("could not convert %s.metadata to map", VCDMachineTemplate)
}
metadata["name"] = nodePoolName
newKubeadmConfigTemplate["metadata"] = metadata
return newKubeadmConfigTemplate, nil
}
func CreateNewMachineDeployment(ctx context.Context, client *vcdsdk.Client, clusterId string, nodePoolName string, nodePoolSize int64) (map[string]interface{}, error) {
newMachineDeployment, err := getWorkerNodePoolObjectBase(ctx, client, clusterId, MachineDeployment)
if err != nil {
return nil, fmt.Errorf("unable to get MachineDeployment: [%v]", err)
}
// change node pool name in metadata
metadataIf, ok := newMachineDeployment["metadata"]
if !ok {
return nil, fmt.Errorf("MachineDeployment base has no metadata attribute")
}
metadata, ok := metadataIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("could not convert %s.metadata to map", VCDMachineTemplate)
}
metadata["name"] = nodePoolName
newMachineDeployment["metadata"] = metadata
// change node pool name in spec.template.spec.bootstrap.configRef.name
specMap, err := GetMapBySpecName(newMachineDeployment, "spec", MachineDeployment)
if err != nil {
return nil, fmt.Errorf("error converting %s.spec to map: [%v]", MachineDeployment, err)
}
templateIf, ok := specMap["template"]
if !ok {
return nil, fmt.Errorf("specMap has no template attribute")
}
template, ok := templateIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert spec.template to map")
}
spec2If, ok := template["spec"]
if !ok {
return nil, fmt.Errorf("spec.template has no spec attribute")
}
spec2, ok := spec2If.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert spec.template.spec to map")
}
bootstrapIf, ok := spec2["bootstrap"]
if !ok {
return nil, fmt.Errorf("spec.template.spec has no bootstrap attribute")
}
bootstrap, ok := bootstrapIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert spec.template.spec.bootstrap to map")
}
configRefIf, ok := bootstrap["configRef"]
if !ok {
return nil, fmt.Errorf("spec.template.spec.bootstrap has no configRef attribute")
}
configRef, ok := configRefIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert spec.template.spec.bootstrap.configRef to map")
}
configRef["name"] = nodePoolName
// change node pool name in spec.template.spec.infrastructureRef.name
infrastructureRefIf, ok := spec2["infrastructureRef"]
if !ok {
return nil, fmt.Errorf("spec.template.spec has no infrastructureRef attribute")
}
infrastructureRef, ok := infrastructureRefIf.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert spec.template.spec.infrastructureRef to map")
}
infrastructureRef["name"] = nodePoolName
// propagating changes up
// also change spec.replicas to nodePoolSize
bootstrap["configRef"] = configRef
spec2["bootstrap"] = bootstrap
spec2["infrastructureRef"] = infrastructureRef
template["spec"] = spec2
specMap["template"] = template
specMap["replicas"] = nodePoolSize
newMachineDeployment["spec"] = specMap
return newMachineDeployment, nil
}