Skip to content

Commit dfc9907

Browse files
authored
Merge pull request #11467 from chrischdi/pr-crs-v1beta2-condition
🌱 crs: implement ResourcesApplied v1beta2 condition
2 parents c800c18 + 7195441 commit dfc9907

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

exp/addons/api/v1beta1/clusterresourceset_types.go

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ import (
2323
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2424
)
2525

26+
// ClusterResourceSet's ResourcesApplied condition and corresponding reasons that will be used in v1Beta2 API version.
27+
const (
28+
// ResourcesAppliedV1Beta2Condition surfaces wether the resources in the ClusterResourceSet are applied to all matching clusters.
29+
// This indicates all resources exist, and no errors during applying them to all clusters.
30+
ResourcesAppliedV1Beta2Condition = "ResourcesApplied"
31+
32+
// ResourcesAppliedV1beta2Reason is the reason used when all resources in the ClusterResourceSet object got applied
33+
// to all matching clusters.
34+
ResourcesAppliedV1beta2Reason = "Applied"
35+
36+
// ResourcesNotAppliedV1Beta2Reason is the reason used when applying at least one of the resources to one of the matching clusters failed.
37+
ResourcesNotAppliedV1Beta2Reason = "NotApplied"
38+
39+
// ResourcesAppliedWrongSecretTypeV1Beta2Reason is the reason used when the Secret's type in the resource list is not supported.
40+
ResourcesAppliedWrongSecretTypeV1Beta2Reason = "WrongSecretType"
41+
42+
// ResourcesAppliedInternalErrorV1Beta2Reason surfaces unexpected failures when reconciling a ClusterResourceSet.
43+
ResourcesAppliedInternalErrorV1Beta2Reason = clusterv1.InternalErrorV1Beta2Reason
44+
)
45+
2646
const (
2747
// ClusterResourceSetSecretType is the only accepted type of secret in resources.
2848
ClusterResourceSetSecretType corev1.SecretType = "addons.cluster.x-k8s.io/resource-set" //nolint:gosec

exp/addons/internal/controllers/clusterresourceset_controller.go

+36
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
resourcepredicates "sigs.k8s.io/cluster-api/exp/addons/internal/controllers/predicates"
4747
"sigs.k8s.io/cluster-api/util"
4848
"sigs.k8s.io/cluster-api/util/conditions"
49+
v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
4950
"sigs.k8s.io/cluster-api/util/finalizers"
5051
"sigs.k8s.io/cluster-api/util/patch"
5152
"sigs.k8s.io/cluster-api/util/paused"
@@ -158,6 +159,12 @@ func (r *ClusterResourceSetReconciler) Reconcile(ctx context.Context, req ctrl.R
158159
if err != nil {
159160
log.Error(err, "Failed fetching clusters that matches ClusterResourceSet labels", "ClusterResourceSet", klog.KObj(clusterResourceSet))
160161
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.ClusterMatchFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
162+
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
163+
Type: addonsv1.ResourcesAppliedV1Beta2Condition,
164+
Status: metav1.ConditionFalse,
165+
Reason: addonsv1.ResourcesAppliedInternalErrorV1Beta2Reason,
166+
Message: "Please check controller logs for errors",
167+
})
161168
return ctrl.Result{}, err
162169
}
163170

@@ -309,8 +316,20 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
309316
if err != nil {
310317
if err == ErrSecretTypeNotSupported {
311318
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.WrongSecretTypeReason, clusterv1.ConditionSeverityWarning, err.Error())
319+
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
320+
Type: addonsv1.ResourcesAppliedV1Beta2Condition,
321+
Status: metav1.ConditionFalse,
322+
Reason: addonsv1.ResourcesAppliedWrongSecretTypeV1Beta2Reason,
323+
Message: fmt.Sprintf("Secret type of resource %s is not supported", resource.Name),
324+
})
312325
} else {
313326
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.RetrievingResourceFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
327+
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
328+
Type: addonsv1.ResourcesAppliedV1Beta2Condition,
329+
Status: metav1.ConditionFalse,
330+
Reason: addonsv1.ResourcesAppliedInternalErrorV1Beta2Reason,
331+
Message: "Please check controller logs for errors",
332+
})
314333

315334
// Continue without adding the error to the aggregate if we can't find the resource.
316335
if apierrors.IsNotFound(err) {
@@ -363,6 +382,12 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
363382
remoteClient, err := r.ClusterCache.GetClient(ctx, util.ObjectKey(cluster))
364383
if err != nil {
365384
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.RemoteClusterClientFailedReason, clusterv1.ConditionSeverityError, err.Error())
385+
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
386+
Type: addonsv1.ResourcesAppliedV1Beta2Condition,
387+
Status: metav1.ConditionFalse,
388+
Reason: clusterv1.InternalErrorV1Beta2Reason,
389+
Message: "Please check controller logs for errors",
390+
})
366391
return err
367392
}
368393

@@ -414,6 +439,12 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
414439
isSuccessful = false
415440
log.Error(err, "Failed to apply ClusterResourceSet resource", resource.Kind, klog.KRef(clusterResourceSet.Namespace, resource.Name))
416441
conditions.MarkFalse(clusterResourceSet, addonsv1.ResourcesAppliedCondition, addonsv1.ApplyFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
442+
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
443+
Type: addonsv1.ResourcesAppliedV1Beta2Condition,
444+
Status: metav1.ConditionFalse,
445+
Reason: addonsv1.ResourcesNotAppliedV1Beta2Reason,
446+
Message: "Failed to apply ClusterResourceSet resources to Cluster",
447+
})
417448
errList = append(errList, err)
418449
}
419450

@@ -429,6 +460,11 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
429460
}
430461

431462
conditions.MarkTrue(clusterResourceSet, addonsv1.ResourcesAppliedCondition)
463+
v1beta2conditions.Set(clusterResourceSet, metav1.Condition{
464+
Type: addonsv1.ResourcesAppliedV1Beta2Condition,
465+
Status: metav1.ConditionTrue,
466+
Reason: addonsv1.ResourcesAppliedV1beta2Reason,
467+
})
432468

433469
return nil
434470
}

exp/addons/internal/controllers/clusterresourceset_controller_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"sigs.k8s.io/cluster-api/internal/test/envtest"
3737
"sigs.k8s.io/cluster-api/util"
3838
"sigs.k8s.io/cluster-api/util/conditions"
39+
v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
3940
)
4041

4142
const (
@@ -897,6 +898,12 @@ metadata:
897898
g.Expect(appliedCondition.Status).To(Equal(corev1.ConditionFalse))
898899
g.Expect(appliedCondition.Reason).To(Equal(addonsv1.ApplyFailedReason))
899900
g.Expect(appliedCondition.Message).To(ContainSubstring("creating object /v1, Kind=ConfigMap %s/cm-missing-namespace", missingNamespace))
901+
902+
appliedConditionV1Beta2 := v1beta2conditions.Get(crs, addonsv1.ResourcesAppliedV1Beta2Condition)
903+
g.Expect(appliedConditionV1Beta2).NotTo(BeNil())
904+
g.Expect(appliedConditionV1Beta2.Status).To(BeEquivalentTo(corev1.ConditionFalse))
905+
g.Expect(appliedConditionV1Beta2.Reason).To(Equal(addonsv1.ResourcesNotAppliedV1Beta2Reason))
906+
g.Expect(appliedConditionV1Beta2.Message).To(Equal("Failed to apply ClusterResourceSet resources to Cluster"))
900907
}, timeout).Should(Succeed())
901908

902909
t.Log("Creating missing namespace")

0 commit comments

Comments
 (0)