Skip to content

Commit 2185a46

Browse files
committed
added docs, rename vars for better mgt-cluster vs wl-cluster understanding.
1 parent 88de8f0 commit 2185a46

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

api/v1beta1/types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,16 @@ type HCloudPlacementGroupStatus struct {
101101

102102
// HetznerSecretRef defines all the names of the secret and the relevant keys needed to access Hetzner API.
103103
type HetznerSecretRef struct {
104-
// Name defines the name of the secret.
104+
// Name defines the name of the secret. The name gets used for reading the credential in the
105+
// mgt-cluster, and it gets used for creating a secret in the wl-cluster. About the secret in
106+
// 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.
110+
//
105111
// +kubebuilder:default=hetzner
106112
Name string `json:"name"`
113+
107114
// Key defines the keys that are used in the secret.
108115
// Need to specify either HCloudToken or both HetznerRobotUser and HetznerRobotPassword.
109116
Key HetznerSecretKeyRef `json:"key"`

controllers/hetznercluster_controller.go

Lines changed: 26 additions & 22 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 = reconcileTargetSecret(ctx, clusterScope)
229+
result, err = reconcileWorkloadClusterSecret(ctx, clusterScope)
230230
if err != nil {
231231
reterr := fmt.Errorf("failed to reconcile target secret: %w", err)
232232
conditions.MarkFalse(
@@ -472,21 +472,24 @@ func hcloudTokenErrorResult(
472472
return res, nil
473473
}
474474

475-
func reconcileTargetSecret(ctx context.Context, clusterScope *scope.ClusterScope) (res reconcile.Result, reterr error) {
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) {
476479
if clusterScope.HetznerCluster.Spec.SkipCreatingHetznerSecretInWorkloadCluster {
477480
// If the secret should not be created in the workload cluster, we just return.
478481
// This means the ccm is running outside of the workload cluster (or getting the secret differently).
479482
return reconcile.Result{}, nil
480483
}
481484

482485
// Checking if control plane is ready
483-
clientConfig, err := clusterScope.ClientConfig(ctx)
486+
wlClientConfig, err := clusterScope.ClientConfig(ctx)
484487
if err != nil {
485488
clusterScope.V(1).Info("failed to get clientconfig with api endpoint")
486489
return reconcile.Result{}, err
487490
}
488491

489-
if err := scope.IsControlPlaneReady(ctx, clientConfig); err != nil {
492+
if err := scope.IsControlPlaneReady(ctx, wlClientConfig); err != nil {
490493
conditions.MarkFalse(
491494
clusterScope.HetznerCluster,
492495
infrav1.TargetClusterSecretReadyCondition,
@@ -500,65 +503,66 @@ func reconcileTargetSecret(ctx context.Context, clusterScope *scope.ClusterScope
500503
// Control plane ready, so we can check if the secret exists already
501504

502505
// getting client
503-
restConfig, err := clientConfig.ClientConfig()
506+
restConfig, err := wlClientConfig.ClientConfig()
504507
if err != nil {
505508
return reconcile.Result{}, fmt.Errorf("failed to get rest config: %w", err)
506509
}
507510

508-
client, err := client.New(restConfig, client.Options{})
511+
// workload cluster client
512+
wlClient, err := client.New(restConfig, client.Options{})
509513
if err != nil {
510514
return reconcile.Result{}, fmt.Errorf("failed to get client: %w", err)
511515
}
512516

513-
secret := &corev1.Secret{
517+
wlSecret := &corev1.Secret{
514518
ObjectMeta: metav1.ObjectMeta{
515519
Name: clusterScope.HetznerCluster.Spec.HetznerSecret.Name,
516520
Namespace: metav1.NamespaceSystem,
517521
},
518522
}
519523

520524
// Make sure secret exists and has the expected values
521-
_, err = controllerutil.CreateOrUpdate(ctx, client, secret, func() error {
522-
tokenSecretName := types.NamespacedName{
525+
_, err = controllerutil.CreateOrUpdate(ctx, wlClient, wlSecret, func() error {
526+
mgtSecretName := types.NamespacedName{
523527
Namespace: clusterScope.HetznerCluster.Namespace,
524528
Name: clusterScope.HetznerCluster.Spec.HetznerSecret.Name,
525529
}
526530
secretManager := secretutil.NewSecretManager(clusterScope.Logger, clusterScope.Client, clusterScope.APIReader)
527-
tokenSecret, err := secretManager.AcquireSecret(ctx, tokenSecretName, clusterScope.HetznerCluster, false, clusterScope.HetznerCluster.DeletionTimestamp.IsZero())
531+
mgtSecret, err := secretManager.AcquireSecret(ctx, mgtSecretName, clusterScope.HetznerCluster, false, clusterScope.HetznerCluster.DeletionTimestamp.IsZero())
528532
if err != nil {
529533
return fmt.Errorf("failed to acquire secret: %w", err)
530534
}
531535

532-
hetznerToken, keyExists := tokenSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken]
536+
hetznerToken, keyExists := mgtSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken]
533537
if !keyExists {
534538
return fmt.Errorf("error key %s does not exist in secret/%s: %w",
535539
clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken,
536-
tokenSecretName,
540+
mgtSecretName,
537541
err,
538542
)
539543
}
540544

541-
if secret.Data == nil {
542-
secret.Data = make(map[string][]byte)
545+
if wlSecret.Data == nil {
546+
wlSecret.Data = make(map[string][]byte)
543547
}
544548

545-
secret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken] = hetznerToken
549+
wlSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HCloudToken] = hetznerToken
546550

547551
// Save robot credentials if available
548552
if clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser != "" {
549-
robotUserName := tokenSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser]
550-
secret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser] = robotUserName
551-
robotPassword := tokenSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword]
552-
secret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword] = robotPassword
553+
robotUserName := mgtSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser]
554+
wlSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotUser] = robotUserName
555+
robotPassword := mgtSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword]
556+
wlSecret.Data[clusterScope.HetznerCluster.Spec.HetznerSecret.Key.HetznerRobotPassword] = robotPassword
553557
}
554558

555559
// Save network ID in secret
556560
if clusterScope.HetznerCluster.Spec.HCloudNetwork.Enabled {
557-
secret.Data["network"] = []byte(strconv.FormatInt(clusterScope.HetznerCluster.Status.Network.ID, 10))
561+
wlSecret.Data["network"] = []byte(strconv.FormatInt(clusterScope.HetznerCluster.Status.Network.ID, 10))
558562
}
559563
// Save api server information
560-
secret.Data["apiserver-host"] = []byte(clusterScope.HetznerCluster.Spec.ControlPlaneEndpoint.Host)
561-
secret.Data["apiserver-port"] = []byte(strconv.Itoa(int(clusterScope.HetznerCluster.Spec.ControlPlaneEndpoint.Port)))
564+
wlSecret.Data["apiserver-host"] = []byte(clusterScope.HetznerCluster.Spec.ControlPlaneEndpoint.Host)
565+
wlSecret.Data["apiserver-port"] = []byte(strconv.Itoa(int(clusterScope.HetznerCluster.Spec.ControlPlaneEndpoint.Port)))
562566

563567
return nil
564568
})

pkg/scope/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (s *ClusterScope) ControlPlaneAPIEndpointPort() int32 {
145145
return int32(s.HetznerCluster.Spec.ControlPlaneLoadBalancer.Port) //nolint:gosec // Validation for the port range (1 to 65535) is already done via kubebuilder.
146146
}
147147

148-
// ClientConfig return a kubernetes client config for the cluster context.
148+
// ClientConfig return a kubernetes client config for the workload cluster.
149149
func (s *ClusterScope) ClientConfig(ctx context.Context) (clientcmd.ClientConfig, error) {
150150
return workloadClientConfigFromKubeconfigSecret(ctx, s.Logger, s.Client, s.APIReader, s.Cluster, s.HetznerCluster)
151151
}

0 commit comments

Comments
 (0)