Skip to content

Commit

Permalink
Merge pull request #1269 from XiShanYongYe-Chang/update-hookenabled-i…
Browse files Browse the repository at this point in the history
…nterface

Update HookEnabled interface with resourceinterpreter
  • Loading branch information
karmada-bot authored Jan 19, 2022
2 parents 0d98cc3 + fc7616e commit e5aeb26
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions pkg/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 10 additions & 11 deletions pkg/resourceinterpreter/customizedinterpreter/customized.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 5 additions & 7 deletions pkg/resourceinterpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions pkg/util/interpreter/rules.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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.
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/objectwatcher/objectwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit e5aeb26

Please sign in to comment.