Skip to content

Commit b00da1b

Browse files
zongzwgitee-org
authored andcommitted
!6 fix reference issue because of filter ExtensionRef
Merge pull request !6 from zongzw/zong-filter-extref
2 parents 03eb930 + efd1d84 commit b00da1b

File tree

2 files changed

+72
-36
lines changed

2 files changed

+72
-36
lines changed

pkg/parser.go

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,35 +136,69 @@ func ParseRelated(gwObjs []*gatewayv1beta1.Gateway, hrObjs []*gatewayv1beta1.HTT
136136
}
137137

138138
func parsePoolsFrom(hr *gatewayv1beta1.HTTPRoute, rlt map[string]interface{}) error {
139+
140+
creatPool := func(ns, n string, rlt map[string]interface{}) error {
141+
name := strings.Join([]string{ns, n}, ".")
142+
rlt["ltm/pool/"+name] = map[string]interface{}{
143+
"name": name,
144+
"monitor": "min 1 of tcp",
145+
"members": []interface{}{},
146+
147+
// "minActiveMembers": 0,
148+
// TODO: there's at least one field for PATCH a pool. or we may need to fix that
149+
// {"code":400,"message":"transaction failed:one or more properties must be specified","errorStack":[],"apiError":2}
150+
}
151+
if fmtmbs, err := parseMembersFrom(ns, n); err != nil {
152+
return err
153+
} else {
154+
rlt["ltm/pool/"+name].(map[string]interface{})["members"] = fmtmbs
155+
}
156+
157+
if mon, err := parseMonitorFrom(ns, n); err != nil {
158+
return err
159+
} else {
160+
rlt["ltm/pool/"+name].(map[string]interface{})["monitor"] = mon
161+
}
162+
return nil
163+
}
164+
139165
for _, rl := range hr.Spec.Rules {
140166
for _, br := range rl.BackendRefs {
141167
ns := hr.Namespace
142168
if br.Namespace != nil {
143169
ns = string(*br.Namespace)
144170
}
145-
name := strings.Join([]string{ns, string(br.Name)}, ".")
146-
rlt["ltm/pool/"+name] = map[string]interface{}{
147-
"name": name,
148-
"monitor": "min 1 of http tcp",
149-
"members": []interface{}{},
150-
151-
// "minActiveMembers": 0,
152-
// TODO: there's at least one field for PATCH. or we may need to fix that
153-
// {"code":400,"message":"transaction failed:one or more properties must be specified","errorStack":[],"apiError":2}
171+
if err := creatPool(ns, string(br.Name), rlt); err != nil {
172+
return err
154173
}
155-
if fmtmbs, err := parseMembersFrom(ns, string(br.Name)); err == nil {
156-
rlt["ltm/pool/"+name].(map[string]interface{})["members"] = fmtmbs
157-
}
158-
159174
// TODO: parse ARP resources for flannel type network.
160175
}
161176
}
162177

163-
// TODO: from ExtensionRef as well.
178+
// pools from ExtensionRef as well.
179+
for _, rl := range hr.Spec.Rules {
180+
for _, fl := range rl.Filters {
181+
if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
182+
er := fl.ExtensionRef
183+
if er.Group != "v1" || er.Kind != "Service" {
184+
return fmt.Errorf("resource %s of '%s' not supported", er.Name, utils.Keyname(string(er.Group), string(er.Kind)))
185+
} else {
186+
if err := creatPool(hr.Namespace, string(er.Name), rlt); err != nil {
187+
return err
188+
}
189+
}
190+
}
191+
}
192+
}
164193

165194
return nil
166195
}
167196

197+
// TODO: find the way to set monitor
198+
func parseMonitorFrom(svcNamespace, svcName string) (string, error) {
199+
return "min 1 of tcp", nil
200+
}
201+
168202
func parseMembersFrom(svcNamespace, svcName string) ([]interface{}, error) {
169203
svc := ActiveSIGs.GetService(utils.Keyname(svcNamespace, svcName))
170204
eps := ActiveSIGs.GetEndpoints(utils.Keyname(svcNamespace, svcName))

pkg/utils.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,31 +221,21 @@ func (c *SIGCache) _serviceRefsOf(hr *gatewayv1beta1.HTTPRoute) []*v1.Service {
221221
}
222222
}
223223
}
224+
for _, rl := range hr.Spec.Rules {
225+
for _, fl := range rl.Filters {
226+
if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
227+
er := fl.ExtensionRef
228+
if er.Group == "v1" && er.Kind == "Service" {
229+
if svc, ok := c.Service[utils.Keyname(hr.Namespace, string(er.Name))]; ok {
230+
svcs = append(svcs, svc)
231+
}
232+
}
233+
}
234+
}
235+
}
224236
return svcs
225237
}
226238

227-
// func (c *SIGCache) IsReferredByHTTPRoute(svcKey string) bool {
228-
// defer utils.TimeItToPrometheus()()
229-
230-
// c.mutex.RLock()
231-
// defer c.mutex.RUnlock()
232-
233-
// for _, hr := range c.HTTPRoute {
234-
// for _, rl := range hr.Spec.Rules {
235-
// for _, br := range rl.BackendRefs {
236-
// ns := hr.Namespace
237-
// if br.Namespace != nil {
238-
// ns = string(*br.Namespace)
239-
// }
240-
// if utils.Keyname(ns, string(br.Name)) == svcKey {
241-
// return true
242-
// }
243-
// }
244-
// }
245-
// }
246-
// return false
247-
// }
248-
249239
func (c *SIGCache) HTTPRoutesRefsOf(svc *v1.Service) []*gatewayv1beta1.HTTPRoute {
250240
defer utils.TimeItToPrometheus()()
251241

@@ -272,6 +262,18 @@ func (c *SIGCache) _HTTPRoutesRefsOf(svc *v1.Service) []*gatewayv1beta1.HTTPRout
272262
}
273263
}
274264
}
265+
for _, rl := range hr.Spec.Rules {
266+
for _, fl := range rl.Filters {
267+
if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
268+
er := fl.ExtensionRef
269+
if er.Group == "v1" && er.Kind == "Service" {
270+
if utils.Keyname(hr.Namespace, string(er.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
271+
return true
272+
}
273+
}
274+
}
275+
}
276+
}
275277
return false
276278
}
277279

0 commit comments

Comments
 (0)