Skip to content

Commit b0b0fb9

Browse files
authored
Merge pull request #3 from zongzw/master
refactor deploying process after adding gatewayclass.
2 parents ce1c56d + 1c270ac commit b0b0fb9

File tree

14 files changed

+1302
-414
lines changed

14 files changed

+1302
-414
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
// "--bigip-url", "https://10.250.15.180",
2323
"--bigip-url", "https://10.250.18.105:8443",
2424
"--bigip-username", "admin",
25-
"--bigip-password", "P@ssw0rd123"
25+
"--bigip-password", "P@ssw0rd123",
26+
"--controller-name", "f5.io/gateway-controller-name"
2627
]
2728
}
2829
]

controllers/gateway_controller.go

Lines changed: 186 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"time"
2323

24-
v1 "k8s.io/api/core/v1"
2524
"k8s.io/apimachinery/pkg/runtime"
2625
ctrl "sigs.k8s.io/controller-runtime"
2726
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -30,6 +29,7 @@ import (
3029
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
3130

3231
"gitee.com/zongzw/bigip-kubernetes-gateway/pkg"
32+
"gitee.com/zongzw/f5-bigip-rest/utils"
3333
)
3434

3535
type GatewayReconciler struct {
@@ -59,56 +59,15 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
5959
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
6060
if client.IgnoreNotFound(err) == nil {
6161
// delete resources
62-
gw := pkg.ActiveSIGs.GetGateway(req.NamespacedName.String())
63-
if ocfgs, err := pkg.ParseRelated([]*gatewayv1beta1.Gateway{gw}, []*gatewayv1beta1.HTTPRoute{}, []*v1.Service{}); err != nil {
64-
return ctrl.Result{}, err
65-
} else {
66-
zlog.V(1).Info("handling + deleting " + req.NamespacedName.String())
67-
hrs := pkg.ActiveSIGs.AttachedHTTPRoutes(gw)
68-
pkg.ActiveSIGs.UnsetGateway(req.NamespacedName.String())
69-
if ncfgs, err := pkg.ParseRelated([]*gatewayv1beta1.Gateway{}, hrs, []*v1.Service{}); 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-
}
80-
}
81-
}
82-
83-
return ctrl.Result{}, nil
62+
defer pkg.ActiveSIGs.UnsetGateway(req.NamespacedName.String())
63+
return handleDeletingGateway(ctx, req)
8464
} else {
8565
return ctrl.Result{}, err
8666
}
8767
} else {
8868
// upsert resources
89-
zlog.V(1).Info("handling + upserting " + req.NamespacedName.String())
90-
ogw := pkg.ActiveSIGs.GetGateway(req.NamespacedName.String())
91-
if ocfgs, err := pkg.ParseRelated([]*gatewayv1beta1.Gateway{ogw}, []*gatewayv1beta1.HTTPRoute{}, []*v1.Service{}); err != nil {
92-
zlog.Error(err, "handling + upserting + parse related ocfgs "+req.NamespacedName.String())
93-
return ctrl.Result{}, err
94-
} else {
95-
ngw := obj.DeepCopy()
96-
pkg.ActiveSIGs.SetGateway(ngw)
97-
if ncfgs, err := pkg.ParseRelated([]*gatewayv1beta1.Gateway{ngw}, []*gatewayv1beta1.HTTPRoute{}, []*v1.Service{}); err != nil {
98-
zlog.Error(err, "handling + upserting + parse related ncfgs "+req.NamespacedName.String())
99-
return ctrl.Result{}, err
100-
} else {
101-
pkg.PendingDeploys <- pkg.DeployRequest{
102-
Meta: fmt.Sprintf("upserting gateway '%s'", req.NamespacedName.String()),
103-
From: &ocfgs,
104-
To: &ncfgs,
105-
StatusFunc: func() {
106-
// do something
107-
},
108-
}
109-
return ctrl.Result{}, nil
110-
}
111-
}
69+
defer pkg.ActiveSIGs.SetGateway(&obj)
70+
return handleUpsertingGateway(ctx, &obj)
11271
}
11372
}
11473

@@ -118,3 +77,184 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
11877
For(&gatewayv1beta1.Gateway{}).
11978
Complete(r)
12079
}
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

Comments
 (0)