openwhisk icon indicating copy to clipboard operation
openwhisk copied to clipboard

How to create an action whose kind is `blackbox` with restful API

Open ZinuoCai opened this issue 5 years ago • 3 comments

Hello, I want to create an action whose kind is blackbox. I refer to the Swagger UI document and find the following data.

{
  "namespace": "string",
  "name": "string",
  "version": "string",
  "publish": true,
  "exec": {
    "kind": "blackbox",
    "code": "string",
    "image": "string",
    "main": "string",
    "binary": true,
    "components": [
      "string"
    ]
  },
  "annotations": [
    {
      "key": "string"
    }
  ],
  "delAnnotations": [
    "string"
  ],
  "parameters": [
    {
      "key": "string"
    }
  ],
  "limits": {
    "timeout": 60000,
    "memory": 256,
    "logs": 10,
    "concurrency": 1
  }
}

But it says that when the kind is not blackbox, we can put the code in the code field. I wonder how can I put my code in such a case. Thank!

ZinuoCai avatar Mar 15 '21 16:03 ZinuoCai

Tip: You can add -v (or -d) to the wsk CLI command to see how the CLI is making the API calls.

rabbah avatar Mar 15 '21 20:03 rabbah

@rabbah Thanks for your advice.

ZinuoCai avatar Mar 16 '21 03:03 ZinuoCai

I ask the question because I want to add a gpu memory limit to the pod. I have change the code of OpenWhisk to satisfy the requirements, but I fail to create the action with openwhisk-cli because it does not support self-defined limit keywords. We can create an action with the restful API. I release my code below.

import requests

code = "[your code here]"
action_name = '[your action name]'
img = '[docker image name]'
# a special limit in my case
gpu_memory = 1024

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}

# change the below to your identity information
auth = ('23bc46b1-71f6-4ed5-8c54-816aa4f8c502', '123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP')
url = 'https://127.0.0.1:31001/api/v1/namespaces/guest/actions/%s' % action_name

data = """
        {
            "exec":{
                "kind":"blackbox",
                "image": "%s",
                "code":"%s",
                "binary":false
            },
            "limits":{
                "gpuMemory": %d
            }
        }
        """ % (img, code, gpu_memory)

response = requests.put(url, headers=headers, data=data, verify=False, auth=auth)
print(response.content)

ZinuoCai avatar Mar 16 '21 03:03 ZinuoCai