Skip to content

Commit 4963ebc

Browse files
authored
Support ObservedGeneration in HTTPRoute status (#254)
Previously, NGINX Kubernetes Gateway used the hard-coded value 123 for the ObservedGeneration in the Conditions reported in the status of an HTTPRoute resource. This commit ensures that the Gateway uses the actual observed Generation of an HTTPRoute resource in the Conditions.
1 parent 9d5f139 commit 4963ebc

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

internal/state/change_processor_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ const (
3131
func createRoute(name string, gateway string, hostname string, backendRefs ...v1beta1.HTTPBackendRef) *v1beta1.HTTPRoute {
3232
return &v1beta1.HTTPRoute{
3333
ObjectMeta: metav1.ObjectMeta{
34-
Namespace: "test",
35-
Name: name,
34+
Namespace: "test",
35+
Name: name,
36+
Generation: 1,
3637
},
3738
Spec: v1beta1.HTTPRouteSpec{
3839
CommonRouteSpec: v1beta1.CommonRouteSpec{
@@ -247,6 +248,7 @@ var _ = Describe("ChangeProcessor", func() {
247248
IgnoredGatewayStatuses: map[types.NamespacedName]state.IgnoredGatewayStatus{},
248249
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
249250
{Namespace: "test", Name: "hr-1"}: {
251+
ObservedGeneration: hr1.Generation,
250252
ParentStatuses: map[string]state.ParentStatus{
251253
"listener-80-1": {Attached: false},
252254
"listener-443-1": {Attached: false},
@@ -331,6 +333,7 @@ var _ = Describe("ChangeProcessor", func() {
331333
IgnoredGatewayStatuses: map[types.NamespacedName]state.IgnoredGatewayStatus{},
332334
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
333335
{Namespace: "test", Name: "hr-1"}: {
336+
ObservedGeneration: hr1.Generation,
334337
ParentStatuses: map[string]state.ParentStatus{
335338
"listener-80-1": {Attached: true},
336339
"listener-443-1": {Attached: true},
@@ -424,6 +427,7 @@ var _ = Describe("ChangeProcessor", func() {
424427
IgnoredGatewayStatuses: map[types.NamespacedName]state.IgnoredGatewayStatus{},
425428
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
426429
{Namespace: "test", Name: "hr-1"}: {
430+
ObservedGeneration: hr1Updated.Generation,
427431
ParentStatuses: map[string]state.ParentStatus{
428432
"listener-80-1": {Attached: true},
429433
"listener-443-1": {Attached: true},
@@ -517,6 +521,7 @@ var _ = Describe("ChangeProcessor", func() {
517521
IgnoredGatewayStatuses: map[types.NamespacedName]state.IgnoredGatewayStatus{},
518522
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
519523
{Namespace: "test", Name: "hr-1"}: {
524+
ObservedGeneration: hr1Updated.Generation,
520525
ParentStatuses: map[string]state.ParentStatus{
521526
"listener-80-1": {Attached: true},
522527
"listener-443-1": {Attached: true},
@@ -610,6 +615,7 @@ var _ = Describe("ChangeProcessor", func() {
610615
IgnoredGatewayStatuses: map[types.NamespacedName]state.IgnoredGatewayStatus{},
611616
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
612617
{Namespace: "test", Name: "hr-1"}: {
618+
ObservedGeneration: hr1Updated.Generation,
613619
ParentStatuses: map[string]state.ParentStatus{
614620
"listener-80-1": {Attached: true},
615621
"listener-443-1": {Attached: true},
@@ -706,6 +712,7 @@ var _ = Describe("ChangeProcessor", func() {
706712
},
707713
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
708714
{Namespace: "test", Name: "hr-1"}: {
715+
ObservedGeneration: hr1Updated.Generation,
709716
ParentStatuses: map[string]state.ParentStatus{
710717
"listener-80-1": {Attached: true},
711718
"listener-443-1": {Attached: true},
@@ -792,12 +799,14 @@ var _ = Describe("ChangeProcessor", func() {
792799
},
793800
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
794801
{Namespace: "test", Name: "hr-1"}: {
802+
ObservedGeneration: hr1Updated.Generation,
795803
ParentStatuses: map[string]state.ParentStatus{
796804
"listener-80-1": {Attached: true},
797805
"listener-443-1": {Attached: true},
798806
},
799807
},
800808
{Namespace: "test", Name: "hr-2"}: {
809+
ObservedGeneration: hr2.Generation,
801810
ParentStatuses: map[string]state.ParentStatus{
802811
"listener-80-1": {Attached: false},
803812
"listener-443-1": {Attached: false},
@@ -880,6 +889,7 @@ var _ = Describe("ChangeProcessor", func() {
880889
IgnoredGatewayStatuses: map[types.NamespacedName]state.IgnoredGatewayStatus{},
881890
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
882891
{Namespace: "test", Name: "hr-2"}: {
892+
ObservedGeneration: hr2.Generation,
883893
ParentStatuses: map[string]state.ParentStatus{
884894
"listener-80-1": {Attached: true},
885895
"listener-443-1": {Attached: true},

internal/state/statuses.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ type ListenerStatus struct {
4444
// ParentStatuses holds the statuses of parents where the key is the section name in a parentRef.
4545
type ParentStatuses map[string]ParentStatus
4646

47+
// HTTPRouteStatus holds the status-related information about an HTTPRoute resource.
4748
type HTTPRouteStatus struct {
49+
// ObservedGeneration is the generation of the resource that was processed.
50+
ObservedGeneration int64
51+
// ParentStatuses holds the statuses for parentRefs of the HTTPRoute.
4852
ParentStatuses ParentStatuses
4953
}
5054

@@ -116,7 +120,8 @@ func buildStatuses(graph *graph) Statuses {
116120
}
117121

118122
statuses.HTTPRouteStatuses[nsname] = HTTPRouteStatus{
119-
ParentStatuses: parentStatuses,
123+
ObservedGeneration: r.Source.Generation,
124+
ParentStatuses: parentStatuses,
120125
}
121126
}
122127

internal/state/statuses_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func TestBuildStatuses(t *testing.T) {
2121

2222
routes := map[types.NamespacedName]*route{
2323
{Namespace: "test", Name: "hr-1"}: {
24+
Source: &v1beta1.HTTPRoute{
25+
ObjectMeta: metav1.ObjectMeta{
26+
Generation: 3,
27+
},
28+
},
2429
ValidSectionNameRefs: map[string]struct{}{
2530
"listener-80-1": {},
2631
},
@@ -32,6 +37,11 @@ func TestBuildStatuses(t *testing.T) {
3237

3338
routesAllRefsInvalid := map[types.NamespacedName]*route{
3439
{Namespace: "test", Name: "hr-1"}: {
40+
Source: &v1beta1.HTTPRoute{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Generation: 4,
43+
},
44+
},
3545
InvalidSectionNameRefs: map[string]struct{}{
3646
"listener-80-2": {},
3747
"listener-80-1": {},
@@ -95,6 +105,7 @@ func TestBuildStatuses(t *testing.T) {
95105
},
96106
HTTPRouteStatuses: map[types.NamespacedName]HTTPRouteStatus{
97107
{Namespace: "test", Name: "hr-1"}: {
108+
ObservedGeneration: 3,
98109
ParentStatuses: map[string]ParentStatus{
99110
"listener-80-1": {
100111
Attached: true,
@@ -136,6 +147,7 @@ func TestBuildStatuses(t *testing.T) {
136147
},
137148
HTTPRouteStatuses: map[types.NamespacedName]HTTPRouteStatus{
138149
{Namespace: "test", Name: "hr-1"}: {
150+
ObservedGeneration: 3,
139151
ParentStatuses: map[string]ParentStatus{
140152
"listener-80-1": {
141153
Attached: false,
@@ -187,6 +199,7 @@ func TestBuildStatuses(t *testing.T) {
187199
},
188200
HTTPRouteStatuses: map[types.NamespacedName]HTTPRouteStatus{
189201
{Namespace: "test", Name: "hr-1"}: {
202+
ObservedGeneration: 3,
190203
ParentStatuses: map[string]ParentStatus{
191204
"listener-80-1": {
192205
Attached: false,
@@ -221,6 +234,7 @@ func TestBuildStatuses(t *testing.T) {
221234
IgnoredGatewayStatuses: map[types.NamespacedName]IgnoredGatewayStatus{},
222235
HTTPRouteStatuses: map[types.NamespacedName]HTTPRouteStatus{
223236
{Namespace: "test", Name: "hr-1"}: {
237+
ObservedGeneration: 4,
224238
ParentStatuses: map[string]ParentStatus{
225239
"listener-80-1": {
226240
Attached: false,

internal/status/httproute.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func prepareHTTPRouteStatus(
3333
ps := status.ParentStatuses[name]
3434

3535
var (
36-
status metav1.ConditionStatus
37-
reason string // FIXME(pleshakov) use RouteConditionReason once we upgrade to v1beta1
36+
conditionStatus metav1.ConditionStatus
37+
reason string // FIXME(pleshakov) use RouteConditionReason once we upgrade to v1beta1
3838
)
3939

4040
if ps.Attached {
41-
status = metav1.ConditionTrue
41+
conditionStatus = metav1.ConditionTrue
4242
reason = "Accepted" // FIXME(pleshakov): use RouteReasonAccepted once we upgrade to v1beta1
4343
} else {
44-
status = metav1.ConditionFalse
44+
conditionStatus = metav1.ConditionFalse
4545
reason = "NotAttached" // FIXME(pleshakov): use a more specific message from the defined constants (available in v1beta1)
4646
}
4747

@@ -56,10 +56,9 @@ func prepareHTTPRouteStatus(
5656
ControllerName: v1beta1.GatewayController(gatewayCtlrName),
5757
Conditions: []metav1.Condition{
5858
{
59-
Type: string(v1beta1.RouteConditionAccepted),
60-
Status: status,
61-
// FIXME(pleshakov) Set the observed generation to the last processed generation of the HTTPRoute resource.
62-
ObservedGeneration: 123,
59+
Type: string(v1beta1.RouteConditionAccepted),
60+
Status: conditionStatus,
61+
ObservedGeneration: status.ObservedGeneration,
6362
LastTransitionTime: transitionTime,
6463
Reason: reason,
6564
Message: "", // FIXME(pleshakov): Figure out a good message

internal/status/httproute_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
func TestPrepareHTTPRouteStatus(t *testing.T) {
1717
status := state.HTTPRouteStatus{
18+
ObservedGeneration: 1,
1819
ParentStatuses: map[string]state.ParentStatus{
1920
"attached": {
2021
Attached: true,
@@ -44,7 +45,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
4445
{
4546
Type: string(v1beta1.RouteConditionAccepted),
4647
Status: metav1.ConditionTrue,
47-
ObservedGeneration: 123,
48+
ObservedGeneration: 1,
4849
LastTransitionTime: transitionTime,
4950
Reason: "Accepted",
5051
},
@@ -61,7 +62,7 @@ func TestPrepareHTTPRouteStatus(t *testing.T) {
6162
{
6263
Type: string(v1beta1.RouteConditionAccepted),
6364
Status: metav1.ConditionFalse,
64-
ObservedGeneration: 123,
65+
ObservedGeneration: 1,
6566
LastTransitionTime: transitionTime,
6667
Reason: "NotAttached",
6768
},

internal/status/updater_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ var _ = Describe("Updater", func() {
9292
},
9393
HTTPRouteStatuses: map[types.NamespacedName]state.HTTPRouteStatus{
9494
{Namespace: "test", Name: "route1"}: {
95+
ObservedGeneration: 5,
9596
ParentStatuses: map[string]state.ParentStatus{
9697
"http": {
9798
Attached: valid,
@@ -210,7 +211,7 @@ var _ = Describe("Updater", func() {
210211
{
211212
Type: string(gatewayv1beta1.RouteConditionAccepted),
212213
Status: metav1.ConditionTrue,
213-
ObservedGeneration: 123,
214+
ObservedGeneration: 5,
214215
LastTransitionTime: fakeClockTime,
215216
Reason: "Accepted",
216217
},

0 commit comments

Comments
 (0)