Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use istioRev to manage cr's reconcile instead of istioRev and configRev #213

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions framework/bootstrap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
MouceL marked this conversation as resolved.
Show resolved Hide resolved
} 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
Expand All @@ -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
}
27 changes: 2 additions & 25 deletions framework/bootstrap/configcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ",")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down