Skip to content

Commit

Permalink
!61 refactor some code, use latest bigip-rest.
Browse files Browse the repository at this point in the history
Merge pull request !61 from zongzw/zong-refine-code
  • Loading branch information
zongzw authored and gitee-org committed Feb 16, 2023
2 parents 0668fbb + 4b29dc0 commit 84c271b
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 46 deletions.
12 changes: 6 additions & 6 deletions controllers/gatewayclass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
// TODO:
// 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"}
// 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"}
if err := r.Status().Update(ctx, ngwc); err != nil {
slog.Errorf("unable to update status: %s", err.Error())
return ctrl.Result{}, err
} else {
slog.Debugf("status updated")
}
// if err := r.Status().Update(ctx, ngwc); err != nil {
// slog.Errorf("unable to update status: %s", err.Error())
// return ctrl.Result{}, err
// } else {
// slog.Debugf("status updated")
// }

// upsert gatewayclass
defer pkg.ActiveSIGs.SetGatewayClass(&obj)
Expand Down
1 change: 1 addition & 0 deletions controllers/v1_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
slog := utils.LogFromContext(lctx)
slog.Debugf("Namespace event: " + req.Name)

// TODO: update resource mappings since namespace labels are changed.
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
if client.IgnoreNotFound(err) == nil {
pkg.ActiveSIGs.UnsetNamespace(req.Name)
Expand Down
2 changes: 1 addition & 1 deletion deploy/2.install-kubernetes-gatewayapi-CRDs-v0.6.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5035,7 +5035,7 @@ spec:
containers:
- name: webhook
image: gcr.io/k8s-staging-gateway-api/admission-server:v0.6.0
imagePullPolicy: Always
imagePullPolicy: IfNotPresent
args:
- -logtostderr
- --tlsCertFile=/etc/certs/cert
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module gitee.com/zongzw/bigip-kubernetes-gateway
go 1.19

require (
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230206042033-3ebe5fb7e08f
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230213065231-bf76bccb90d2
github.com/google/uuid v1.3.0
github.com/onsi/ginkgo/v2 v2.6.0
github.com/onsi/gomega v1.24.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230206042033-3ebe5fb7e08f h1:G9bCWQDlOgFVEHuM/bQcFRwH/E8L1iC4HuxDEA8p2ZM=
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230206042033-3ebe5fb7e08f/go.mod h1:jx0Y6qhir/J/75V5tCYrLlmKPaZUEfbCg8fS6Xo6Syw=
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230213065231-bf76bccb90d2 h1:AAGcidjOi+GtY2A4QyVEU5U+TGk265cvvfaK4CKdZco=
gitee.com/zongzw/f5-bigip-rest v0.0.0-20230213065231-bf76bccb90d2/go.mod h1:jx0Y6qhir/J/75V5tCYrLlmKPaZUEfbCg8fS6Xo6Syw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func main() {
setupLog.Error(err, "failed to setup BIG-IPs")
os.Exit(1)
}
pkg.LogLevel = level
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Expand Down Expand Up @@ -276,7 +277,7 @@ func setupBIGIPs(credsDir, confDir string) error {
}
url := fmt.Sprintf("https://%s:%d", c.Management.IpAddress, *c.Management.Port)
username := c.Management.Username
bigip := f5_bigip.Initialize(url, username, pkg.BIPPassword, level)
bigip := f5_bigip.New(url, username, pkg.BIPPassword)
pkg.BIGIPs = append(pkg.BIGIPs, bigip)

bc := &f5_bigip.BIGIPContext{BIGIP: *bigip, Context: context.TODO()}
Expand Down
65 changes: 36 additions & 29 deletions pkg/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,36 +382,43 @@ func (c *SIGCache) _HTTPRoutesRefsOf(svc *v1.Service) []*gatewayv1beta1.HTTPRout
return []*gatewayv1beta1.HTTPRoute{}
}

refered := func(hr *gatewayv1beta1.HTTPRoute) bool {
for _, rl := range hr.Spec.Rules {
for _, br := range rl.BackendRefs {
ns := hr.Namespace
if br.Namespace != nil {
ns = string(*br.Namespace)
}
if utils.Keyname(ns, string(br.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
return true
}
}
}
for _, rl := range hr.Spec.Rules {
for _, fl := range rl.Filters {
if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
er := fl.ExtensionRef
if er.Group == "" && er.Kind == "Service" {
if utils.Keyname(hr.Namespace, string(er.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
return true
}
}
}
}
}
return false
}
// To performance perspective, it may be good.
// But the implementation is similiar to _attachedServices, would easily introduce issues.
// refered := func(hr *gatewayv1beta1.HTTPRoute) bool {
// for _, rl := range hr.Spec.Rules {
// for _, br := range rl.BackendRefs {
// ns := hr.Namespace
// if br.Namespace != nil {
// ns = string(*br.Namespace)
// }
// if utils.Keyname(ns, string(br.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
// return true
// }
// }
// }
// for _, rl := range hr.Spec.Rules {
// for _, fl := range rl.Filters {
// if fl.Type == gatewayv1beta1.HTTPRouteFilterExtensionRef && fl.ExtensionRef != nil {
// er := fl.ExtensionRef
// if er.Group == "" && er.Kind == "Service" {
// if utils.Keyname(hr.Namespace, string(er.Name)) == utils.Keyname(svc.Namespace, svc.Name) {
// return true
// }
// }
// }
// }
// }
// return false
// }

hrKeys := []string{}
for _, hr := range c.HTTPRoute {
if refered(hr) {
svcs := c._attachedServices(hr)
svcKeys := []string{}
for _, s := range svcs {
svcKeys = append(svcKeys, utils.Keyname(s.Namespace, s.Name))
}
if utils.Contains(svcKeys, utils.Keyname(svc.Namespace, svc.Name)) {
hrKeys = append(hrKeys, utils.Keyname(hr.Namespace, hr.Name))
}
}
Expand Down Expand Up @@ -523,7 +530,7 @@ func (c *SIGCache) _canRefer(from, to client.Object) bool {

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

func (c *SIGCache) syncGatewayResources(mgr manager.Manager) error {
defer utils.TimeItToPrometheus()()
slog := utils.LogFromContext(context.TODO())
slog := utils.LogFromContext(context.TODO()).WithLevel(LogLevel)

checkAndWaitCacheStarted := func() error {
var gtwList gatewayv1beta1.GatewayList
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func parseiRulesFrom(className string, hr *gatewayv1beta1.HTTPRoute, rlt map[str
%s
%s
set url $rscheme://$rhostname:$rport$ruri
log local0. "request redirect to $url"
# log local0. "request redirect to $url"
HTTP::respond %d Location $url
`, setScheme, setHostName, setUri, setPort, *rr.StatusCode))
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ type DeployRequest struct {

type CtxKeyType string

type ParseRequest struct {
Gateway *gatewayv1beta1.Gateway
HTTPRoute *gatewayv1beta1.HTTPRoute
}

type SIGCache struct {
mutex sync.RWMutex
SyncedAtStart bool
Expand Down
1 change: 1 addition & 0 deletions pkg/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var (
BIPConfigs BIGIPConfigs
BIPPassword string
refFromTo *ReferenceGrantFromTo
LogLevel string
)

const (
Expand Down

0 comments on commit 84c271b

Please sign in to comment.