forked from openshift/installer
/
installconfig.go
144 lines (122 loc) · 3.57 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
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/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: "v1beta2",
},
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
if err := a.setDefaults(); err != nil {
return errors.Wrapf(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.Wrapf(err, "failed to unmarshal")
}
a.Config = config
if err := a.setDefaults(); err != nil {
return false, errors.Wrapf(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
}