forked from openshift/installer
/
openshift.go
162 lines (138 loc) · 4.58 KB
/
openshift.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
package manifests
import (
"encoding/base64"
"path/filepath"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/ghodss/yaml"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/machines"
osmachine "github.com/openshift/installer/pkg/asset/machines/openstack"
"github.com/openshift/installer/pkg/asset/password"
"github.com/openshift/installer/pkg/asset/templates/content/openshift"
)
const (
openshiftManifestDir = "openshift"
)
var (
_ asset.WritableAsset = (*Openshift)(nil)
)
// Openshift generates the dependent resource manifests for openShift (as against bootkube)
type Openshift struct {
FileList []*asset.File
}
// Name returns a human friendly name for the operator
func (o *Openshift) Name() string {
return "Openshift Manifests"
}
// Dependencies returns all of the dependencies directly needed by the
// Openshift asset
func (o *Openshift) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.InstallConfig{},
&password.KubeadminPassword{},
&openshift.BindingDiscovery{},
&openshift.CloudCredsSecret{},
&openshift.KubeadminPasswordSecret{},
&openshift.RoleCloudCredsSecretReader{},
}
}
// Generate generates the respective operator config.yml files
func (o *Openshift) Generate(dependencies asset.Parents) error {
installConfig := &installconfig.InstallConfig{}
kubeadminPassword := &password.KubeadminPassword{}
dependencies.Get(installConfig, kubeadminPassword)
var cloudCreds cloudCredsSecretData
platform := installConfig.Config.Platform.Name()
switch platform {
case "aws":
ssn := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
creds, err := ssn.Config.Credentials.Get()
if err != nil {
return err
}
cloudCreds = cloudCredsSecretData{
AWS: &AwsCredsSecretData{
Base64encodeAccessKeyID: base64.StdEncoding.EncodeToString([]byte(creds.AccessKeyID)),
Base64encodeSecretAccessKey: base64.StdEncoding.EncodeToString([]byte(creds.SecretAccessKey)),
},
}
case "openstack":
opts := new(clientconfig.ClientOpts)
opts.Cloud = installConfig.Config.Platform.OpenStack.Cloud
cloud, err := clientconfig.GetCloudFromYAML(opts)
if err != nil {
return err
}
clouds := make(map[string]map[string]*clientconfig.Cloud)
clouds["clouds"] = map[string]*clientconfig.Cloud{
osmachine.CloudName: cloud,
}
marshalled, err := yaml.Marshal(clouds)
if err != nil {
return err
}
credsEncoded := base64.StdEncoding.EncodeToString(marshalled)
cloudCreds = cloudCredsSecretData{
OpenStack: &OpenStackCredsSecretData{
Base64encodeCloudCreds: credsEncoded,
},
}
}
templateData := &openshiftTemplateData{
CloudCreds: cloudCreds,
Base64EncodedKubeadminPwHash: base64.StdEncoding.EncodeToString(kubeadminPassword.PasswordHash),
}
bindingDiscovery := &openshift.BindingDiscovery{}
cloudCredsSecret := &openshift.CloudCredsSecret{}
kubeadminPasswordSecret := &openshift.KubeadminPasswordSecret{}
roleCloudCredsSecretReader := &openshift.RoleCloudCredsSecretReader{}
dependencies.Get(
bindingDiscovery,
cloudCredsSecret,
kubeadminPasswordSecret,
roleCloudCredsSecretReader)
assetData := map[string][]byte{
"99_binding-discovery.yaml": []byte(bindingDiscovery.Files()[0].Data),
"99_kubeadmin-password-secret.yaml": applyTemplateData(kubeadminPasswordSecret.Files()[0].Data, templateData),
}
switch platform {
case "aws", "openstack":
assetData["99_cloud-creds-secret.yaml"] = applyTemplateData(cloudCredsSecret.Files()[0].Data, templateData)
assetData["99_role-cloud-creds-secret-reader.yaml"] = applyTemplateData(roleCloudCredsSecretReader.Files()[0].Data, templateData)
}
o.FileList = []*asset.File{}
for name, data := range assetData {
if len(data) == 0 {
continue
}
o.FileList = append(o.FileList, &asset.File{
Filename: filepath.Join(openshiftManifestDir, name),
Data: data,
})
}
asset.SortFiles(o.FileList)
return nil
}
// Files returns the files generated by the asset.
func (o *Openshift) Files() []*asset.File {
return o.FileList
}
// Load returns the openshift asset from disk.
func (o *Openshift) Load(f asset.FileFetcher) (bool, error) {
fileList, err := f.FetchByPattern(filepath.Join(openshiftManifestDir, "*"))
if err != nil {
return false, err
}
for _, file := range fileList {
if machines.IsMachineManifest(file) {
continue
}
o.FileList = append(o.FileList, file)
}
asset.SortFiles(o.FileList)
return len(o.FileList) > 0, nil
}