Skip to content

Commit 990a5b5

Browse files
committed
create both secrets for compatibilty.
1 parent 09a030a commit 990a5b5

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

api/v1beta1/types.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ type HetznerSecretRef struct {
104104
// Name defines the name of the secret. The name gets used for reading the credential in the
105105
// mgt-cluster, and it gets used for creating a secret in the wl-cluster. About the secret in
106106
// the wl-cluster: Attention, the upstream hcloud-ccm helm chart expects the name to be
107-
// "hcloud". The Syself ccm defaults to "hetzner". The secret will be created in the namespace
108-
// "mgt-system" of the workload-cluster. Set `spec.skipCreatingHetznerSecretInWorkloadCluster`,
109-
// if you don't want that secret in the wl-cluster to be created.
107+
// "hcloud". The Syself ccm defaults to "hetzner". For compatibilty with upstream hcloud-ccm the
108+
// controller creates two secrets, if the name is different from "hcloud" (one with name
109+
// "hcloud", one with name being the value of this setting). The secret will be created in the
110+
// namespace "mgt-system" of the workload-cluster. Set
111+
// `spec.skipCreatingHetznerSecretInWorkloadCluster`, if you don't want that secret in the
112+
// wl-cluster to be created.
110113
//
111-
// +kubebuilder:default=hetzner
114+
// +kubebuilder:default=hcloud
112115
Name string `json:"name"`
113116

114117
// Key defines the keys that are used in the secret.

controllers/hetznercluster_controller.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (r *HetznerClusterReconciler) reconcileNormal(ctx context.Context, clusterS
226226
// target cluster is ready
227227
conditions.MarkTrue(hetznerCluster, infrav1.TargetClusterReadyCondition)
228228

229-
result, err = reconcileWorkloadClusterSecret(ctx, clusterScope)
229+
result, err = reconcileWorkloadClusterSecrets(ctx, clusterScope)
230230
if err != nil {
231231
reterr := fmt.Errorf("failed to reconcile target secret: %w", err)
232232
conditions.MarkFalse(
@@ -472,10 +472,12 @@ func hcloudTokenErrorResult(
472472
return res, nil
473473
}
474474

475-
// reconcileWorkloadClusterSecret ensures that the workload-cluster has the secret needed by the ccm. The
476-
// name of the secret is read from HetznerCluster.Spec.HetznerSecret.Name. Creating the secret gets
477-
// skipped, if HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster is set.
478-
func reconcileWorkloadClusterSecret(ctx context.Context, clusterScope *scope.ClusterScope) (res reconcile.Result, reterr error) {
475+
// reconcileWorkloadClusterSecrets ensures that the workload-cluster has the secret needed by the
476+
// ccm. The name of the secret is read from HetznerCluster.Spec.HetznerSecret.Name. If
477+
// HetznerSecret.Name is "hcloud", then only one secret gets created in the wl-cluster. If not, two
478+
// secrets are created in the wl-cluster. This ensures compatiblity between CCMs. Creating the
479+
// secret gets skipped, if HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster is set.
480+
func reconcileWorkloadClusterSecrets(ctx context.Context, clusterScope *scope.ClusterScope) (res reconcile.Result, reterr error) {
479481
if clusterScope.HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster {
480482
// If the secret should not be created in the workload cluster, we just return.
481483
// This means the ccm is running outside of the workload cluster (or getting the secret differently).
@@ -514,15 +516,33 @@ func reconcileWorkloadClusterSecret(ctx context.Context, clusterScope *scope.Clu
514516
return reconcile.Result{}, fmt.Errorf("failed to get client: %w", err)
515517
}
516518

519+
// To ensure compatibilty with both CCMs, create always a secret with name "hcloud" in the
520+
// wl-cluster.
521+
names := []string{clusterScope.HetznerCluster.Spec.HetznerSecret.Name}
522+
if clusterScope.HetznerCluster.Spec.HetznerSecret.Name != "hcloud" {
523+
names = append(names, "hcloud")
524+
}
525+
526+
for _, name := range names {
527+
err = reconcileOneWorkloadClusterSecret(ctx, clusterScope, wlClient, name)
528+
if err != nil {
529+
return reconcile.Result{}, fmt.Errorf("failed to reconcile wl-cluster secret %q: %w",
530+
name, err)
531+
}
532+
}
533+
return reconcile.Result{}, nil
534+
}
535+
536+
func reconcileOneWorkloadClusterSecret(ctx context.Context, clusterScope *scope.ClusterScope, wlClient client.Client, name string) error {
517537
wlSecret := &corev1.Secret{
518538
ObjectMeta: metav1.ObjectMeta{
519-
Name: clusterScope.HetznerCluster.Spec.HetznerSecret.Name,
539+
Name: name,
520540
Namespace: metav1.NamespaceSystem,
521541
},
522542
}
523543

524544
// Make sure secret exists and has the expected values
525-
_, err = controllerutil.CreateOrUpdate(ctx, wlClient, wlSecret, func() error {
545+
_, err := controllerutil.CreateOrUpdate(ctx, wlClient, wlSecret, func() error {
526546
mgtSecretName := types.NamespacedName{
527547
Namespace: clusterScope.HetznerCluster.Namespace,
528548
Name: clusterScope.HetznerCluster.Spec.HetznerSecret.Name,
@@ -576,10 +596,10 @@ func reconcileWorkloadClusterSecret(ctx context.Context, clusterScope *scope.Clu
576596
return nil
577597
})
578598
if err != nil {
579-
return reconcile.Result{}, fmt.Errorf("failed to create or update secret: %w", err)
599+
return fmt.Errorf("failed to create or update secret: %w", err)
580600
}
581601

582-
return res, nil
602+
return nil
583603
}
584604

585605
func (r *HetznerClusterReconciler) reconcileTargetClusterManager(ctx context.Context, clusterScope *scope.ClusterScope) (res reconcile.Result, err error) {

0 commit comments

Comments
 (0)