From 37653c753b3239c24fd05fb1e291c6ac8d6edfd9 Mon Sep 17 00:00:00 2001 From: zongzw Date: Mon, 12 Dec 2022 15:28:44 +0800 Subject: [PATCH] eliminate endless periodical false-positive node events --- controllers/v1_controller.go | 74 ++++++------------------------------ main.go | 24 ++++++++++++ pkg/parser.go | 26 ++++++++++++- 3 files changed, 59 insertions(+), 65 deletions(-) diff --git a/controllers/v1_controller.go b/controllers/v1_controller.go index e167f70..340f375 100644 --- a/controllers/v1_controller.go +++ b/controllers/v1_controller.go @@ -19,6 +19,7 @@ package controllers import ( "context" "fmt" + "reflect" "time" "gitee.com/zongzw/bigip-kubernetes-gateway/k8s" @@ -93,48 +94,17 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. return ctrl.Result{Requeue: true}, nil } - oIpAddresses := []string{} - nIpAddresses := []string{} ocfgs := map[string]interface{}{} ncfgs := map[string]interface{}{} - oIpToMacV4 := map[string]string{} - // oIpToMacV6 := map[string]string{} - nIpToMacV4 := map[string]string{} - // nIpToMacV6 := map[string]string{} - var obj v1.Node - zlog := log.FromContext(ctx) - zlog.V(1).Info("resource event: " + req.NamespacedName.String()) + // zlog := log.FromContext(ctx) + // zlog.V(1).Info("resource event: " + req.NamespacedName.String()) if err := r.Get(ctx, req.NamespacedName, &obj); err != nil { if client.IgnoreNotFound(err) == nil { - if pkg.ActiveSIGs.Mode == "calico" { - oIpAddresses = k8s.NodeCache.AllIpAddresses() - if ocfgs, err = pkg.ParseNeighsFrom("gwcBGP", "64512", "64512", oIpAddresses); err != nil { - return ctrl.Result{}, err - } - } - if pkg.ActiveSIGs.Mode == "flannel" { - oIpToMacV4, _ = k8s.NodeCache.AllIpToMac() - if ocfgs, err = pkg.ParseFdbsFrom(pkg.ActiveSIGs.VxlanTunnelName, oIpToMacV4); err != nil { - return ctrl.Result{}, err - } - } - k8s.NodeCache.Unset(req.Name) - - if pkg.ActiveSIGs.Mode == "calico" { - nIpAddresses = k8s.NodeCache.AllIpAddresses() - - if ncfgs, err = pkg.ParseNeighsFrom("gwcBGP", "64512", "64512", nIpAddresses); err != nil { - return ctrl.Result{}, err - } - } - if pkg.ActiveSIGs.Mode == "flannel" { - nIpToMacV4, _ = k8s.NodeCache.AllIpToMac() - if ncfgs, err = pkg.ParseFdbsFrom(pkg.ActiveSIGs.VxlanTunnelName, nIpToMacV4); err != nil { - return ctrl.Result{}, err - } + if ncfgs, err = pkg.ParseNodeConfigs(); err != nil { + return ctrl.Result{}, err } pkg.PendingDeploys <- pkg.DeployRequest{ Meta: fmt.Sprintf("refreshing for request '%s'", req.Name), @@ -148,36 +118,14 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. return ctrl.Result{}, err } } else { - if pkg.ActiveSIGs.Mode == "calico" { - oIpAddresses = k8s.NodeCache.AllIpAddresses() - - if ocfgs, err = pkg.ParseNeighsFrom("gwcBGP", "64512", "64512", oIpAddresses); err != nil { - return ctrl.Result{}, err - } - } - - if pkg.ActiveSIGs.Mode == "flannel" { - oIpToMacV4, _ = k8s.NodeCache.AllIpToMac() - if ocfgs, err = pkg.ParseFdbsFrom(pkg.ActiveSIGs.VxlanTunnelName, oIpToMacV4); err != nil { - return ctrl.Result{}, err - } - } - + orig := k8s.NodeCache.Get(obj.Name) k8s.NodeCache.Set(obj.DeepCopy()) - - if pkg.ActiveSIGs.Mode == "calico" { - nIpAddresses = k8s.NodeCache.AllIpAddresses() - if ncfgs, err = pkg.ParseNeighsFrom("gwcBGP", "64512", "64512", nIpAddresses); err != nil { - return ctrl.Result{}, err - } - + // use reflect.DeepEqual to eliminate endless false-positive node events + if newa := k8s.NodeCache.Get(obj.Name); reflect.DeepEqual(orig, newa) { + return ctrl.Result{}, nil } - - if pkg.ActiveSIGs.Mode == "flannel" { - nIpToMacV4, _ = k8s.NodeCache.AllIpToMac() - if ncfgs, err = pkg.ParseFdbsFrom(pkg.ActiveSIGs.VxlanTunnelName, nIpToMacV4); err != nil { - return ctrl.Result{}, err - } + if ncfgs, err = pkg.ParseNodeConfigs(); err != nil { + return ctrl.Result{}, err } pkg.PendingDeploys <- pkg.DeployRequest{ Meta: fmt.Sprintf("refreshing for request '%s'", req.Name), diff --git a/main.go b/main.go index cda9feb..5e16ce7 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "os" "strings" + "time" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -237,6 +238,29 @@ func main() { go pkg.ActiveSIGs.SyncAllResources(mgr) + go func() { + for { + <-time.After(100 * time.Millisecond) + if pkg.ActiveSIGs.SyncedAtStart { + break + } + } + + if ncfgs, err := pkg.ParseNodeConfigs(); err != nil { + setupLog.Error(err, "unable to parse nodes config for net setup") + os.Exit(1) + } else { + pkg.PendingDeploys <- pkg.DeployRequest{ + Meta: "net setup at startup", + From: nil, + To: &ncfgs, + StatusFunc: func() {}, + Partition: "Common", + Context: context.TODO(), + } + } + }() + setupLog.Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") diff --git a/pkg/parser.go b/pkg/parser.go index 64b105c..a18b587 100644 --- a/pkg/parser.go +++ b/pkg/parser.go @@ -636,7 +636,7 @@ func parseiRulesFrom(className string, hr *gatewayv1beta1.HTTPRoute, rlt map[str return nil } -func ParseNeighsFrom(routerName, localAs, remoteAs string, addresses []string) (map[string]interface{}, error) { +func parseNeighsFrom(routerName, localAs, remoteAs string, addresses []string) (map[string]interface{}, error) { rlt := map[string]interface{}{} name := strings.Join([]string{"Common", routerName}, ".") @@ -662,7 +662,7 @@ func ParseNeighsFrom(routerName, localAs, remoteAs string, addresses []string) ( }, nil } -func ParseFdbsFrom(tunnelName string, iPToMac map[string]string) (map[string]interface{}, error) { +func parseFdbsFrom(tunnelName string, iPToMac map[string]string) (map[string]interface{}, error) { rlt := map[string]interface{}{} rlt["net/fdb/tunnel/"+tunnelName] = map[string]interface{}{ @@ -684,3 +684,25 @@ func ParseFdbsFrom(tunnelName string, iPToMac map[string]string) (map[string]int "": rlt, }, nil } + +func ParseNodeConfigs() (map[string]interface{}, error) { + cfgs := map[string]interface{}{} + var err error + + if ActiveSIGs.Mode == "calico" { + nIpAddresses := k8s.NodeCache.AllIpAddresses() + if cfgs, err = parseNeighsFrom("gwcBGP", "64512", "64512", nIpAddresses); err != nil { + return map[string]interface{}{}, err + } + + } + + if ActiveSIGs.Mode == "flannel" { + nIpToMacV4, _ := k8s.NodeCache.AllIpToMac() + if cfgs, err = parseFdbsFrom(ActiveSIGs.VxlanTunnelName, nIpToMacV4); err != nil { + return map[string]interface{}{}, err + } + } + + return cfgs, nil +}