Skip to content

Commit ff0d77d

Browse files
committed
vsphereclusteridentity: implement v1beta2 condition
1 parent ad7777b commit ff0d77d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

apis/v1beta1/vsphereclusteridentity_types.go

+21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ const (
2828
VSphereClusterIdentityFinalizer = "vsphereclusteridentity/infrastructure.cluster.x-k8s.io"
2929
)
3030

31+
// VSphereClusterIdentity's CredentialsAvailable condition and corresponding reasons that will be used in v1Beta2 API version.
32+
const (
33+
// VSphereClusterIdentityCredentialsAvailableV1Beta2Condition documents the status of the credentials for a VSphereClusterIdentity.
34+
VSphereClusterIdentityCredentialsAvailableV1Beta2Condition = "CredentialsAvailable"
35+
36+
// VSphereClusterIdentityCredentialsAvailableV1Beta2Reason surfaces when the VSphereClusterIdentity credentials are available.
37+
VSphereClusterIdentityCredentialsAvailableV1Beta2Reason = clusterv1.AvailableV1Beta2Reason
38+
39+
// VSphereClusterIdentityCredentialsSecretNotAvailableV1Beta2Reason surfaces when the VSphereClusterIdentity secret is not available.
40+
VSphereClusterIdentityCredentialsSecretNotAvailableV1Beta2Reason = "SecretNotAvailable"
41+
42+
// VSphereClusterIdentityCredentialsAvailableV1Beta2Reason surfaces when the VSphereClusterIdentity secret is already in use.
43+
VSphereClusterIdentityCredentialsSecretAlreadyInUseV1Beta2Reason = "SecretAlreadyInUse"
44+
45+
// VSphereClusterIdentityCredentialsAvailableV1Beta2Reason surfaces when setting the owner reference on the VSphereClusterIdentity secret failed.
46+
VSphereClusterIdentityCredentialsSecretOwnerReferenceFailedV1Beta2Reason = "SecretOwnerReferenceFailed"
47+
48+
// VSphereClusterIdentityCredentialsDeletingV1Beta2Reason surfaces when the credentials for a VSphereClusterIdentity are being deleted.
49+
VSphereClusterIdentityCredentialsDeletingV1Beta2Reason = clusterv1.DeletingV1Beta2Reason
50+
)
51+
3152
// VSphereClusterIdentitySpec contains a secret reference and a group of allowed namespaces.
3253
type VSphereClusterIdentitySpec struct {
3354
// SecretName references a Secret inside the controller namespace with the credentials to use

controllers/vsphereclusteridentity_controller.go

+33
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3131
clusterutilv1 "sigs.k8s.io/cluster-api/util"
3232
"sigs.k8s.io/cluster-api/util/conditions"
33+
v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
3334
"sigs.k8s.io/cluster-api/util/finalizers"
3435
"sigs.k8s.io/cluster-api/util/patch"
3536
"sigs.k8s.io/cluster-api/util/paused"
@@ -102,6 +103,7 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
102103

103104
if err := patchHelper.Patch(ctx, identity, patch.WithOwnedV1Beta2Conditions{Conditions: []string{
104105
clusterv1.PausedV1Beta2Condition,
106+
infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Condition,
105107
}}); err != nil {
106108
reterr = kerrors.NewAggregate([]error{reterr, err})
107109
}
@@ -119,12 +121,24 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
119121
}
120122
if err := r.Client.Get(ctx, secretKey, secret); err != nil {
121123
conditions.MarkFalse(identity, infrav1.CredentialsAvailableCondidtion, infrav1.SecretNotAvailableReason, clusterv1.ConditionSeverityWarning, err.Error())
124+
v1beta2conditions.Set(identity, metav1.Condition{
125+
Type: infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Condition,
126+
Status: metav1.ConditionFalse,
127+
Reason: infrav1.VSphereClusterIdentityCredentialsSecretNotAvailableV1Beta2Reason,
128+
Message: err.Error(),
129+
})
122130
return reconcile.Result{}, errors.Wrapf(err, "failed to get Secret %s", klog.KRef(secretKey.Namespace, secretKey.Name))
123131
}
124132

125133
// If this secret is owned by a different VSphereClusterIdentity or a VSphereCluster, mark the identity as not ready and return an error.
126134
if !clusterutilv1.IsOwnedByObject(secret, identity) && pkgidentity.IsOwnedByIdentityOrCluster(secret.GetOwnerReferences()) {
127135
conditions.MarkFalse(identity, infrav1.CredentialsAvailableCondidtion, infrav1.SecretAlreadyInUseReason, clusterv1.ConditionSeverityError, "secret being used by another Cluster/VSphereIdentity")
136+
v1beta2conditions.Set(identity, metav1.Condition{
137+
Type: infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Condition,
138+
Status: metav1.ConditionFalse,
139+
Reason: infrav1.VSphereClusterIdentityCredentialsSecretAlreadyInUseV1Beta2Reason,
140+
Message: "secret being used by another Cluster/VSphereIdentity",
141+
})
128142
identity.Status.Ready = false
129143
return reconcile.Result{}, errors.New("secret being used by another Cluster/VSphereIdentity")
130144
}
@@ -145,10 +159,22 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
145159
err = r.Client.Update(ctx, secret)
146160
if err != nil {
147161
conditions.MarkFalse(identity, infrav1.CredentialsAvailableCondidtion, infrav1.SecretOwnerReferenceFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
162+
v1beta2conditions.Set(identity, metav1.Condition{
163+
Type: infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Condition,
164+
Status: metav1.ConditionFalse,
165+
Reason: infrav1.VSphereClusterIdentityCredentialsSecretOwnerReferenceFailedV1Beta2Reason,
166+
Message: err.Error(),
167+
})
148168
return reconcile.Result{}, err
149169
}
150170

151171
conditions.MarkTrue(identity, infrav1.CredentialsAvailableCondidtion)
172+
v1beta2conditions.Set(identity, metav1.Condition{
173+
Type: infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Condition,
174+
Status: metav1.ConditionTrue,
175+
Reason: infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Reason,
176+
})
177+
152178
identity.Status.Ready = true
153179
return reconcile.Result{}, nil
154180
}
@@ -160,6 +186,13 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity
160186
Namespace: r.ControllerManagerCtx.Namespace,
161187
Name: identity.Spec.SecretName,
162188
}
189+
190+
v1beta2conditions.Set(identity, metav1.Condition{
191+
Type: infrav1.VSphereClusterIdentityCredentialsAvailableV1Beta2Condition,
192+
Status: metav1.ConditionFalse,
193+
Reason: infrav1.VSphereClusterIdentityCredentialsDeletingV1Beta2Reason,
194+
})
195+
163196
err := r.Client.Get(ctx, secretKey, secret)
164197
if err != nil {
165198
if apierrors.IsNotFound(err) {

0 commit comments

Comments
 (0)