Repository: syself/cluster-api-provider-hetzner
Pull request #885 (2023-09-01 10:53:23)

:seedling: set HCloudTokenAvailableCondition

<!-- If this is your first PR, welcome! Please make sure you read the [contributing guidelines](../CONTRIBUTING.md#contributing-a-patch). -->
<!-- please add an icon to the title of this PR and delete this line and similar ones -->
<!-- the icon will be either ⚠️ (:warning:, major or breaking changes), ✨ (:sparkles:, feature additions), 🐛 (:bug:, patches and bugfixes), 📖 (:book:, documentation or proposals), or 🌱 (:seedling:, minor or other) -->

**What this PR does / why we need it**:

don't set InstanceReady.

And use MarkTrue if everything is fine.



Patch:
diff --git a/api/v1beta1/conditions_const.go b/api/v1beta1/conditions_const.go
index 38f4e1de..5c72a397 100644
--- a/api/v1beta1/conditions_const.go
+++ b/api/v1beta1/conditions_const.go
@@ -69,8 +69,8 @@ const (
 )
 
 const (
-	// HetznerClusterReadyCondition reports on whether the Hetzner cluster is in ready state.
-	HetznerClusterReadyCondition clusterv1.ConditionType = "HetznerClusterReady"
+	// HCloudTokenAvailableCondition reports on whether the HCloud Token is available.
+	HCloudTokenAvailableCondition clusterv1.ConditionType = "HCloudTokenAvailable"
 	// HetznerSecretUnreachableReason indicates that Hetzner secret is unreachable.
 	HetznerSecretUnreachableReason = "HetznerSecretUnreachable" // #nosec
 	// HCloudCredentialsInvalidReason indicates that credentials for HCloud are invalid.
diff --git a/controllers/hcloudmachine_controller.go b/controllers/hcloudmachine_controller.go
index 4effdba5..0958bc35 100644
--- a/controllers/hcloudmachine_controller.go
+++ b/controllers/hcloudmachine_controller.go
@@ -26,6 +26,7 @@ import (
 	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
 	"sigs.k8s.io/cluster-api/util"
 	"sigs.k8s.io/cluster-api/util/annotations"
+	"sigs.k8s.io/cluster-api/util/conditions"
 	"sigs.k8s.io/cluster-api/util/predicates"
 	ctrl "sigs.k8s.io/controller-runtime"
 	"sigs.k8s.io/controller-runtime/pkg/client"
@@ -118,9 +119,11 @@ func (r *HCloudMachineReconciler) Reconcile(ctx context.Context, req reconcile.R
 	secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader)
 	hcloudToken, hetznerSecret, err := getAndValidateHCloudToken(ctx, req.Namespace, hetznerCluster, secretManager)
 	if err != nil {
-		return hcloudTokenErrorResult(ctx, err, hcloudMachine, infrav1.InstanceReadyCondition, r.Client)
+		return hcloudTokenErrorResult(ctx, err, hcloudMachine, infrav1.HCloudTokenAvailableCondition, r.Client)
 	}
 
+	conditions.MarkTrue(hcloudMachine, infrav1.HCloudTokenAvailableCondition)
+
 	hcc := r.HCloudClientFactory.NewClient(hcloudToken)
 
 	machineScope, err := scope.NewMachineScope(scope.MachineScopeParams{
diff --git a/controllers/hcloudmachinetemplate_controller.go b/controllers/hcloudmachinetemplate_controller.go
index 78264153..c00d5c1e 100644
--- a/controllers/hcloudmachinetemplate_controller.go
+++ b/controllers/hcloudmachinetemplate_controller.go
@@ -23,6 +23,7 @@ import (
 
 	"k8s.io/klog/v2"
 	"sigs.k8s.io/cluster-api/util"
+	"sigs.k8s.io/cluster-api/util/conditions"
 	"sigs.k8s.io/cluster-api/util/predicates"
 	ctrl "sigs.k8s.io/controller-runtime"
 	"sigs.k8s.io/controller-runtime/pkg/client"
@@ -93,8 +94,9 @@ func (r *HCloudMachineTemplateReconciler) Reconcile(ctx context.Context, req rec
 	secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader)
 	hcloudToken, _, err := getAndValidateHCloudToken(ctx, req.Namespace, hetznerCluster, secretManager)
 	if err != nil {
-		return hcloudTokenErrorResult(ctx, err, machineTemplate, infrav1.InstanceReadyCondition, r.Client)
+		return hcloudTokenErrorResult(ctx, err, machineTemplate, infrav1.HCloudTokenAvailableCondition, r.Client)
 	}
+	conditions.MarkTrue(machineTemplate, infrav1.HCloudTokenAvailableCondition)
 
 	hcc := r.HCloudClientFactory.NewClient(hcloudToken)
 
diff --git a/controllers/hcloudremediation_controller.go b/controllers/hcloudremediation_controller.go
index f3ddc85f..e94a16ea 100644
--- a/controllers/hcloudremediation_controller.go
+++ b/controllers/hcloudremediation_controller.go
@@ -25,6 +25,7 @@ import (
 	"k8s.io/klog/v2"
 	"sigs.k8s.io/cluster-api/util"
 	"sigs.k8s.io/cluster-api/util/annotations"
+	"sigs.k8s.io/cluster-api/util/conditions"
 	"sigs.k8s.io/cluster-api/util/patch"
 	"sigs.k8s.io/cluster-api/util/predicates"
 	ctrl "sigs.k8s.io/controller-runtime"
@@ -128,9 +129,11 @@ func (r *HCloudRemediationReconciler) Reconcile(ctx context.Context, req reconci
 	secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader)
 	hcloudToken, _, err := getAndValidateHCloudToken(ctx, req.Namespace, hetznerCluster, secretManager)
 	if err != nil {
-		return hcloudTokenErrorResult(ctx, err, hcloudMachine, infrav1.InstanceReadyCondition, r.Client)
+		return hcloudTokenErrorResult(ctx, err, hcloudMachine, infrav1.HCloudTokenAvailableCondition, r.Client)
 	}
 
+	conditions.MarkTrue(hcloudMachine, infrav1.HCloudTokenAvailableCondition)
+
 	hcc := r.HCloudClientFactory.NewClient(hcloudToken)
 
 	remediationScope, err := scope.NewHCloudRemediationScope(scope.HCloudRemediationScopeParams{
diff --git a/controllers/hetznerbaremetalmachine_controller.go b/controllers/hetznerbaremetalmachine_controller.go
index 314bb0ae..5ac3ae71 100644
--- a/controllers/hetznerbaremetalmachine_controller.go
+++ b/controllers/hetznerbaremetalmachine_controller.go
@@ -28,6 +28,7 @@ import (
 	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
 	"sigs.k8s.io/cluster-api/util"
 	"sigs.k8s.io/cluster-api/util/annotations"
+	"sigs.k8s.io/cluster-api/util/conditions"
 	"sigs.k8s.io/cluster-api/util/predicates"
 	ctrl "sigs.k8s.io/controller-runtime"
 	"sigs.k8s.io/controller-runtime/pkg/client"
@@ -114,9 +115,11 @@ func (r *HetznerBareMetalMachineReconciler) Reconcile(ctx context.Context, req r
 	secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader)
 	hcloudToken, _, err := getAndValidateHCloudToken(ctx, req.Namespace, hetznerCluster, secretManager)
 	if err != nil {
-		return hcloudTokenErrorResult(ctx, err, hbmMachine, infrav1.InstanceReadyCondition, r.Client)
+		return hcloudTokenErrorResult(ctx, err, hbmMachine, infrav1.HCloudTokenAvailableCondition, r.Client)
 	}
 
+	conditions.MarkTrue(hbmMachine, infrav1.HCloudTokenAvailableCondition)
+
 	hcc := r.HCloudClientFactory.NewClient(hcloudToken)
 
 	// Create the scope.
diff --git a/controllers/hetznercluster_controller.go b/controllers/hetznercluster_controller.go
index e5482a7b..f41ae3a0 100644
--- a/controllers/hetznercluster_controller.go
+++ b/controllers/hetznercluster_controller.go
@@ -125,9 +125,9 @@ func (r *HetznerClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque
 	secretManager := secretutil.NewSecretManager(log, r.Client, r.APIReader)
 	hcloudToken, hetznerSecret, err := getAndValidateHCloudToken(ctx, req.Namespace, hetznerCluster, secretManager)
 	if err != nil {
-		return hcloudTokenErrorResult(ctx, err, hetznerCluster, infrav1.HetznerClusterReadyCondition, r.Client)
+		return hcloudTokenErrorResult(ctx, err, hetznerCluster, infrav1.HCloudTokenAvailableCondition, r.Client)
 	}
-
+	conditions.MarkTrue(hetznerCluster, infrav1.HCloudTokenAvailableCondition)
 	hcloudClient := r.HCloudClientFactory.NewClient(hcloudToken)
 
 	clusterScope, err := scope.NewClusterScope(scope.ClusterScopeParams{
@@ -393,6 +393,12 @@ func hcloudTokenErrorResult(
 		)
 
 	default:
+		conditions.MarkFalse(setter,
+			conditionType,
+			infrav1.HCloudCredentialsInvalidReason,
+			clusterv1.ConditionSeverityError,
+			err.Error(),
+		)
 		return ctrl.Result{}, fmt.Errorf("an unhandled failure occurred with the Hetzner secret: %w", err)
 	}
 


Test patch:
diff --git a/controllers/hcloudmachine_controller_test.go b/controllers/hcloudmachine_controller_test.go
index 76ed4323..0c68034d 100644
--- a/controllers/hcloudmachine_controller_test.go
+++ b/controllers/hcloudmachine_controller_test.go
@@ -516,7 +516,7 @@ var _ = Describe("Hetzner secret", func() {
 				if err := testEnv.Get(ctx, key, hcloudMachine); err != nil {
 					return false
 				}
-				return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.InstanceReadyCondition, expectedReason)
+				return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.HCloudTokenAvailableCondition, expectedReason)
 			}, timeout, time.Second).Should(BeTrue())
 		},
 		Entry("no Hetzner secret/wrong reference", corev1.Secret{
diff --git a/controllers/hetznercluster_controller_test.go b/controllers/hetznercluster_controller_test.go
index 3dab5fc9..9bdb6b58 100644
--- a/controllers/hetznercluster_controller_test.go
+++ b/controllers/hetznercluster_controller_test.go
@@ -883,7 +883,7 @@ var _ = Describe("Hetzner secret", func() {
 				if err := testEnv.Get(ctx, key, hetznerCluster); err != nil {
 					return false
 				}
-				return isPresentAndFalseWithReason(key, hetznerCluster, infrav1.HetznerClusterReadyCondition, expectedReason)
+				return isPresentAndFalseWithReason(key, hetznerCluster, infrav1.HCloudTokenAvailableCondition, expectedReason)
 			}, timeout, time.Second).Should(BeTrue())
 		},
 		Entry("no Hetzner secret/wrong reference", corev1.Secret{