# OpenWhisk

The `openwhisk` plugin invokes an [Apache OpenWhisk](https://openwhisk.apache.org/) action as a blocking request. It returns the action result or a structured status, headers, and body.

The plugin forwards only the request body. Client query parameters and headers are not forwarded; OpenWhisk receives a `POST` request with a JSON content type and the configured Basic credentials.

## Examples[​](#examples "Direct link to Examples")

The examples show how to invoke an action with JSON and return a custom HTTP response from an action.

### Set Up OpenWhisk[​](#set-up-openwhisk "Direct link to Set Up OpenWhisk")

Use OpenWhisk Standalone for local evaluation, or connect the gateway to an existing OpenWhisk deployment.

#### Start OpenWhisk Standalone[​](#start-openwhisk-standalone "Direct link to Start OpenWhisk Standalone")

Start [OpenWhisk Standalone](https://github.com/apache/openwhisk/blob/master/core/standalone/README.md), install the `wsk` CLI, and connect the gateway container to its network:

```
export GATEWAY_CONTAINER=replace-with-gateway-container-name

docker network create gateway-openwhisk-net

docker network connect gateway-openwhisk-net "$GATEWAY_CONTAINER"

docker run -d --name ow-standalone \

  --network gateway-openwhisk-net \

  -e DOCKER_API_VERSION=1.40 \

  -p 127.0.0.1:3233:3233 \

  -p 127.0.0.1:3232:3232 \

  -v /var/run/docker.sock:/var/run/docker.sock \

  openwhisk/standalone:nightly

docker exec ow-standalone waitready
```

The explicit Docker API version allows the older Docker client bundled in the Standalone image to communicate with current Docker engines.

Configure `wsk` with the API host and guest credentials printed by the Standalone container, then export the values used by the gateway:

```
docker logs ow-standalone 2>&1 | grep 'wsk property set'



wsk property set \

  --apihost "http://127.0.0.1:3233" \

  --auth "replace-with-guest-uuid:replace-with-guest-secret"



export OPENWHISK_API_HOST="http://ow-standalone:3233"

export OPENWHISK_SERVICE_TOKEN="replace-with-guest-uuid:replace-with-guest-secret"

export OPENWHISK_NAMESPACE="guest"
```

#### Connect to an Existing Deployment[​](#connect-to-an-existing-deployment "Direct link to Connect to an Existing Deployment")

For a shared or production deployment, configure `wsk` with the existing OpenWhisk API, then export an API host reachable from every gateway instance and a service token for the action namespace:

```
wsk property set \

  --apihost "https://replace-with-openwhisk-api-host" \

  --auth "replace-with-service-token"



export OPENWHISK_API_HOST="https://replace-with-openwhisk-api-host"

export OPENWHISK_SERVICE_TOKEN="replace-with-service-token"

export OPENWHISK_NAMESPACE="replace-with-action-namespace"
```

### Invoke an Action with JSON[​](#invoke-an-action-with-json "Direct link to Invoke an Action with JSON")

Create an action that returns a greeting:

hello.js

```
function main(params) {

  return { greeting: `Hello ${params.name}!` };

}
```

Deploy the action:

```
wsk action update hello hello.js --kind nodejs:default
```

Configure a route to invoke the action:

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes/openwhisk-route" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d @- <<EOF

{

  "uri": "/openwhisk",

  "plugins": {

    "openwhisk": {

      "api_host": "${OPENWHISK_API_HOST}",

      "service_token": "${OPENWHISK_SERVICE_TOKEN}",

      "namespace": "${OPENWHISK_NAMESPACE}",

      "action": "hello",

      "timeout": 60000

    }

  },

  "upstream": {

    "type": "roundrobin",

    "nodes": {

      "httpbin.org:80": 1

    }

  }

}

EOF
```

adc.yaml

```
services:

  - name: openwhisk-service

    labels:

      docs-example: openwhisk

    routes:

      - name: openwhisk-route

        uris:

          - /openwhisk

        plugins:

          openwhisk:

            api_host: "${OPENWHISK_API_HOST}"

            service_token: "${OPENWHISK_SERVICE_TOKEN}"

            namespace: "${OPENWHISK_NAMESPACE}"

            action: hello

            timeout: 60000

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

Preview the scoped changes:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openwhisk
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openwhisk
```

[Set up the Ingress Controller and gateway](https://docs.api7.ai/ingress-controller/set-up-ingress-controller-and-gateway.md) in the `aic` namespace before continuing. Replace the OpenWhisk API host, token, and action namespace.

* Gateway API
* APISIX CRD

openwhisk-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

  ports:

    - port: 80

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: openwhisk-route

spec:

  plugins:

    - name: openwhisk

      config:

        api_host: https://replace-with-openwhisk-api-host

        service_token: replace-with-service-token

        namespace: replace-with-action-namespace

        action: hello

        timeout: 60000

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: openwhisk-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /openwhisk

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: openwhisk-route

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

openwhisk-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

  ports:

    - port: 80

---

apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: openwhisk-route

spec:

  ingressClassName: apisix

  plugins:

    - name: openwhisk

      enable: true

      config:

        api_host: https://replace-with-openwhisk-api-host

        service_token: replace-with-service-token

        namespace: replace-with-action-namespace

        action: hello

        timeout: 60000

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: openwhisk-route

spec:

  ingressClassName: apisix

  http:

    - name: openwhisk-route

      match:

        paths:

          - /openwhisk

      backends:

        - serviceName: httpbin-external-domain

          servicePort: 80

      plugin_config_name: openwhisk-route
```

Apply the configuration:

```
kubectl apply -f openwhisk-ic.yaml
```

The configurations include a conventional fallback backend. The plugin returns the action response before that backend is contacted. The 60-second timeout allows the local Standalone deployment to pull and start an action runtime on its first invocation.

Invoke the action with a JSON body:

```
curl "http://127.0.0.1:9080/openwhisk" \

  -H "Content-Type: application/json" \

  -d '{"name":"API7"}'
```

The gateway returns the action result:

```
{"greeting":"Hello API7!"}
```

### Return a Custom HTTP Response[​](#return-a-custom-http-response "Direct link to Return a Custom HTTP Response")

Create an action that returns a status, header, and body:

custom-response.js

```
function main() {

  return {

    statusCode: 201,

    headers: { "X-Action": "created" },

    body: "created by OpenWhisk",

  };

}
```

Deploy the action:

```
wsk action update custom-response custom-response.js --kind nodejs:default
```

Update the existing plugin configuration to invoke the new action:

* Admin API
* ADC
* Ingress Controller

Replace the plugin configuration on the existing route:

```
curl "http://127.0.0.1:9180/apisix/admin/routes/openwhisk-route/plugins/openwhisk" -X PATCH \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d @- <<EOF

{

  "api_host": "${OPENWHISK_API_HOST}",

  "service_token": "${OPENWHISK_SERVICE_TOKEN}",

  "namespace": "${OPENWHISK_NAMESPACE}",

  "action": "custom-response",

  "timeout": 60000

}

EOF
```

In `adc.yaml`, replace the plugin configuration on the existing route:

adc.yaml (route plugin)

```
plugins:

  openwhisk:

    api_host: "${OPENWHISK_API_HOST}"

    service_token: "${OPENWHISK_SERVICE_TOKEN}"

    namespace: "${OPENWHISK_NAMESPACE}"

    action: custom-response

    timeout: 60000
```

Preview the updated service:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openwhisk
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openwhisk
```

Update the existing plugin configuration with the Kubernetes API used by the deployment:

* Gateway API
* APISIX CRD

openwhisk-ic.yaml

```
apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: openwhisk-route

spec:

  plugins:

    - name: openwhisk

      config:

        api_host: https://replace-with-openwhisk-api-host

        service_token: replace-with-service-token

        namespace: replace-with-action-namespace

        action: custom-response

        timeout: 60000
```

openwhisk-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: openwhisk-route

spec:

  ingressClassName: apisix

  plugins:

    - name: openwhisk

      enable: true

      config:

        api_host: https://replace-with-openwhisk-api-host

        service_token: replace-with-service-token

        namespace: replace-with-action-namespace

        action: custom-response

        timeout: 60000
```

Apply the updated plugin configuration:

```
kubectl apply -f openwhisk-ic.yaml
```

Send the request again:

```
curl --include "http://127.0.0.1:9080/openwhisk" \

  -H "Content-Type: application/json" \

  -d '{}'
```

The action controls the client response:

```
HTTP/1.1 201 Created

X-Action: created

...

created by OpenWhisk
```
