-
Notifications
You must be signed in to change notification settings - Fork 1
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
Support user-supplied bootstrap configuration #66
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
469cfb2
initial implementation
eaudetcobello 413e3c5
change switch to if
eaudetcobello 28eeba6
revert templates/aws
eaudetcobello bffa5e4
revert
eaudetcobello a909e9c
update documentation
eaudetcobello 4ed4fea
add secret name to err
eaudetcobello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,6 +404,32 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err | |
return nil | ||
} | ||
|
||
// resolveUserBootstrapConfig returns the bootstrap configuration provided by the user. | ||
// It can resolve string content, a reference to a secret, or an empty string if no configuration was provided. | ||
func (r *CK8sConfigReconciler) resolveUserBootstrapConfig(ctx context.Context, cfg *bootstrapv1.CK8sConfig) (string, error) { | ||
// User did not provide a bootstrap configuration | ||
if cfg.Spec.BootstrapConfig == nil { | ||
return "", nil | ||
} | ||
|
||
// User provided a bootstrap configuration through content | ||
if cfg.Spec.BootstrapConfig.Content != "" { | ||
return cfg.Spec.BootstrapConfig.Content, nil | ||
} | ||
|
||
// User referenced a secret for the bootstrap configuration | ||
if cfg.Spec.BootstrapConfig.ContentFrom == nil { | ||
return "", nil | ||
} | ||
|
||
data, err := r.resolveSecretFileContent(ctx, cfg.Namespace, *cfg.Spec.BootstrapConfig.ContentFrom) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read bootstrap configuration from secret %q: %w", cfg.Spec.BootstrapConfig.ContentFrom.Secret.Name, err) | ||
} | ||
|
||
return string(data), nil | ||
} | ||
|
||
// resolveFiles maps .Spec.Files into cloudinit.Files, resolving any object references | ||
// along the way. | ||
func (r *CK8sConfigReconciler) resolveFiles(ctx context.Context, cfg *bootstrapv1.CK8sConfig) ([]bootstrapv1.File, error) { | ||
|
@@ -412,7 +438,7 @@ func (r *CK8sConfigReconciler) resolveFiles(ctx context.Context, cfg *bootstrapv | |
for i := range cfg.Spec.Files { | ||
in := cfg.Spec.Files[i] | ||
if in.ContentFrom != nil { | ||
data, err := r.resolveSecretFileContent(ctx, cfg.Namespace, in) | ||
data, err := r.resolveSecretFileContent(ctx, cfg.Namespace, *in.ContentFrom) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to resolve file source: %w", err) | ||
} | ||
|
@@ -505,18 +531,18 @@ func (r *CK8sConfigReconciler) getSnapInstallDataFromSpec(spec bootstrapv1.CK8sC | |
} | ||
|
||
// resolveSecretFileContent returns file content fetched from a referenced secret object. | ||
func (r *CK8sConfigReconciler) resolveSecretFileContent(ctx context.Context, ns string, source bootstrapv1.File) ([]byte, error) { | ||
func (r *CK8sConfigReconciler) resolveSecretFileContent(ctx context.Context, ns string, source bootstrapv1.FileSource) ([]byte, error) { | ||
secret := &corev1.Secret{} | ||
key := types.NamespacedName{Namespace: ns, Name: source.ContentFrom.Secret.Name} | ||
key := types.NamespacedName{Namespace: ns, Name: source.Secret.Name} | ||
if err := r.Client.Get(ctx, key, secret); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return nil, fmt.Errorf("secret not found %s: %w", key, err) | ||
} | ||
return nil, fmt.Errorf("failed to retrieve Secret %q: %w", key, err) | ||
} | ||
data, ok := secret.Data[source.ContentFrom.Secret.Key] | ||
data, ok := secret.Data[source.Secret.Key] | ||
if !ok { | ||
return nil, fmt.Errorf("secret references non-existent secret key %q: %w", source.ContentFrom.Secret.Key, ErrInvalidRef) | ||
return nil, fmt.Errorf("secret references non-existent secret key %q: %w", source.Secret.Key, ErrInvalidRef) | ||
} | ||
return data, nil | ||
} | ||
|
@@ -636,6 +662,12 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, | |
return ctrl.Result{}, err | ||
} | ||
|
||
userSuppliedBootstrapConfig, err := r.resolveUserBootstrapConfig(ctx, scope.Config) | ||
if err != nil { | ||
conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
Comment on lines
+665
to
+670
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. resolveUserBootstrapConfig returns an empty string if there is no user-supplied configuration, we check this later in common.go. |
||
microclusterPort := scope.Config.Spec.ControlPlaneConfig.GetMicroclusterPort() | ||
ds, err := ck8s.RenderK8sdProxyDaemonSetManifest(ck8s.K8sdProxyDaemonSetInput{K8sdPort: microclusterPort}) | ||
if err != nil { | ||
|
@@ -654,6 +686,7 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, | |
PreRunCommands: scope.Config.Spec.PreRunCommands, | ||
PostRunCommands: scope.Config.Spec.PostRunCommands, | ||
KubernetesVersion: scope.Config.Spec.Version, | ||
BootstrapConfig: userSuppliedBootstrapConfig, | ||
SnapInstallData: snapInstallData, | ||
ExtraFiles: cloudinit.FilesFromAPI(files), | ||
ConfigFileContents: string(initConfig), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the parameter type from File to FileSource because 1. We do not use any of the fields from the File type, except ContentFrom. 2. This makes the function reusable for any other types that have a FileSource (and therefore reference a secret), but are not an explicit abstraction of a File.