Skip to content

Commit 2e15201

Browse files
authored
Merge pull request #120 from awslabs/conditions
refactor: simplify resource group controller error handling and condition management
2 parents 22a0e5a + be9feca commit 2e15201

File tree

12 files changed

+305
-426
lines changed

12 files changed

+305
-426
lines changed

api/v1alpha1/conditions.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,37 @@ type Condition struct {
7373
// +kubebuilder:validation:Minimum=0
7474
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
7575
}
76+
77+
// NewCondition returns a new Condition instance.
78+
func NewCondition(t ConditionType, status metav1.ConditionStatus, reason, message string) Condition {
79+
return Condition{
80+
Type: t,
81+
Status: status,
82+
LastTransitionTime: &metav1.Time{Time: metav1.Now().Time},
83+
Reason: &reason,
84+
Message: &message,
85+
}
86+
}
87+
88+
func GetCondition(conditions []Condition, t ConditionType) *Condition {
89+
for _, c := range conditions {
90+
if c.Type == t {
91+
return &c
92+
}
93+
}
94+
return nil
95+
}
96+
97+
func SetCondition(conditions []Condition, condition Condition) []Condition {
98+
for i, c := range conditions {
99+
if c.Type == condition.Type {
100+
conditions[i] = condition
101+
return conditions
102+
}
103+
}
104+
return append(conditions, condition)
105+
}
106+
107+
func HasCondition(conditions []Condition, t ConditionType) bool {
108+
return GetCondition(conditions, t) != nil
109+
}

internal/controller/resourcegroup/condition/condition.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

internal/controller/resourcegroup/condition/condition_instance.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

internal/controller/resourcegroup/condition/condition_resourcegroup.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

internal/controller/resourcegroup/controller.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,16 @@ func (r *ResourceGroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
8181
Complete(r)
8282
}
8383

84-
// Reconcile is part of the main kubernetes reconciliation loop which aims to
85-
// move the current state of the cluster closer to the desired state.
86-
// TODO(user): Modify the Reconcile function to compare the state specified by
87-
// the ResourceGroup object against the actual cluster state, and then
88-
// perform operations to make the cluster state reflect the state specified by
89-
// the user.
90-
//
91-
// For more details, check Reconcile and its Result here:
92-
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
9384
func (r *ResourceGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
9485
rlog := r.log.WithValues("resourcegroup", req.NamespacedName)
9586
ctx = log.IntoContext(ctx, rlog)
9687

9788
var resourcegroup v1alpha1.ResourceGroup
98-
err := r.Get(ctx, req.NamespacedName, &resourcegroup)
99-
if err != nil {
89+
if err := r.Get(ctx, req.NamespacedName, &resourcegroup); err != nil {
10090
return ctrl.Result{}, client.IgnoreNotFound(err)
10191
}
10292

103-
// reconcile resourcegroup fiesta
104-
err = r.reconcile(ctx, &resourcegroup)
105-
if err != nil {
93+
if err := r.reconcile(ctx, &resourcegroup); err != nil {
10694
return ctrl.Result{}, err
10795
}
10896

@@ -111,39 +99,33 @@ func (r *ResourceGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
11199

112100
func (r *ResourceGroupReconciler) reconcile(ctx context.Context, resourcegroup *v1alpha1.ResourceGroup) error {
113101
log, _ := logr.FromContext(ctx)
114-
// if deletion timestamp is set, call cleanupResourceGroup
102+
115103
if !resourcegroup.DeletionTimestamp.IsZero() {
116104
log.V(1).Info("ResourceGroup is being deleted")
117-
err := r.cleanupResourceGroup(ctx, resourcegroup)
118-
if err != nil {
105+
if err := r.cleanupResourceGroup(ctx, resourcegroup); err != nil {
119106
return err
120107
}
121108

122109
log.V(1).Info("Setting resourcegroup as unmanaged")
123-
// remove finalizer
124-
err = r.setUnmanaged(ctx, resourcegroup)
125-
if err != nil {
110+
if err := r.setUnmanaged(ctx, resourcegroup); err != nil {
126111
return err
127112
}
128113

129114
return nil
130115
}
131116

132117
log.V(1).Info("Setting resource group as managed")
133-
// set finalizer
134-
err := r.setManaged(ctx, resourcegroup)
135-
if err != nil {
118+
if err := r.setManaged(ctx, resourcegroup); err != nil {
136119
return err
137120
}
138121

139122
log.V(1).Info("Syncing resourcegroup")
140123
topologicalOrder, resourcesInformation, reconcileErr := r.reconcileResourceGroup(ctx, resourcegroup)
141124

142125
log.V(1).Info("Setting resourcegroup status")
143-
// set status
144-
err = r.setResourceGroupStatus(ctx, resourcegroup, topologicalOrder, resourcesInformation, reconcileErr)
145-
if err != nil {
126+
if err := r.setResourceGroupStatus(ctx, resourcegroup, topologicalOrder, resourcesInformation, reconcileErr); err != nil {
146127
return err
147128
}
129+
148130
return nil
149131
}

internal/controller/resourcegroup/controller_cleanup.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,57 @@ import (
2626
"github.com/awslabs/kro/internal/metadata"
2727
)
2828

29+
// cleanupResourceGroup handles the deletion of a ResourceGroup by shutting down its associated
30+
// microcontroller and cleaning up the CRD if enabled. It executes cleanup operations in order:
31+
// 1. Shuts down the microcontroller
32+
// 2. Deletes the associated CRD (if CRD deletion is enabled)
2933
func (r *ResourceGroupReconciler) cleanupResourceGroup(ctx context.Context, rg *v1alpha1.ResourceGroup) error {
3034
log, _ := logr.FromContext(ctx)
35+
log.V(1).Info("cleaning up resource group", "name", rg.Name)
3136

32-
log.V(1).Info("Cleaning up resource group")
37+
// shutdown microcontroller
3338
gvr := metadata.GetResourceGroupInstanceGVR(rg.Spec.Schema.APIVersion, rg.Spec.Schema.Kind)
34-
35-
log.V(1).Info("Shutting down resource group microcontroller")
36-
err := r.shutdownResourceGroupMicroController(ctx, &gvr)
37-
if err != nil {
38-
return err
39+
if err := r.shutdownResourceGroupMicroController(ctx, &gvr); err != nil {
40+
return fmt.Errorf("failed to shutdown microcontroller: %w", err)
3941
}
4042

41-
crdName := r.extractCRDName(rg.Spec.Schema.Kind)
42-
log.V(1).Info("Cleaning up resource group CRD", "crd", crdName)
43-
err = r.cleanupResourceGroupCRD(ctx, crdName)
44-
if err != nil {
45-
return err
43+
// cleanup CRD
44+
crdName := extractCRDName(rg.Spec.Schema.Kind)
45+
if err := r.cleanupResourceGroupCRD(ctx, crdName); err != nil {
46+
return fmt.Errorf("failed to cleanup CRD %s: %w", crdName, err)
4647
}
4748

4849
return nil
4950
}
5051

51-
func (r *ResourceGroupReconciler) extractCRDName(kind string) string {
52-
pluralKind := flect.Pluralize(strings.ToLower(kind))
53-
return fmt.Sprintf("%s.%s", pluralKind, v1alpha1.KroDomainName)
54-
}
55-
52+
// shutdownResourceGroupMicroController stops the dynamic controller associated with the given GVR.
53+
// This ensures no new reconciliations occur for this resource type.
5654
func (r *ResourceGroupReconciler) shutdownResourceGroupMicroController(ctx context.Context, gvr *schema.GroupVersionResource) error {
57-
return r.dynamicController.StopServiceGVK(ctx, *gvr)
55+
if err := r.dynamicController.StopServiceGVK(ctx, *gvr); err != nil {
56+
return fmt.Errorf("error stopping service: %w", err)
57+
}
58+
return nil
5859
}
5960

61+
// cleanupResourceGroupCRD deletes the CRD with the given name if CRD deletion is enabled.
62+
// If CRD deletion is disabled, it logs the skip and returns nil.
6063
func (r *ResourceGroupReconciler) cleanupResourceGroupCRD(ctx context.Context, crdName string) error {
61-
if r.allowCRDDeletion {
62-
err := r.crdManager.Delete(ctx, crdName)
63-
if err != nil {
64-
return err
65-
}
66-
} else {
67-
r.log.Info("CRD deletion is disabled, skipping CRD deletion", "crd", crdName)
64+
if !r.allowCRDDeletion {
65+
log, _ := logr.FromContext(ctx)
66+
log.Info("skipping CRD deletion (disabled)", "crd", crdName)
67+
return nil
68+
}
69+
70+
if err := r.crdManager.Delete(ctx, crdName); err != nil {
71+
return fmt.Errorf("error deleting CRD: %w", err)
6872
}
6973
return nil
7074
}
75+
76+
// extractCRDName generates the CRD name from a given kind by converting it to plural form
77+
// and appending the Kro domain name.
78+
func extractCRDName(kind string) string {
79+
return fmt.Sprintf("%s.%s",
80+
flect.Pluralize(strings.ToLower(kind)),
81+
v1alpha1.KroDomainName)
82+
}

0 commit comments

Comments
 (0)