Skip to content

Commit 3a294dd

Browse files
committed
Implement GetComponents method for ConfigurableInterpreter
Signed-off-by: wei-chenglai <[email protected]>
1 parent 1c6f1db commit 3a294dd

File tree

6 files changed

+366
-0
lines changed

6 files changed

+366
-0
lines changed

pkg/resourceinterpreter/customized/declarative/configmanager/accessor.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type LuaScriptAccessor interface {
3131

3232
GetRetentionLuaScript() string
3333
GetReplicaResourceLuaScript() string
34+
GetComponentResourceLuaScript() string
3435
GetReplicaRevisionLuaScript() string
3536
GetStatusReflectionLuaScript() string
3637
GetStatusAggregationLuaScript() string
@@ -46,6 +47,7 @@ type CustomAccessor interface {
4647
type resourceCustomAccessor struct {
4748
retention *configv1alpha1.LocalValueRetention
4849
replicaResource *configv1alpha1.ReplicaResourceRequirement
50+
componentResource *configv1alpha1.ComponentResourceRequirement
4951
replicaRevision *configv1alpha1.ReplicaRevision
5052
statusReflection *configv1alpha1.StatusReflection
5153
statusAggregation *configv1alpha1.StatusAggregation
@@ -66,6 +68,9 @@ func (a *resourceCustomAccessor) Merge(rules configv1alpha1.CustomizationRules)
6668
if rules.ReplicaResource != nil {
6769
a.setReplicaResource(rules.ReplicaResource)
6870
}
71+
if rules.ComponentResource != nil {
72+
a.setComponentResource(rules.ComponentResource)
73+
}
6974
if rules.ReplicaRevision != nil {
7075
a.setReplicaRevision(rules.ReplicaRevision)
7176
}
@@ -97,6 +102,13 @@ func (a *resourceCustomAccessor) GetReplicaResourceLuaScript() string {
97102
return a.replicaResource.LuaScript
98103
}
99104

105+
func (a *resourceCustomAccessor) GetComponentResourceLuaScript() string {
106+
if a.componentResource == nil {
107+
return ""
108+
}
109+
return a.componentResource.LuaScript
110+
}
111+
100112
func (a *resourceCustomAccessor) GetReplicaRevisionLuaScript() string {
101113
if a.replicaRevision == nil {
102114
return ""
@@ -161,6 +173,17 @@ func (a *resourceCustomAccessor) setReplicaResource(replicaResource *configv1alp
161173
}
162174
}
163175

176+
func (a *resourceCustomAccessor) setComponentResource(componentResource *configv1alpha1.ComponentResourceRequirement) {
177+
if a.componentResource == nil {
178+
a.componentResource = componentResource
179+
return
180+
}
181+
182+
if componentResource.LuaScript != "" && a.componentResource.LuaScript == "" {
183+
a.componentResource.LuaScript = componentResource.LuaScript
184+
}
185+
}
186+
164187
func (a *resourceCustomAccessor) setReplicaRevision(replicaRevision *configv1alpha1.ReplicaRevision) {
165188
if a.replicaRevision == nil {
166189
a.replicaRevision = replicaRevision

pkg/resourceinterpreter/customized/declarative/configmanager/accessor_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ func TestGetReplicaResourceLuaScript(t *testing.T) {
8585
}
8686
}
8787

88+
func TestGetComponentResourceLuaScript(t *testing.T) {
89+
tests := []struct {
90+
name string
91+
accessor *resourceCustomAccessor
92+
want string
93+
}{
94+
{
95+
name: "nil component resource",
96+
accessor: &resourceCustomAccessor{},
97+
want: "",
98+
},
99+
{
100+
name: "with script",
101+
accessor: &resourceCustomAccessor{
102+
componentResource: &configv1alpha1.ComponentResourceRequirement{LuaScript: "test-script"},
103+
},
104+
want: "test-script",
105+
},
106+
}
107+
108+
for _, tt := range tests {
109+
t.Run(tt.name, func(t *testing.T) {
110+
got := tt.accessor.GetComponentResourceLuaScript()
111+
assert.Equal(t, tt.want, got)
112+
})
113+
}
114+
}
115+
88116
func TestGetReplicaRevisionLuaScript(t *testing.T) {
89117
tests := []struct {
90118
name string
@@ -465,6 +493,45 @@ func TestSetReplicaResource(t *testing.T) {
465493
}
466494
}
467495

496+
func TestSetComponentResource(t *testing.T) {
497+
tests := []struct {
498+
name string
499+
initial *resourceCustomAccessor
500+
input *configv1alpha1.ComponentResourceRequirement
501+
want string
502+
}{
503+
{
504+
name: "set on nil field",
505+
initial: &resourceCustomAccessor{},
506+
input: &configv1alpha1.ComponentResourceRequirement{LuaScript: "script1"},
507+
want: "script1",
508+
},
509+
{
510+
name: "set on empty script",
511+
initial: &resourceCustomAccessor{
512+
componentResource: &configv1alpha1.ComponentResourceRequirement{LuaScript: ""},
513+
},
514+
input: &configv1alpha1.ComponentResourceRequirement{LuaScript: "script1"},
515+
want: "script1",
516+
},
517+
{
518+
name: "set on non-empty script",
519+
initial: &resourceCustomAccessor{
520+
componentResource: &configv1alpha1.ComponentResourceRequirement{LuaScript: "existing"},
521+
},
522+
input: &configv1alpha1.ComponentResourceRequirement{LuaScript: "script1"},
523+
want: "existing",
524+
},
525+
}
526+
527+
for _, tt := range tests {
528+
t.Run(tt.name, func(t *testing.T) {
529+
tt.initial.setComponentResource(tt.input)
530+
assert.Equal(t, tt.want, tt.initial.GetComponentResourceLuaScript())
531+
})
532+
}
533+
}
534+
468535
func TestSetReplicaRevision(t *testing.T) {
469536
tests := []struct {
470537
name string

pkg/resourceinterpreter/customized/declarative/configurable.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func (c *ConfigurableInterpreter) HookEnabled(kind schema.GroupVersionKind, oper
7070
script = accessor.GetHealthInterpretationLuaScript()
7171
case configv1alpha1.InterpreterOperationInterpretReplica:
7272
script = accessor.GetReplicaResourceLuaScript()
73+
case configv1alpha1.InterpreterOperationInterpretComponent:
74+
script = accessor.GetComponentResourceLuaScript()
7375
case configv1alpha1.InterpreterOperationInterpretStatus:
7476
script = accessor.GetStatusReflectionLuaScript()
7577
case configv1alpha1.InterpreterOperationRetain:
@@ -99,6 +101,25 @@ func (c *ConfigurableInterpreter) GetReplicas(object *unstructured.Unstructured)
99101
return
100102
}
101103

104+
// GetComponents returns the desired components of the object.
105+
func (c *ConfigurableInterpreter) GetComponents(object *unstructured.Unstructured) (components []workv1alpha2.ComponentRequirements, enabled bool, err error) {
106+
klog.V(4).Infof("Get components for object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())
107+
108+
accessor, enabled := c.getCustomAccessor(object.GroupVersionKind())
109+
if !enabled {
110+
return
111+
}
112+
113+
script := accessor.GetComponentResourceLuaScript()
114+
if len(script) == 0 {
115+
enabled = false
116+
return
117+
}
118+
119+
components, err = c.luaVM.GetComponents(object, script)
120+
return
121+
}
122+
102123
// ReviseReplica revises the replica of the given object.
103124
func (c *ConfigurableInterpreter) ReviseReplica(object *unstructured.Unstructured, replica int64) (revised *unstructured.Unstructured, enabled bool, err error) {
104125
klog.V(4).Infof("Revise replicas for object: %v %s/%s with configurable interpreter.", object.GroupVersionKind(), object.GetNamespace(), object.GetName())

pkg/resourceinterpreter/customized/declarative/luavm/lua.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ func (vm *VM) GetReplicas(obj *unstructured.Unstructured, script string) (replic
154154
return
155155
}
156156

157+
// GetComponents returns the desired components of the object by executing a Lua script.
158+
func (vm *VM) GetComponents(obj *unstructured.Unstructured, script string) ([]workv1alpha2.ComponentRequirements, error) {
159+
results, err := vm.RunScript(script, "GetComponents", 1, obj)
160+
if err != nil {
161+
return nil, fmt.Errorf("failed to run 'GetComponents' script: %w", err)
162+
}
163+
164+
componentsResult := results[0]
165+
var components []workv1alpha2.ComponentRequirements
166+
167+
switch componentsResult.Type() {
168+
case lua.LTTable:
169+
if err := ConvertLuaResultInto(componentsResult.(*lua.LTable), &components); err != nil {
170+
klog.Errorf("Failed to convert Lua result for GetComponents: %v", err)
171+
return nil, fmt.Errorf("failed to convert lua table for components: %w", err)
172+
}
173+
case lua.LTNil:
174+
// This is a valid case where the script returns no components.
175+
// The 'components' slice is already nil, so we do nothing.
176+
default:
177+
// Handle unexpected return types.
178+
return nil, fmt.Errorf("expected result type 'table' or 'nil' from GetComponents, but got '%s'", componentsResult.Type())
179+
}
180+
181+
return components, nil
182+
}
183+
157184
// ReviseReplica revises the replica of the given object by lua.
158185
func (vm *VM) ReviseReplica(object *unstructured.Unstructured, replica int64, script string) (*unstructured.Unstructured, error) {
159186
results, err := vm.RunScript(script, "ReviseReplica", 1, object, replica)

0 commit comments

Comments
 (0)