From fc7616e23476d87b65eb4bd5ba61e21dfa9bd460 Mon Sep 17 00:00:00 2001 From: changzhen Date: Mon, 17 Jan 2022 21:30:31 +0800 Subject: [PATCH] update HookEnabled interface with resourceinterpreter Signed-off-by: changzhen --- pkg/controllers/binding/common.go | 2 +- pkg/detector/detector.go | 6 +++--- .../customizedinterpreter/customized.go | 21 +++++++++---------- pkg/resourceinterpreter/interpreter.go | 12 +++++------ pkg/util/interpreter/rules.go | 10 ++++----- pkg/util/objectwatcher/objectwatcher.go | 2 +- 6 files changed, 25 insertions(+), 28 deletions(-) diff --git a/pkg/controllers/binding/common.go b/pkg/controllers/binding/common.go index da9d5286e097..dfcdd4dd5577 100644 --- a/pkg/controllers/binding/common.go +++ b/pkg/controllers/binding/common.go @@ -102,7 +102,7 @@ func ensureWork( workLabel := mergeLabel(clonedWorkload, workNamespace, binding, scope) - if hasScheduledReplica && resourceInterpreter.HookEnabled(clonedWorkload, configv1alpha1.InterpreterOperationReviseReplica) { + if hasScheduledReplica && resourceInterpreter.HookEnabled(clonedWorkload.GroupVersionKind(), configv1alpha1.InterpreterOperationReviseReplica) { clonedWorkload, err = resourceInterpreter.ReviseReplica(clonedWorkload, desireReplicaInfos[targetCluster.Name]) if err != nil { klog.Errorf("failed to revise replica for %s/%s/%s in cluster %s, err is: %v", diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index e96abc650003..7947241cf79f 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -650,7 +650,7 @@ func (d *ResourceDetector) BuildResourceBinding(object *unstructured.Unstructure }, } - if d.ResourceInterpreter.HookEnabled(object, configv1alpha1.InterpreterOperationInterpretReplica) { + if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) { replicas, replicaRequirements, err := d.ResourceInterpreter.GetReplicas(object) if err != nil { klog.Errorf("Failed to customize replicas for %s(%s), %v", object.GroupVersionKind(), object.GetName(), err) @@ -686,7 +686,7 @@ func (d *ResourceDetector) BuildClusterResourceBinding(object *unstructured.Unst }, } - if d.ResourceInterpreter.HookEnabled(object, configv1alpha1.InterpreterOperationInterpretReplica) { + if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) { replicas, replicaRequirements, err := d.ResourceInterpreter.GetReplicas(object) if err != nil { klog.Errorf("Failed to customize replicas for %s(%s), %v", object.GroupVersionKind(), object.GetName(), err) @@ -1041,7 +1041,7 @@ func (d *ResourceDetector) ReconcileResourceBinding(key util.QueueKey) error { return err } - if !d.ResourceInterpreter.HookEnabled(obj, configv1alpha1.InterpreterOperationAggregateStatus) { + if !d.ResourceInterpreter.HookEnabled(obj.GroupVersionKind(), configv1alpha1.InterpreterOperationAggregateStatus) { return nil } newObj, err := d.ResourceInterpreter.AggregateStatus(obj, binding.Status.AggregatedStatus) diff --git a/pkg/resourceinterpreter/customizedinterpreter/customized.go b/pkg/resourceinterpreter/customizedinterpreter/customized.go index 27790b113b5f..2a782d005b0f 100644 --- a/pkg/resourceinterpreter/customizedinterpreter/customized.go +++ b/pkg/resourceinterpreter/customizedinterpreter/customized.go @@ -51,17 +51,16 @@ func NewCustomizedInterpreter(kubeconfig string, informer informermanager.Single }, nil } -// HookEnabled tells if any hook exist for specific resource type and operation type. -func (e *CustomizedInterpreter) HookEnabled(attributes *webhook.RequestAttributes) bool { +// HookEnabled tells if any hook exist for specific resource gvk and operation type. +func (e *CustomizedInterpreter) HookEnabled(objGVK schema.GroupVersionKind, operation configv1alpha1.InterpreterOperation) bool { if !e.hookManager.HasSynced() { klog.Errorf("not yet ready to handle request") return false } - hook := e.getFirstRelevantHook(attributes) + hook := e.getFirstRelevantHook(objGVK, operation) if hook == nil { - klog.V(4).Infof("Hook interpreter is not enabled for kind %q with operation %q.", - attributes.Object.GroupVersionKind(), attributes.Operation) + klog.V(4).Infof("Hook interpreter is not enabled for kind %q with operation %q.", objGVK, operation) } return hook != nil } @@ -99,10 +98,10 @@ func (e *CustomizedInterpreter) Patch(ctx context.Context, attributes *webhook.R return } -func (e *CustomizedInterpreter) getFirstRelevantHook(attributes *webhook.RequestAttributes) configmanager.WebhookAccessor { +func (e *CustomizedInterpreter) getFirstRelevantHook(objGVK schema.GroupVersionKind, operation configv1alpha1.InterpreterOperation) configmanager.WebhookAccessor { relevantHooks := make([]configmanager.WebhookAccessor, 0) for _, hook := range e.hookManager.HookAccessors() { - if shouldCallHook(hook, attributes) { + if shouldCallHook(hook, objGVK, operation) { relevantHooks = append(relevantHooks, hook) } } @@ -123,7 +122,7 @@ func (e *CustomizedInterpreter) interpret(ctx context.Context, attributes *webho return nil, false, fmt.Errorf("not yet ready to handle request") } - hook := e.getFirstRelevantHook(attributes) + hook := e.getFirstRelevantHook(attributes.Object.GroupVersionKind(), attributes.Operation) if hook == nil { return nil, false, nil } @@ -161,11 +160,11 @@ func (e *CustomizedInterpreter) interpret(ctx context.Context, attributes *webho return response, true, nil } -func shouldCallHook(hook configmanager.WebhookAccessor, attributes *webhook.RequestAttributes) bool { +func shouldCallHook(hook configmanager.WebhookAccessor, objGVK schema.GroupVersionKind, operation configv1alpha1.InterpreterOperation) bool { for _, rule := range hook.GetRules() { matcher := interpreterutil.Matcher{ - Operation: attributes.Operation, - Object: attributes.Object, + ObjGVK: objGVK, + Operation: operation, Rule: rule, } if matcher.Matches() { diff --git a/pkg/resourceinterpreter/interpreter.go b/pkg/resourceinterpreter/interpreter.go index 5e1d1d507bb7..45ad345576a6 100644 --- a/pkg/resourceinterpreter/interpreter.go +++ b/pkg/resourceinterpreter/interpreter.go @@ -4,6 +4,7 @@ import ( "context" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/klog/v2" configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1" @@ -20,7 +21,7 @@ type ResourceInterpreter interface { Start(ctx context.Context) (err error) // HookEnabled tells if any hook exist for specific resource type and operation. - HookEnabled(object *unstructured.Unstructured, operationType configv1alpha1.InterpreterOperation) bool + HookEnabled(objGVK schema.GroupVersionKind, operationType configv1alpha1.InterpreterOperation) bool // GetReplicas returns the desired replicas of the object as well as the requirements of each replica. GetReplicas(object *unstructured.Unstructured) (replica int32, replicaRequires *workv1alpha2.ReplicaRequirements, err error) @@ -71,12 +72,9 @@ func (i *customResourceInterpreterImpl) Start(ctx context.Context) (err error) { } // HookEnabled tells if any hook exist for specific resource type and operation. -func (i *customResourceInterpreterImpl) HookEnabled(object *unstructured.Unstructured, operation configv1alpha1.InterpreterOperation) bool { - attributes := &webhook.RequestAttributes{ - Operation: operation, - Object: object, - } - return i.customizedInterpreter.HookEnabled(attributes) || i.defaultInterpreter.HookEnabled(object.GroupVersionKind(), operation) +func (i *customResourceInterpreterImpl) HookEnabled(objGVK schema.GroupVersionKind, operation configv1alpha1.InterpreterOperation) bool { + return i.customizedInterpreter.HookEnabled(objGVK, operation) || + i.defaultInterpreter.HookEnabled(objGVK, operation) } // GetReplicas returns the desired replicas of the object as well as the requirements of each replica. diff --git a/pkg/util/interpreter/rules.go b/pkg/util/interpreter/rules.go index c0d8429dc3f8..7b67db07b58b 100644 --- a/pkg/util/interpreter/rules.go +++ b/pkg/util/interpreter/rules.go @@ -1,7 +1,7 @@ package interpreter import ( - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1" ) @@ -13,9 +13,9 @@ const ( // Matcher determines if the Object matches the Rule. type Matcher struct { + ObjGVK schema.GroupVersionKind Operation configv1alpha1.InterpreterOperation Rule configv1alpha1.RuleWithOperations - Object *unstructured.Unstructured } // Matches tells if the Operation, Object matches the Rule. @@ -33,15 +33,15 @@ func (m *Matcher) operation() bool { } func (m *Matcher) group() bool { - return exactOrWildcard(m.Object.GroupVersionKind().Group, m.Rule.APIGroups) + return exactOrWildcard(m.ObjGVK.Group, m.Rule.APIGroups) } func (m *Matcher) version() bool { - return exactOrWildcard(m.Object.GroupVersionKind().Version, m.Rule.APIVersions) + return exactOrWildcard(m.ObjGVK.Version, m.Rule.APIVersions) } func (m *Matcher) kind() bool { - return exactOrWildcard(m.Object.GetKind(), m.Rule.Kinds) + return exactOrWildcard(m.ObjGVK.Kind, m.Rule.Kinds) } func exactOrWildcard(requested string, items []string) bool { diff --git a/pkg/util/objectwatcher/objectwatcher.go b/pkg/util/objectwatcher/objectwatcher.go index b4c03d03e0e1..ff3f950f7950 100644 --- a/pkg/util/objectwatcher/objectwatcher.go +++ b/pkg/util/objectwatcher/objectwatcher.go @@ -129,7 +129,7 @@ func (o *objectWatcherImpl) retainClusterFields(desired, observed *unstructured. // and be set by user in karmada-controller-plane. util.MergeAnnotations(desired, observed) - if o.resourceInterpreter.HookEnabled(desired, configv1alpha1.InterpreterOperationRetain) { + if o.resourceInterpreter.HookEnabled(desired.GroupVersionKind(), configv1alpha1.InterpreterOperationRetain) { return o.resourceInterpreter.Retain(desired, observed) }