You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/11/22 08:15:18 UTC

[GitHub] [apisix] membphis commented on a change in pull request #5518: feat: add Apache OpenWhisk plugin

membphis commented on a change in pull request #5518:
URL: https://github.com/apache/apisix/pull/5518#discussion_r754027296



##########
File path: apisix/core/request.lua
##########
@@ -285,4 +285,9 @@ function _M.get_http_version()
     return ngx.req.http_version()
 end
 
+
+function _M.get_method()

Review comment:
       style:
   
   `_M.get_method = ngx.req.get_method` is better style.

##########
File path: docs/en/latest/plugins/openwhisk.md
##########
@@ -0,0 +1,107 @@
+---
+title: serverless
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Description**](#description)
+- [**Attributes**](#attributes)
+- [**Example**](#example)
+- [**Notice**](#Notice)
+
+## Description
+
+The `openwhisk` plugin is used to support integration with the [Apache OpenWhisk](https://openwhisk.apache.org) serverless platform and can be set up on a route in place of Upstream, which will take over the request and send it to the OpenWhisk API endpoint.
+
+Users can call the OpenWhisk action via APISIX, pass the request parameters via JSON and get the response content.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| -- | -- | -- | -- | -- | -- |
+| api_host | string | yes |   |   | OpenWhisk API host (eg. https://localhost:3233) |
+| ssl_verify | bool | no | true |   | Whether to verify the certificate |
+| service_token | string | yes |   |   | OpenWhisk ServiceToken (The format is `xxx:xxx`,Passed through Basic Auth when calling the API) |
+| namespace | string | yes |   |   | OpenWhisk  Namespace (eg. guest) |
+| action | string | yes |   |   | OpenWhisk Action (eg. hello) |
+| result | bool | no | true |   | Whether to get Action metadata (default to execute function and get response; false to get Action metadata but not execute Action, including runtime, function body, restrictions, etc.) |
+| timeout | integer | no | 60000ms | [1, 60000]ms | OpenWhisk Action and HTTP call timeout. |
+| keepalive | bool | no | true |   | HTTP keepalive |
+| keepalive_timeout | integer | no | 60000ms |   | keepalive idle timeout |
+| keepalive_pool | integer | no | 5 |   | Connection pool limit |
+
+:::note
+
+- The `timeout` property controls both the time taken by the OpenWhisk Action to execute and the timeout of the HTTP client in APISIX. OpenWhisk Action calls may consume time on pulling the runtime image and starting the container, so if you set the value too small, you may cause a large number of requests to fail. OpenWhisk supports timeouts ranging from 1ms to 60000ms, and we recommended to set at least 1000ms or more.
+
+:::
+
+## Example
+
+First, you need to run the OpenWhisk environment. Here is an example of using OpenWhisk standalone mode.
+
+```shell
+docker run --rm -d \
+  -h openwhisk --name openwhisk \
+  -p 3233:3233 -p 3232:3232 \
+  -v /var/run/docker.sock:/var/run/docker.sock \
+  openwhisk/standalone:nightly
+docker exec openwhisk waitready
+```
+
+Then, you need to create an Action for testing.
+
+```shell
+wsk property set --apihost "http://localhost:3233" --auth "23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP"
+wsk action update test <(echo 'function main(){return {"ready":true}}') --kind nodejs:14
+```
+
+Here is an example of creating a Route and enabling this plugin
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "uri": "/hello",
+    "plugins": {
+        "openwhisk": {
+            "api_host": "http://localhost:3233",
+            "service_token": "23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP",
+            "namespace": "guest",
+            "action": "test"
+        }
+    },
+    "upstream": {

Review comment:
       we should remove `upstream`

##########
File path: docs/zh/latest/plugins/openwhisk.md
##########
@@ -0,0 +1,107 @@
+---
+title: openwhisk
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**描述**](#描述)
+- [**属性**](#属性)
+- [**使用示例**](#使用示例)
+- [**注意**](#注意)
+
+## 描述
+
+这个插件是用于支持集成 [Apache OpenWhisk](https://openwhisk.apache.org) 无服务器平台的插件,它可以被设置在路由上以替代 Upstream,其将接管请求并发送至 OpenWhisk 的 API 端点。
+
+用户可以通过 APISIX 调用 OpenWhisk action,通过 JSON 传递请求参数并获取响应内容。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| -- | -- | -- | -- | -- | -- |
+| api_host | string | 是 |   |   | OpenWhisk API 地址(例: https://localhost:3233) |
+| ssl_verify | bool | 否 | true |   | 是否认证证书 |
+| service_token | string | 是 |   |   | OpenWhisk 服务令牌 (格式为 `xxx:xxx`,调用 API 时通过 Basic Auth 传递) |
+| namespace | string | 是 |   |   | OpenWhisk 命名空间 |
+| action | string | 是 |   |   | OpenWhisk Action 名称(例:hello) |
+| result | bool | 否 | true |   | 是否获取 Action 元数据(默认为执行函数并获取响应值;设置为 false 时,获取元数据,包含运行时、程序内容、限制等) |
+| timeout | integer | 否 | 60000 |   | OpenWhisk Action 和 HTTP 的超时时间 (ms) |
+| keepalive | bool | 否 | true |   | 是否启用 HTTP 长连接以避免过多的请求 |
+| keepalive_timeout | integer | 否 | 60000 |   | HTTP 长连接超时时间 (ms) |
+| keepalive_pool | integer | 否 | 5 |   | 连接池连接数限制 |
+
+:::note
+
+- `timeout` 属性同时控制 OpenWhisk Action 执行耗时和 APISIX 中 HTTP 客户端的超时时间。其中 OpenWhisk Action 调用时有可能会进行拉去运行时镜像和启动容器的工作,因此如果你设置的值过小,将可能导致大量请求失败。OpenWhisk 支持的超时时间范围为1ms至60000ms,建议至少设置为1000ms以上。
+
+:::
+
+## 使用示例
+
+首先,你需要运行起 OpenWhisk 环境,以下是一个使用 OpenWhisk 独立模式的示例
+
+```shell
+docker run --rm -d \
+  -h openwhisk --name openwhisk \
+  -p 3233:3233 -p 3232:3232 \
+  -v /var/run/docker.sock:/var/run/docker.sock \
+  openwhisk/standalone:nightly
+docker exec openwhisk waitready
+```
+
+之后,你需要创建一个 Action 用于测试
+
+```shell
+wsk property set --apihost "http://localhost:3233" --auth "23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP"
+wsk action update test <(echo 'function main(){return {"ready":true}}') --kind nodejs:14
+```
+
+以下是一个示例,创建 Route 并启用此插件
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "uri": "/hello",
+    "plugins": {
+        "openwhisk": {
+            "api_host": "http://localhost:3233",
+            "service_token": "23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP",
+            "namespace": "guest",
+            "action": "test"
+        }
+    },
+    "upstream": {

Review comment:
       ditto

##########
File path: apisix/plugins/openwhisk.lua
##########
@@ -0,0 +1,121 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core              = require("apisix.core")
+local http              = require("resty.http")
+local ngx_encode_base64 = ngx.encode_base64
+local tostring          = tostring
+
+local schema = {
+    type = "object",
+    properties = {
+        api_host = {type = "string"},
+        ssl_verify = {
+            type = "boolean",
+            default = true,
+        },
+        service_token = {type = "string"},
+        namespace = {type = "string"},
+        action = {type = "string"},
+        result = {
+            type = "boolean",
+            default = true,
+        },
+        timeout = {
+            type = "integer",
+            minimum = 1000,
+            maximum = 60000,
+            default = 3000,
+            description = "timeout in milliseconds",
+        },
+        keepalive = {type = "boolean", default = true},
+        keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+        keepalive_pool = {type = "integer", minimum = 1, default = 5}
+    },
+    required = {"api_host", "service_token", "namespace", "action"}
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 601,
+    name = "openwhisk-serverless",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return false, err
+    end
+
+    return true
+end
+
+
+function _M.access(conf, ctx)
+    if core.request.get_method() == "POST" then
+        if core.request.header(ctx, "Content-Type") ~= "application/json" then
+            core.log.error("only support json request body")
+            return 400, "only support json request body"
+        end
+    end
+
+    local params = {
+        method = "POST",
+        body = core.request.get_body(),
+        query = {
+            blocking = "true",
+            result = tostring(conf.result),
+            timeout = conf.timeout
+        },
+        headers = {
+            ["Authorization"] = "Basic " .. ngx_encode_base64(conf.service_token),
+            ["Content-Type"] = "application/json",
+        },
+        keepalive = conf.keepalive,
+        ssl_verify = conf.ssl_verify
+    }
+
+    if conf.keepalive then
+        params.keepalive_timeout = conf.keepalive_timeout
+        params.keepalive_pool = conf.keepalive_pool
+    end
+
+    -- OpenWhisk action endpoint
+    local endpoint = conf.api_host .. "/api/v1/namespaces/" .. conf.namespace ..

Review comment:
       current way is better




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org