This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
worker_template.go
232 lines (203 loc) · 5.72 KB
/
worker_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package cloudconfig
import (
"context"
"fmt"
"github.com/giantswarm/apiextensions/v3/pkg/apis/provider/v1alpha1"
"github.com/giantswarm/certs/v3/pkg/certs"
k8scloudconfig "github.com/giantswarm/k8scloudconfig/v10/pkg/template"
"github.com/giantswarm/microerror"
"github.com/giantswarm/kvm-operator/v4/service/controller/key"
)
// NewWorkerTemplate generates a new worker cloud config template and returns it
// as a base64 encoded string.
func (c *CloudConfig) NewWorkerTemplate(ctx context.Context, cr v1alpha1.KVMConfig, data IgnitionTemplateData, node v1alpha1.ClusterNode, nodeIndex int) (string, error) {
var extension *workerExtension
{
certFiles, err := fetchCertFiles(ctx, data.CertsSearcher, key.ClusterID(cr), workerCertFiles)
if err != nil {
return "", microerror.Mask(err)
}
extension = &workerExtension{
certs: certFiles,
customObject: cr,
nodeIndex: nodeIndex,
}
}
var params k8scloudconfig.Params
{
params.BaseDomain = key.BaseDomain(cr)
params.Cluster = cr.Spec.Cluster
params.Extension = extension
params.Images = data.Images
params.Versions = data.Versions
params.Node = node
params.Proxy.HTTP = c.proxy.http
params.Proxy.HTTPS = c.proxy.https
params.Proxy.NoProxy = c.proxy.noProxy
params.RegistryMirrors = c.registryMirrors
params.SSOPublicKey = c.ssoPublicKey
params.ImagePullProgressDeadline = key.DefaultImagePullProgressDeadline
params.DockerhubToken = c.dockerhubToken
var workerFound bool
// Find index of worker in cr.Spec.Cluster.Workers to match it with the corresponding cr.Spec.KVM.Workers
for i, worker := range cr.Spec.Cluster.Workers {
if worker.ID == node.ID {
workerFound = true
for _, hostVolume := range cr.Spec.KVM.Workers[i].HostVolumes {
params.KVMWorkerMountTags = append(params.KVMWorkerMountTags, hostVolume.MountTag)
}
break
}
}
if !workerFound {
return "", microerror.Maskf(notFoundError, "node with id %#q not found in .Spec.Cluster.Workers", node.ID)
}
ignitionPath := k8scloudconfig.GetIgnitionPath(c.ignitionPath)
{
var err error
params.Files, err = k8scloudconfig.RenderFiles(ignitionPath, params)
if err != nil {
return "", microerror.Mask(err)
}
}
}
var newCloudConfig *k8scloudconfig.CloudConfig
{
cloudConfigConfig := k8scloudconfig.CloudConfigConfig{
Params: params,
Template: k8scloudconfig.WorkerTemplate,
}
var err error
newCloudConfig, err = k8scloudconfig.NewCloudConfig(cloudConfigConfig)
if err != nil {
return "", microerror.Mask(err)
}
err = newCloudConfig.ExecuteTemplate()
if err != nil {
return "", microerror.Mask(err)
}
}
return newCloudConfig.Base64(), nil
}
type workerExtension struct {
certs []certs.File
customObject v1alpha1.KVMConfig
nodeIndex int
}
func (e *workerExtension) Files() ([]k8scloudconfig.FileAsset, error) {
var filesMeta []k8scloudconfig.FileMetadata
for _, f := range e.certs {
m := k8scloudconfig.FileMetadata{
AssetContent: string(f.Data),
Path: f.AbsolutePath,
Owner: k8scloudconfig.Owner{
User: k8scloudconfig.User{
Name: FileOwnerUserName,
},
Group: k8scloudconfig.Group{
Name: FileOwnerGroupName,
},
},
Permissions: FilePermission,
}
filesMeta = append(filesMeta, m)
}
iscsiInitiatorFile := k8scloudconfig.FileMetadata{
AssetContent: fmt.Sprintf("InitiatorName=%s", key.IscsiInitiatorName(e.customObject, e.nodeIndex, key.WorkerID)),
Path: IscsiInitiatorNameFilePath,
Owner: k8scloudconfig.Owner{
User: k8scloudconfig.User{
Name: FileOwnerUserName,
},
Group: k8scloudconfig.Group{
Name: FileOwnerGroupName,
},
},
Permissions: IscsiInitiatorFilePermissions,
}
filesMeta = append(filesMeta, iscsiInitiatorFile)
// iscsi config as workaround for this bug https://github.com/kubernetes/kubernetes/issues/73181
iscsiConfigFile := k8scloudconfig.FileMetadata{
AssetContent: IscsiConfigFileContent,
Path: IscsiConfigFilePath,
Owner: k8scloudconfig.Owner{
User: k8scloudconfig.User{
Name: FileOwnerUserName,
},
Group: k8scloudconfig.Group{
Name: FileOwnerGroupName,
},
},
Permissions: IscsiConfigFilePermissions,
}
filesMeta = append(filesMeta, iscsiConfigFile)
var newFiles []k8scloudconfig.FileAsset
for _, fm := range filesMeta {
c, err := k8scloudconfig.RenderFileAssetContent(fm.AssetContent, nil)
if err != nil {
return nil, microerror.Mask(err)
}
fileAsset := k8scloudconfig.FileAsset{
Metadata: fm,
Content: c,
}
newFiles = append(newFiles, fileAsset)
}
return newFiles, nil
}
func (e *workerExtension) Units() ([]k8scloudconfig.UnitAsset, error) {
unitsMeta := []k8scloudconfig.UnitMetadata{
{
AssetContent: `[Unit]
Before=docker.service
Description=Mount for docker volume
[Mount]
What=/dev/disk/by-id/virtio-dockerfs
Where=/var/lib/docker
Type=xfs
[Install]
WantedBy=multi-user.target
`,
Name: "var-lib-docker.mount",
Enabled: true,
},
{
AssetContent: `[Unit]
Before=docker.service
Description=Mount for kubelet volume
[Mount]
What=/dev/disk/by-id/virtio-kubeletfs
Where=/var/lib/kubelet
Type=xfs
[Install]
WantedBy=multi-user.target
`,
Name: "var-lib-kubelet.mount",
Enabled: true,
},
{
Name: "iscsid.service",
Enabled: true,
},
{
Name: "multipathd.service",
Enabled: true,
},
}
var newUnits []k8scloudconfig.UnitAsset
for _, fm := range unitsMeta {
c, err := k8scloudconfig.RenderAssetContent(fm.AssetContent, nil)
if err != nil {
return nil, microerror.Mask(err)
}
unitAsset := k8scloudconfig.UnitAsset{
Metadata: fm,
Content: c,
}
newUnits = append(newUnits, unitAsset)
}
return newUnits, nil
}
func (e *workerExtension) VerbatimSections() []k8scloudconfig.VerbatimSection {
return nil
}