Skip to content

Commit

Permalink
provisioner: Ensure GatewayClass condition generations are updated
Browse files Browse the repository at this point in the history
Related to kubernetes-sigs/gateway-api#1586

Signed-off-by: Sunjay Bhatia <sunjayb@vmware.com>
  • Loading branch information
sunjayBhatia committed Dec 16, 2022
1 parent a81e387 commit cdff1bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
22 changes: 13 additions & 9 deletions internal/provisioner/controller/gatewayclass.go
Expand Up @@ -246,11 +246,22 @@ func (r *gatewayClassReconciler) setAcceptedCondition(
reason gatewayapi_v1beta1.GatewayClassConditionReason,
message string,
) error {
currentAcceptedCondition := metav1.Condition{
Type: string(gatewayapi_v1beta1.GatewayClassConditionStatusAccepted),
Status: status,
ObservedGeneration: gatewayClass.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Message: message,
}
var newConds []metav1.Condition
for _, cond := range gatewayClass.Status.Conditions {
if cond.Type == string(gatewayapi_v1beta1.GatewayClassConditionStatusAccepted) {
if cond.Status == status {
return nil
// If status hasn't changed, don't change the condition, just
// update the generation.
currentAcceptedCondition = cond
currentAcceptedCondition.ObservedGeneration = gatewayClass.Generation
}

continue
Expand All @@ -262,14 +273,7 @@ func (r *gatewayClassReconciler) setAcceptedCondition(
r.log.WithValues("gatewayclass-name", gatewayClass.Name).Info(fmt.Sprintf("setting gateway class's Accepted condition to %s", status))

// nolint:gocritic
gatewayClass.Status.Conditions = append(newConds, metav1.Condition{
Type: string(gatewayapi_v1beta1.GatewayClassConditionStatusAccepted),
Status: status,
ObservedGeneration: gatewayClass.Generation,
LastTransitionTime: metav1.Now(),
Reason: string(reason),
Message: message,
})
gatewayClass.Status.Conditions = append(newConds, currentAcceptedCondition)

if err := r.client.Status().Update(ctx, gatewayClass); err != nil {
return fmt.Errorf("failed to set gatewayclass %s accepted condition: %w", gatewayClass.Name, err)
Expand Down
26 changes: 26 additions & 0 deletions internal/provisioner/controller/gatewayclass_test.go
Expand Up @@ -384,6 +384,31 @@ func TestGatewayClassReconcile(t *testing.T) {
Reason: string(gatewayv1beta1.GatewayClassReasonInvalidParameters),
},
},
"gatewayclass with status from previous generation is updated": {
gatewayClass: &gatewayv1beta1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: "gatewayclass-1",
Generation: 2,
},
Spec: gatewayv1beta1.GatewayClassSpec{
ControllerName: "projectcontour.io/gateway-controller",
},
Status: gatewayv1beta1.GatewayClassStatus{
Conditions: []metav1.Condition{{
Type: string(gatewayv1beta1.GatewayClassConditionStatusAccepted),
Status: metav1.ConditionTrue,
Reason: string(gatewayv1beta1.GatewayClassReasonAccepted),
ObservedGeneration: 1,
}},
},
},
wantCondition: &metav1.Condition{
Type: string(gatewayv1beta1.GatewayClassConditionStatusAccepted),
Status: metav1.ConditionTrue,
Reason: string(gatewayv1beta1.GatewayClassReasonAccepted),
ObservedGeneration: 2,
},
},
}

for name, tc := range tests {
Expand Down Expand Up @@ -424,6 +449,7 @@ func TestGatewayClassReconcile(t *testing.T) {
assert.Equal(t, tc.wantCondition.Type, res.Status.Conditions[0].Type)
assert.Equal(t, tc.wantCondition.Status, res.Status.Conditions[0].Status)
assert.Equal(t, tc.wantCondition.Reason, res.Status.Conditions[0].Reason)
assert.Equal(t, tc.wantCondition.ObservedGeneration, res.Status.Conditions[0].ObservedGeneration)
}

if tc.assertions != nil {
Expand Down

0 comments on commit cdff1bd

Please sign in to comment.