-
Notifications
You must be signed in to change notification settings - Fork 142
render: generate cloud config when infrastructure and configmap are provided #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package kube_cloud_config | ||
| package kubecloudconfig | ||
|
|
||
| import ( | ||
| "bytes" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package kube_cloud_config | ||
| package kubecloudconfig | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package kube_cloud_config | ||
| package kubecloudconfig | ||
|
|
||
| import ( | ||
| "bytes" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package kube_cloud_config | ||
| package kubecloudconfig | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package kubecloudconfig | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| operatorclient "github.com/openshift/cluster-config-operator/pkg/operator/operatorclient" | ||
| ) | ||
|
|
||
| // ValidateFile verifies a file exists, has content, and is a regular file | ||
| func ValidateFile(path string) error { | ||
| st, err := os.Stat(path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to stat %s: %w", path, err) | ||
| } | ||
| if !st.Mode().IsRegular() { | ||
| return fmt.Errorf("%s is not a regular file", path) | ||
| } | ||
| if st.Size() <= 0 { | ||
| return fmt.Errorf("%s is empty", path) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // BootstrapTransform implements the cloudConfigTransformer during bootstrapping. | ||
| // It uses the input ConfigMap and Infrastructure provided by files on the bootstrap | ||
| // host to create a new config that has the cloud field set. | ||
| func BootstrapTransform(infrastructureFile string, cloudProviderFile string) ([]byte, error) { | ||
|
|
||
| // Read, parse, and save the infrastructure object | ||
| var clusterInfrastructure configv1.Infrastructure | ||
| fileData, err := ioutil.ReadFile(infrastructureFile) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read infrastructure file: %w", err) | ||
| } | ||
| err = yaml.Unmarshal(fileData, &clusterInfrastructure) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed unmarshal infrastructure: %w", err) | ||
| } | ||
|
|
||
| // Read, parse, and save the user provided cloud configmap | ||
| var cloudProviderConfigInput corev1.ConfigMap | ||
| if len(cloudProviderFile) > 0 { | ||
| fileData, err = ioutil.ReadFile(cloudProviderFile) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read cloud provider file: %w", err) | ||
| } | ||
| err = yaml.Unmarshal(fileData, &cloudProviderConfigInput) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal cloud provider: %w", err) | ||
| } | ||
| } | ||
|
|
||
| // Determine the platform type | ||
| var platformName configv1.PlatformType | ||
| if pstatus := clusterInfrastructure.Status.PlatformStatus; pstatus != nil { | ||
| platformName = pstatus.Type | ||
| } | ||
| if len(platformName) == 0 { | ||
| platformName = clusterInfrastructure.Status.Platform | ||
| } | ||
|
|
||
| // Determine the platform specific transformer method to use | ||
| cloudConfigTransformers := cloudConfigTransformers() | ||
| cloudConfigTransformerFn, ok := cloudConfigTransformers[platformName] | ||
| if !ok { | ||
| cloudConfigTransformerFn = asIsTransformer | ||
| } | ||
| target, err := cloudConfigTransformerFn(&cloudProviderConfigInput, clusterInfrastructure.Spec.CloudConfig.Key, &clusterInfrastructure) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to transform cloud config: %w", err) | ||
| } | ||
|
|
||
| target.Name = targetConfigName | ||
| target.Namespace = operatorclient.GlobalMachineSpecifiedConfigNamespace | ||
| /* ApplyConfigMap() */ | ||
|
|
||
| targetCloudConfigMapData, err := yaml.Marshal(target) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marhsal cloud config: %w", err) | ||
| } | ||
|
|
||
| return targetCloudConfigMapData, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package kube_cloud_config | ||
| package kubecloudconfig | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
@@ -49,13 +49,10 @@ func NewController(operatorClient operatorv1helpers.OperatorClient, | |
| openshiftConfigConfigMapInformer cache.SharedIndexInformer, openshiftConfigManagedConfigMapInformer cache.SharedIndexInformer, | ||
| recorder events.Recorder) factory.Controller { | ||
| c := &KubeCloudConfigController{ | ||
| infraClient: infraClient.Infrastructures(), | ||
| infraLister: infraLister, | ||
| configMapClient: configMapClient, | ||
| cloudConfigTransformers: map[configv1.PlatformType]cloudConfigTransformer{ | ||
| configv1.AWSPlatformType: awsTransformer, | ||
| configv1.AzurePlatformType: azureTransformer, | ||
| }, | ||
| infraClient: infraClient.Infrastructures(), | ||
| infraLister: infraLister, | ||
| configMapClient: configMapClient, | ||
| cloudConfigTransformers: cloudConfigTransformers(), | ||
| } | ||
| return factory.New(). | ||
| WithInformers( | ||
|
|
@@ -154,3 +151,12 @@ func asIsTransformer(input *corev1.ConfigMap, sourceKey string, _ *configv1.Infr | |
|
|
||
| return output, nil | ||
| } | ||
|
|
||
| // cloudConfigTransformers returns all configured cloud transformers | ||
| func cloudConfigTransformers() map[configv1.PlatformType]cloudConfigTransformer { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this a global var. No need to do this dynamically.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently, global maps in golang can't be initialized. I think it makes more sense and is cleaner. If global, then it will need to be initialized somewhere. Since it is used in a few places (and possibly more in the future), this will begin look uglier IMO. Let me know your thoughts. |
||
| cloudConfigTransformers := map[configv1.PlatformType]cloudConfigTransformer{ | ||
| configv1.AWSPlatformType: awsTransformer, | ||
| configv1.AzurePlatformType: azureTransformer, | ||
| } | ||
| return cloudConfigTransformers | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| // Package kube_cloud_config controller is responsible for stitching the user provided kuberneted cloud configuration file and | ||
| // Package kubecloudconfig controller is responsible for stitching the user provided kuberneted cloud configuration file and | ||
| // the various platform specific settings provided in the infrastructures.config.openshift.io | ||
| package kube_cloud_config | ||
| package kubecloudconfig |
Uh oh!
There was an error while loading. Please reload this page.