From 668a9bb9783e96fbe055b43ea4ce4955f3ac6f62 Mon Sep 17 00:00:00 2001 From: MouceL Date: Tue, 8 Nov 2022 16:21:10 +0800 Subject: [PATCH] use istioRev to manage cr reconcile instead of (istioRev and configRev) --- framework/bootstrap/config.go | 40 ++++----- framework/bootstrap/configcontroller.go | 27 +----- .../modules/lazyload/controllers/process.go | 6 +- .../controllers/servicefence_controller.go | 90 +------------------ .../modules/limiter/controllers/process.go | 4 +- .../controllers/smartlimiter_controller.go | 2 +- .../controllers/envoyplugin_controller.go | 2 +- .../controllers/pluginmanager_controller.go | 2 +- 8 files changed, 30 insertions(+), 143 deletions(-) diff --git a/framework/bootstrap/config.go b/framework/bootstrap/config.go index 46c19ce6..73519ba7 100644 --- a/framework/bootstrap/config.go +++ b/framework/bootstrap/config.go @@ -203,39 +203,24 @@ func (env *Environment) IstioRev() string { } // RevInScope check revision -// when StrictRev is true, return true if revision in global.IstioRev or global.configRev -// when StrictRev is false, return true if revision in (global.IstioRev or global.configRev) or revision is empty or global.IstioRev is empty +// when StrictRev is true, return true if revision in global.IstioRev +// when StrictRev is false, return true if revision in global.IstioRev or revision is empty or global.IstioRev is empty func (env *Environment) RevInScope(rev string) bool { - if env == nil || env.Config == nil || env.Config.Global == nil { + // if IstioRev is "", strictRev is meaningless, we will manage all resource + if env == nil || env.Config == nil || env.Config.Global == nil || env.Config.Global.IstioRev == "" { return true } - configRevs := initConfigRevision(env.Config.Global.IstioRev, env.Config.Global.ConfigRev) - revs := strings.Split(configRevs, ",") + istioRevs := strings.Split(env.Config.Global.IstioRev, ",") if env.Config.Global.StrictRev { - return inRevs(rev, revs) + return inRevs(rev, istioRevs) } else { - return rev == "" || configRevs == "" || inRevs(rev, revs) + return rev == "" || inRevs(rev, istioRevs) } } -func inRevs(rev string, revs []string) bool { - for _, item := range revs { - item = strings.Trim(item, " ") - - if item == rev { - return true - } - } - return false -} - -func (env *Environment) ConfigRevs() string { - return initConfigRevision(env.Config.Global.IstioRev, env.Config.Global.ConfigRev) -} - // SelfResourceRev // if SelfResourceRev is specified, the value of SelfResourceRev will be patched to resources which generated by slime itself, just like serviceFence // smartlimiter/envoyplugin/pluginmanager is excluded because these envoyfilter are generated by users or higher-level services @@ -247,3 +232,14 @@ func (env *Environment) SelfResourceRev() string { } return env.Config.Global.SelfResourceRev } + +func inRevs(rev string, revs []string) bool { + for _, item := range revs { + item = strings.Trim(item, " ") + + if item == rev { + return true + } + } + return false +} diff --git a/framework/bootstrap/configcontroller.go b/framework/bootstrap/configcontroller.go index a706a13e..7d2f485a 100644 --- a/framework/bootstrap/configcontroller.go +++ b/framework/bootstrap/configcontroller.go @@ -48,7 +48,7 @@ func NewConfigController(config *bootconfig.Config, cfg *rest.Config) (ConfigCon stopCh := make(chan struct{}) var mcs []*monitorController - configRevision := initConfigRevision(config.Global.IstioRev, config.Global.ConfigRev) + configRevision := config.Global.ConfigRev xdsSourceEnableIncPush := config.Global.Misc["xdsSourceEnableIncPush"] == "true" // init all monitorControllers @@ -512,27 +512,4 @@ func startXdsMonitorController(mc *monitorController, configRevision string, xds log.Infof("init xds config source [%s] successfully", mc.configSource.Address) return nil -} - -func initConfigRevision(rev, configRev string) string { - var ( - revs []string - has bool - ) - - if configRev != "" { - for _, p := range strings.Split(configRev, ",") { - p = strings.Trim(p, " ") - if !has && p == rev { - has = true - } - - revs = append(revs, p) - } - } - - if !has { - revs = append(revs, rev) - } - return strings.Join(revs, ",") -} +} \ No newline at end of file diff --git a/staging/src/slime.io/slime/modules/lazyload/controllers/process.go b/staging/src/slime.io/slime/modules/lazyload/controllers/process.go index 08f7257e..0ba1593a 100644 --- a/staging/src/slime.io/slime/modules/lazyload/controllers/process.go +++ b/staging/src/slime.io/slime/modules/lazyload/controllers/process.go @@ -84,7 +84,7 @@ func (r *ServicefenceReconciler) Refresh(req reconcile.Request, value map[string // if rev not in scope, skip if foundRev := model.IstioRevFromLabel(sf.Labels); !r.env.RevInScope(foundRev) { log.Debugf("existing smartlimiter %v istiorev %s but our %s, skip ...", - req.NamespacedName, foundRev, r.env.ConfigRevs()) + req.NamespacedName, foundRev, r.env.IstioRev()) return ctrl.Result{}, nil } @@ -93,7 +93,7 @@ func (r *ServicefenceReconciler) Refresh(req reconcile.Request, value map[string return reconcile.Result{}, nil } else if rev := model.IstioRevFromLabel(sf.Labels); !r.env.RevInScope(rev) { log.Infof("existing sf %v istioRev %s but our %s, skip ...", - req.NamespacedName, rev, r.env.ConfigRevs()) + req.NamespacedName, rev, r.env.IstioRev()) return reconcile.Result{}, nil } @@ -290,7 +290,7 @@ func (r *ServicefenceReconciler) refreshFenceStatusOfService(ctx context.Context } else if rev := model.IstioRevFromLabel(sf.Labels); !r.env.RevInScope(rev) { // check if svc needs auto fence created log.Errorf("existed fence %v istioRev %s but our rev %s, skip ...", - nsName, rev, r.env.ConfigRevs()) + nsName, rev, r.env.IstioRev()) } else if isFenceCreatedByController(sf) && (svc == nil || !r.isServiceFenced(ctx, svc)) { if err := r.Client.Delete(ctx, sf); err != nil { log.Errorf("delete fence %s failed, %+v", nsName, err) diff --git a/staging/src/slime.io/slime/modules/lazyload/controllers/servicefence_controller.go b/staging/src/slime.io/slime/modules/lazyload/controllers/servicefence_controller.go index b775b42e..b38e409b 100644 --- a/staging/src/slime.io/slime/modules/lazyload/controllers/servicefence_controller.go +++ b/staging/src/slime.io/slime/modules/lazyload/controllers/servicefence_controller.go @@ -146,7 +146,7 @@ func (r *ServicefenceReconciler) Reconcile(ctx context.Context, req ctrl.Request if rev := model.IstioRevFromLabel(instance.Labels); !r.env.RevInScope(rev) { // remove watch ? log.Infof("exsiting sf %v istioRev %s but our %s, skip...", - req.NamespacedName, rev, r.env.ConfigRevs()) + req.NamespacedName, rev, r.env.IstioRev()) return reconcile.Result{}, nil } log.Infof("ServicefenceReconciler got serviceFence request, %+v", req.NamespacedName) @@ -217,7 +217,7 @@ func (r *ServicefenceReconciler) refreshSidecar(instance *lazyloadv1alpha1.Servi } } else if foundRev := model.IstioRevFromLabel(found.Labels); !r.env.RevInScope(foundRev) { log.Infof("existed sidecar %v istioRev %s but our rev %s, skip update ...", - nsName, foundRev, r.env.ConfigRevs()) + nsName, foundRev, r.env.IstioRev()) } else { if !reflect.DeepEqual(found.Spec, sidecar.Spec) || !reflect.DeepEqual(found.Labels, sidecar.Labels) { log.Infof("Update a Sidecar in %s:%s", sidecar.Namespace, sidecar.Name) @@ -648,92 +648,6 @@ func (r *ServicefenceReconciler) newSidecar(sf *lazyloadv1alpha1.ServiceFence, e return ret, nil } -//func (r *ServicefenceReconciler) Subscribe(host string, destination interface{}) { // FIXME not used? -// svc, namespace, ok := util.IsK8SService(host) -// if !ok { -// return -// } -// -// sf := &lazyloadv1alpha1.ServiceFence{} -// if err := r.Client.Get(context.TODO(), types.NamespacedName{Name: svc, Namespace: namespace}, sf); err != nil { -// return -// } -// istioRev := model.IstioRevFromLabel(sf.Labels) -// if !r.env.RevInScope(istioRev) { -// return -// } -// -// for k := range sf.Status.Visitor { -// i := strings.Index(k, "/") -// if i < 0 { -// continue -// } -// -// visitorSf := &lazyloadv1alpha1.ServiceFence{} -// visitorNamespace := k[:i] -// visitorName := k[i+1:] -// -// err := r.Client.Get(context.TODO(), types.NamespacedName{Name: visitorName, Namespace: visitorNamespace}, visitorSf) -// if err != nil { -// return -// } -// visitorRev := model.IstioRevFromLabel(visitorSf.Labels) -// if !r.env.RevInScope(visitorRev) { -// return -// } -// -// r.updateVisitedHostStatus(visitorSf) -// -// sidecar, err := r.newSidecar(visitorSf, r.env) -// if sidecar == nil { -// continue -// } -// if err != nil { -// return -// } -// -// // Set VisitedHost instance as the owner and controller -// if err := controllerutil.SetControllerReference(visitorSf, sidecar, r.Scheme); err != nil { -// return -// } -// model.PatchIstioRevLabel(&sidecar.Labels, visitorRev) -// -// // Check for existence -// found := &v1alpha3.Sidecar{} -// nsName := types.NamespacedName{Name: sidecar.Name, Namespace: sidecar.Namespace} -// err = r.Client.Get(context.TODO(), nsName, found) -// if err != nil { -// if errors.IsNotFound(err) { -// found = nil -// err = nil -// } else { -// return -// } -// } -// -// if found == nil { -// err = r.Client.Create(context.TODO(), sidecar) -// if err != nil { -// log.Errorf("create sidecar %+v met err %v", sidecar, err) -// } -// } else if scRev := model.IstioRevFromLabel(found.Labels); scRev != visitorRev { -// log.Infof("existing sidecar %v istioRev %s but our %s, skip ...", -// nsName, scRev, visitorRev) -// } else { -// if !reflect.DeepEqual(found.Spec, sidecar.Spec) { -// sidecar.ResourceVersion = found.ResourceVersion -// err = r.Client.Update(context.TODO(), sidecar) -// -// if err != nil { -// log.Errorf("create sidecar %+v met err %v", sidecar, err) -// } -// } -// } -// return -// } -// return -//} - func getDestination(k string) []string { if i := controllers.HostDestinationMapping.Get(k); i != nil { if hs, ok := i.([]string); ok { diff --git a/staging/src/slime.io/slime/modules/limiter/controllers/process.go b/staging/src/slime.io/slime/modules/limiter/controllers/process.go index 1973a846..805d7803 100644 --- a/staging/src/slime.io/slime/modules/limiter/controllers/process.go +++ b/staging/src/slime.io/slime/modules/limiter/controllers/process.go @@ -135,7 +135,7 @@ func (r *SmartLimiterReconciler) Refresh(request reconcile.Request, args map[str // if rev not in scope, skip if !r.env.RevInScope(slime_model.IstioRevFromLabel(instance.Labels)) { log.Debugf("existing smartlimiter %v istiorev %s but our %s, skip ...", - request.NamespacedName, slime_model.IstioRevFromLabel(instance.Labels), r.env.ConfigRevs()) + request.NamespacedName, slime_model.IstioRevFromLabel(instance.Labels), r.env.IstioRev()) return ctrl.Result{}, nil } @@ -271,7 +271,7 @@ func refreshEnvoyFilter(instance *microservicev1alpha2.SmartLimiter, r *SmartLim } } else if foundRev := slime_model.IstioRevFromLabel(found.Labels); !r.env.RevInScope(foundRev) { log.Debugf("existing envoyfilter %v istioRev %s but our %s, skip ...", - loc, foundRev, r.env.ConfigRevs()) + loc, foundRev, r.env.IstioRev()) } else { foundSpec, err := json.Marshal(found.Spec) if err != nil { diff --git a/staging/src/slime.io/slime/modules/limiter/controllers/smartlimiter_controller.go b/staging/src/slime.io/slime/modules/limiter/controllers/smartlimiter_controller.go index 1a013a5e..230df201 100644 --- a/staging/src/slime.io/slime/modules/limiter/controllers/smartlimiter_controller.go +++ b/staging/src/slime.io/slime/modules/limiter/controllers/smartlimiter_controller.go @@ -101,7 +101,7 @@ func (r *SmartLimiterReconciler) Reconcile(ctx context.Context, req ctrl.Request log.Infof("%v is added or updated", req) if !r.env.RevInScope(slime_model.IstioRevFromLabel(instance.Labels)) { log.Debugf("existing smartlimiter %v istiorev %s but our %s, skip ...", - req.NamespacedName, slime_model.IstioRevFromLabel(instance.Labels), r.env.ConfigRevs()) + req.NamespacedName, slime_model.IstioRevFromLabel(instance.Labels), r.env.IstioRev()) return ctrl.Result{}, nil } r.lastUpdatePolicyLock.RLock() diff --git a/staging/src/slime.io/slime/modules/plugin/controllers/envoyplugin_controller.go b/staging/src/slime.io/slime/modules/plugin/controllers/envoyplugin_controller.go index c15dec77..9b566c73 100644 --- a/staging/src/slime.io/slime/modules/plugin/controllers/envoyplugin_controller.go +++ b/staging/src/slime.io/slime/modules/plugin/controllers/envoyplugin_controller.go @@ -102,7 +102,7 @@ func (r *EnvoyPluginReconciler) Reconcile(ctx context.Context, req ctrl.Request) } } else if foundRev := model.IstioRevFromLabel(found.Labels); !r.Env.RevInScope(foundRev) { log.Debugf("existed envoyfilter %v istioRev %s but our rev %s, skip updating to %+v", - req.NamespacedName, found, r.Env.ConfigRevs(), ef) + req.NamespacedName, found, r.Env.IstioRev(), ef) } else { log.Infof("Update a EnvoyFilter in %s:%s", ef.Namespace, ef.Name) ef.ResourceVersion = found.ResourceVersion diff --git a/staging/src/slime.io/slime/modules/plugin/controllers/pluginmanager_controller.go b/staging/src/slime.io/slime/modules/plugin/controllers/pluginmanager_controller.go index 2b4121c8..4043efe0 100644 --- a/staging/src/slime.io/slime/modules/plugin/controllers/pluginmanager_controller.go +++ b/staging/src/slime.io/slime/modules/plugin/controllers/pluginmanager_controller.go @@ -135,7 +135,7 @@ func (r *PluginManagerReconciler) reconcile(ctx context.Context, nn types.Namesp } } else if foundRev := model.IstioRevFromLabel(found.Labels); !r.env.RevInScope(foundRev) { log.Debugf("existing envoyfilter %v istioRev %s but our %s, skip ...", - nsName, foundRev, r.env.ConfigRevs()) + nsName, foundRev, r.env.IstioRev()) return reconcile.Result{}, nil } else { log.Infof("Update a EnvoyFilter in %v", nsName)