forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installconfig.go
157 lines (133 loc) · 4.03 KB
/
installconfig.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
package installconfig
import (
"os"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/conversion"
"github.com/openshift/installer/pkg/types/defaults"
openstackvalidation "github.com/openshift/installer/pkg/types/openstack/validation"
"github.com/openshift/installer/pkg/types/validation"
)
const (
installConfigFilename = "install-config.yaml"
)
// InstallConfig generates the install-config.yaml file.
type InstallConfig struct {
Config *types.InstallConfig `json:"config"`
File *asset.File `json:"file"`
}
var _ asset.WritableAsset = (*InstallConfig)(nil)
// Dependencies returns all of the dependencies directly needed by an
// InstallConfig asset.
func (a *InstallConfig) Dependencies() []asset.Asset {
return []asset.Asset{
&sshPublicKey{},
&baseDomain{},
&clusterName{},
&pullSecret{},
&platform{},
}
}
// Generate generates the install-config.yaml file.
func (a *InstallConfig) Generate(parents asset.Parents) error {
sshPublicKey := &sshPublicKey{}
baseDomain := &baseDomain{}
clusterName := &clusterName{}
pullSecret := &pullSecret{}
platform := &platform{}
parents.Get(
sshPublicKey,
baseDomain,
clusterName,
pullSecret,
platform,
)
a.Config = &types.InstallConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: types.InstallConfigVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: clusterName.ClusterName,
},
SSHKey: sshPublicKey.Key,
BaseDomain: baseDomain.BaseDomain,
PullSecret: pullSecret.PullSecret,
}
a.Config.AWS = platform.AWS
a.Config.Libvirt = platform.Libvirt
a.Config.None = platform.None
a.Config.OpenStack = platform.OpenStack
a.Config.VSphere = platform.VSphere
if err := a.setDefaults(); err != nil {
return errors.Wrap(err, "failed to set defaults for install config")
}
if err := validation.ValidateInstallConfig(a.Config, openstackvalidation.NewValidValuesFetcher()).ToAggregate(); err != nil {
return errors.Wrap(err, "invalid install config")
}
data, err := yaml.Marshal(a.Config)
if err != nil {
return errors.Wrap(err, "failed to Marshal InstallConfig")
}
a.File = &asset.File{
Filename: installConfigFilename,
Data: data,
}
return nil
}
// Name returns the human-friendly name of the asset.
func (a *InstallConfig) Name() string {
return "Install Config"
}
// Files returns the files generated by the asset.
func (a *InstallConfig) Files() []*asset.File {
if a.File != nil {
return []*asset.File{a.File}
}
return []*asset.File{}
}
// Load returns the installconfig from disk.
func (a *InstallConfig) Load(f asset.FileFetcher) (found bool, err error) {
file, err := f.FetchByName(installConfigFilename)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
config := &types.InstallConfig{}
if err := yaml.Unmarshal(file.Data, config); err != nil {
return false, errors.Wrap(err, "failed to unmarshal")
}
a.Config = config
// Upconvert any deprecated fields
if err := a.convert(); err != nil {
return false, errors.Wrap(err, "failed to upconvert install config")
}
if err := a.setDefaults(); err != nil {
return false, errors.Wrap(err, "failed to set defaults for install config")
}
if err := validation.ValidateInstallConfig(a.Config, openstackvalidation.NewValidValuesFetcher()).ToAggregate(); err != nil {
return false, errors.Wrapf(err, "invalid %q file", installConfigFilename)
}
data, err := yaml.Marshal(a.Config)
if err != nil {
return false, errors.Wrap(err, "failed to Marshal InstallConfig")
}
a.File = &asset.File{
Filename: installConfigFilename,
Data: data,
}
return true, nil
}
func (a *InstallConfig) setDefaults() error {
defaults.SetInstallConfigDefaults(a.Config)
return nil
}
// convert converts possibly older versions of the install config to
// the current version, relocating deprecated fields.
func (a *InstallConfig) convert() error {
return conversion.ConvertInstallConfig(a.Config)
}