-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2871 from ikaven1024/feature/lua-resource
add kube lib for lua
- Loading branch information
Showing
6 changed files
with
207 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
pkg/resourceinterpreter/configurableinterpreter/luavm/kube.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package luavm | ||
|
||
import ( | ||
lua "github.com/yuin/gopher-lua" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
|
||
"github.com/karmada-io/karmada/pkg/util/helper" | ||
) | ||
|
||
const ( | ||
// KubeLibName is the name of the kube library. | ||
KubeLibName = "kube" | ||
) | ||
|
||
// KubeLoader loads the kube library. Import this library before your script by: | ||
// | ||
// local kube = require("kube") | ||
// | ||
// Then you can call functions in this library by: | ||
// | ||
// kube.xxx() | ||
// | ||
// This library Contains: | ||
// - function resourceAdd(r1, r2, ...) | ||
// accruing the quantity of resources. Example: | ||
// cpu = kube.resourceAdd(r1.cpu, r2.cpu, r3.cpu) | ||
// - function accuratePodRequirements(pod) requirements | ||
// accurate total resource requirements for pod. Example: | ||
// requirements = kube.accuratePodRequirements(pod) | ||
func KubeLoader(ls *lua.LState) int { | ||
mod := ls.SetFuncs(ls.NewTable(), kubeFuncs) | ||
ls.Push(mod) | ||
return 1 | ||
} | ||
|
||
var kubeFuncs = map[string]lua.LGFunction{ | ||
"resourceAdd": resourceAdd, | ||
"accuratePodRequirements": accuratePodRequirements, | ||
} | ||
|
||
func resourceAdd(ls *lua.LState) int { | ||
res := resource.Quantity{} | ||
n := ls.GetTop() | ||
for i := 1; i <= n; i++ { | ||
q := checkResourceQuantity(ls, i) | ||
res.Add(q) | ||
} | ||
|
||
s := res.String() | ||
ls.Push(lua.LString(s)) | ||
return 1 | ||
} | ||
|
||
func accuratePodRequirements(ls *lua.LState) int { | ||
n := ls.GetTop() | ||
if n != 1 { | ||
ls.RaiseError("getPodRequirements only accepts one argument") | ||
return 0 | ||
} | ||
|
||
v := ls.CheckTable(1) | ||
pod := &corev1.PodTemplateSpec{} | ||
err := ConvertLuaResultInto(v, pod) | ||
if err != nil { | ||
ls.RaiseError("fail to convert lua value %#v to PodTemplateSpec: %v", v, err) | ||
return 0 | ||
} | ||
|
||
requirements := helper.GenerateReplicaRequirements(pod) | ||
retValue, err := decodeValue(ls, requirements) | ||
if err != nil { | ||
ls.RaiseError("fail to convert %#v to Lua value: %v", requirements, err) | ||
return 0 | ||
} | ||
|
||
ls.Push(retValue) | ||
return 1 | ||
} | ||
|
||
func checkResourceQuantity(ls *lua.LState, n int) resource.Quantity { | ||
v := ls.Get(n) | ||
switch typ := v.Type(); typ { | ||
case lua.LTNil: | ||
return resource.Quantity{} | ||
case lua.LTString: | ||
s := string(v.(lua.LString)) | ||
return resource.MustParse(s) | ||
default: | ||
ls.TypeError(n, lua.LTString) | ||
return resource.Quantity{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters