Skip to content

Commit 8d0469b

Browse files
author
Danil-Grigorev
committed
Implement cloud config parser and writer methods
1 parent 7f8951c commit 8d0469b

2 files changed

Lines changed: 239 additions & 26 deletions

File tree

pkg/render/render.go

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,21 @@ import (
1313
"github.com/openshift/cluster-cloud-controller-manager-operator/pkg/config"
1414
"github.com/openshift/cluster-cloud-controller-manager-operator/pkg/substitution"
1515
corev1 "k8s.io/api/core/v1"
16-
"k8s.io/apimachinery/pkg/runtime"
1716
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1817
"k8s.io/klog"
1918
"sigs.k8s.io/controller-runtime/pkg/client"
2019
"sigs.k8s.io/yaml"
2120
)
2221

2322
const (
23+
configDataKey = "cloud.conf"
2424
bootstrapNamespace = "kube-system"
2525
bootstrapPrefix = "bootstrap"
26+
secretPrefix = "secrets"
2627
// bootstrapFileName is built from bootstrapPrefix, resource name and kind
2728
bootstrapFileName = "%s/%s-%s.yaml"
2829
)
2930

30-
var scheme *runtime.Scheme
31-
32-
func init() {
33-
scheme = runtime.NewScheme()
34-
}
35-
3631
// Render defines render config for use in bootstrap mode
3732
type Render struct {
3833
// path to rendered configv1.Infrastructure manifest
@@ -56,7 +51,7 @@ func New(infrastructureFile, imagesFile, cloudConfigFile string) *Render {
5651
// Run runs boostrap for Machine Config Controller
5752
// It writes all the assets to destDir
5853
func (r *Render) Run(destinationDir string) error {
59-
infra, imagesMap, err := r.readAssets()
54+
infra, imagesMap, cloudConfig, err := r.readAssets()
6055
if err != nil {
6156
klog.Errorf("Cannot read assets from provided paths: %v", err)
6257
return err
@@ -74,36 +69,51 @@ func (r *Render) Run(destinationDir string) error {
7469
klog.Infof("Collected resource %s %q successfully", resource.GetObjectKind().GroupVersionKind(), client.ObjectKeyFromObject(resource))
7570
}
7671

77-
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)
7878
}
7979

8080
// readAssets collects infrastructure resource and images config map from provided paths
81-
func (r *Render) readAssets() (*configv1.Infrastructure, *corev1.ConfigMap, error) {
81+
func (r *Render) readAssets() (*configv1.Infrastructure, *corev1.ConfigMap, string, error) {
8282
infraData, err := ioutil.ReadFile(r.infrastructureFile)
8383
if err != nil {
8484
klog.Errorf("Unable to read data from %q: %v", r.infrastructureFile, err)
85-
return nil, nil, err
85+
return nil, nil, "", err
8686
}
8787

8888
infra := &configv1.Infrastructure{}
8989
if err := yaml.UnmarshalStrict(infraData, infra); err != nil {
9090
klog.Errorf("Cannot decode data into configv1.Infrastructure from %q: %v", r.infrastructureFile, err)
91-
return nil, nil, err
91+
return nil, nil, "", err
9292
}
9393

9494
imagesData, err := ioutil.ReadFile(r.imagesFile)
9595
if err != nil {
9696
klog.Errorf("Unable to read data from %q: %v", r.imagesFile, err)
97-
return nil, nil, err
97+
return nil, nil, "", err
9898
}
9999

100100
imagesConfigMap := &corev1.ConfigMap{}
101101
if err := yaml.UnmarshalStrict(imagesData, imagesConfigMap); err != nil {
102102
klog.Errorf("Cannot decode data into v1.ConfigMap from %q: %v", r.imagesFile, err)
103-
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+
}
104114
}
105115

106-
return infra, imagesConfigMap, nil
116+
return infra, imagesConfigMap, cloudConfig, nil
107117
}
108118

109119
// writeAssets writes static pods to disk into <destinationDir>/<bootstrapPrefix>/<resourceName>-<resourceKind>.yaml
@@ -129,5 +139,48 @@ func writeAssets(destinationDir string, resources []client.Object) error {
129139
return err
130140
}
131141
}
142+
132143
return nil
133144
}
145+
146+
// writeCloudConfig creates secrets folder and writes resources such as cloud-config file
147+
// for use in bootstrap
148+
func writeCloudConfig(destinationDir string, cloudConfig string) error {
149+
// Create secrets directory in advance to ensure it is present for any provider
150+
secretsDir := filepath.Join(destinationDir, secretPrefix)
151+
if err := os.MkdirAll(secretsDir, fs.ModePerm); err != nil {
152+
klog.Errorf("Unable to create destination dir %q: %v", secretsDir, err)
153+
return err
154+
}
155+
156+
if cloudConfig != "" {
157+
cloudConfigFile := filepath.Join(secretsDir, 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+
167+
return nil
168+
}
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)