Repository navigation
🌱 Step 2: support upstream hcloud-ccm secret/key conventions (II) #1920
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 63 commits
1d7c69b
9045f83
754bb78
fa881ba
2b26274
2221309
565e24a
4076672
7711985
b1db27f
f52a463
0256ed7
4b8e3ce
46d0eb5
e64277e
e2d0dc4
d6d700c
5b254b3
dab01e2
ce71208
e273531
cc0ec43
9995edf
e7ae474
f8a1f8f
5fcbe30
b593759
77bac89
b52d849
994265f
100c3ea
5e54931
e4a0f53
2648f61
3ee7ead
753d380
c7c93e1
b678259
7b9150d
cfe1283
077529e
78650ee
76c492c
a778f9d
5b80084
1f86185
ce511d1
6cea60a
67c7153
df86d8d
3f5c7fe
c69c6a3
2e7f9a3
2aeee8f
50d838e
199007b
b8cc56b
60e9d47
f96f293
0cf506d
5a82110
bb28fe3
1582375
a158bef
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 |
|---|---|---|
|
|
@@ -101,9 +101,23 @@ type HCloudPlacementGroupStatus struct { | |
|
|
||
| // HetznerSecretRef defines all the names of the secret and the relevant keys needed to access Hetzner API. | ||
| type HetznerSecretRef struct { | ||
| // Name defines the name of the secret. | ||
| // Name defines the name of the secret. The controller reads the credential from the | ||
| // management-cluster secret with this name and creates a workload-cluster secret with the same | ||
| // name. The upstream hcloud-ccm helm chart expects the secret name "hcloud", while the Syself | ||
| // ccm fork defaults to "hetzner". | ||
| // | ||
| // To facilitate migrations to the upstream hcloud-ccm, it is ensured that the secret it expects | ||
| // is created in the proper way. This means that CAPH ensures that in the secret called "hcloud" | ||
| // there is the hcloud token stored under the key "token". This might lead to the same value | ||
| // being stored in two different keys of the same secret, depending on the configuration used | ||
| // here. | ||
| // | ||
| // We recommend to use the value "hcloud". Set `spec.skipCreatingHetznerSecretInWorkloadCluster` | ||
| // if you don't want workload-cluster secrets to be created. | ||
| // | ||
| // +kubebuilder:default=hetzner | ||
| Name string `json:"name"` | ||
|
|
||
| // Key defines the keys that are used in the secret. | ||
| // Need to specify either HCloudToken or both HetznerRobotUser and HetznerRobotPassword. | ||
| Key HetznerSecretKeyRef `json:"key"` | ||
|
|
@@ -113,18 +127,38 @@ type HetznerSecretRef struct { | |
| // Need to specify either HCloudToken or both HetznerRobotUser and HetznerRobotPassword. | ||
| type HetznerSecretKeyRef struct { | ||
| // HCloudToken defines the name of the key where the token for the Hetzner Cloud API is stored. | ||
| // +optional | ||
| // +kubebuilder:default=hcloud-token | ||
| // The controller reads the token from the management-cluster secret using this key and writes | ||
| // it to workload-cluster secrets using the same key. Upstream hcloud-ccm expects the key | ||
| // "token", while the legacy Syself ccm fork uses "hcloud". The configured workload-cluster | ||
| // secret remains untouched. CAPH only adds the compatibility key "token" inside the | ||
| // workload-cluster secret named "hcloud", and only if this field is not already "token". If | ||
| // the configured secret name is already "hcloud", the same token value is stored under both | ||
| // keys in that single secret. We recommend to use "token". | ||
| // | ||
| // +optional +kubebuilder:default=hcloud-token | ||
| HCloudToken string `json:"hcloudToken"` | ||
| // HetznerRobotUser defines the name of the key where the username for the Hetzner Robot API is stored. | ||
|
|
||
| // HetznerRobotUser defines the name of the key where the username for the Hetzner Robot API is | ||
|
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. why don't we create two keys here for compatibility?
Contributor
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. @janiskemper I added a comparison to the PR description. Both CCMS use the same keys for robot-user and robot-password.
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. We only create one secret additionally, if the specified one is NOT the one the hcloud ccm wants. So we also only need to add another key to the secret that the hcloud ccm wants, right? We don't have to add another key to the secret that is irrelevant to hcloud ccm.. We only do this in the first place to facilitate migrations to the upstream hcloud ccm. Therefore, we can keep it lean and only focus on what that ccm wants, and that's all we need to do
Contributor
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. @janiskemper ok, I adapted the PR accordingly.
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. Cool! I think I now have a clear picture here as well. I think we should revamp the whole comments here that we added in the PR and align it around the following principle:
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.
@guettli so that means that both CCMs use the same key, but here in our default we have a different one than that? In the code I don't see that we hardcode the value that you mention "robot-user". Therefore, this secret would actually be created wrongly if I specify something else with this configuration than "robot-user".
Contributor
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. @janiskemper I can do that, but I dont think this is needed. In all our templates, and in our commercial offering we use
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. okay, but we should still fix the code, because we cannot rely on people just using the templates, right? Currently, our code is wrong and we need to fix it by using the right configuration
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. @guettli then this is left before I review again (but I think we are almost there)
Contributor
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. @janiskemper done in 50d838e . The |
||
| // stored. It gets used for reading the credential in the mgt-cluster, and it gets used for | ||
| // creating a secret in the wl-cluster. We recommend to use "robot-user", because this is the | ||
| // default of upstream hcloud-ccm. | ||
| // | ||
| // +optional | ||
| // +kubebuilder:default=hetzner-robot-user | ||
| HetznerRobotUser string `json:"hetznerRobotUser"` | ||
| // HetznerRobotPassword defines the name of the key where the password for the Hetzner Robot API is stored. | ||
|
|
||
| // HetznerRobotPassword defines the name of the key where the password for the Hetzner Robot API | ||
| // is stored. It gets used for reading the credential in the mgt-cluster, and it gets used for | ||
| // creating a secret in the wl-cluster. We recommend to use "robot-password", because this is | ||
| // the default of upstream hcloud-ccm. | ||
| // | ||
| // +optional | ||
| // +kubebuilder:default=hetzner-robot-password | ||
| HetznerRobotPassword string `json:"hetznerRobotPassword"` | ||
| // SSHKey defines the name of the ssh key. | ||
|
|
||
| // SSHKey defines the name of the ssh key. It is only used in the mgt-cluster. It is not synced | ||
| // to the wl-cluster. | ||
| // | ||
| // +optional | ||
| // +kubebuilder:default=hcloud-ssh-key-name | ||
| SSHKey string `json:"sshKey"` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,9 +489,11 @@ func hcloudTokenErrorResult( | |
| return res, nil | ||
| } | ||
|
|
||
| // reconcileWorkloadClusterSecrets ensures that the workload-cluster has the secret needed by the | ||
| // ccm. The name of the secret is read from HetznerCluster.Spec.HetznerSecret.Name. Creating the | ||
| // secret gets skipped, if HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster is set. | ||
| // reconcileWorkloadClusterSecrets syncs Hetzner credentials from the management cluster into | ||
| // workload-cluster secrets for CCM consumption. It creates the secret named in | ||
|
Dhairya-Arora01 marked this conversation as resolved.
Resolved
|
||
| // HetznerCluster.Spec.HetznerSecret.Name and, unless that name is already "hcloud", also creates a | ||
| // second secret named "hcloud" for upstream hcloud-ccm compatibility. Secret creation is skipped | ||
| // if HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster is set. | ||
| func reconcileWorkloadClusterSecrets(ctx context.Context, clusterScope *scope.ClusterScope) (res reconcile.Result, reterr error) { | ||
| if clusterScope.HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster { | ||
| // If the secret should not be created in the workload cluster, we just return. | ||
|
|
@@ -531,15 +533,57 @@ func reconcileWorkloadClusterSecrets(ctx context.Context, clusterScope *scope.Cl | |
| return reconcile.Result{}, fmt.Errorf("failed to get client: %w", err) | ||
| } | ||
|
|
||
| if err := reconcileAllWorkloadClusterSecrets(ctx, clusterScope, wlClient); err != nil { | ||
| return reconcile.Result{}, err | ||
| } | ||
| return reconcile.Result{}, nil | ||
| } | ||
|
|
||
| func workloadClusterSecretNames(secretName string) []string { | ||
| names := []string{secretName} | ||
| if secretName != "hcloud" { | ||
| names = append(names, "hcloud") | ||
| } | ||
| return names | ||
| } | ||
|
|
||
| // workloadClusterCompatibilityKeys returns the configured key for every secret. | ||
| // When reconciling the secret named "hcloud", it also returns the compatibility | ||
| // key unless it matches the configured key. | ||
| func workloadClusterCompatibilityKeys(secretName, configuredKey, compatibilityKey string) []string { | ||
|
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. plz explain this function in a comment. It's not trivial at all.
Contributor
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. @janiskemper I simplified the function and added a comment. |
||
| if secretName != "hcloud" { | ||
| // Only the secret named "hcloud" gets the compatibility key. | ||
| return []string{configuredKey} | ||
| } | ||
|
|
||
| if configuredKey == compatibilityKey { | ||
| // Avoid duplicating the key when it already matches the compatibility key. | ||
| return []string{configuredKey} | ||
| } | ||
|
|
||
| // The compatibility secret exposes both the configured key and the compatibility key. | ||
| return []string{configuredKey, compatibilityKey} | ||
| } | ||
|
|
||
| func reconcileAllWorkloadClusterSecrets(ctx context.Context, clusterScope *scope.ClusterScope, wlClient client.Client) error { | ||
| for _, name := range workloadClusterSecretNames(clusterScope.HetznerCluster.Spec.HetznerSecret.Name) { | ||
| if err := reconcileOneWorkloadClusterSecret(ctx, clusterScope, wlClient, name); err != nil { | ||
| return fmt.Errorf("failed to reconcile wl-cluster secret %q: %w", name, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func reconcileOneWorkloadClusterSecret(ctx context.Context, clusterScope *scope.ClusterScope, wlClient client.Client, name string) error { | ||
| wlSecret := &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: clusterScope.HetznerCluster.Spec.HetznerSecret.Name, | ||
| Name: name, | ||
| Namespace: metav1.NamespaceSystem, | ||
| }, | ||
| } | ||
|
|
||
| // Make sure secret exists and has the expected values | ||
| _, err = controllerutil.CreateOrUpdate(ctx, wlClient, wlSecret, func() error { | ||
| _, err := controllerutil.CreateOrUpdate(ctx, wlClient, wlSecret, func() error { | ||
| mgtSecretName := types.NamespacedName{ | ||
| Namespace: clusterScope.HetznerCluster.Namespace, | ||
| Name: clusterScope.HetznerCluster.Spec.HetznerSecret.Name, | ||
|
|
@@ -562,14 +606,20 @@ func reconcileWorkloadClusterSecrets(ctx context.Context, clusterScope *scope.Cl | |
| wlSecret.Data = make(map[string][]byte) | ||
| } | ||
|
|
||
| wlSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken] = hcloudToken | ||
| for _, key := range workloadClusterCompatibilityKeys(name, clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken, "token") { | ||
| wlSecret.Data[key] = hcloudToken | ||
| } | ||
|
|
||
| // Save robot credentials if available | ||
| if clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser != "" { | ||
| robotUserName := mgtSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser] | ||
| wlSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser] = robotUserName | ||
| for _, key := range workloadClusterCompatibilityKeys(name, clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser, "robot-user") { | ||
| wlSecret.Data[key] = robotUserName | ||
| } | ||
| robotPassword := mgtSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword] | ||
| wlSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword] = robotPassword | ||
| for _, key := range workloadClusterCompatibilityKeys(name, clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword, "robot-password") { | ||
| wlSecret.Data[key] = robotPassword | ||
| } | ||
| } | ||
|
|
||
| // Save network ID in secret | ||
|
|
@@ -583,10 +633,10 @@ func reconcileWorkloadClusterSecrets(ctx context.Context, clusterScope *scope.Cl | |
| return nil | ||
| }) | ||
| if err != nil { | ||
| return reconcile.Result{}, fmt.Errorf("failed to create or update secret: %w", err) | ||
| return fmt.Errorf("failed to create or update secret: %w", err) | ||
| } | ||
|
|
||
| return reconcile.Result{}, nil | ||
| return nil | ||
| } | ||
|
|
||
| func (r *HetznerClusterReconciler) reconcileTargetClusterManager(ctx context.Context, clusterScope *scope.ClusterScope) (res reconcile.Result, err error) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.