Skip to content

Commit

Permalink
add support OpenFunction v1beta2 API (OpenFunction#15)
Browse files Browse the repository at this point in the history
* add support OpenFunction v1beta2 API

Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>

* add support OpenFunction v1beta2 API

Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>

---------

Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
  • Loading branch information
wanjunlei committed Jun 12, 2023
1 parent 88acc5c commit 0b5586e
Show file tree
Hide file tree
Showing 21 changed files with 601 additions and 192 deletions.
2 changes: 1 addition & 1 deletion functions-framework-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<groupId>dev.openfunction.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,35 @@

package dev.openfunction.functions;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

public class Component {
private static final String ComponentTypeBinding = "bindings";
private static final String ComponentTypePubsub = "pubsub";

@Deprecated
private String uri;
private String topic;
private String componentName;
private String componentType;
private Map<String, String> metadata;
private String operation;


@Deprecated
public String getUri() {
return uri;
if (!StringUtils.isBlank(uri)) {
return uri;
} else if (!StringUtils.isBlank(topic)) {
return topic;
} else {
return componentName;
}
}

@Deprecated
public void setUri(String uri) {
this.uri = uri;
}
Expand Down Expand Up @@ -65,5 +80,27 @@ public String getOperation() {
public void setOperation(String operation) {
this.operation = operation;
}

public String getTopic() {
if (StringUtils.isNotBlank(topic)) {
return topic;
} else if (StringUtils.isNotBlank(uri) && !Objects.equals(uri, componentName)) {
return uri;
} else {
return null;
}
}

public void setTopic(String topic) {
this.topic = topic;
}

public boolean isPubsub() {
return componentType.startsWith(ComponentTypePubsub);
}
public boolean isBinding() {
return componentType.startsWith(ComponentTypeBinding);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface Context {
* @param data Data String
* @return Error
*/
@Deprecated
Error send(String outputName, String data);

/**
Expand Down Expand Up @@ -90,6 +91,13 @@ public interface Context {
*/
String getHttpPattern();

/**
* getInputs returns the inputs of function.
*
* @return Inputs
*/
Map<String, Component> getInputs();

/**
* getOutputs returns the Outputs of function.
*
Expand All @@ -112,4 +120,6 @@ public interface Context {
* @return Dapr client
*/
DaprClient getDaprClient();

CloudEvent packageAsCloudevent(String payload);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2022 The OpenFunction Authors.
Licensed 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 dev.openfunction.functions;

import java.util.Map;

public interface Hook {
/**
* name return the name of this plugin.
*
* @return Plugin name
*/
String name();

/**
* version return the version of this plugin.
*
* @return Plugin name
*/
String version();

/**
* init will create a new plugin, and execute hook in this calling.
* If you do not want to use a new plugin to execute hook, just return `this`.
*
* @return Plugin
*/
Hook init();

/**
* execute executes the hook.
*
* @param ctx Runtime context
* @return error
*/
Error execute(Context ctx);

Boolean needToTracing();

Map<String, String> tagsAddToTracing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Map;

@Deprecated
public interface Plugin {
/**
* name return the name of this plugin.
Expand Down
4 changes: 2 additions & 2 deletions functions-framework-invoker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>dev.openfunction.functions</groupId>
<artifactId>functions-framework-invoker</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<properties>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
Expand Down Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>dev.openfunction.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package dev.openfunction.invoker.runtime;
package dev.openfunction.invoker;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@


import dev.openfunction.invoker.context.RuntimeContext;
import dev.openfunction.invoker.runtime.AsynchronousRuntime;
import dev.openfunction.invoker.runtime.Runtime;
import dev.openfunction.invoker.runtime.SynchronizeRuntime;
import dev.openfunction.invoker.trigger.DaprTrigger;
import dev.openfunction.invoker.trigger.HttpTrigger;
import dev.openfunction.invoker.trigger.Trigger;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
Expand All @@ -42,6 +43,7 @@ public class Runner {
private static final Logger logger = Logger.getLogger(Runner.class.getName());

private static final String FunctionContext = "FUNC_CONTEXT";
private static final String FunctionContextV1beta2 = "FUNC_CONTEXT_V1BETA2";
private static final String FunctionTarget = "FUNCTION_TARGET";
private static final String FunctionClasspath = "FUNCTION_CLASSPATH";

Expand All @@ -53,26 +55,36 @@ public static void main(String[] args) {
}
String target = System.getenv(FunctionTarget);

if (!System.getenv().containsKey(FunctionContext)) {
throw new Error(FunctionContext + " not set");
String functionContext = "";
if (System.getenv().containsKey(FunctionContext)) {
functionContext = System.getenv(FunctionContext);
}

if (System.getenv().containsKey(FunctionContextV1beta2)) {
functionContext = System.getenv(FunctionContextV1beta2);
}

if (StringUtils.isEmpty(functionContext)) {
throw new Error("Function context not set");
}
String functionContext = System.getenv(FunctionContext);

String classPath = System.getenv().getOrDefault(FunctionClasspath, System.getProperty("user.dir") + "/*");
ClassLoader functionClassLoader = new URLClassLoader(classpathToUrls(classPath));
RuntimeContext runtimeContext = new RuntimeContext(functionContext, functionClassLoader);

Runtime runtime;
Class<?>[] functionClasses = loadTargets(target, functionClassLoader);
if (Objects.equals(runtimeContext.getRuntime(), RuntimeContext.SyncRuntime)) {
runtime = new SynchronizeRuntime(runtimeContext, functionClasses);
} else if (Objects.equals(runtimeContext.getRuntime(), RuntimeContext.AsyncRuntime)) {
runtime = new AsynchronousRuntime(runtimeContext, functionClasses);
} else {
throw new Exception("Unknown runtime");
Set<Trigger> triggers = new HashSet<>();
if (runtimeContext.hasHttpTrigger()) {
triggers.add(new HttpTrigger(runtimeContext, functionClasses));
}

if (runtimeContext.hasDaprTrigger()) {
triggers.add(new DaprTrigger(runtimeContext, functionClasses));
}

runtime.start();
for (Trigger trigger : triggers) {
trigger.start();
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to run function", e);
e.printStackTrace();
Expand All @@ -82,7 +94,7 @@ public static void main(String[] args) {
private static Class<?>[] loadTargets(String target, ClassLoader functionClassLoader) throws ClassNotFoundException {
String[] targets = target.split(",");
Class<?>[] classes = new Class<?>[targets.length];
for (int i=0; i < targets.length; i++) {
for (int i = 0; i < targets.length; i++) {
classes[i] = functionClassLoader.loadClass(targets[i]);
}

Expand Down
Loading

0 comments on commit 0b5586e

Please sign in to comment.