-
Notifications
You must be signed in to change notification settings - Fork 272
/
template.go
216 lines (189 loc) Β· 9.26 KB
/
template.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
214
215
216
package nutanix
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/nutanix-cloud-native/prism-go-client/environment/credentials"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/crypto"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/templater"
"github.com/aws/eks-anywhere/pkg/types"
)
var jsonMarshal = json.Marshal
// TemplateBuilder builds templates for nutanix.
type TemplateBuilder struct {
datacenterSpec *v1alpha1.NutanixDatacenterConfigSpec
controlPlaneMachineSpec *v1alpha1.NutanixMachineConfigSpec
etcdMachineSpec *v1alpha1.NutanixMachineConfigSpec
workerNodeGroupMachineSpecs map[string]v1alpha1.NutanixMachineConfigSpec
creds credentials.BasicAuthCredential
now types.NowFunc
}
var _ providers.TemplateBuilder = &TemplateBuilder{}
func NewNutanixTemplateBuilder(
datacenterSpec *v1alpha1.NutanixDatacenterConfigSpec,
controlPlaneMachineSpec,
etcdMachineSpec *v1alpha1.NutanixMachineConfigSpec,
workerNodeGroupMachineSpecs map[string]v1alpha1.NutanixMachineConfigSpec,
creds credentials.BasicAuthCredential,
now types.NowFunc,
) *TemplateBuilder {
return &TemplateBuilder{
datacenterSpec: datacenterSpec,
controlPlaneMachineSpec: controlPlaneMachineSpec,
etcdMachineSpec: etcdMachineSpec,
workerNodeGroupMachineSpecs: workerNodeGroupMachineSpecs,
creds: creds,
now: now,
}
}
func (ntb *TemplateBuilder) GenerateCAPISpecControlPlane(clusterSpec *cluster.Spec, buildOptions ...providers.BuildMapOption) (content []byte, err error) {
var etcdMachineSpec v1alpha1.NutanixMachineConfigSpec
if clusterSpec.Cluster.Spec.ExternalEtcdConfiguration != nil {
etcdMachineSpec = *ntb.etcdMachineSpec
}
values := buildTemplateMapCP(ntb.datacenterSpec, clusterSpec, *ntb.controlPlaneMachineSpec, etcdMachineSpec)
for _, buildOption := range buildOptions {
buildOption(values)
}
bytes, err := templater.Execute(defaultCAPIConfigCP, values)
if err != nil {
return nil, err
}
return bytes, nil
}
func (ntb *TemplateBuilder) GenerateCAPISpecWorkers(clusterSpec *cluster.Spec, workloadTemplateNames, kubeadmconfigTemplateNames map[string]string) (content []byte, err error) {
workerSpecs := make([][]byte, 0, len(clusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations))
for _, workerNodeGroupConfiguration := range clusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations {
values := buildTemplateMapMD(clusterSpec, ntb.workerNodeGroupMachineSpecs[workerNodeGroupConfiguration.MachineGroupRef.Name], workerNodeGroupConfiguration)
values["workloadTemplateName"] = workloadTemplateNames[workerNodeGroupConfiguration.Name]
values["workloadkubeadmconfigTemplateName"] = kubeadmconfigTemplateNames[workerNodeGroupConfiguration.Name]
values["autoscalingConfig"] = workerNodeGroupConfiguration.AutoScalingConfiguration
bytes, err := templater.Execute(defaultClusterConfigMD, values)
if err != nil {
return nil, err
}
workerSpecs = append(workerSpecs, bytes)
}
return templater.AppendYamlResources(workerSpecs...), nil
}
func (ntb *TemplateBuilder) GenerateCAPISpecSecret(clusterSpec *cluster.Spec, buildOptions ...providers.BuildMapOption) (content []byte, err error) {
encodedCreds, err := jsonMarshal(ntb.creds)
if err != nil {
return nil, err
}
nutanixCreds := []credentials.Credential{{
Type: credentials.BasicAuthCredentialType,
Data: encodedCreds,
}}
credsJSON, err := jsonMarshal(nutanixCreds)
if err != nil {
return nil, err
}
values := buildTemplateMapSecret(clusterSpec, credsJSON)
for _, buildOption := range buildOptions {
buildOption(values)
}
bytes, err := templater.Execute(secretTemplate, values)
if err != nil {
return nil, err
}
return bytes, nil
}
func machineDeploymentName(clusterName, nodeGroupName string) string {
return fmt.Sprintf("%s-%s", clusterName, nodeGroupName)
}
func buildTemplateMapCP(
datacenterSpec *v1alpha1.NutanixDatacenterConfigSpec,
clusterSpec *cluster.Spec,
controlPlaneMachineSpec v1alpha1.NutanixMachineConfigSpec,
etcdMachineSpec v1alpha1.NutanixMachineConfigSpec,
) map[string]interface{} {
bundle := clusterSpec.VersionsBundle
format := "cloud-config"
values := map[string]interface{}{
"clusterName": clusterSpec.Cluster.Name,
"controlPlaneEndpointIp": clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Endpoint.Host,
"controlPlaneReplicas": clusterSpec.Cluster.Spec.ControlPlaneConfiguration.Count,
"controlPlaneSshAuthorizedKey": controlPlaneMachineSpec.Users[0].SshAuthorizedKeys[0],
"controlPlaneSshUsername": controlPlaneMachineSpec.Users[0].Name,
"eksaSystemNamespace": constants.EksaSystemNamespace,
"format": format,
"podCidrs": clusterSpec.Cluster.Spec.ClusterNetwork.Pods.CidrBlocks,
"serviceCidrs": clusterSpec.Cluster.Spec.ClusterNetwork.Services.CidrBlocks,
"kubernetesVersion": bundle.KubeDistro.Kubernetes.Tag,
"kubernetesRepository": bundle.KubeDistro.Kubernetes.Repository,
"corednsRepository": bundle.KubeDistro.CoreDNS.Repository,
"corednsVersion": bundle.KubeDistro.CoreDNS.Tag,
"etcdRepository": bundle.KubeDistro.Etcd.Repository,
"etcdImageTag": bundle.KubeDistro.Etcd.Tag,
"kubeVipImage": bundle.Nutanix.KubeVip.VersionedImage(),
"kubeVipSvcEnable": false,
"kubeVipLBEnable": false,
"externalEtcdVersion": bundle.KubeDistro.EtcdVersion,
"etcdCipherSuites": crypto.SecureCipherSuitesString(),
"nutanixEndpoint": datacenterSpec.Endpoint,
"nutanixPort": datacenterSpec.Port,
"nutanixAdditionalTrustBundle": datacenterSpec.AdditionalTrustBundle,
"nutanixInsecure": datacenterSpec.Insecure,
"vcpusPerSocket": controlPlaneMachineSpec.VCPUsPerSocket,
"vcpuSockets": controlPlaneMachineSpec.VCPUSockets,
"memorySize": controlPlaneMachineSpec.MemorySize.String(),
"systemDiskSize": controlPlaneMachineSpec.SystemDiskSize.String(),
"imageIDType": controlPlaneMachineSpec.Image.Type,
"imageName": controlPlaneMachineSpec.Image.Name,
"imageUUID": controlPlaneMachineSpec.Image.UUID,
"nutanixPEClusterIDType": controlPlaneMachineSpec.Cluster.Type,
"nutanixPEClusterName": controlPlaneMachineSpec.Cluster.Name,
"nutanixPEClusterUUID": controlPlaneMachineSpec.Cluster.UUID,
"subnetIDType": controlPlaneMachineSpec.Subnet.Type,
"subnetName": controlPlaneMachineSpec.Subnet.Name,
"subnetUUID": controlPlaneMachineSpec.Subnet.UUID,
}
if clusterSpec.Cluster.Spec.ExternalEtcdConfiguration != nil {
values["externalEtcd"] = true
values["externalEtcdReplicas"] = clusterSpec.Cluster.Spec.ExternalEtcdConfiguration.Count
values["etcdSshUsername"] = etcdMachineSpec.Users[0].Name
}
return values
}
func buildTemplateMapMD(clusterSpec *cluster.Spec, workerNodeGroupMachineSpec v1alpha1.NutanixMachineConfigSpec, workerNodeGroupConfiguration v1alpha1.WorkerNodeGroupConfiguration) map[string]interface{} {
bundle := clusterSpec.VersionsBundle
format := "cloud-config"
values := map[string]interface{}{
"clusterName": clusterSpec.Cluster.Name,
"eksaSystemNamespace": constants.EksaSystemNamespace,
"format": format,
"kubernetesVersion": bundle.KubeDistro.Kubernetes.Tag,
"workerReplicas": *workerNodeGroupConfiguration.Count,
"workerPoolName": "md-0",
"workerSshAuthorizedKey": workerNodeGroupMachineSpec.Users[0].SshAuthorizedKeys[0],
"workerSshUsername": workerNodeGroupMachineSpec.Users[0].Name,
"vcpusPerSocket": workerNodeGroupMachineSpec.VCPUsPerSocket,
"vcpuSockets": workerNodeGroupMachineSpec.VCPUSockets,
"memorySize": workerNodeGroupMachineSpec.MemorySize.String(),
"systemDiskSize": workerNodeGroupMachineSpec.SystemDiskSize.String(),
"imageIDType": workerNodeGroupMachineSpec.Image.Type,
"imageName": workerNodeGroupMachineSpec.Image.Name,
"imageUUID": workerNodeGroupMachineSpec.Image.UUID,
"nutanixPEClusterIDType": workerNodeGroupMachineSpec.Cluster.Type,
"nutanixPEClusterName": workerNodeGroupMachineSpec.Cluster.Name,
"nutanixPEClusterUUID": workerNodeGroupMachineSpec.Cluster.UUID,
"subnetIDType": workerNodeGroupMachineSpec.Subnet.Type,
"subnetName": workerNodeGroupMachineSpec.Subnet.Name,
"subnetUUID": workerNodeGroupMachineSpec.Subnet.UUID,
"workerNodeGroupName": fmt.Sprintf("%s-%s", clusterSpec.Cluster.Name, workerNodeGroupConfiguration.Name),
}
return values
}
func buildTemplateMapSecret(clusterSpec *cluster.Spec, creds []byte) map[string]interface{} {
values := map[string]interface{}{
"clusterName": clusterSpec.Cluster.Name,
"eksaSystemNamespace": constants.EksaSystemNamespace,
"base64EncodedCredentials": base64.StdEncoding.EncodeToString(creds),
}
return values
}