This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
master_template.go
286 lines (256 loc) · 7.11 KB
/
master_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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/service/controller/key"
)
// NewMasterTemplate generates a new worker cloud config template and returns it
// as a base64 encoded string.
func (c *CloudConfig) NewMasterTemplate(ctx context.Context, cr v1alpha1.KVMConfig, data IgnitionTemplateData, node v1alpha1.ClusterNode, nodeIndex int) (string, error) {
var extension *masterExtension
{
certFiles, err := fetchCertFiles(ctx, data.CertsSearcher, key.ClusterID(cr), masterCertFiles)
if err != nil {
return "", microerror.Mask(err)
}
extension = &masterExtension{
certs: certFiles,
customObject: cr,
nodeIndex: nodeIndex,
}
}
var params k8scloudconfig.Params
{
params.APIServerEncryptionKey = string(data.ClusterKeys.APIServerEncryptionKey)
params.BaseDomain = key.BaseDomain(cr)
params.Cluster = cr.Spec.Cluster
// Ingress controller service remains in k8scloudconfig and will be
// removed in a later migration.
params.DisableIngressControllerService = true
params.Etcd = k8scloudconfig.Etcd{
ClientPort: key.EtcdPort,
HighAvailability: false,
InitialClusterState: k8scloudconfig.InitialClusterStateNew,
}
params.Extension = extension
params.ImagePullProgressDeadline = key.DefaultImagePullProgressDeadline
params.Node = node
params.Kubernetes.Apiserver.CommandExtraArgs = c.k8sAPIExtraArgs
params.Images = data.Images
params.Versions = data.Versions
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.DockerhubToken = c.dockerhubToken
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.MasterTemplate,
}
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 masterExtension struct {
certs []certs.File
customObject v1alpha1.KVMConfig
nodeIndex int
}
func (e *masterExtension) 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.MasterID)),
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)
calicoKubeKillFile := k8scloudconfig.FileMetadata{
AssetContent: calicoKubeKillScript,
Path: "/opt/calico-kube-kill",
Owner: k8scloudconfig.Owner{
User: k8scloudconfig.User{
Name: FileOwnerUserName,
},
Group: k8scloudconfig.Group{
Name: FileOwnerGroupName,
},
},
Permissions: 0755,
}
filesMeta = append(filesMeta, calicoKubeKillFile)
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 *masterExtension) Units() ([]k8scloudconfig.UnitAsset, error) {
unitsMeta := []k8scloudconfig.UnitMetadata{
// Mount etcd volume when directory first accessed
// This automount is workaround for
// https://bugzilla.redhat.com/show_bug.cgi?id=1184122
{
AssetContent: `[Unit]
Description=Automount for etcd volume
[Automount]
Where=/var/lib/etcd
[Install]
WantedBy=multi-user.target
`,
Name: "var-lib-etcd.automount",
Enabled: true,
},
// Mount for etcd volume activated by automount
{
AssetContent: `[Unit]
Description=Mount for etcd volume
[Mount]
What=etcdshare
Where=/var/lib/etcd
Options=trans=virtio,version=9p2000.L,cache=mmap
Type=9p
[Install]
WantedBy=multi-user.target
`,
Name: "var-lib-etcd.mount",
Enabled: false,
},
{
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,
},
{
AssetContent: `[Unit]
Description=Temporary fix for issues with calico-node and kube-proxy after master restart
Requires=k8s-kubelet.service
After=k8s-kubelet.service
[Service]
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/bin"
Environment="KUBECONFIG=/etc/kubernetes/kubeconfig/addons.yaml"
ExecStart=/opt/calico-kube-kill
[Install]
WantedBy=multi-user.target`,
Name: "calico-kube-kill.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 *masterExtension) VerbatimSections() []k8scloudconfig.VerbatimSection {
return nil
}