# OpenWhisk

`openwhisk` 插件以阻塞请求调用 [Apache OpenWhisk](https://openwhisk.apache.org/) Action，并返回 Action 结果，或结构化的状态、响应头和响应体。

插件仅转发请求体，不转发客户端查询参数和请求头。OpenWhisk 收到的是使用 JSON 内容类型和所配置 Basic 凭证的 `POST` 请求。

## 示例[​](#examples "示例的直接链接")

以下示例展示如何使用 JSON 调用 Action，以及如何从 Action 返回自定义 HTTP 响应。

### 设置 OpenWhisk[​](#set-up-openwhisk "设置 OpenWhisk的直接链接")

可以使用 OpenWhisk Standalone 进行本地评估，也可以将网关连接到现有的 OpenWhisk 部署。

#### 启动 OpenWhisk Standalone[​](#start-openwhisk-standalone "启动 OpenWhisk Standalone的直接链接")

启动 [OpenWhisk Standalone](https://github.com/apache/openwhisk/blob/master/core/standalone/README.md)，安装 `wsk` CLI，并将网关容器连接到其网络：

```
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
```

显式指定 Docker API 版本可让 Standalone 镜像内置的旧版 Docker 客户端与当前 Docker 引擎通信。

使用 Standalone 容器输出的 API 主机地址和访客凭证配置 `wsk`，然后导出网关使用的值：

```
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 "连接到现有部署的直接链接")

对于共享或生产部署，请使用现有 OpenWhisk API 配置 `wsk`，然后导出每个网关实例均可访问的 API 主机地址以及 Action 命名空间的服务 Token：

```
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"
```

### 使用 JSON 调用 Action[​](#invoke-an-action-with-json "使用 JSON 调用 Action的直接链接")

创建一个返回问候语的 Action：

hello.js

```
function main(params) {

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

}
```

部署 Action：

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

配置路由以调用 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
```

预览限定范围的变更：

```
adc diff -f adc.yaml \

  --include-resource-type service \

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

检查 `diff.yaml`，然后同步带有该标签的服务：

```
adc sync -f adc.yaml \

  --include-resource-type service \

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

继续之前，请在 `aic` 命名空间中[设置 Ingress Controller 和网关](https://docs.apiseven.com/ingress-controller/set-up-ingress-controller-and-gateway.md)。替换 OpenWhisk API 主机地址、Token 和 Action 命名空间。

* 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
```

应用配置：

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

这些配置包含常规的后备后端。插件会在联系该后端之前返回 Action 响应。60 秒的超时允许本地 Standalone 部署在首次调用时拉取并启动 Action 运行时。

使用 JSON 请求体调用 Action：

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

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

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

网关返回 Action 结果：

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

### 返回自定义 HTTP 响应[​](#return-a-custom-http-response "返回自定义 HTTP 响应的直接链接")

创建一个返回状态、响应头和响应体的 Action：

custom-response.js

```
function main() {

  return {

    statusCode: 201,

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

    body: "created by OpenWhisk",

  };

}
```

部署 Action：

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

更新现有插件配置以调用新的 Action：

* Admin API
* ADC
* Ingress Controller

替换现有路由上的插件配置：

```
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
```

在 `adc.yaml` 中替换现有路由上的插件配置：

adc.yaml (route plugin)

```
plugins:

  openwhisk:

    api_host: "${OPENWHISK_API_HOST}"

    service_token: "${OPENWHISK_SERVICE_TOKEN}"

    namespace: "${OPENWHISK_NAMESPACE}"

    action: custom-response

    timeout: 60000
```

预览更新后的服务：

```
adc diff -f adc.yaml \

  --include-resource-type service \

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

检查 `diff.yaml`，然后同步带有该标签的服务：

```
adc sync -f adc.yaml \

  --include-resource-type service \

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

使用该部署所用的 Kubernetes API 更新现有插件配置：

* 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
```

应用更新后的插件配置：

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

再次发送请求：

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

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

  -d '{}'
```

Action 控制返回给客户端的响应：

```
HTTP/1.1 201 Created

X-Action: created

...

created by OpenWhisk
```
