Skip to content

Commit 2625bc2

Browse files
Danil-GrigorevDanil Grigorev
authored andcommitted
Implement cloud config parser and writer methods
1 parent 27b7309 commit 2625bc2

2 files changed

Lines changed: 239 additions & 19 deletions

File tree

pkg/render/render.go

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
)
2121

2222
const (
23+
configDataKey = "cloud.conf"
2324
bootstrapNamespace = "kube-system"
2425
bootstrapPrefix = "bootstrap"
26+
configPrefix = "config"
2527
// bootstrapFileName is built from bootstrapPrefix, resource name and kind
2628
bootstrapFileName = "%s/%s-%s.yaml"
2729
)
@@ -49,7 +51,7 @@ func New(infrastructureFile, imagesFile, cloudConfigFile string) *Render {
4951
// Run runs boostrap for Machine Config Controller
5052
// It writes all the assets to destDir
5153
func (r *Render) Run(destinationDir string) error {
52-
infra, imagesMap, err := r.readAssets()
54+
infra, imagesMap, cloudConfig, err := r.readAssets()
5355
if err != nil {
5456
klog.Errorf("Cannot read assets from provided paths: %v", err)
5557
return err
@@ -67,36 +69,51 @@ func (r *Render) Run(destinationDir string) error {
6769
klog.Infof("Collected resource %s %q successfully", resource.GetObjectKind().GroupVersionKind(), client.ObjectKeyFromObject(resource))
6870
}
6971

70-
return writeAssets(destinationDir, resources)
72+
if err := writeAssets(destinationDir, resources); err != nil {
73+
klog.Errorf("Could not write assets to bootstrap dir: %v", err)
74+
return err
75+
}
76+
77+
return writeCloudConfig(destinationDir, cloudConfig)
7178
}
7279

7380
// readAssets collects infrastructure resource and images config map from provided paths
74-
func (r *Render) readAssets() (*configv1.Infrastructure, *corev1.ConfigMap, error) {
81+
func (r *Render) readAssets() (*configv1.Infrastructure, *corev1.ConfigMap, string, error) {
7582
infraData, err := ioutil.ReadFile(r.infrastructureFile)
7683
if err != nil {
7784
klog.Errorf("Unable to read data from %q: %v", r.infrastructureFile, err)
78-
return nil, nil, err
85+
return nil, nil, "", err
7986
}
8087

8188
infra := &configv1.Infrastructure{}
8289
if err := yaml.UnmarshalStrict(infraData, infra); err != nil {
8390
klog.Errorf("Cannot decode data into configv1.Infrastructure from %q: %v", r.infrastructureFile, err)
84-
return nil, nil, err
91+
return nil, nil, "", err
8592
}
8693

8794
imagesData, err := ioutil.ReadFile(r.imagesFile)
8895
if err != nil {
8996
klog.Errorf("Unable to read data from %q: %v", r.imagesFile, err)
90-
return nil, nil, err
97+
return nil, nil, "", err
9198
}
9299

93100
imagesConfigMap := &corev1.ConfigMap{}
94101
if err := yaml.UnmarshalStrict(imagesData, imagesConfigMap); err != nil {
95102
klog.Errorf("Cannot decode data into v1.ConfigMap from %q: %v", r.imagesFile, err)
96-
return nil, nil, err
103+
return nil, nil, "", err
104+
}
105+
106+
cloudConfig := ""
107+
// if the cloudConfig is set in infra read the cloudConfigFile
108+
if infra.Spec.CloudConfig.Name != "" {
109+
cloudConfig, err = loadBootstrapCloudProviderConfig(infra, r.cloudConfigFile)
110+
if err != nil {
111+
klog.Errorf("failed to load the cloud provider config: %v", err)
112+
return nil, nil, "", err
113+
}
97114
}
98115

99-
return infra, imagesConfigMap, nil
116+
return infra, imagesConfigMap, cloudConfig, nil
100117
}
101118

102119
// writeAssets writes static pods to disk into <destinationDir>/<bootstrapPrefix>/<resourceName>-<resourceKind>.yaml
@@ -122,5 +139,48 @@ func writeAssets(destinationDir string, resources []client.Object) error {
122139
return err
123140
}
124141
}
142+
143+
return nil
144+
}
145+
146+
// writeCloudConfig creates config folder and writes resources such as cloud-config file
147+
// for use in bootstrap
148+
func writeCloudConfig(destinationDir string, cloudConfig string) error {
149+
// Create config directory in advance to ensure it is present for any provider
150+
configDir := filepath.Join(destinationDir, configPrefix)
151+
if err := os.MkdirAll(configDir, fs.ModePerm); err != nil {
152+
klog.Errorf("Unable to create destination dir %q: %v", configDir, err)
153+
return err
154+
}
155+
156+
if cloudConfig != "" {
157+
cloudConfigFile := filepath.Join(configDir, configDataKey)
158+
159+
klog.Infof("Writing cloud config on disk in %q", cloudConfigFile)
160+
err := os.WriteFile(cloudConfigFile, []byte(cloudConfig), 0666)
161+
if err != nil {
162+
klog.Errorf("Failed to write cloud config to disk in %q: %v", cloudConfigFile, err)
163+
return err
164+
}
165+
}
166+
125167
return nil
126168
}
169+
170+
// loadBootstrapCloudProviderConfig reads the cloud provider config from cloudConfigFile based on infra object.
171+
func loadBootstrapCloudProviderConfig(infra *configv1.Infrastructure, cloudConfigFile string) (string, error) {
172+
data, err := os.ReadFile(cloudConfigFile)
173+
if err != nil {
174+
return "", err
175+
}
176+
cloudConfigMap := &corev1.ConfigMap{}
177+
if err := yaml.UnmarshalStrict(data, cloudConfigMap); err != nil {
178+
return "", err
179+
}
180+
cloudConf, ok := cloudConfigMap.Data[configDataKey]
181+
if !ok {
182+
klog.Infof("falling back to reading cloud provider config from user specified key %s", infra.Spec.CloudConfig.Key)
183+
cloudConf = cloudConfigMap.Data[infra.Spec.CloudConfig.Key]
184+
}
185+
return cloudConf, nil
186+
}

0 commit comments

Comments
 (0)