Skip to content

Commit 0b5586e

Browse files
authored
add support OpenFunction v1beta2 API (#15)
* add support OpenFunction v1beta2 API Signed-off-by: wanjunlei <[email protected]> * add support OpenFunction v1beta2 API Signed-off-by: wanjunlei <[email protected]> --------- Signed-off-by: wanjunlei <[email protected]>
1 parent 88acc5c commit 0b5586e

File tree

21 files changed

+601
-192
lines changed

21 files changed

+601
-192
lines changed

functions-framework-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
<groupId>dev.openfunction.functions</groupId>
4040
<artifactId>functions-framework-api</artifactId>
41-
<version>1.1.0-SNAPSHOT</version>
41+
<version>1.2.0-SNAPSHOT</version>
4242

4343
<properties>
4444
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

functions-framework-api/src/main/java/dev/openfunction/functions/Component.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,35 @@
1616

1717
package dev.openfunction.functions;
1818

19+
import org.apache.commons.lang3.StringUtils;
20+
1921
import java.util.Map;
22+
import java.util.Objects;
2023

2124
public class Component {
25+
private static final String ComponentTypeBinding = "bindings";
26+
private static final String ComponentTypePubsub = "pubsub";
27+
28+
@Deprecated
2229
private String uri;
30+
private String topic;
2331
private String componentName;
2432
private String componentType;
2533
private Map<String, String> metadata;
2634
private String operation;
2735

28-
36+
@Deprecated
2937
public String getUri() {
30-
return uri;
38+
if (!StringUtils.isBlank(uri)) {
39+
return uri;
40+
} else if (!StringUtils.isBlank(topic)) {
41+
return topic;
42+
} else {
43+
return componentName;
44+
}
3145
}
3246

47+
@Deprecated
3348
public void setUri(String uri) {
3449
this.uri = uri;
3550
}
@@ -65,5 +80,27 @@ public String getOperation() {
6580
public void setOperation(String operation) {
6681
this.operation = operation;
6782
}
83+
84+
public String getTopic() {
85+
if (StringUtils.isNotBlank(topic)) {
86+
return topic;
87+
} else if (StringUtils.isNotBlank(uri) && !Objects.equals(uri, componentName)) {
88+
return uri;
89+
} else {
90+
return null;
91+
}
92+
}
93+
94+
public void setTopic(String topic) {
95+
this.topic = topic;
96+
}
97+
98+
public boolean isPubsub() {
99+
return componentType.startsWith(ComponentTypePubsub);
100+
}
101+
public boolean isBinding() {
102+
return componentType.startsWith(ComponentTypeBinding);
103+
}
104+
68105
}
69106

functions-framework-api/src/main/java/dev/openfunction/functions/Context.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public interface Context {
3232
* @param data Data String
3333
* @return Error
3434
*/
35+
@Deprecated
3536
Error send(String outputName, String data);
3637

3738
/**
@@ -90,6 +91,13 @@ public interface Context {
9091
*/
9192
String getHttpPattern();
9293

94+
/**
95+
* getInputs returns the inputs of function.
96+
*
97+
* @return Inputs
98+
*/
99+
Map<String, Component> getInputs();
100+
93101
/**
94102
* getOutputs returns the Outputs of function.
95103
*
@@ -112,4 +120,6 @@ public interface Context {
112120
* @return Dapr client
113121
*/
114122
DaprClient getDaprClient();
123+
124+
CloudEvent packageAsCloudevent(String payload);
115125
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2022 The OpenFunction Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package dev.openfunction.functions;
18+
19+
import java.util.Map;
20+
21+
public interface Hook {
22+
/**
23+
* name return the name of this plugin.
24+
*
25+
* @return Plugin name
26+
*/
27+
String name();
28+
29+
/**
30+
* version return the version of this plugin.
31+
*
32+
* @return Plugin name
33+
*/
34+
String version();
35+
36+
/**
37+
* init will create a new plugin, and execute hook in this calling.
38+
* If you do not want to use a new plugin to execute hook, just return `this`.
39+
*
40+
* @return Plugin
41+
*/
42+
Hook init();
43+
44+
/**
45+
* execute executes the hook.
46+
*
47+
* @param ctx Runtime context
48+
* @return error
49+
*/
50+
Error execute(Context ctx);
51+
52+
Boolean needToTracing();
53+
54+
Map<String, String> tagsAddToTracing();
55+
}

functions-framework-api/src/main/java/dev/openfunction/functions/Plugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
@Deprecated
2122
public interface Plugin {
2223
/**
2324
* name return the name of this plugin.

functions-framework-invoker/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>dev.openfunction.functions</groupId>
1212
<artifactId>functions-framework-invoker</artifactId>
13-
<version>1.1.0-SNAPSHOT</version>
13+
<version>1.2.0-SNAPSHOT</version>
1414

1515
<properties>
1616
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
@@ -60,7 +60,7 @@
6060
<dependency>
6161
<groupId>dev.openfunction.functions</groupId>
6262
<artifactId>functions-framework-api</artifactId>
63-
<version>1.1.0-SNAPSHOT</version>
63+
<version>1.2.0-SNAPSHOT</version>
6464
</dependency>
6565
<dependency>
6666
<groupId>io.cloudevents</groupId>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package dev.openfunction.invoker.runtime;
17+
package dev.openfunction.invoker;
1818

1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.databind.JsonNode;

functions-framework-invoker/src/main/java/dev/openfunction/invoker/Runner.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919

2020
import dev.openfunction.invoker.context.RuntimeContext;
21-
import dev.openfunction.invoker.runtime.AsynchronousRuntime;
22-
import dev.openfunction.invoker.runtime.Runtime;
23-
import dev.openfunction.invoker.runtime.SynchronizeRuntime;
21+
import dev.openfunction.invoker.trigger.DaprTrigger;
22+
import dev.openfunction.invoker.trigger.HttpTrigger;
23+
import dev.openfunction.invoker.trigger.Trigger;
24+
import org.apache.commons.lang3.StringUtils;
2425

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

4445
private static final String FunctionContext = "FUNC_CONTEXT";
46+
private static final String FunctionContextV1beta2 = "FUNC_CONTEXT_V1BETA2";
4547
private static final String FunctionTarget = "FUNCTION_TARGET";
4648
private static final String FunctionClasspath = "FUNCTION_CLASSPATH";
4749

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

56-
if (!System.getenv().containsKey(FunctionContext)) {
57-
throw new Error(FunctionContext + " not set");
58+
String functionContext = "";
59+
if (System.getenv().containsKey(FunctionContext)) {
60+
functionContext = System.getenv(FunctionContext);
61+
}
62+
63+
if (System.getenv().containsKey(FunctionContextV1beta2)) {
64+
functionContext = System.getenv(FunctionContextV1beta2);
65+
}
66+
67+
if (StringUtils.isEmpty(functionContext)) {
68+
throw new Error("Function context not set");
5869
}
59-
String functionContext = System.getenv(FunctionContext);
6070

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

65-
Runtime runtime;
6675
Class<?>[] functionClasses = loadTargets(target, functionClassLoader);
67-
if (Objects.equals(runtimeContext.getRuntime(), RuntimeContext.SyncRuntime)) {
68-
runtime = new SynchronizeRuntime(runtimeContext, functionClasses);
69-
} else if (Objects.equals(runtimeContext.getRuntime(), RuntimeContext.AsyncRuntime)) {
70-
runtime = new AsynchronousRuntime(runtimeContext, functionClasses);
71-
} else {
72-
throw new Exception("Unknown runtime");
76+
Set<Trigger> triggers = new HashSet<>();
77+
if (runtimeContext.hasHttpTrigger()) {
78+
triggers.add(new HttpTrigger(runtimeContext, functionClasses));
79+
}
80+
81+
if (runtimeContext.hasDaprTrigger()) {
82+
triggers.add(new DaprTrigger(runtimeContext, functionClasses));
7383
}
7484

75-
runtime.start();
85+
for (Trigger trigger : triggers) {
86+
trigger.start();
87+
}
7688
} catch (Exception e) {
7789
logger.log(Level.SEVERE, "Failed to run function", e);
7890
e.printStackTrace();
@@ -82,7 +94,7 @@ public static void main(String[] args) {
8294
private static Class<?>[] loadTargets(String target, ClassLoader functionClassLoader) throws ClassNotFoundException {
8395
String[] targets = target.split(",");
8496
Class<?>[] classes = new Class<?>[targets.length];
85-
for (int i=0; i < targets.length; i++) {
97+
for (int i = 0; i < targets.length; i++) {
8698
classes[i] = functionClassLoader.loadClass(targets[i]);
8799
}
88100

0 commit comments

Comments
 (0)