Skip to content

Commit

Permalink
Predefined ConfigurableInterpreter
Browse files Browse the repository at this point in the history
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
  • Loading branch information
chaunceyjiang committed Mar 10, 2023
1 parent e4f5b62 commit a2e4d6f
Show file tree
Hide file tree
Showing 15 changed files with 480 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pkg/karmadactl/interpret/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"k8s.io/cli-runtime/pkg/resource"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurableinterpreter/luavm"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurable/luavm"
)

func (o *Options) runCheck() error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configmanager
package accessor

import (
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/klog/v2"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurable/accessor"
"github.com/karmada-io/karmada/pkg/util/fedinformer"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
"github.com/karmada-io/karmada/pkg/util/helper"
Expand All @@ -25,7 +26,7 @@ var resourceInterpreterCustomizationsGVR = schema.GroupVersionResource{

// ConfigManager can list custom resource interpreter.
type ConfigManager interface {
CustomAccessors() map[schema.GroupVersionKind]CustomAccessor
CustomAccessors() map[schema.GroupVersionKind]accessor.CustomAccessor
HasSynced() bool
LoadConfig(customizations []*configv1alpha1.ResourceInterpreterCustomization)
}
Expand All @@ -38,8 +39,8 @@ type interpreterConfigManager struct {
}

// CustomAccessors returns all cached configurations.
func (configManager *interpreterConfigManager) CustomAccessors() map[schema.GroupVersionKind]CustomAccessor {
return configManager.configuration.Load().(map[schema.GroupVersionKind]CustomAccessor)
func (configManager *interpreterConfigManager) CustomAccessors() map[schema.GroupVersionKind]accessor.CustomAccessor {
return configManager.configuration.Load().(map[schema.GroupVersionKind]accessor.CustomAccessor)
}

// HasSynced returns true when the cache is synced.
Expand All @@ -64,7 +65,7 @@ func (configManager *interpreterConfigManager) HasSynced() bool {
// the configurations in the cache.
func NewInterpreterConfigManager(informer genericmanager.SingleClusterInformerManager) ConfigManager {
manager := &interpreterConfigManager{}
manager.configuration.Store(make(map[schema.GroupVersionKind]CustomAccessor))
manager.configuration.Store(make(map[schema.GroupVersionKind]accessor.CustomAccessor))

// In interpret command, rules are not loaded from server, so we don't start informer for it.
if informer != nil {
Expand Down Expand Up @@ -104,17 +105,17 @@ func (configManager *interpreterConfigManager) LoadConfig(configs []*configv1alp
return configs[i].Name < configs[j].Name
})

accessors := make(map[schema.GroupVersionKind]CustomAccessor)
accessors := make(map[schema.GroupVersionKind]accessor.CustomAccessor)
for _, config := range configs {
key := schema.FromAPIVersionAndKind(config.Spec.Target.APIVersion, config.Spec.Target.Kind)

var accessor CustomAccessor
var ac accessor.CustomAccessor
var ok bool
if accessor, ok = accessors[key]; !ok {
accessor = NewResourceCustomAccessor()
if ac, ok = accessors[key]; !ok {
ac = accessor.NewResourceCustomAccessor()
}
accessor.Merge(config.Spec.Customizations)
accessors[key] = accessor
ac.Merge(config.Spec.Customizations)
accessors[key] = ac
}

configManager.configuration.Store(accessors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/client-go/tools/cache"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurable/accessor"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
"github.com/karmada-io/karmada/pkg/util/gclient"
)
Expand Down Expand Up @@ -66,40 +67,52 @@ func Test_interpreterConfigManager_LuaScriptAccessors(t *testing.T) {
tests := []struct {
name string
args args
want map[schema.GroupVersionKind]CustomAccessor
want map[schema.GroupVersionKind]accessor.CustomAccessor
}{
{
name: "single ResourceInterpreterCustomization",
args: args{[]runtime.Object{customization01}},
want: map[schema.GroupVersionKind]CustomAccessor{
deploymentGVK: &resourceCustomAccessor{
retention: &configv1alpha1.LocalValueRetention{LuaScript: "a=0"},
replicaResource: &configv1alpha1.ReplicaResourceRequirement{LuaScript: "b=0"},
},
want: map[schema.GroupVersionKind]accessor.CustomAccessor{
deploymentGVK: func() accessor.CustomAccessor {
customAccessor := accessor.NewResourceCustomAccessor()
customAccessor.Merge(configv1alpha1.CustomizationRules{
Retention: &configv1alpha1.LocalValueRetention{LuaScript: "a=0"},
ReplicaResource: &configv1alpha1.ReplicaResourceRequirement{LuaScript: "b=0"},
})
return customAccessor
}(),
},
},
{
name: "multi ResourceInterpreterCustomization with no redundant operation",
args: args{[]runtime.Object{customization01, customization02}},
want: map[schema.GroupVersionKind]CustomAccessor{
deploymentGVK: &resourceCustomAccessor{
retention: &configv1alpha1.LocalValueRetention{LuaScript: "a=0"},
replicaResource: &configv1alpha1.ReplicaResourceRequirement{LuaScript: "b=0"},
replicaRevision: &configv1alpha1.ReplicaRevision{LuaScript: "c=0"},
statusReflection: &configv1alpha1.StatusReflection{LuaScript: "d=0"},
},
want: map[schema.GroupVersionKind]accessor.CustomAccessor{
deploymentGVK: func() accessor.CustomAccessor {
customAccessor := accessor.NewResourceCustomAccessor()
customAccessor.Merge(configv1alpha1.CustomizationRules{
Retention: &configv1alpha1.LocalValueRetention{LuaScript: "a=0"},
ReplicaResource: &configv1alpha1.ReplicaResourceRequirement{LuaScript: "b=0"},
ReplicaRevision: &configv1alpha1.ReplicaRevision{LuaScript: "c=0"},
StatusReflection: &configv1alpha1.StatusReflection{LuaScript: "d=0"},
})
return customAccessor
}(),
},
},
{
name: "multi ResourceInterpreterCustomization with redundant operation",
args: args{[]runtime.Object{customization03, customization02, customization01}},
want: map[schema.GroupVersionKind]CustomAccessor{
deploymentGVK: &resourceCustomAccessor{
retention: &configv1alpha1.LocalValueRetention{LuaScript: "a=0"},
replicaResource: &configv1alpha1.ReplicaResourceRequirement{LuaScript: "b=0"},
replicaRevision: &configv1alpha1.ReplicaRevision{LuaScript: "c=0"},
statusReflection: &configv1alpha1.StatusReflection{LuaScript: "d=0"},
},
want: map[schema.GroupVersionKind]accessor.CustomAccessor{
deploymentGVK: func() accessor.CustomAccessor {
customAccessor := accessor.NewResourceCustomAccessor()
customAccessor.Merge(configv1alpha1.CustomizationRules{
Retention: &configv1alpha1.LocalValueRetention{LuaScript: "a=0"},
ReplicaResource: &configv1alpha1.ReplicaResourceRequirement{LuaScript: "b=0"},
ReplicaRevision: &configv1alpha1.ReplicaRevision{LuaScript: "c=0"},
StatusReflection: &configv1alpha1.StatusReflection{LuaScript: "d=0"},
})
return customAccessor
}(),
},
},
}
Expand Down
53 changes: 27 additions & 26 deletions pkg/resourceinterpreter/configurableinterpreter/configurable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurable/accessor"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurable/luavm"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurableinterpreter/configmanager"
"github.com/karmada-io/karmada/pkg/resourceinterpreter/configurableinterpreter/luavm"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
)

Expand All @@ -35,30 +36,30 @@ func NewConfigurableInterpreter(informer genericmanager.SingleClusterInformerMan

// HookEnabled tells if any hook exist for specific resource gvk and operation type.
func (c *ConfigurableInterpreter) HookEnabled(kind schema.GroupVersionKind, operationType configv1alpha1.InterpreterOperation) bool {
accessor, exist := c.getCustomAccessor(kind)
customAccessor, exist := c.getCustomAccessor(kind)
if !exist {
return exist
}

if operationType == configv1alpha1.InterpreterOperationInterpretDependency {
scripts := accessor.GetDependencyInterpretationLuaScripts()
scripts := customAccessor.GetDependencyInterpretationLuaScripts()
return scripts != nil
}

var script string
switch operationType {
case configv1alpha1.InterpreterOperationAggregateStatus:
script = accessor.GetStatusAggregationLuaScript()
script = customAccessor.GetStatusAggregationLuaScript()
case configv1alpha1.InterpreterOperationInterpretHealth:
script = accessor.GetHealthInterpretationLuaScript()
script = customAccessor.GetHealthInterpretationLuaScript()
case configv1alpha1.InterpreterOperationInterpretReplica:
script = accessor.GetReplicaResourceLuaScript()
script = customAccessor.GetReplicaResourceLuaScript()
case configv1alpha1.InterpreterOperationInterpretStatus:
script = accessor.GetStatusReflectionLuaScript()
script = customAccessor.GetStatusReflectionLuaScript()
case configv1alpha1.InterpreterOperationRetain:
script = accessor.GetRetentionLuaScript()
script = customAccessor.GetRetentionLuaScript()
case configv1alpha1.InterpreterOperationReviseReplica:
script = accessor.GetReplicaRevisionLuaScript()
script = customAccessor.GetReplicaRevisionLuaScript()
}
return len(script) > 0
}
Expand All @@ -67,12 +68,12 @@ func (c *ConfigurableInterpreter) HookEnabled(kind schema.GroupVersionKind, oper
func (c *ConfigurableInterpreter) GetReplicas(object *unstructured.Unstructured) (replicas int32, requires *workv1alpha2.ReplicaRequirements, enabled bool, err error) {
klog.V(4).Infof("Get replicas for object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())

accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
if !enabled {
return
}

script := accessor.GetReplicaResourceLuaScript()
script := customAccessor.GetReplicaResourceLuaScript()
if len(script) == 0 {
enabled = false
return
Expand All @@ -86,12 +87,12 @@ func (c *ConfigurableInterpreter) GetReplicas(object *unstructured.Unstructured)
func (c *ConfigurableInterpreter) ReviseReplica(object *unstructured.Unstructured, replica int64) (revised *unstructured.Unstructured, enabled bool, err error) {
klog.V(4).Infof("Revise replicas for object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())

accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
if !enabled {
return
}

script := accessor.GetReplicaRevisionLuaScript()
script := customAccessor.GetReplicaRevisionLuaScript()
if len(script) == 0 {
enabled = false
return
Expand All @@ -105,12 +106,12 @@ func (c *ConfigurableInterpreter) ReviseReplica(object *unstructured.Unstructure
func (c *ConfigurableInterpreter) Retain(desired *unstructured.Unstructured, observed *unstructured.Unstructured) (retained *unstructured.Unstructured, enabled bool, err error) {
klog.V(4).Infof("Retain object: %v %s/%s with configurable interpreter.", desired.GroupVersionKind(), desired.GetNamespace(), desired.GetName())

accessor, enabled := c.getCustomAccessor(desired.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(desired.GroupVersionKind())
if !enabled {
return
}

script := accessor.GetRetentionLuaScript()
script := customAccessor.GetRetentionLuaScript()
if len(script) == 0 {
enabled = false
return
Expand All @@ -123,12 +124,12 @@ func (c *ConfigurableInterpreter) Retain(desired *unstructured.Unstructured, obs
// AggregateStatus returns the objects that based on the 'object' but with status aggregated.
func (c *ConfigurableInterpreter) AggregateStatus(object *unstructured.Unstructured, aggregatedStatusItems []workv1alpha2.AggregatedStatusItem) (status *unstructured.Unstructured, enabled bool, err error) {
klog.V(4).Infof("Aggregate status of object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())
accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
if !enabled {
return
}

script := accessor.GetStatusAggregationLuaScript()
script := customAccessor.GetStatusAggregationLuaScript()
if len(script) == 0 {
enabled = false
return
Expand All @@ -142,12 +143,12 @@ func (c *ConfigurableInterpreter) AggregateStatus(object *unstructured.Unstructu
func (c *ConfigurableInterpreter) GetDependencies(object *unstructured.Unstructured) (dependencies []configv1alpha1.DependentObjectReference, enabled bool, err error) {
klog.V(4).Infof("Get dependencies of object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())

accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
if !enabled {
return
}

scripts := accessor.GetDependencyInterpretationLuaScripts()
scripts := customAccessor.GetDependencyInterpretationLuaScripts()
if scripts == nil {
enabled = false
return
Expand Down Expand Up @@ -186,12 +187,12 @@ func (c *ConfigurableInterpreter) GetDependencies(object *unstructured.Unstructu
func (c *ConfigurableInterpreter) ReflectStatus(object *unstructured.Unstructured) (status *runtime.RawExtension, enabled bool, err error) {
klog.V(4).Infof("Reflect status of object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())

accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
if !enabled {
return
}

script := accessor.GetStatusReflectionLuaScript()
script := customAccessor.GetStatusReflectionLuaScript()
if len(script) == 0 {
enabled = false
return
Expand All @@ -205,12 +206,12 @@ func (c *ConfigurableInterpreter) ReflectStatus(object *unstructured.Unstructure
func (c *ConfigurableInterpreter) InterpretHealth(object *unstructured.Unstructured) (health bool, enabled bool, err error) {
klog.V(4).Infof("Get health status of object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())

accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
customAccessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
if !enabled {
return
}

script := accessor.GetHealthInterpretationLuaScript()
script := customAccessor.GetHealthInterpretationLuaScript()
if len(script) == 0 {
enabled = false
return
Expand All @@ -220,14 +221,14 @@ func (c *ConfigurableInterpreter) InterpretHealth(object *unstructured.Unstructu
return
}

func (c *ConfigurableInterpreter) getCustomAccessor(kind schema.GroupVersionKind) (configmanager.CustomAccessor, bool) {
func (c *ConfigurableInterpreter) getCustomAccessor(kind schema.GroupVersionKind) (accessor.CustomAccessor, bool) {
if !c.configManager.HasSynced() {
klog.Errorf("not yet ready to handle request")
return nil, false
}

accessor, exist := c.configManager.CustomAccessors()[kind]
return accessor, exist
customAccessor, exist := c.configManager.CustomAccessors()[kind]
return customAccessor, exist
}

// LoadConfig loads and stores rules from customizations
Expand Down

0 comments on commit a2e4d6f

Please sign in to comment.