Skip to content

Commit cd54cf6

Browse files
committed
filter gateways
Change-Id: Id002c733688759d3dfbad8975e53805d626f2d01
1 parent c2d9920 commit cd54cf6

File tree

2 files changed

+73
-14
lines changed

2 files changed

+73
-14
lines changed

pkg/controller/controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ func startCloudControllerManager(ctx context.Context, clusterName string, config
276276
return nil, err
277277
}
278278

279+
err = gatewayController.Init(ctx)
280+
if err != nil {
281+
klog.Errorf("Failed to initialize gateway controller: %v", err)
282+
cancel()
283+
return nil, err
284+
}
285+
279286
go gatewayController.Run(ctx)
280287

281288
sharedGwInformers.Start(ctx.Done())

pkg/gateway/controller.go

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
const (
2424
controllerName = "kind.sigs.k8s.io/gateway-controller"
25-
gwClassName = "cloud-provider-kind"
25+
GWClassName = "cloud-provider-kind"
2626
maxRetries = 5
2727
workers = 5
2828
)
@@ -73,7 +73,7 @@ func New(
7373
_, err := gatewayInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
7474
AddFunc: func(obj interface{}) {
7575
gw := obj.(*gatewayv1.Gateway)
76-
if gw.Spec.GatewayClassName != gwClassName {
76+
if gw.Spec.GatewayClassName != GWClassName {
7777
return
7878
}
7979
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
@@ -83,7 +83,7 @@ func New(
8383
},
8484
UpdateFunc: func(oldObj, newObj interface{}) {
8585
gw := newObj.(*gatewayv1.Gateway)
86-
if gw.Spec.GatewayClassName != gwClassName {
86+
if gw.Spec.GatewayClassName != GWClassName {
8787
return
8888
}
8989
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(newObj)
@@ -93,7 +93,7 @@ func New(
9393
},
9494
DeleteFunc: func(obj interface{}) {
9595
gw := obj.(*gatewayv1.Gateway)
96-
if gw.Spec.GatewayClassName != gwClassName {
96+
if gw.Spec.GatewayClassName != GWClassName {
9797
return
9898
}
9999
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
@@ -108,18 +108,30 @@ func New(
108108

109109
_, err = httprouteInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
110110
AddFunc: func(obj interface{}) {
111+
httproute := obj.(*gatewayv1.HTTPRoute)
112+
if !c.isOwned(httproute.Spec.ParentRefs) {
113+
return
114+
}
111115
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
112116
if err == nil {
113117
c.httproutequeue.Add(key)
114118
}
115119
},
116120
UpdateFunc: func(oldObj, newObj interface{}) {
121+
httproute := newObj.(*gatewayv1.HTTPRoute)
122+
if !c.isOwned(httproute.Spec.ParentRefs) {
123+
return
124+
}
117125
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(newObj)
118126
if err == nil {
119127
c.httproutequeue.Add(key)
120128
}
121129
},
122130
DeleteFunc: func(obj interface{}) {
131+
httproute := obj.(*gatewayv1.HTTPRoute)
132+
if !c.isOwned(httproute.Spec.ParentRefs) {
133+
return
134+
}
123135
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
124136
if err == nil {
125137
c.httproutequeue.Add(key)
@@ -132,18 +144,30 @@ func New(
132144

133145
_, err = grpcrouteInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
134146
AddFunc: func(obj interface{}) {
147+
grpcroute := obj.(*gatewayv1.GRPCRoute)
148+
if !c.isOwned(grpcroute.Spec.ParentRefs) {
149+
return
150+
}
135151
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
136152
if err == nil {
137153
c.grpcroutequeue.Add(key)
138154
}
139155
},
140156
UpdateFunc: func(oldObj, newObj interface{}) {
157+
grpcroute := newObj.(*gatewayv1.GRPCRoute)
158+
if !c.isOwned(grpcroute.Spec.ParentRefs) {
159+
return
160+
}
141161
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(newObj)
142162
if err == nil {
143163
c.grpcroutequeue.Add(key)
144164
}
145165
},
146166
DeleteFunc: func(obj interface{}) {
167+
grpcroute := obj.(*gatewayv1.GRPCRoute)
168+
if !c.isOwned(grpcroute.Spec.ParentRefs) {
169+
return
170+
}
147171
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
148172
if err == nil {
149173
c.grpcroutequeue.Add(key)
@@ -157,35 +181,43 @@ func New(
157181
return c, nil
158182
}
159183

160-
// Run begins watching and syncing.
161-
func (c *Controller) Run(ctx context.Context) error {
184+
// Init install CRDs and creates GatewayClass
185+
func (c *Controller) Init(ctx context.Context) error {
162186
defer runtime.HandleCrashWithContext(ctx)
163-
164-
// Let the workers stop when we are done
165-
defer c.gatewayqueue.ShutDown()
166-
defer c.httproutequeue.ShutDown()
167-
defer c.grpcroutequeue.ShutDown()
168-
klog.Info("Starting Gateway API controller")
187+
// Install GatewayAPI CRDs if do not exist
169188

170189
// Create GatewayClass if it does not exist
171190
gwClass := gatewayv1.GatewayClass{
172191
ObjectMeta: metav1.ObjectMeta{
173-
Name: gwClassName,
192+
Name: GWClassName,
174193
},
175194
Spec: gatewayv1.GatewayClassSpec{
176195
ControllerName: controllerName,
177196
Description: ptr.To("cloud-provider-kind gateway API"),
178197
},
179198
}
180199

181-
if _, err := c.gwClient.GatewayV1().GatewayClasses().Get(ctx, gwClassName, metav1.GetOptions{}); apierrors.IsNotFound(err) {
200+
if _, err := c.gwClient.GatewayV1().GatewayClasses().Get(ctx, GWClassName, metav1.GetOptions{}); apierrors.IsNotFound(err) {
182201
_, err := c.gwClient.GatewayV1().GatewayClasses().Create(ctx, &gwClass, metav1.CreateOptions{})
183202
if err != nil {
184203
klog.Infof("faile to create cloud-provider-kind GatewayClass: %v")
185204
return err
186205
}
187206
}
188207

208+
return nil
209+
}
210+
211+
// Run begins watching and syncing.
212+
func (c *Controller) Run(ctx context.Context) error {
213+
defer runtime.HandleCrashWithContext(ctx)
214+
215+
// Let the workers stop when we are done
216+
defer c.gatewayqueue.ShutDown()
217+
defer c.httproutequeue.ShutDown()
218+
defer c.grpcroutequeue.ShutDown()
219+
klog.Info("Starting Gateway API controller")
220+
189221
// Wait for all involved caches to be synced, before processing items from the queue is started
190222
if !cache.WaitForNamedCacheSync(controllerName, ctx.Done(), c.gatewayListerSynced, c.httprouteListerSynced, c.grpcrouteListerSynced) {
191223
return fmt.Errorf("Timed out waiting for caches to sync")
@@ -201,3 +233,23 @@ func (c *Controller) Run(ctx context.Context) error {
201233
klog.Info("Stopping Gateway API controller")
202234
return nil
203235
}
236+
237+
func (c *Controller) isOwned(references []gatewayv1.ParentReference) bool {
238+
for _, ref := range references {
239+
if string(*ref.Group) != "gateway.networking.k8s.io" && string(*ref.Group) != "" {
240+
continue
241+
}
242+
if string(*ref.Kind) != "Gateway" {
243+
continue
244+
}
245+
246+
gw, err := c.gatewayLister.Gateways(string(*ref.Namespace)).Get(string(ref.Name))
247+
if err != nil {
248+
continue
249+
}
250+
if gw.Spec.GatewayClassName == GWClassName {
251+
return true
252+
}
253+
}
254+
return false
255+
}

0 commit comments

Comments
 (0)