Merge pull request #1269 from XiShanYongYe-Chang/update-hookenabled-interface

Update HookEnabled interface with resourceinterpreter
This commit is contained in:
karmada-bot 2022-01-19 14:36:23 +08:00 committed by GitHub
commit e5aeb2608c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 28 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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() {

View File

@ -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.

View File

@ -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 {

View File

@ -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)
}