Skip to content

Commit d568a3e

Browse files
authored
stop infinite reconciles (#193)
* stop infinite reconciles * oops * now works
1 parent 7801086 commit d568a3e

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

go/controller/internal/autogen/autogen_reconciler.go

+42-38
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"sync"
77

8+
"k8s.io/apimachinery/pkg/api/meta"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910

1011
autogen_client "github.com/kagent-dev/kagent/go/autogen/client"
@@ -87,21 +88,22 @@ func (a *autogenReconciler) reconcileAgentStatus(ctx context.Context, agent *v1a
8788
status = metav1.ConditionTrue
8889
reason = "AgentReconciled"
8990
}
90-
agent.Status = v1alpha1.AgentStatus{
91-
ObservedGeneration: agent.Generation,
92-
Conditions: []metav1.Condition{{
93-
Type: v1alpha1.AgentConditionTypeAccepted,
94-
Status: status,
95-
LastTransitionTime: metav1.Now(),
96-
Reason: reason,
97-
Message: message,
98-
}},
99-
}
10091

101-
if err := a.kube.Status().Update(ctx, agent); err != nil {
102-
return fmt.Errorf("failed to update agent status: %v", err)
103-
}
92+
conditionChanged := meta.SetStatusCondition(&agent.Status.Conditions, metav1.Condition{
93+
Type: v1alpha1.AgentConditionTypeAccepted,
94+
Status: status,
95+
LastTransitionTime: metav1.Now(),
96+
Reason: reason,
97+
Message: message,
98+
})
10499

100+
// update the status if it has changed or the generation has changed
101+
if conditionChanged || agent.Status.ObservedGeneration != agent.Generation {
102+
agent.Status.ObservedGeneration = agent.Generation
103+
if err := a.kube.Status().Update(ctx, agent); err != nil {
104+
return fmt.Errorf("failed to update agent status: %v", err)
105+
}
106+
}
105107
return nil
106108
}
107109

@@ -147,21 +149,22 @@ func (a *autogenReconciler) reconcileModelConfigStatus(ctx context.Context, mode
147149
status = metav1.ConditionTrue
148150
reason = "ModelConfigReconciled"
149151
}
150-
modelConfig.Status = v1alpha1.ModelConfigStatus{
151-
ObservedGeneration: modelConfig.Generation,
152-
Conditions: []metav1.Condition{{
153-
Type: v1alpha1.ModelConfigConditionTypeAccepted,
154-
Status: status,
155-
LastTransitionTime: metav1.Now(),
156-
Reason: reason,
157-
Message: message,
158-
}},
159-
}
160152

161-
if err := a.kube.Status().Update(ctx, modelConfig); err != nil {
162-
return fmt.Errorf("failed to update model config status: %v", err)
163-
}
153+
conditionChanged := meta.SetStatusCondition(&modelConfig.Status.Conditions, metav1.Condition{
154+
Type: v1alpha1.ModelConfigConditionTypeAccepted,
155+
Status: status,
156+
LastTransitionTime: metav1.Now(),
157+
Reason: reason,
158+
Message: message,
159+
})
164160

161+
// update the status if it has changed or the generation has changed
162+
if conditionChanged || modelConfig.Status.ObservedGeneration != modelConfig.Generation {
163+
modelConfig.Status.ObservedGeneration = modelConfig.Generation
164+
if err := a.kube.Status().Update(ctx, modelConfig); err != nil {
165+
return fmt.Errorf("failed to update model config status: %v", err)
166+
}
167+
}
165168
return nil
166169
}
167170

@@ -189,19 +192,20 @@ func (a *autogenReconciler) reconcileTeamStatus(ctx context.Context, team *v1alp
189192
status = metav1.ConditionTrue
190193
reason = "TeamReconciled"
191194
}
192-
team.Status = v1alpha1.TeamStatus{
193-
ObservedGeneration: team.Generation,
194-
Conditions: []metav1.Condition{{
195-
Type: v1alpha1.TeamConditionTypeAccepted,
196-
Status: status,
197-
LastTransitionTime: metav1.Now(),
198-
Reason: reason,
199-
Message: message,
200-
}},
201-
}
202195

203-
if err := a.kube.Status().Update(ctx, team); err != nil {
204-
return fmt.Errorf("failed to update team status: %v", err)
196+
conditionChanged := meta.SetStatusCondition(&team.Status.Conditions, metav1.Condition{
197+
Type: v1alpha1.TeamConditionTypeAccepted,
198+
Status: status,
199+
LastTransitionTime: metav1.Now(),
200+
Reason: reason,
201+
Message: message,
202+
})
203+
204+
if conditionChanged || team.Status.ObservedGeneration != team.Generation {
205+
team.Status.ObservedGeneration = team.Generation
206+
if err := a.kube.Status().Update(ctx, team); err != nil {
207+
return fmt.Errorf("failed to update team status: %v", err)
208+
}
205209
}
206210

207211
return nil

0 commit comments

Comments
 (0)