forked from openshift/installer
/
ignition.go
544 lines (462 loc) · 16.7 KB
/
ignition.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package image
import (
"context"
"fmt"
"net"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/coreos/ignition/v2/config/util"
igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
"github.com/coreos/stream-metadata-go/arch"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
"github.com/openshift/assisted-service/api/v1beta1"
"github.com/openshift/assisted-service/models"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/agent/agentconfig"
"github.com/openshift/installer/pkg/asset/agent/manifests"
"github.com/openshift/installer/pkg/asset/agent/mirror"
"github.com/openshift/installer/pkg/asset/ignition"
"github.com/openshift/installer/pkg/asset/ignition/bootstrap"
"github.com/openshift/installer/pkg/asset/password"
"github.com/openshift/installer/pkg/asset/tls"
"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/agent"
"github.com/openshift/installer/pkg/version"
)
const rendezvousHostEnvPath = "/etc/assisted/rendezvous-host.env"
const manifestPath = "/etc/assisted/manifests"
const hostnamesPath = "/etc/assisted/hostnames"
const nmConnectionsPath = "/etc/assisted/network"
const extraManifestPath = "/etc/assisted/extra-manifests"
const registriesConfPath = "/etc/containers/registries.conf"
const registryCABundlePath = "/etc/pki/ca-trust/source/anchors/domain.crt"
// Ignition is an asset that generates the agent installer ignition file.
type Ignition struct {
Config *igntypes.Config
CPUArch string
RendezvousIP string
}
// agentTemplateData is the data used to replace values in agent template
// files.
type agentTemplateData struct {
ServiceProtocol string
PullSecret string
ControlPlaneAgents int
WorkerAgents int
ReleaseImages string
ReleaseImage string
ReleaseImageMirror string
HaveMirrorConfig bool
PublicContainerRegistries string
InfraEnvID string
ClusterName string
OSImage *models.OsImage
Proxy *v1beta1.Proxy
ConfigImageFiles string
}
// Name returns the human-friendly name of the asset.
func (a *Ignition) Name() string {
return "Agent Installer Ignition"
}
// Dependencies returns the assets on which the Ignition asset depends.
func (a *Ignition) Dependencies() []asset.Asset {
return []asset.Asset{
&manifests.AgentManifests{},
&manifests.ExtraManifests{},
&tls.KubeAPIServerLBSignerCertKey{},
&tls.KubeAPIServerLocalhostSignerCertKey{},
&tls.KubeAPIServerServiceNetworkSignerCertKey{},
&tls.AdminKubeConfigSignerCertKey{},
&password.KubeadminPassword{},
&agentconfig.AgentConfig{},
&mirror.RegistriesConf{},
&mirror.CaBundle{},
}
}
// Generate generates the agent installer ignition.
func (a *Ignition) Generate(dependencies asset.Parents) error {
agentManifests := &manifests.AgentManifests{}
agentConfigAsset := &agentconfig.AgentConfig{}
extraManifests := &manifests.ExtraManifests{}
dependencies.Get(agentManifests, agentConfigAsset, extraManifests)
pwd := &password.KubeadminPassword{}
dependencies.Get(pwd)
pwdHash := string(pwd.PasswordHash)
infraEnv := agentManifests.InfraEnv
config := igntypes.Config{
Ignition: igntypes.Ignition{
Version: igntypes.MaxVersion.String(),
},
Passwd: igntypes.Passwd{
Users: []igntypes.PasswdUser{
{
Name: "core",
SSHAuthorizedKeys: []igntypes.SSHAuthorizedKey{
igntypes.SSHAuthorizedKey(infraEnv.Spec.SSHAuthorizedKey),
},
PasswordHash: &pwdHash,
},
},
},
}
nodeZeroIP, err := RetrieveRendezvousIP(agentConfigAsset.Config, agentManifests.NMStateConfigs)
if err != nil {
return err
}
logrus.Infof("The rendezvous host IP (node0 IP) is %s", nodeZeroIP)
a.RendezvousIP = nodeZeroIP
// Default to x86_64
archName := arch.RpmArch(types.ArchitectureAMD64)
if infraEnv.Spec.CpuArchitecture != "" {
archName = infraEnv.Spec.CpuArchitecture
}
releaseImageList, err := releaseImageList(agentManifests.ClusterImageSet.Spec.ReleaseImage, archName)
if err != nil {
return err
}
registriesConfig := &mirror.RegistriesConf{}
registryCABundle := &mirror.CaBundle{}
dependencies.Get(registriesConfig, registryCABundle)
publicContainerRegistries := getPublicContainerRegistries(registriesConfig)
releaseImageMirror := mirror.GetMirrorFromRelease(agentManifests.ClusterImageSet.Spec.ReleaseImage, registriesConfig)
infraEnvID := uuid.New().String()
logrus.Debug("Generated random infra-env id ", infraEnvID)
osImage, err := getOSImagesInfo(archName)
if err != nil {
return err
}
a.CPUArch = *osImage.CPUArchitecture
clusterName := fmt.Sprintf("%s.%s",
agentManifests.ClusterDeployment.Spec.ClusterName,
agentManifests.ClusterDeployment.Spec.BaseDomain)
agentTemplateData := getTemplateData(
clusterName,
agentManifests.GetPullSecretData(),
releaseImageList,
agentManifests.ClusterImageSet.Spec.ReleaseImage,
releaseImageMirror,
len(registriesConfig.MirrorConfig) > 0,
publicContainerRegistries,
agentManifests.AgentClusterInstall,
infraEnvID,
osImage,
infraEnv.Spec.Proxy)
err = bootstrap.AddStorageFiles(&config, "/", "agent/files", agentTemplateData)
if err != nil {
return err
}
rendezvousHostFile := ignition.FileFromString(rendezvousHostEnvPath,
"root", 0644,
getRendezvousHostEnv(agentTemplateData.ServiceProtocol, nodeZeroIP))
config.Storage.Files = append(config.Storage.Files, rendezvousHostFile)
err = addBootstrapScripts(&config, agentManifests.ClusterImageSet.Spec.ReleaseImage)
if err != nil {
return err
}
// add ZTP manifests to manifestPath
for _, file := range agentManifests.FileList {
manifestFile := ignition.FileFromBytes(filepath.Join(manifestPath, filepath.Base(file.Filename)),
"root", 0600, file.Data)
config.Storage.Files = append(config.Storage.Files, manifestFile)
}
// add AgentConfig if provided
if agentConfigAsset.Config != nil {
agentConfigFile := ignition.FileFromBytes(filepath.Join(manifestPath, filepath.Base(agentConfigAsset.File.Filename)),
"root", 0600, agentConfigAsset.File.Data)
config.Storage.Files = append(config.Storage.Files, agentConfigFile)
}
addMacAddressToHostnameMappings(&config, agentConfigAsset)
err = addStaticNetworkConfig(&config, agentManifests.StaticNetworkConfigs)
if err != nil {
return err
}
enabledServices := getDefaultEnabledServices()
// Enable pre-network-manager-config.service only when there are network configs defined
if len(agentManifests.StaticNetworkConfigs) != 0 {
enabledServices = append(enabledServices, "pre-network-manager-config.service")
}
err = bootstrap.AddSystemdUnits(&config, "agent/systemd/units", agentTemplateData, enabledServices)
if err != nil {
return err
}
addTLSData(&config, dependencies)
addMirrorData(&config, registriesConfig, registryCABundle)
err = addHostConfig(&config, agentConfigAsset)
if err != nil {
return err
}
err = addExtraManifests(&config, extraManifests)
if err != nil {
return err
}
a.Config = &config
return nil
}
func getDefaultEnabledServices() []string {
return []string{
"agent-interactive-console.service",
"agent-register-infraenv.service",
"agent-register-cluster.service",
"agent.service",
"assisted-service-db.service",
"assisted-service-pod.service",
"assisted-service.service",
"node-zero.service",
"multipathd.service",
"selinux.service",
"install-status.service",
"set-hostname.service",
"start-cluster-installation.service",
}
}
func addBootstrapScripts(config *igntypes.Config, releaseImage string) (err error) {
// Set up bootstrap service recording
if err := bootstrap.AddStorageFiles(config,
"/usr/local/bin/bootstrap-service-record.sh",
"bootstrap/files/usr/local/bin/bootstrap-service-record.sh",
nil); err != nil {
return err
}
// Use bootstrap script to get container images
relImgData := struct{ ReleaseImage string }{
ReleaseImage: releaseImage,
}
for _, script := range []string{"release-image.sh", "release-image-download.sh"} {
if err := bootstrap.AddStorageFiles(config,
"/usr/local/bin/"+script,
"bootstrap/files/usr/local/bin/"+script+".template",
relImgData); err != nil {
return err
}
}
return nil
}
func getTemplateData(name, pullSecret, releaseImageList, releaseImage,
releaseImageMirror string, haveMirrorConfig bool, publicContainerRegistries string,
agentClusterInstall *hiveext.AgentClusterInstall,
infraEnvID string,
osImage *models.OsImage,
proxy *v1beta1.Proxy) *agentTemplateData {
return &agentTemplateData{
ServiceProtocol: "http",
PullSecret: pullSecret,
ControlPlaneAgents: agentClusterInstall.Spec.ProvisionRequirements.ControlPlaneAgents,
WorkerAgents: agentClusterInstall.Spec.ProvisionRequirements.WorkerAgents,
ReleaseImages: releaseImageList,
ReleaseImage: releaseImage,
ReleaseImageMirror: releaseImageMirror,
HaveMirrorConfig: haveMirrorConfig,
PublicContainerRegistries: publicContainerRegistries,
InfraEnvID: infraEnvID,
ClusterName: name,
OSImage: osImage,
Proxy: proxy,
}
}
func getRendezvousHostEnv(serviceProtocol, nodeZeroIP string) string {
serviceBaseURL := url.URL{
Scheme: serviceProtocol,
Host: net.JoinHostPort(nodeZeroIP, "8090"),
Path: "/",
}
imageServiceBaseURL := url.URL{
Scheme: serviceProtocol,
Host: net.JoinHostPort(nodeZeroIP, "8888"),
Path: "/",
}
return fmt.Sprintf(`NODE_ZERO_IP=%s
SERVICE_BASE_URL=%s
IMAGE_SERVICE_BASE_URL=%s
`, nodeZeroIP, serviceBaseURL.String(), imageServiceBaseURL.String())
}
func addStaticNetworkConfig(config *igntypes.Config, staticNetworkConfig []*models.HostStaticNetworkConfig) (err error) {
if len(staticNetworkConfig) == 0 {
return nil
}
// Get the static network configuration from nmstate and generate NetworkManager ignition files
filesList, err := manifests.GetNMIgnitionFiles(staticNetworkConfig)
if err != nil {
return err
}
for i := range filesList {
nmFilePath := path.Join(nmConnectionsPath, filesList[i].FilePath)
nmStateIgnFile := ignition.FileFromBytes(nmFilePath, "root", 0600, []byte(filesList[i].FileContents))
config.Storage.Files = append(config.Storage.Files, nmStateIgnFile)
}
nmStateScriptFilePath := "/usr/local/bin/pre-network-manager-config.sh"
// A local version of the assisted-service internal script is currently used
nmStateScript := ignition.FileFromBytes(nmStateScriptFilePath, "root", 0755, []byte(manifests.PreNetworkConfigScript))
config.Storage.Files = append(config.Storage.Files, nmStateScript)
return nil
}
func addTLSData(config *igntypes.Config, dependencies asset.Parents) {
certKeys := []asset.Asset{
&tls.KubeAPIServerLBSignerCertKey{},
&tls.KubeAPIServerLocalhostSignerCertKey{},
&tls.KubeAPIServerServiceNetworkSignerCertKey{},
&tls.AdminKubeConfigSignerCertKey{},
}
dependencies.Get(certKeys...)
for _, ck := range certKeys {
for _, d := range ck.(asset.WritableAsset).Files() {
f := ignition.FileFromBytes(path.Join("/opt/agent", d.Filename), "root", 0600, d.Data)
config.Storage.Files = append(config.Storage.Files, f)
}
}
pwd := &password.KubeadminPassword{}
dependencies.Get(pwd)
config.Storage.Files = append(config.Storage.Files,
ignition.FileFromBytes("/opt/agent/tls/kubeadmin-password.hash", "root", 0600, pwd.PasswordHash))
}
func addMirrorData(config *igntypes.Config, registriesConfig *mirror.RegistriesConf, registryCABundle *mirror.CaBundle) {
// This is required for assisted-service to build the ICSP for openshift-install
if registriesConfig.File != nil {
registriesFile := ignition.FileFromBytes(registriesConfPath,
"root", 0600, registriesConfig.File.Data)
config.Storage.Files = append(config.Storage.Files, registriesFile)
}
// This is required for the agent to run the podman commands to the mirror
if registryCABundle.File != nil && len(registryCABundle.File.Data) > 0 {
caFile := ignition.FileFromBytes(registryCABundlePath,
"root", 0600, registryCABundle.File.Data)
config.Storage.Files = append(config.Storage.Files, caFile)
}
}
// Creates a file named with a host's MAC address. The desired hostname
// is the file's content. The files are read by a systemd service that
// sets the hostname using "hostnamectl set-hostname" when the ISO boots.
func addMacAddressToHostnameMappings(
config *igntypes.Config,
agentConfigAsset *agentconfig.AgentConfig) {
if agentConfigAsset.Config == nil || len(agentConfigAsset.Config.Hosts) == 0 {
return
}
for _, host := range agentConfigAsset.Config.Hosts {
if host.Hostname != "" {
file := ignition.FileFromBytes(filepath.Join(hostnamesPath,
strings.ToLower(filepath.Base(host.Interfaces[0].MacAddress))),
"root", 0600, []byte(host.Hostname))
config.Storage.Files = append(config.Storage.Files, file)
}
}
}
func addHostConfig(config *igntypes.Config, agentConfig *agentconfig.AgentConfig) error {
confs, err := agentConfig.HostConfigFiles()
if err != nil {
return err
}
for path, content := range confs {
hostConfigFile := ignition.FileFromBytes(filepath.Join("/etc/assisted/hostconfig", path), "root", 0644, content)
config.Storage.Files = append(config.Storage.Files, hostConfigFile)
}
return nil
}
func addExtraManifests(config *igntypes.Config, extraManifests *manifests.ExtraManifests) error {
user := "root"
mode := 0644
config.Storage.Directories = append(config.Storage.Directories, igntypes.Directory{
Node: igntypes.Node{
Path: extraManifestPath,
User: igntypes.NodeUser{
Name: &user,
},
Overwrite: util.BoolToPtr(true),
},
DirectoryEmbedded1: igntypes.DirectoryEmbedded1{
Mode: &mode,
},
})
for _, file := range extraManifests.FileList {
type unstructured map[string]interface{}
yamlList, err := manifests.GetMultipleYamls[unstructured](file.Data)
if err != nil {
return errors.Wrapf(err, "could not decode YAML for %s", file.Filename)
}
for n, manifest := range yamlList {
m, err := yaml.Marshal(manifest)
if err != nil {
return err
}
base := filepath.Base(file.Filename)
ext := filepath.Ext(file.Filename)
baseWithoutExt := strings.TrimSuffix(base, ext)
baseFileName := filepath.Join(extraManifestPath, baseWithoutExt)
fileName := fmt.Sprintf("%s-%d%s", baseFileName, n, ext)
extraFile := ignition.FileFromBytes(fileName, user, mode, m)
config.Storage.Files = append(config.Storage.Files, extraFile)
}
}
return nil
}
func getOSImagesInfo(cpuArch string) (*models.OsImage, error) {
st, err := rhcos.FetchCoreOSBuild(context.Background())
if err != nil {
return nil, err
}
osImage := &models.OsImage{
CPUArchitecture: &cpuArch,
}
openshiftVersion, err := version.Version()
if err != nil {
return nil, err
}
osImage.OpenshiftVersion = &openshiftVersion
streamArch, err := st.GetArchitecture(cpuArch)
if err != nil {
return nil, err
}
artifacts, ok := streamArch.Artifacts["metal"]
if !ok {
return nil, fmt.Errorf("failed to retrieve coreos metal info for architecture %s", cpuArch)
}
osImage.Version = &artifacts.Release
isoFormat, ok := artifacts.Formats["iso"]
if !ok {
return nil, fmt.Errorf("failed to retrieve coreos ISO info for architecture %s", cpuArch)
}
osImage.URL = &isoFormat.Disk.Location
return osImage, nil
}
// RetrieveRendezvousIP Returns the Rendezvous IP from either AgentConfig or NMStateConfig
func RetrieveRendezvousIP(agentConfig *agent.Config, nmStateConfigs []*v1beta1.NMStateConfig) (string, error) {
var err error
var rendezvousIP string
if agentConfig != nil && agentConfig.RendezvousIP != "" {
rendezvousIP = agentConfig.RendezvousIP
logrus.Debug("RendezvousIP from the AgentConfig ", rendezvousIP)
} else if len(nmStateConfigs) > 0 {
rendezvousIP, err = manifests.GetNodeZeroIP(nmStateConfigs)
logrus.Debug("RendezvousIP from the NMStateConfig ", rendezvousIP)
} else {
err = errors.New("missing rendezvousIP in agent-config or at least one NMStateConfig manifest")
return "", err
}
// Convert IPv6 address to canonical to match host format for comparisons
addr := net.ParseIP(rendezvousIP)
if addr == nil {
err = errors.New(fmt.Sprintf("invalid rendezvous IP: %s", rendezvousIP))
return "", err
}
return addr.String(), err
}
func getPublicContainerRegistries(registriesConfig *mirror.RegistriesConf) string {
if len(registriesConfig.MirrorConfig) > 0 {
registries := []string{}
for _, config := range registriesConfig.MirrorConfig {
location := strings.SplitN(config.Location, "/", 2)[0]
allRegs := fmt.Sprint(registries)
if !strings.Contains(allRegs, location) {
registries = append(registries, location)
}
}
return strings.Join(registries, ",")
}
return "quay.io"
}