Skip to content

Commit

Permalink
feat: add openfunction plugin (#7634)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Zhang <tokers@apache.org>
Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
Co-authored-by: Fei Han <97138894+hf400159@users.noreply.github.com>
Co-authored-by: LCW <Lcw769193516@163.com>
  • Loading branch information
5 people authored Sep 2, 2022
1 parent 8593900 commit 47187fa
Show file tree
Hide file tree
Showing 19 changed files with 872 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ jobs:
- name: Start CI env (PLUGIN_TEST)
if: steps.test_env.outputs.type == 'plugin'
run: |
sh ci/pod/openfunction/build-function-image.sh
make ci-env-up project_compose_ci=ci/pod/docker-compose.${{ steps.test_env.outputs.type }}.yml
sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/centos7-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
- name: Start CI env (PLUGIN_TEST)
if: steps.test_env.outputs.type == 'plugin'
run: |
sh ci/pod/openfunction/build-function-image.sh
make ci-env-up project_compose_ci=ci/pod/docker-compose.${{ steps.test_env.outputs.type }}.yml
./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh
Expand Down
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,4 @@ The following components are provided under the Apache License. See project link
The text of each license is the standard Apache 2.0 license.

ewma.lua file from kubernetes/ingress-nginx: https://github.com/kubernetes/ingress-nginx Apache 2.0
hello.go file from OpenFunction/samples: https://github.com/OpenFunction/samples Apache 2.0
35 changes: 35 additions & 0 deletions apisix/plugins/openfunction.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--
-- 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 ngx_encode_base64 = ngx.encode_base64
local plugin_name, plugin_version, priority = "openfunction", 0.1, -1902

local openfunction_authz_schema = {
service_token = {type = "string"}
}

local function request_processor(conf, ctx, params)
local headers = params.headers or {}
-- setting authorization headers if authorization.service_token exists
if conf.authorization and conf.authorization.service_token then
headers["authorization"] = "Basic " .. ngx_encode_base64(conf.authorization.service_token)
end

params.headers = headers
end

return require("apisix.plugins.serverless.generic-upstream")(plugin_name,
plugin_version, priority, request_processor, openfunction_authz_schema)
29 changes: 29 additions & 0 deletions ci/pod/docker-compose.plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ services:
xpack.security.enabled: 'true'


# The function services of OpenFunction
test-header:
image: test-header-image:latest
restart: unless-stopped
ports:
- "30583:8080"
environment:
CONTEXT_MODE: "self-host"
FUNC_CONTEXT: "{\"name\":\"HelloWorld\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}"

test-uri:
image: test-uri-image:latest
restart: unless-stopped
ports:
- "30584:8080"
environment:
CONTEXT_MODE: "self-host"
FUNC_CONTEXT: "{\"name\":\"HelloWorld\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}"

test-body:
image: test-body-image:latest
restart: unless-stopped
ports:
- "30585:8080"
environment:
CONTEXT_MODE: "self-host"
FUNC_CONTEXT: "{\"name\":\"HelloWorld\",\"version\":\"v1.0.0\",\"port\":\"8080\",\"runtime\":\"Knative\"}"


networks:
apisix_net:
kafka_net:
Expand Down
25 changes: 25 additions & 0 deletions ci/pod/openfunction/build-function-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
#
# 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.
#

wget https://github.com/buildpacks/pack/releases/download/v0.27.0/pack-v0.27.0-linux.tgz
tar -zxvf pack-v0.27.0-linux.tgz

# please update function-example/*/hello.go if you want to update function
./pack build test-uri-image --path ./ci/pod/openfunction/function-example/test-uri --builder openfunction/builder-go:v2.4.0-1.17 --env FUNC_NAME="HelloWorld" --env FUNC_CLEAR_SOURCE=true --env FUNC_GOPROXY="https://goproxy.cn"
./pack build test-body-image --path ./ci/pod/openfunction/function-example/test-body --builder openfunction/builder-go:v2.4.0-1.17 --env FUNC_NAME="HelloWorld" --env FUNC_CLEAR_SOURCE=true --env FUNC_GOPROXY="https://goproxy.cn"
./pack build test-header-image --path ./ci/pod/openfunction/function-example/test-header --builder openfunction/builder-go:v2.4.0-1.17 --env FUNC_NAME="HelloWorld" --env FUNC_CLEAR_SOURCE=true --env FUNC_GOPROXY="https://goproxy.cn"
5 changes: 5 additions & 0 deletions ci/pod/openfunction/function-example/test-body/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module example.com/hello

go 1.16

require github.com/OpenFunction/functions-framework-go v0.3.0
36 changes: 36 additions & 0 deletions ci/pod/openfunction/function-example/test-body/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 The OpenFunction Authors.
*
* 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.
*/

package hello

import (
"fmt"
"net/http"
"io/ioutil"
"github.com/OpenFunction/functions-framework-go/functions"
)

func init() {
functions.HTTP("HelloWorld", HelloWorld)
}

func HelloWorld(w http.ResponseWriter, r *http.Request) {
body,_ := ioutil.ReadAll(r.Body)
fmt.Fprintf(w, "Hello, %s!\n", string(body))
}
3 changes: 3 additions & 0 deletions ci/pod/openfunction/function-example/test-header/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/hello

go 1.16
30 changes: 30 additions & 0 deletions ci/pod/openfunction/function-example/test-header/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2022 The OpenFunction Authors.
*
* 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.
*/

package hello

import (
"fmt"
"net/http"
)

func HelloWorld(w http.ResponseWriter, r *http.Request) {
header := r.Header
fmt.Fprintf(w, "%s", header["Authorization"])
}
5 changes: 5 additions & 0 deletions ci/pod/openfunction/function-example/test-uri/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module example.com/hello

go 1.17

require github.com/OpenFunction/functions-framework-go v0.4.0
38 changes: 38 additions & 0 deletions ci/pod/openfunction/function-example/test-uri/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 The OpenFunction Authors.
*
* 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.
*/

package hello

import (
"fmt"
ofctx "github.com/OpenFunction/functions-framework-go/context"
"net/http"

"github.com/OpenFunction/functions-framework-go/functions"
)

func init() {
functions.HTTP("HelloWorld", HelloWorld,
functions.WithFunctionPath("/{greeting}"))
}

func HelloWorld(w http.ResponseWriter, r *http.Request) {
vars := ofctx.VarsFromCtx(r.Context())
fmt.Fprintf(w, "Hello, %s!\n", vars["greeting"])
}
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ plugins: # plugin list (sorted by priority)
- aws-lambda # priority: -1899
- azure-functions # priority: -1900
- openwhisk # priority: -1901
- openfunction # priority: -1902
- serverless-post-function # priority: -2000
- ext-plugin-post-req # priority: -3000
- ext-plugin-post-resp # priority: -4000
Expand Down
3 changes: 2 additions & 1 deletion docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
"plugins/azure-functions",
"plugins/openwhisk",
"plugins/aws-lambda",
"plugins/workflow"
"plugins/workflow",
"plugins/openfunction"
]
},
{
Expand Down
Loading

0 comments on commit 47187fa

Please sign in to comment.