From 3c731e5d087a6583f709effd0e67f4450a5735f4 Mon Sep 17 00:00:00 2001 From: chaosi-zju Date: Wed, 29 May 2024 15:11:06 +0800 Subject: [PATCH] fix demo for dependencyInterpretation Signed-off-by: chaosi-zju --- api/openapi-spec/swagger.json | 2 +- ...karmada.io_resourceinterpretercustomizations.yaml | 8 ++++---- .../resourceinterpretercustomization_types.go | 8 ++++---- pkg/generated/openapi/zz_generated.openapi.go | 2 +- pkg/util/interpreter/rule.go | 10 +++++----- pkg/util/interpreter/rule_test.go | 10 +++++----- .../resourceinterpretercustomization/helper_test.go | 12 ++++++------ 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/api/openapi-spec/swagger.json b/api/openapi-spec/swagger.json index 28d7ec826..c1af8ea00 100644 --- a/api/openapi-spec/swagger.json +++ b/api/openapi-spec/swagger.json @@ -17605,7 +17605,7 @@ ], "properties": { "luaScript": { - "description": "LuaScript holds the Lua script that is used to interpret the dependencies of a specific resource. The script should implement a function as follows:\n\n```\n luaScript: \u003e\n function GetDependencies(desiredObj)\n dependencies = {}\n if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= \"default\" then\n dependency = {}\n dependency.apiVersion = \"v1\"\n dependency.kind = \"ServiceAccount\"\n dependency.name = desiredObj.spec.serviceAccountName\n dependency.namespace = desiredObj.namespace\n dependencies[1] = {}\n dependencies[1] = dependency\n end\n return dependencies\n end\n```\n\nThe content of the LuaScript needs to be a whole function including both declaration and implementation.\n\nThe parameters will be supplied by the system:\n - desiredObj: the object represents the configuration to be applied\n to the member cluster.\n\nThe returned value should be expressed by a slice of DependentObjectReference.", + "description": "LuaScript holds the Lua script that is used to interpret the dependencies of a specific resource. The script should implement a function as follows:\n\n```\n luaScript: \u003e\n function GetDependencies(desiredObj)\n dependencies = {}\n serviceAccountName = desiredObj.spec.template.spec.serviceAccountName\n if serviceAccountName ~= nil and serviceAccountName ~= \"default\" then\n dependency = {}\n dependency.apiVersion = \"v1\"\n dependency.kind = \"ServiceAccount\"\n dependency.name = serviceAccountName\n dependency.namespace = desiredObj.metadata.namespace\n dependencies[1] = dependency\n end\n return dependencies\n end\n```\n\nThe content of the LuaScript needs to be a whole function including both declaration and implementation.\n\nThe parameters will be supplied by the system:\n - desiredObj: the object represents the configuration to be applied\n to the member cluster.\n\nThe returned value should be expressed by a slice of DependentObjectReference.", "type": "string", "default": "" } diff --git a/charts/karmada/_crds/bases/config/config.karmada.io_resourceinterpretercustomizations.yaml b/charts/karmada/_crds/bases/config/config.karmada.io_resourceinterpretercustomizations.yaml index 595541d11..212f08552 100644 --- a/charts/karmada/_crds/bases/config/config.karmada.io_resourceinterpretercustomizations.yaml +++ b/charts/karmada/_crds/bases/config/config.karmada.io_resourceinterpretercustomizations.yaml @@ -69,13 +69,13 @@ spec: luaScript: > function GetDependencies(desiredObj) dependencies = {} - if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= "default" then + serviceAccountName = desiredObj.spec.template.spec.serviceAccountName + if serviceAccountName ~= nil and serviceAccountName ~= "default" then dependency = {} dependency.apiVersion = "v1" dependency.kind = "ServiceAccount" - dependency.name = desiredObj.spec.serviceAccountName - dependency.namespace = desiredObj.namespace - dependencies[1] = {} + dependency.name = serviceAccountName + dependency.namespace = desiredObj.metadata.namespace dependencies[1] = dependency end return dependencies diff --git a/pkg/apis/config/v1alpha1/resourceinterpretercustomization_types.go b/pkg/apis/config/v1alpha1/resourceinterpretercustomization_types.go index f8e7b9ab3..384613dd5 100644 --- a/pkg/apis/config/v1alpha1/resourceinterpretercustomization_types.go +++ b/pkg/apis/config/v1alpha1/resourceinterpretercustomization_types.go @@ -322,13 +322,13 @@ type DependencyInterpretation struct { // luaScript: > // function GetDependencies(desiredObj) // dependencies = {} - // if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= "default" then + // serviceAccountName = desiredObj.spec.template.spec.serviceAccountName + // if serviceAccountName ~= nil and serviceAccountName ~= "default" then // dependency = {} // dependency.apiVersion = "v1" // dependency.kind = "ServiceAccount" - // dependency.name = desiredObj.spec.serviceAccountName - // dependency.namespace = desiredObj.namespace - // dependencies[1] = {} + // dependency.name = serviceAccountName + // dependency.namespace = desiredObj.metadata.namespace // dependencies[1] = dependency // end // return dependencies diff --git a/pkg/generated/openapi/zz_generated.openapi.go b/pkg/generated/openapi/zz_generated.openapi.go index 86f22ae4f..6e66e36b2 100644 --- a/pkg/generated/openapi/zz_generated.openapi.go +++ b/pkg/generated/openapi/zz_generated.openapi.go @@ -2112,7 +2112,7 @@ func schema_pkg_apis_config_v1alpha1_DependencyInterpretation(ref common.Referen Properties: map[string]spec.Schema{ "luaScript": { SchemaProps: spec.SchemaProps{ - Description: "LuaScript holds the Lua script that is used to interpret the dependencies of a specific resource. The script should implement a function as follows:\n\n```\n luaScript: >\n function GetDependencies(desiredObj)\n dependencies = {}\n if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= \"default\" then\n dependency = {}\n dependency.apiVersion = \"v1\"\n dependency.kind = \"ServiceAccount\"\n dependency.name = desiredObj.spec.serviceAccountName\n dependency.namespace = desiredObj.namespace\n dependencies[1] = {}\n dependencies[1] = dependency\n end\n return dependencies\n end\n```\n\nThe content of the LuaScript needs to be a whole function including both declaration and implementation.\n\nThe parameters will be supplied by the system:\n - desiredObj: the object represents the configuration to be applied\n to the member cluster.\n\nThe returned value should be expressed by a slice of DependentObjectReference.", + Description: "LuaScript holds the Lua script that is used to interpret the dependencies of a specific resource. The script should implement a function as follows:\n\n```\n luaScript: >\n function GetDependencies(desiredObj)\n dependencies = {}\n serviceAccountName = desiredObj.spec.template.spec.serviceAccountName\n if serviceAccountName ~= nil and serviceAccountName ~= \"default\" then\n dependency = {}\n dependency.apiVersion = \"v1\"\n dependency.kind = \"ServiceAccount\"\n dependency.name = serviceAccountName\n dependency.namespace = desiredObj.metadata.namespace\n dependencies[1] = dependency\n end\n return dependencies\n end\n```\n\nThe content of the LuaScript needs to be a whole function including both declaration and implementation.\n\nThe parameters will be supplied by the system:\n - desiredObj: the object represents the configuration to be applied\n to the member cluster.\n\nThe returned value should be expressed by a slice of DependentObjectReference.", Default: "", Type: []string{"string"}, Format: "", diff --git a/pkg/util/interpreter/rule.go b/pkg/util/interpreter/rule.go index b03e281f6..526078e87 100644 --- a/pkg/util/interpreter/rule.go +++ b/pkg/util/interpreter/rule.go @@ -367,14 +367,14 @@ func (d *dependencyInterpretationRule) Document() string { The script should implement a function as follows: function GetDependencies(desiredObj) dependencies = {} - if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= "default" then + serviceAccountName = desiredObj.spec.template.spec.serviceAccountName + if serviceAccountName ~= nil and serviceAccountName ~= "default" then dependency = {} dependency.apiVersion = "v1" dependency.kind = "ServiceAccount" - dependency.name = desiredObj.spec.serviceAccountName - dependency.namespace = desiredObj.namespace - dependencies[0] = {} - dependencies[0] = dependency + dependency.name = serviceAccountName + dependency.namespace = desiredObj.metadata.namespace + dependencies[1] = dependency end return dependencies end` diff --git a/pkg/util/interpreter/rule_test.go b/pkg/util/interpreter/rule_test.go index 5daa07677..d86385865 100644 --- a/pkg/util/interpreter/rule_test.go +++ b/pkg/util/interpreter/rule_test.go @@ -587,14 +587,14 @@ func TestDependencyInterpretationRule_Document(t *testing.T) { The script should implement a function as follows: function GetDependencies(desiredObj) dependencies = {} - if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= "default" then + serviceAccountName = desiredObj.spec.template.spec.serviceAccountName + if serviceAccountName ~= nil and serviceAccountName ~= "default" then dependency = {} dependency.apiVersion = "v1" dependency.kind = "ServiceAccount" - dependency.name = desiredObj.spec.serviceAccountName - dependency.namespace = desiredObj.namespace - dependencies[0] = {} - dependencies[0] = dependency + dependency.name = serviceAccountName + dependency.namespace = desiredObj.metadata.namespace + dependencies[1] = dependency end return dependencies end` diff --git a/pkg/webhook/resourceinterpretercustomization/helper_test.go b/pkg/webhook/resourceinterpretercustomization/helper_test.go index ab23e5548..3d9a2b9bf 100644 --- a/pkg/webhook/resourceinterpretercustomization/helper_test.go +++ b/pkg/webhook/resourceinterpretercustomization/helper_test.go @@ -391,15 +391,15 @@ end `}, DependencyInterpretation: &configv1alpha1.DependencyInterpretation{LuaScript: ` function GetDependencies(desiredObj) - dependencies = {} - if desiredObj.spec.serviceAccountName ~= nil and desiredObj.spec.serviceAccountName ~= "default" then + dependencies = {} + serviceAccountName = desiredObj.spec.template.spec.serviceAccountName + if serviceAccountName ~= nil and serviceAccountName ~= "default" then dependency = {} dependency.apiVersion = "v1" dependency.kind = "ServiceAccount" - dependency.name = desiredObj.spec.serviceAccountName - dependency.namespace = desiredObj.namespace - dependencies[0] = {} - dependencies[0] = dependency + dependency.name = serviceAccountName + dependency.namespace = desiredObj.metadata.namespace + dependencies[1] = dependency end return dependencies end