Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit 84c271b

Browse files
zongzwgitee-org
authored andcommitted
!61 refactor some code, use latest bigip-rest.
Merge pull request !61 from zongzw/zong-refine-code
2 parents 0668fbb + 4b29dc0 commit 84c271b

File tree

10 files changed

+51
-46
lines changed

10 files changed

+51
-46
lines changed

controllers/gatewayclass_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
8484
// TODO:
8585
// 1.671457016981118e+09 DEBUG handling gatewayclass bigip {"controller": "gatewayclass", "controllerGroup": "gateway.networking.k8s.io", "controllerKind": "GatewayClass", "GatewayClass": {"name":"bigip"}, "namespace": "", "name": "bigip", "reconcileID": "bcd01fdd-8dfc-4be9-b3ab-c3d3a3fdb67e"}
8686
// 1.6714570170091689e+09 ERROR unable to update status {"controller": "gatewayclass", "controllerGroup": "gateway.networking.k8s.io", "controllerKind": "GatewayClass", "GatewayClass": {"name":"bigip"}, "namespace": "", "name": "bigip", "reconcileID": "bcd01fdd-8dfc-4be9-b3ab-c3d3a3fdb67e", "error": "Operation cannot be fulfilled on gatewayclasses.gateway.networking.k8s.io \"bigip\": the object has been modified; please apply your changes to the latest version and try again"}
87-
if err := r.Status().Update(ctx, ngwc); err != nil {
88-
slog.Errorf("unable to update status: %s", err.Error())
89-
return ctrl.Result{}, err
90-
} else {
91-
slog.Debugf("status updated")
92-
}
87+
// if err := r.Status().Update(ctx, ngwc); err != nil {
88+
// slog.Errorf("unable to update status: %s", err.Error())
89+
// return ctrl.Result{}, err
90+
// } else {
91+
// slog.Debugf("status updated")
92+
// }
9393

9494
// upsert gatewayclass
9595
defer pkg.ActiveSIGs.SetGatewayClass(&obj)

controllers/v1_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
6969
slog := utils.LogFromContext(lctx)
7070
slog.Debugf("Namespace event: " + req.Name)
7171

72+
// TODO: update resource mappings since namespace labels are changed.
7273
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
7374
if client.IgnoreNotFound(err) == nil {
7475
pkg.ActiveSIGs.UnsetNamespace(req.Name)

deploy/2.install-kubernetes-gatewayapi-CRDs-v0.6.0.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5035,7 +5035,7 @@ spec:
50355035
containers:
50365036
- name: webhook
50375037
image: gcr.io/k8s-staging-gateway-api/admission-server:v0.6.0
5038-
imagePullPolicy: Always
5038+
imagePullPolicy: IfNotPresent
50395039
args:
50405040
- -logtostderr
50415041
- --tlsCertFile=/etc/certs/cert

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module gitee.com/zongzw/bigip-kubernetes-gateway
33
go 1.19
44

55
require (
6-
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230206042033-3ebe5fb7e08f
6+
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230213065231-bf76bccb90d2
77
github.com/google/uuid v1.3.0
88
github.com/onsi/ginkgo/v2 v2.6.0
99
github.com/onsi/gomega v1.24.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
3131
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
3232
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
3333
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
34-
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230206042033-3ebe5fb7e08f h1:G9bCWQDlOgFVEHuM/bQcFRwH/E8L1iC4HuxDEA8p2ZM=
35-
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230206042033-3ebe5fb7e08f/go.mod h1:jx0Y6qhir/J/75V5tCYrLlmKPaZUEfbCg8fS6Xo6Syw=
34+
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230213065231-bf76bccb90d2 h1:AAGcidjOi+GtY2A4QyVEU5U+TGk265cvvfaK4CKdZco=
35+
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230213065231-bf76bccb90d2/go.mod h1:jx0Y6qhir/J/75V5tCYrLlmKPaZUEfbCg8fS6Xo6Syw=
3636
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3737
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
3838
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func main() {
9999
setupLog.Error(err, "failed to setup BIG-IPs")
100100
os.Exit(1)
101101
}
102+
pkg.LogLevel = level
102103
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
103104
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
104105
Scheme: scheme,
@@ -276,7 +277,7 @@ func setupBIGIPs(credsDir, confDir string) error {
276277
}
277278
url := fmt.Sprintf("https://%s:%d", c.Management.IpAddress, *c.Management.Port)
278279
username := c.Management.Username
279-
bigip := f5_bigip.Initialize(url, username, pkg.BIPPassword, level)
280+
bigip := f5_bigip.New(url, username, pkg.BIPPassword)
280281
pkg.BIGIPs = append(pkg.BIGIPs, bigip)
281282

282283
bc := &f5_bigip.BIGIPContext{BIGIP: *bigip, Context: context.TODO()}

pkg/cache.go

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -382,36 +382,43 @@ func (c *SIGCache) _HTTPRoutesRefsOf(svc *v1.Service) []*gatewayv1beta1.HTTPRout
382382
return []*gatewayv1beta1.HTTPRoute{}
383383
}
384384

385-
refered := func(hr *gatewayv1beta1.HTTPRoute) bool {
386-
for _, rl := range hr.Spec.Rules {
387-
for _, br := range rl.BackendRefs {
388-
ns := hr.Namespace
389-
if br.Namespace != nil {
390-
ns = string(*br.Namespace)
391-
}
392-
if utils.Keyname(ns, string(br.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
393-
return true
394-
}
395-
}
396-
}
397-
for _, rl := range hr.Spec.Rules {
398-
for _, fl := range rl.Filters {
399-
if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
400-
er := fl.ExtensionRef
401-
if er.Group == "" && er.Kind == "Service" {
402-
if utils.Keyname(hr.Namespace, string(er.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
403-
return true
404-
}
405-
}
406-
}
407-
}
408-
}
409-
return false
410-
}
385+
// To performance perspective, it may be good.
386+
// But the implementation is similiar to _attachedServices, would easily introduce issues.
387+
// refered := func(hr *gatewayv1beta1.HTTPRoute) bool {
388+
// for _, rl := range hr.Spec.Rules {
389+
// for _, br := range rl.BackendRefs {
390+
// ns := hr.Namespace
391+
// if br.Namespace != nil {
392+
// ns = string(*br.Namespace)
393+
// }
394+
// if utils.Keyname(ns, string(br.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
395+
// return true
396+
// }
397+
// }
398+
// }
399+
// for _, rl := range hr.Spec.Rules {
400+
// for _, fl := range rl.Filters {
401+
// if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
402+
// er := fl.ExtensionRef
403+
// if er.Group == "" && er.Kind == "Service" {
404+
// if utils.Keyname(hr.Namespace, string(er.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
405+
// return true
406+
// }
407+
// }
408+
// }
409+
// }
410+
// }
411+
// return false
412+
// }
411413

412414
hrKeys := []string{}
413415
for _, hr := range c.HTTPRoute {
414-
if refered(hr) {
416+
svcs := c._attachedServices(hr)
417+
svcKeys := []string{}
418+
for _, s := range svcs {
419+
svcKeys = append(svcKeys, utils.Keyname(s.Namespace, s.Name))
420+
}
421+
if utils.Contains(svcKeys, utils.Keyname(svc.Namespace, svc.Name)) {
415422
hrKeys = append(hrKeys, utils.Keyname(hr.Namespace, hr.Name))
416423
}
417424
}
@@ -523,7 +530,7 @@ func (c *SIGCache) _canRefer(from, to client.Object) bool {
523530

524531
func (c *SIGCache) syncCoreV1Resources(mgr manager.Manager) error {
525532
defer utils.TimeItToPrometheus()()
526-
slog := utils.LogFromContext(context.TODO())
533+
slog := utils.LogFromContext(context.TODO()).WithLevel(LogLevel)
527534
kubeClient, err := kubernetes.NewForConfig(mgr.GetConfig())
528535
if err != nil {
529536
return fmt.Errorf("unable to create kubeclient: %s", err.Error())
@@ -570,7 +577,7 @@ func (c *SIGCache) syncCoreV1Resources(mgr manager.Manager) error {
570577

571578
func (c *SIGCache) syncGatewayResources(mgr manager.Manager) error {
572579
defer utils.TimeItToPrometheus()()
573-
slog := utils.LogFromContext(context.TODO())
580+
slog := utils.LogFromContext(context.TODO()).WithLevel(LogLevel)
574581

575582
checkAndWaitCacheStarted := func() error {
576583
var gtwList gatewayv1beta1.GatewayList

pkg/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func parseiRulesFrom(className string, hr *gatewayv1beta1.HTTPRoute, rlt map[str
418418
%s
419419
%s
420420
set url $rscheme://$rhostname:$rport$ruri
421-
log local0. "request redirect to $url"
421+
# log local0. "request redirect to $url"
422422
HTTP::respond %d Location $url
423423
`, setScheme, setHostName, setUri, setPort, *rr.StatusCode))
424424
}

pkg/types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ type DeployRequest struct {
1919

2020
type CtxKeyType string
2121

22-
type ParseRequest struct {
23-
Gateway *gatewayv1beta1.Gateway
24-
HTTPRoute *gatewayv1beta1.HTTPRoute
25-
}
26-
2722
type SIGCache struct {
2823
mutex sync.RWMutex
2924
SyncedAtStart bool

pkg/vars.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var (
99
BIPConfigs BIGIPConfigs
1010
BIPPassword string
1111
refFromTo *ReferenceGrantFromTo
12+
LogLevel string
1213
)
1314

1415
const (

0 commit comments

Comments
 (0)