@@ -29,6 +29,7 @@ import (
2929 gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
3030
3131 "gitee.com/zongzw/bigip-kubernetes-gateway/pkg"
32+ "gitee.com/zongzw/f5-bigip-rest/utils"
3233)
3334
3435type GatewayReconciler struct {
@@ -58,111 +59,15 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
5859 if err := r .Get (ctx , req .NamespacedName , & obj ); err != nil {
5960 if client .IgnoreNotFound (err ) == nil {
6061 // delete resources
61- gw := pkg .ActiveSIGs .GetGateway (req .NamespacedName .String ())
62- // Only when we know all the gateways can we know exactly which routes need to be cleared because of this gateway event.
63- gws := pkg .ActiveSIGs .GetNeighborGateways (gw )
64- if ocfgs , err := pkg .ParseGatewayRelatedForClass (string (gw .Spec .GatewayClassName ), append (gws , gw )); err != nil {
65- return ctrl.Result {}, err
66- } else {
67- zlog .V (1 ).Info ("handling + deleting " + req .NamespacedName .String ())
68- pkg .ActiveSIGs .UnsetGateway (req .NamespacedName .String ())
69- if ncfgs , err := pkg .ParseGatewayRelatedForClass (string (gw .Spec .GatewayClassName ), gws ); err != nil {
70- return ctrl.Result {}, err
71- } else {
72- pkg .PendingDeploys <- pkg.DeployRequest {
73- Meta : fmt .Sprintf ("deleting gateway '%s'" , req .NamespacedName .String ()),
74- From : & ocfgs ,
75- To : & ncfgs ,
76- StatusFunc : func () {
77- // do something
78- },
79- Partition : string (gw .Spec .GatewayClassName ),
80- }
81- }
82- }
83-
84- return ctrl.Result {}, nil
62+ defer pkg .ActiveSIGs .UnsetGateway (req .NamespacedName .String ())
63+ return handleDeletingGateway (ctx , req )
8564 } else {
8665 return ctrl.Result {}, err
8766 }
8867 } else {
8968 // upsert resources
90- zlog .V (1 ).Info ("handling + upserting " + req .NamespacedName .String ())
91- ogw := pkg .ActiveSIGs .GetGateway (req .NamespacedName .String ())
92- if ogw == nil {
93- ogw = & obj
94- pkg .ActiveSIGs .SetGateway (obj .DeepCopy ())
95- }
96-
97- if ocfgs , err := pkg .ParseGatewayRelatedForClass (string (ogw .Spec .GatewayClassName ), []* gatewayv1beta1.Gateway {ogw }); err != nil {
98- zlog .Error (err , "handling + upserting + parse related ocfgs " + req .NamespacedName .String ())
99- return ctrl.Result {}, err
100- } else {
101- ngw := obj .DeepCopy ()
102- if ngw .Spec .GatewayClassName == ogw .Spec .GatewayClassName {
103- pkg .ActiveSIGs .SetGateway (ngw )
104- if ncfgs , err := pkg .ParseGatewayRelatedForClass (string (ngw .Spec .GatewayClassName ), []* gatewayv1beta1.Gateway {ngw }); err != nil {
105- zlog .Error (err , "handling + upserting + parse related ncfgs " + req .NamespacedName .String ())
106- return ctrl.Result {}, err
107- } else {
108- pkg .PendingDeploys <- pkg.DeployRequest {
109- Meta : fmt .Sprintf ("upserting gateway '%s'" , req .NamespacedName .String ()),
110- From : & ocfgs ,
111- To : & ncfgs ,
112- StatusFunc : func () {
113- // do something
114- },
115- Partition : string (ngw .Spec .GatewayClassName ),
116- }
117- return ctrl.Result {}, nil
118- }
119- } else {
120- // original state of new gatewayclass env
121- // gateway is go away
122- ngs := pkg .ActiveSIGs .GetNeighborGateways (ogw )
123-
124- ocfgs , err := pkg .ParseGatewayRelatedForClass (string (ngw .Spec .GatewayClassName ), append (ngs , ogw ))
125- if err != nil {
126- return ctrl.Result {}, err
127- }
128-
129- if ncfgs , err := pkg .ParseGatewayRelatedForClass (string (ogw .Spec .GatewayClassName ), ngs ); err != nil {
130- return ctrl.Result {}, err
131- } else {
132- pkg .PendingDeploys <- pkg.DeployRequest {
133- Meta : fmt .Sprintf ("upserting gateway '%s'" , req .NamespacedName .String ()),
134- From : & ocfgs ,
135- To : & ncfgs ,
136- StatusFunc : func () {
137- // do something
138- },
139- Partition : string (ogw .Spec .GatewayClassName ),
140- }
141- }
142-
143- pkg .ActiveSIGs .SetGateway (ngw )
144-
145- ocfgs , err = pkg .ParseGatewayRelatedForClass (string (ngw .Spec .GatewayClassName ), ngs )
146- if err != nil {
147- return ctrl.Result {}, err
148- }
149- ncfgs , err := pkg .ParseGatewayRelatedForClass (string (ogw .Spec .GatewayClassName ), append (ngs , ngw ))
150- if err != nil {
151- return ctrl.Result {}, err
152- }
153- pkg .PendingDeploys <- pkg.DeployRequest {
154- Meta : fmt .Sprintf ("upserting gateway '%s'" , req .NamespacedName .String ()),
155- From : & ocfgs ,
156- To : & ncfgs ,
157- StatusFunc : func () {
158- // do something
159- },
160- Partition : string (ngw .Spec .GatewayClassName ),
161- }
162-
163- return ctrl.Result {}, nil
164- }
165- }
69+ defer pkg .ActiveSIGs .SetGateway (& obj )
70+ return handleUpsertingGateway (ctx , & obj )
16671 }
16772}
16873
@@ -172,3 +77,184 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
17277 For (& gatewayv1beta1.Gateway {}).
17378 Complete (r )
17479}
80+
81+ func handleDeletingGateway (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
82+ zlog := log .FromContext (ctx )
83+
84+ gw := pkg .ActiveSIGs .GetGateway (req .NamespacedName .String ())
85+ // Only when we know all the gateways can we know exactly which routes need to be cleared because of this gateway event.
86+ gws := pkg .ActiveSIGs .GetNeighborGateways (gw )
87+
88+ ocfgs , ncfgs := map [string ]interface {}{}, map [string ]interface {}{}
89+ opcfgs , npcfgs := map [string ]interface {}{}, map [string ]interface {}{}
90+ var err error
91+
92+ if ocfgs , err = pkg .ParseGatewayRelatedForClass (string (gw .Spec .GatewayClassName ), append (gws , gw )); err != nil {
93+ return ctrl.Result {}, err
94+ }
95+ if opcfgs , err = pkg .ParseServicesRelatedForAll (); err != nil {
96+ return ctrl.Result {}, err
97+ }
98+
99+ zlog .V (1 ).Info ("handling + deleting " + req .NamespacedName .String ())
100+ pkg .ActiveSIGs .UnsetGateway (req .NamespacedName .String ())
101+
102+ if ncfgs , err = pkg .ParseGatewayRelatedForClass (string (gw .Spec .GatewayClassName ), gws ); err != nil {
103+ return ctrl.Result {}, err
104+ }
105+ if npcfgs , err = pkg .ParseServicesRelatedForAll (); err != nil {
106+ return ctrl.Result {}, err
107+ }
108+
109+ pkg .PendingDeploys <- pkg.DeployRequest {
110+ Meta : fmt .Sprintf ("deleting gateway '%s'" , req .NamespacedName .String ()),
111+ From : & ocfgs ,
112+ To : & ncfgs ,
113+ StatusFunc : func () {
114+ // do something
115+ },
116+ Partition : string (gw .Spec .GatewayClassName ),
117+ }
118+
119+ pkg .PendingDeploys <- pkg.DeployRequest {
120+ Meta : fmt .Sprintf ("updating services for event '%s'" , req .NamespacedName .String ()),
121+ From : & opcfgs ,
122+ To : & npcfgs ,
123+ StatusFunc : func () {
124+ // do something
125+ },
126+ Partition : "cis-c-tenant" ,
127+ }
128+ return ctrl.Result {}, nil
129+ }
130+
131+ func handleUpsertingGateway (ctx context.Context , obj * gatewayv1beta1.Gateway ) (ctrl.Result , error ) {
132+ zlog := log .FromContext (ctx )
133+
134+ reqnsn := utils .Keyname (obj .Namespace , obj .Name )
135+ zlog .V (1 ).Info ("handling + upserting " + reqnsn )
136+
137+ ogw := pkg .ActiveSIGs .GetGateway (reqnsn )
138+ if ogw == nil {
139+ ogw = obj
140+ pkg .ActiveSIGs .SetGateway (obj .DeepCopy ())
141+ }
142+
143+ var err error
144+
145+ ngw := obj .DeepCopy ()
146+ if ngw .Spec .GatewayClassName == ogw .Spec .GatewayClassName {
147+
148+ ocfgs , ncfgs := map [string ]interface {}{}, map [string ]interface {}{}
149+ opcfgs , npcfgs := map [string ]interface {}{}, map [string ]interface {}{}
150+ ocfgs , err = pkg .ParseGatewayRelatedForClass (string (ogw .Spec .GatewayClassName ), []* gatewayv1beta1.Gateway {ogw })
151+ if err != nil {
152+ zlog .Error (err , "handling + upserting + parse related ocfgs " + reqnsn )
153+ return ctrl.Result {}, err
154+ }
155+ opcfgs , err = pkg .ParseServicesRelatedForAll ()
156+ if err != nil {
157+ return ctrl.Result {}, err
158+ }
159+
160+ pkg .ActiveSIGs .SetGateway (ngw )
161+
162+ ncfgs , err = pkg .ParseGatewayRelatedForClass (string (ngw .Spec .GatewayClassName ), []* gatewayv1beta1.Gateway {ngw })
163+ if err != nil {
164+ zlog .Error (err , "handling + upserting + parse related ncfgs " + reqnsn )
165+ return ctrl.Result {}, err
166+ }
167+ npcfgs , err = pkg .ParseServicesRelatedForAll ()
168+ if err != nil {
169+ return ctrl.Result {}, err
170+ }
171+
172+ pkg .PendingDeploys <- pkg.DeployRequest {
173+ Meta : fmt .Sprintf ("upserting services for gateway '%s'" , reqnsn ),
174+ From : & opcfgs ,
175+ To : & npcfgs ,
176+ StatusFunc : func () {
177+ // do something
178+ },
179+ Partition : "cis-c-tenant" ,
180+ }
181+
182+ pkg .PendingDeploys <- pkg.DeployRequest {
183+ Meta : fmt .Sprintf ("upserting gateway '%s'" , reqnsn ),
184+ From : & ocfgs ,
185+ To : & ncfgs ,
186+ StatusFunc : func () {
187+ // do something
188+ },
189+ Partition : string (ngw .Spec .GatewayClassName ),
190+ }
191+ return ctrl.Result {}, nil
192+
193+ } else {
194+ ocfgs1 , ncfgs1 := map [string ]interface {}{}, map [string ]interface {}{} // for original class
195+ ocfgs2 , ncfgs2 := map [string ]interface {}{}, map [string ]interface {}{} // for target class
196+ opcfgs , npcfgs := map [string ]interface {}{}, map [string ]interface {}{}
197+
198+ // gateway is go away
199+ ngs := pkg .ActiveSIGs .GetNeighborGateways (ogw )
200+
201+ if opcfgs , err = pkg .ParseServicesRelatedForAll (); err != nil {
202+ return ctrl.Result {}, err
203+ }
204+
205+ pkg .ActiveSIGs .SetGateway (ngw )
206+
207+ if npcfgs , err = pkg .ParseServicesRelatedForAll (); err != nil {
208+ return ctrl.Result {}, err
209+ }
210+
211+ pkg .PendingDeploys <- pkg.DeployRequest {
212+ Meta : fmt .Sprintf ("upserting services for gateway '%s'" , reqnsn ),
213+ From : & opcfgs ,
214+ To : & npcfgs ,
215+ StatusFunc : func () {
216+ // do something
217+ },
218+ Partition : "cis-c-tenant" ,
219+ }
220+
221+ ocfgs1 , err = pkg .ParseGatewayRelatedForClass (string (ogw .Spec .GatewayClassName ), append (ngs , ogw ))
222+ if err != nil {
223+ return ctrl.Result {}, err
224+ }
225+ if ncfgs1 , err = pkg .ParseGatewayRelatedForClass (string (ogw .Spec .GatewayClassName ), ngs ); err != nil {
226+ return ctrl.Result {}, err
227+ }
228+
229+ pkg .PendingDeploys <- pkg.DeployRequest {
230+ Meta : fmt .Sprintf ("upserting gateway '%s'" , reqnsn ),
231+ From : & ocfgs1 ,
232+ To : & ncfgs1 ,
233+ StatusFunc : func () {
234+ // do something
235+ },
236+ Partition : string (ogw .Spec .GatewayClassName ),
237+ }
238+
239+ ocfgs2 , err = pkg .ParseGatewayRelatedForClass (string (ngw .Spec .GatewayClassName ), ngs )
240+ if err != nil {
241+ return ctrl.Result {}, err
242+ }
243+ ncfgs2 , err := pkg .ParseGatewayRelatedForClass (string (ngw .Spec .GatewayClassName ), append (ngs , ngw ))
244+ if err != nil {
245+ return ctrl.Result {}, err
246+ }
247+
248+ pkg .PendingDeploys <- pkg.DeployRequest {
249+ Meta : fmt .Sprintf ("upserting gateway '%s'" , reqnsn ),
250+ From : & ocfgs2 ,
251+ To : & ncfgs2 ,
252+ StatusFunc : func () {
253+ // do something
254+ },
255+ Partition : string (ngw .Spec .GatewayClassName ),
256+ }
257+
258+ return ctrl.Result {}, nil
259+ }
260+ }
0 commit comments