forked from openshift/machine-config-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
174 lines (160 loc) · 4.85 KB
/
bootstrap.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
package operator
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/glog"
configv1 "github.com/openshift/api/config/v1"
configscheme "github.com/openshift/client-go/config/clientset/versioned/scheme"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
templatectrl "github.com/openshift/machine-config-operator/pkg/controller/template"
)
// RenderBootstrap writes to destinationDir static Pods.
func RenderBootstrap(
proxyFile,
clusterConfigConfigMapFile,
infraFile, networkFile,
cloudConfigFile,
etcdCAFile, etcdMetricCAFile, rootCAFile, kubeAPIServerServingCA, pullSecretFile string,
imgs *Images,
destinationDir string,
) error {
filesData := map[string][]byte{}
files := []string{
proxyFile,
clusterConfigConfigMapFile,
infraFile,
networkFile,
rootCAFile,
etcdCAFile,
etcdMetricCAFile,
pullSecretFile,
}
if kubeAPIServerServingCA != "" {
files = append(files, kubeAPIServerServingCA)
}
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
filesData[file] = data
}
// create ControllerConfigSpec
obji, err := runtime.Decode(configscheme.Codecs.UniversalDecoder(configv1.SchemeGroupVersion), filesData[infraFile])
if err != nil {
return err
}
infra, ok := obji.(*configv1.Infrastructure)
if !ok {
return fmt.Errorf("expected *configv1.Infrastructure found %T", obji)
}
obji, err = runtime.Decode(configscheme.Codecs.UniversalDecoder(configv1.SchemeGroupVersion), filesData[proxyFile])
if err != nil {
return err
}
proxy, ok := obji.(*configv1.Proxy)
if !ok {
return fmt.Errorf("expected *configv1.Proxy found %T", obji)
}
obji, err = runtime.Decode(configscheme.Codecs.UniversalDecoder(configv1.SchemeGroupVersion), filesData[networkFile])
if err != nil {
return err
}
network, ok := obji.(*configv1.Network)
if !ok {
return fmt.Errorf("expected *configv1.Network found %T", obji)
}
spec, err := createDiscoveredControllerConfigSpec(infra, network, proxy)
if err != nil {
return err
}
// if the cloudConfig is set in infra read the cloudConfigFile
if infra.Spec.CloudConfig.Name != "" {
data, err := ioutil.ReadFile(cloudConfigFile)
if err != nil {
return err
}
obji, err := runtime.Decode(scheme.Codecs.UniversalDecoder(corev1.SchemeGroupVersion), data)
if err != nil {
return err
}
cm, ok := obji.(*corev1.ConfigMap)
if !ok {
return fmt.Errorf("expected *corev1.ConfigMap found %T", obji)
}
spec.CloudProviderConfig = cm.Data[infra.Spec.CloudConfig.Key]
}
bundle := make([]byte, 0)
bundle = append(bundle, filesData[rootCAFile]...)
// Append the kube-ca if given.
if _, ok := filesData[kubeAPIServerServingCA]; ok {
bundle = append(bundle, filesData[kubeAPIServerServingCA]...)
}
spec.EtcdCAData = filesData[etcdCAFile]
spec.EtcdMetricCAData = filesData[etcdMetricCAFile]
spec.RootCAData = bundle
spec.PullSecret = nil
spec.OSImageURL = imgs.MachineOSContent
spec.Images = map[string]string{
templatectrl.EtcdImageKey: imgs.Etcd,
templatectrl.SetupEtcdEnvKey: imgs.MachineConfigOperator,
templatectrl.InfraImageKey: imgs.InfraImage,
templatectrl.KubeClientAgentImageKey: imgs.KubeClientAgent,
}
config := getRenderConfig("", string(filesData[kubeAPIServerServingCA]), spec, &imgs.RenderConfigImages, infra.Status.APIServerInternalURL)
manifests := []struct {
name string
data []byte
filename string
}{{
name: "manifests/machineconfigcontroller/controllerconfig.yaml",
filename: "bootstrap/manifests/machineconfigcontroller-controllerconfig.yaml",
}, {
name: "manifests/master.machineconfigpool.yaml",
filename: "bootstrap/manifests/master.machineconfigpool.yaml",
}, {
name: "manifests/worker.machineconfigpool.yaml",
filename: "bootstrap/manifests/worker.machineconfigpool.yaml",
}, {
name: "manifests/bootstrap-pod-v2.yaml",
filename: "bootstrap/machineconfigoperator-bootstrap-pod.yaml",
}, {
data: filesData[pullSecretFile],
filename: "bootstrap/manifests/machineconfigcontroller-pull-secret",
}, {
name: "manifests/machineconfigserver/csr-bootstrap-role-binding.yaml",
filename: "manifests/csr-bootstrap-role-binding.yaml",
}, {
name: "manifests/machineconfigserver/kube-apiserver-serving-ca-configmap.yaml",
filename: "manifests/kube-apiserver-serving-ca-configmap.yaml",
}}
for _, m := range manifests {
var b []byte
var err error
switch {
case len(m.name) > 0:
glog.Info(m.name)
b, err = renderAsset(config, m.name)
if err != nil {
return err
}
case len(m.data) > 0:
b = m.data
default:
continue
}
path := filepath.Join(destinationDir, m.filename)
dirname := filepath.Dir(path)
if err := os.MkdirAll(dirname, 0655); err != nil {
return err
}
if err := ioutil.WriteFile(path, b, 0655); err != nil {
return err
}
}
return nil
}