11package controllers
22
33import (
4- "context"
5-
64 corev1 "k8s.io/api/core/v1"
75 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
86 gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
97)
108
11- // updateGatewayStatus computes the new Gateway status, setting its ready condition and all the
12- // ready listeners's ready conditions to true, unless a resolvedRefs error is discovered. In
13- // that case, the proper listener ready condition and the gateway one are set to false.
14- // The addresses are updated as well.
15- func updateGatewayStatus (_ context.Context , gateway * gatewayv1beta1.Gateway , svc * corev1.Service ) {
16- // gateway addresses
17- gwaddrs := make ([]gatewayv1beta1.GatewayStatusAddress , 0 , len (svc .Status .LoadBalancer .Ingress ))
9+ func setGatewayStatusAddresses (gateway * gatewayv1beta1.Gateway , svc * corev1.Service ) {
10+ gwaddrs := []gatewayv1beta1.GatewayStatusAddress {}
1811 for _ , addr := range svc .Status .LoadBalancer .Ingress {
1912 if addr .IP != "" {
2013 gwaddrs = append (gwaddrs , gatewayv1beta1.GatewayStatusAddress {
@@ -30,17 +23,10 @@ func updateGatewayStatus(_ context.Context, gateway *gatewayv1beta1.Gateway, svc
3023 }
3124 }
3225 gateway .Status .Addresses = gwaddrs
26+ }
3327
34- // gateway conditions
35- newGatewayAcceptedCondition := metav1.Condition {
36- Type : string (gatewayv1beta1 .GatewayConditionAccepted ),
37- Status : metav1 .ConditionTrue ,
38- Reason : string (gatewayv1beta1 .GatewayReasonAccepted ),
39- ObservedGeneration : gateway .Generation ,
40- LastTransitionTime : metav1 .Now (),
41- Message : "blixt controlplane accepts responsibility for the Gateway" ,
42- }
43- newGatewayProgrammedCondition := metav1.Condition {
28+ func setGatewayListenerConditionsAndProgrammed (gateway * gatewayv1beta1.Gateway ) {
29+ programmed := metav1.Condition {
4430 Type : string (gatewayv1beta1 .GatewayConditionProgrammed ),
4531 Status : metav1 .ConditionTrue ,
4632 Reason : string (gatewayv1beta1 .GatewayReasonProgrammed ),
@@ -49,7 +35,6 @@ func updateGatewayStatus(_ context.Context, gateway *gatewayv1beta1.Gateway, svc
4935 Message : "the gateway is ready to route traffic" ,
5036 }
5137
52- // gateway listeners conditions
5338 listenersStatus := make ([]gatewayv1beta1.ListenerStatus , 0 , len (gateway .Spec .Listeners ))
5439 for _ , l := range gateway .Spec .Listeners {
5540 supportedKinds , resolvedRefsCondition := getSupportedKinds (gateway .Generation , l )
@@ -63,6 +48,13 @@ func updateGatewayStatus(_ context.Context, gateway *gatewayv1beta1.Gateway, svc
6348 Name : l .Name ,
6449 SupportedKinds : supportedKinds ,
6550 Conditions : []metav1.Condition {
51+ {
52+ Type : string (gatewayv1beta1 .ListenerConditionAccepted ),
53+ Status : metav1 .ConditionTrue ,
54+ Reason : string (gatewayv1beta1 .ListenerReasonAccepted ),
55+ ObservedGeneration : gateway .Generation ,
56+ LastTransitionTime : metav1 .Now (),
57+ },
6658 {
6759 Type : string (gatewayv1beta1 .ListenerConditionProgrammed ),
6860 Status : metav1 .ConditionStatus (listenerProgrammedStatus ),
@@ -74,42 +66,16 @@ func updateGatewayStatus(_ context.Context, gateway *gatewayv1beta1.Gateway, svc
7466 },
7567 })
7668 if resolvedRefsCondition .Status == metav1 .ConditionFalse {
77- newGatewayProgrammedCondition .Status = metav1 .ConditionFalse
78- newGatewayProgrammedCondition .Reason = string (gatewayv1beta1 .GatewayReasonAddressNotAssigned )
79- newGatewayProgrammedCondition .Message = "the gateway is not ready to route traffic"
69+ programmed .Status = metav1 .ConditionFalse
70+ programmed .Reason = string (gatewayv1beta1 .GatewayReasonAddressNotAssigned )
71+ programmed .Message = "the gateway is not ready to route traffic"
8072 }
8173 }
82-
83- gateway .Status .Conditions = []metav1.Condition {
84- newGatewayAcceptedCondition ,
85- newGatewayProgrammedCondition ,
86- }
8774 gateway .Status .Listeners = listenersStatus
75+ setCond (gateway , programmed )
8876}
8977
90- // initGatewayStatus initializes the GatewayStatus, setting the ready condition to
91- // not ready and all the listeners ready status to not ready as well.
92- func initGatewayStatus (gateway * gatewayv1beta1.Gateway ) {
93- gateway .Status = gatewayv1beta1.GatewayStatus {
94- Conditions : []metav1.Condition {
95- {
96- Type : string (gatewayv1beta1 .GatewayConditionAccepted ),
97- Status : metav1 .ConditionTrue ,
98- Reason : string (gatewayv1beta1 .GatewayReasonAccepted ),
99- ObservedGeneration : gateway .Generation ,
100- LastTransitionTime : metav1 .Now (),
101- Message : "blixt controlplane accepts responsibility for the Gateway" ,
102- },
103- {
104- Type : string (gatewayv1beta1 .GatewayConditionProgrammed ),
105- Status : metav1 .ConditionFalse ,
106- Reason : string (gatewayv1beta1 .GatewayReasonAddressNotAssigned ),
107- ObservedGeneration : gateway .Generation ,
108- LastTransitionTime : metav1 .Now (),
109- Message : "the gateway is not ready to route traffic" ,
110- },
111- },
112- }
78+ func setGatewayListenerStatus (gateway * gatewayv1beta1.Gateway ) {
11379 gateway .Status .Listeners = make ([]gatewayv1beta1.ListenerStatus , 0 , len (gateway .Spec .Listeners ))
11480 for _ , l := range gateway .Spec .Listeners {
11581 supportedKinds , resolvedRefsCondition := getSupportedKinds (gateway .Generation , l )
@@ -160,12 +126,12 @@ func getSupportedKinds(generation int64, listener gatewayv1beta1.Listener) (supp
160126 case gatewayv1beta1 .HTTPProtocolType :
161127 supportedKinds = append (supportedKinds , gatewayv1beta1.RouteGroupKind {
162128 Group : (* gatewayv1beta1 .Group )(& gatewayv1beta1 .GroupVersion .Group ),
163- Kind : "TCPRoute " ,
129+ Kind : "HTTPRoute " ,
164130 })
165131 case gatewayv1beta1 .HTTPSProtocolType :
166132 supportedKinds = append (supportedKinds , gatewayv1beta1.RouteGroupKind {
167133 Group : (* gatewayv1beta1 .Group )(& gatewayv1beta1 .GroupVersion .Group ),
168- Kind : "TCPRoute " ,
134+ Kind : "HTTPRoute " ,
169135 })
170136 default :
171137 resolvedRefsCondition .Status = metav1 .ConditionFalse
@@ -188,38 +154,20 @@ func getSupportedKinds(generation int64, listener gatewayv1beta1.Listener) (supp
188154 return supportedKinds , resolvedRefsCondition
189155}
190156
191- // factorizeStatus takes the old gateway conditions not transitioned and copies them
157+ // updateConditionGeneration takes the old gateway conditions not transitioned and copies them
192158// into the new gateway status, so that only the transitioning conditions gets actually patched.
193- func factorizeStatus (gateway , oldGateway * gatewayv1beta1.Gateway ) {
194- for i , c := range gateway .Status .Conditions {
195- for _ , oldC := range oldGateway .Status .Conditions {
196- if c .Type == oldC .Type {
197- if c .Status == oldC .Status && c .Reason == oldC .Reason {
198- gateway .Status .Conditions [i ] = oldC
199- }
200- }
201- }
202- }
203-
159+ func updateConditionGeneration (gateway * gatewayv1beta1.Gateway ) {
204160 for i := 0 ; i < len (gateway .Status .Conditions ); i ++ {
205161 gateway .Status .Conditions [0 ].ObservedGeneration = gateway .Generation
206162 }
207163
208- for i , l := range gateway .Status .Listeners {
209- for j , lc := range l .Conditions {
210- for _ , ol := range oldGateway .Status .Listeners {
211- if ol .Name != l .Name {
212- continue
213- }
214- for _ , olc := range ol .Conditions {
215- if lc .Type == olc .Type {
216- if lc .Status == olc .Status && lc .Reason == olc .Reason {
217- gateway .Status .Listeners [i ].Conditions [j ] = olc
218- }
219- }
220- }
221- }
164+ for i := 0 ; i < len (gateway .Status .Listeners ); i ++ {
165+ updatedListenerConditions := []metav1.Condition {}
166+ for _ , cond := range gateway .Status .Listeners [0 ].Conditions {
167+ cond .ObservedGeneration = gateway .Generation
168+ updatedListenerConditions = append (updatedListenerConditions , cond )
222169 }
170+ gateway .Status .Listeners [0 ].Conditions = updatedListenerConditions
223171 }
224172}
225173
@@ -232,28 +180,34 @@ func isGatewayAccepted(gateway *gatewayv1beta1.Gateway) bool {
232180}
233181
234182func getAcceptedConditionForGateway (gateway * gatewayv1beta1.Gateway ) * metav1.Condition {
235- for _ , c := range gateway .Status .Conditions {
236- if c .Type == string (gatewayv1beta1 .GatewayConditionAccepted ) {
237- return & c
238- }
239- }
240- return nil
183+ return getCond (gateway , string (gatewayv1beta1 .GatewayConditionAccepted ))
241184}
242185
243- // isGatewayProgrammed returns two boolean values:
244- // - the status of the programmed condition
245- // - a boolean flag to check if the condition exists
246- func isGatewayProgrammed (gateway * gatewayv1beta1.Gateway ) (status bool , isSet bool ) {
247- for _ , c := range gateway .Status .Conditions {
248- if c .Type == string (gatewayv1beta1 .GatewayConditionProgrammed ) {
249- return c .Status == metav1 .ConditionTrue , true
186+ func setCond (gateway * gatewayv1beta1.Gateway , setCond metav1.Condition ) {
187+ updatedConditions := make ([]metav1.Condition , 0 , len (gateway .Status .Conditions ))
188+
189+ found := false
190+ for _ , oldCond := range gateway .Status .Conditions {
191+ if oldCond .Type == setCond .Type {
192+ found = true
193+ updatedConditions = append (updatedConditions , setCond )
194+ } else {
195+ updatedConditions = append (updatedConditions , oldCond )
250196 }
251197 }
252- return false , false
198+
199+ if ! found {
200+ updatedConditions = append (updatedConditions , setCond )
201+ }
202+
203+ gateway .Status .Conditions = updatedConditions
253204}
254205
255- // sameConditions returns true if the type, status and reason match for
256- // the two provided metav1.Conditions.
257- func sameConditions (cond1 , cond2 * metav1.Condition ) bool {
258- return cond1 .Type == cond2 .Type && cond1 .Status == cond2 .Status && cond1 .Reason == cond2 .Reason && cond1 .Message == cond2 .Message && cond1 .ObservedGeneration == cond2 .ObservedGeneration
206+ func getCond (gateway * gatewayv1beta1.Gateway , requestedType string ) * metav1.Condition {
207+ for _ , cond := range gateway .Status .Conditions {
208+ if cond .Type == requestedType {
209+ return & cond
210+ }
211+ }
212+ return nil
259213}
0 commit comments