Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to use lua's built-in string function in ResourceInterpreterCustomization lua script #3247

Closed
XiShanYongYe-Chang opened this issue Mar 7, 2023 · 4 comments · Fixed by #3256
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug.

Comments

@XiShanYongYe-Chang
Copy link
Member

XiShanYongYe-Chang commented Mar 7, 2023

What happened:

For the following ResourceInterpreterCustomization resource:

apiVersion: config.karmada.io/v1alpha1
kind: ResourceInterpreterCustomization
metadata:
  name: declarative-configuration-example
spec:
  target:
    apiVersion: v1
    kind: Service
  customizations:
    statusReflection:
      luaScript: >
        function ReflectStatus (observedObj)
          print(string.len("abc"))
          return observedObj.status
        end

I want to interpret the Service Status resource with the interpretStatus operation:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2023-03-06T12:24:10Z"
  labels:
    component: apiserver
    provider: kubernetes
  name: kubernetes
  namespace: default
  resourceVersion: "190"
  uid: 79febb3b-af42-4e83-aa4f-9095112771fb
spec:
  clusterIP: 10.96.0.1
  clusterIPs:
  - 10.96.0.1
  internalTrafficPolicy: Cluster
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - name: https
    port: 443
    protocol: TCP
    targetPort: 5443
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer:
    ingress:
    - ip: 172.1.1.1
    - ip: 192.1.3.2

When I run with the following command:

karmadactl interpret -f reflectstatus.yaml --operation interpretStatus --observed-file service-status.yaml

I got the bellow error:

Execute InterpretStatus error: <string>:2: attempt to index a non-table object(nil) with key 'len'
stack traceback:
        <string>:2: in main chunk
        [G]: ?

Then I tried other string functions, and it returned similar errors.

What you expected to happen:

The string function can be used normally.

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

This issue is related to lua VM initialization: pkg/resourceinterpreter/configurableinterpreter/luavm/lua.go:43

Environment:

  • Karmada version: karmada v1.4 and later
  • kubectl-karmada or karmadactl version (the result of kubectl-karmada version or karmadactl version): karmada v1.4 and later
  • Others:
@XiShanYongYe-Chang XiShanYongYe-Chang added the kind/bug Categorizes issue or PR as related to a bug. label Mar 7, 2023
@chaunceyjiang
Copy link
Member

Have you tested the #str method?

@XiShanYongYe-Chang
Copy link
Member Author

Have you tested the #str method?

Can you give me a demo usage?

@chaunceyjiang
Copy link
Member

/assign

@XiShanYongYe-Chang
Copy link
Member Author

Found the same problem that argo-cd had before: argoproj/argo-cd#6039

The solution is to introduce the useOpenLibs parameter, allow administrator enable lua standard library for health checks:

argoproj/argo-cd#6087

For us, opening a parameter for user settings may not be the best option.

The reason of disable lua standard library is:

// oslib_safe contains a subset of the lua OS library. For security reasons, we do not expose
// the entirety of lua OS library to custom actions, such as ones which can exit, read files, etc.
// Only the safe functions like os.time(), os.date() are exposed. Implementation was copied from
// github.com/yuin/gopher-lua.

The lua standard libraries we want to use are string, math, so we can set up these two standard libraries additionally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants