mirror of https://github.com/knative/func.git
Updating to latest spring cloud release and refactoring code (#838)
* updating to latest spring cloud release and refactoring code * new health endpoints * implementing simplified echo function in template * adding banner * adding NL at EOF * updating native to release * fixing trailing whitespace * adding invocation back * adding Newline * whitespaces * fix lint and regenerate * regenerate zz filesystem after rebase
This commit is contained in:
parent
e5039891eb
commit
57909f71fe
|
@ -15,3 +15,4 @@ __pycache__
|
|||
|
||||
.vscode
|
||||
.idea
|
||||
.DS_Store
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
Welcome to your new Function project!
|
||||
|
||||
This sample project contains a single function based on Spring Cloud Function: `functions.CloudFunctionApplication.uppercase()`, which returns the uppercase of the data passed via CloudEvents.
|
||||
This sample project contains a single function based on Spring Cloud Function: `echo.EchoFunction`, which returns an echo of the data passed via CloudEvents.
|
||||
|
||||
## Local execution
|
||||
|
||||
|
@ -95,11 +95,11 @@ Using CloudEvents `Ce-Type` routing:
|
|||
curl -v "$URL/" \
|
||||
-H "Content-Type:application/json" \
|
||||
-H "Ce-Id:1" \
|
||||
-H "Ce-Subject:Uppercase" \
|
||||
-H "Ce-Subject:Echo" \
|
||||
-H "Ce-Source:cloud-event-example" \
|
||||
-H "Ce-Type:uppercase" \
|
||||
-H "Ce-Type:MyEvent" \
|
||||
-H "Ce-Specversion:1.0" \
|
||||
-d "{\"input\": \"$(whoami)\"}\""
|
||||
-d "hello"
|
||||
```
|
||||
|
||||
Using Path-Based routing:
|
||||
|
@ -107,11 +107,11 @@ Using Path-Based routing:
|
|||
curl -v "$URL/uppercase" \
|
||||
-H "Content-Type:application/json" \
|
||||
-H "Ce-Id:1" \
|
||||
-H "Ce-Subject:Uppercase" \
|
||||
-H "Ce-Subject:Echo" \
|
||||
-H "Ce-Source:cloud-event-example" \
|
||||
-H "Ce-Type:my-event" \
|
||||
-H "Ce-Type:MyEvent" \
|
||||
-H "Ce-Specversion:1.0" \
|
||||
-d "{\"input\": \"$(whoami)\"}\""
|
||||
-d "hello"
|
||||
```
|
||||
|
||||
### HTTPie
|
||||
|
@ -121,23 +121,23 @@ Using CloudEvents `Ce-Type` routing:
|
|||
http -v "$URL/" \
|
||||
Content-Type:application/json \
|
||||
Ce-Id:1 \
|
||||
Ce-Subject:Uppercase \
|
||||
Ce-Subject:Echo \
|
||||
Ce-Source:cloud-event-example \
|
||||
Ce-Type:uppercase \
|
||||
Ce-Type:MyEvent \
|
||||
Ce-Specversion:1.0 \
|
||||
input=$(whoami)
|
||||
hello
|
||||
```
|
||||
|
||||
Using Path-Based routing:
|
||||
```shell script
|
||||
http -v "$URL/uppercase" \
|
||||
http -v "$URL/echo" \
|
||||
Content-Type:application/json \
|
||||
Ce-Id:1 \
|
||||
Ce-Subject:Uppercase \
|
||||
Ce-Subject:Echo \
|
||||
Ce-Source:cloud-event-example \
|
||||
Ce-Type:uppercase \
|
||||
Ce-Type:MyEvent \
|
||||
Ce-Specversion:1.0 \
|
||||
input=$(whoami)
|
||||
hello
|
||||
```
|
||||
|
||||
## Cleanup
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
builders:
|
||||
default: gcr.io/paketo-buildpacks/builder:base
|
||||
buildEnvs:
|
||||
- name: BP_NATIVE_IMAGE
|
||||
value: "false"
|
||||
healthEndpoints:
|
||||
liveness: /actuator/health
|
||||
readiness: /actuator/health
|
||||
# optional. Invocation defines hints for how Functions created using this
|
||||
# template can be invoked. These settings can be updated on the resultant
|
||||
# Function as development progresses to ensure 'invoke' can always trigger the
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.3</version>
|
||||
<version>2.6.3</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
|
@ -18,10 +18,9 @@
|
|||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<spring-cloud.version>2020.0.4</spring-cloud.version>
|
||||
<spring-native.version>0.10.3</spring-native.version>
|
||||
<spring-cloud.version>2021.0.1</spring-cloud.version>
|
||||
<spring-native.version>0.11.3</spring-native.version>
|
||||
<compiler-plugin.version>3.8.1</compiler-plugin.version>
|
||||
<log4j2.version>2.16.0</log4j2.version>
|
||||
<maven.compiler.parameters>true</maven.compiler.parameters>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
|
@ -42,6 +41,10 @@
|
|||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
|
@ -75,10 +78,9 @@
|
|||
<configuration>
|
||||
<classifier>${repackage.classifier}</classifier>
|
||||
<image>
|
||||
<name>apps/native-image</name>
|
||||
<builder>paketobuildpacks/builder:tiny</builder>
|
||||
<env>
|
||||
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
|
||||
<BP_NATIVE_IMAGE>false</BP_NATIVE_IMAGE>
|
||||
</env>
|
||||
</image>
|
||||
</configuration>
|
||||
|
@ -102,41 +104,9 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Tests.java</include>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/Abstract*.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot-local</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/libs-milestone-local</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-releases</id>
|
||||
<name>Spring Releases</name>
|
||||
|
@ -147,46 +117,27 @@
|
|||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot-local</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/libs-milestone-local</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-releases</id>
|
||||
<name>Spring Releases</name>
|
||||
<url>https://repo.spring.io/libs-release-local</url>
|
||||
<url>https://repo.spring.io/release</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>native</id>
|
||||
<properties>
|
||||
<repackage.classifier>exec</repackage.classifier>
|
||||
<native-buildtools.version>0.9.0</native-buildtools.version>
|
||||
<native-buildtools.version>0.9.9</native-buildtools.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.graalvm.buildtools</groupId>
|
||||
<artifactId>junit-platform-native</artifactId>
|
||||
<version>${native-buildtools.version}</version>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-launcher</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -196,6 +147,7 @@
|
|||
<groupId>org.graalvm.buildtools</groupId>
|
||||
<artifactId>native-maven-plugin</artifactId>
|
||||
<version>${native-buildtools.version}</version>
|
||||
<extensions>true</extensions>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>test-native</id>
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package echo;
|
||||
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventMessageBuilder;
|
||||
import org.springframework.cloud.function.web.util.HeaderUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.*;
|
||||
|
||||
@Component("echo")
|
||||
public class EchoFunction implements Function<Message<String>, Message<String>> {
|
||||
private static final Logger LOGGER = Logger.getLogger(
|
||||
EchoFunction.class.getName());
|
||||
|
||||
@Override
|
||||
public Message<String> apply(Message<String> inputMessage) {
|
||||
/*
|
||||
* YOUR CODE HERE
|
||||
*
|
||||
* Try running `mvn test`. Add more test as you code in `test/java/echo/SpringCloudEventsApplicationTests`.
|
||||
*/
|
||||
HttpHeaders httpHeaders = HeaderUtils.fromMessage(inputMessage.getHeaders());
|
||||
|
||||
LOGGER.log(Level.INFO, "Input CE Id:{0}", httpHeaders.getFirst(ID));
|
||||
LOGGER.log(Level.INFO, "Input CE Spec Version:{0}", httpHeaders.getFirst(SPECVERSION));
|
||||
LOGGER.log(Level.INFO, "Input CE Source:{0}", httpHeaders.getFirst(SOURCE));
|
||||
LOGGER.log(Level.INFO, "Input CE Subject:{0}", httpHeaders.getFirst(SUBJECT));
|
||||
String input = inputMessage.getPayload();
|
||||
LOGGER.log(Level.INFO, "Input {0} ", input);
|
||||
|
||||
return CloudEventMessageBuilder.withData(input)
|
||||
.setType("Echo").setId(UUID.randomUUID().toString())
|
||||
.setSubject("Echo event")
|
||||
.setSource(URI.create("http://example.com/echo")).build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package echo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringCloudEventsApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringCloudEventsApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package functions;
|
||||
|
||||
public class Input {
|
||||
|
||||
public String input;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Input{" +
|
||||
"input='" + input + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package functions;
|
||||
|
||||
public class Output {
|
||||
|
||||
public String input;
|
||||
public String operation;
|
||||
public String output;
|
||||
public String error;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Output{" +
|
||||
"input='" + input + '\'' +
|
||||
", operation='" + operation + '\'' +
|
||||
", output='" + output + '\'' +
|
||||
", error='" + error + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package functions;
|
||||
|
||||
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.ID;
|
||||
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SOURCE;
|
||||
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SPECVERSION;
|
||||
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SUBJECT;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.function.cloudevent.CloudEventHeaderEnricher;
|
||||
import org.springframework.cloud.function.web.util.HeaderUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringCloudEventsApplication {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(
|
||||
SpringCloudEventsApplication.class.getName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringCloudEventsApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Message<Input>, Output> uppercase(CloudEventHeaderEnricher enricher) {
|
||||
return m -> {
|
||||
HttpHeaders httpHeaders = HeaderUtils.fromMessage(m.getHeaders());
|
||||
|
||||
LOGGER.log(Level.INFO, "Input CE Id:{0}", httpHeaders.getFirst(
|
||||
ID));
|
||||
LOGGER.log(Level.INFO, "Input CE Spec Version:{0}",
|
||||
httpHeaders.getFirst(SPECVERSION));
|
||||
LOGGER.log(Level.INFO, "Input CE Source:{0}",
|
||||
httpHeaders.getFirst(SOURCE));
|
||||
LOGGER.log(Level.INFO, "Input CE Subject:{0}",
|
||||
httpHeaders.getFirst(SUBJECT));
|
||||
|
||||
Input input = m.getPayload();
|
||||
LOGGER.log(Level.INFO, "Input {0} ", input);
|
||||
Output output = new Output();
|
||||
output.input = input.input;
|
||||
output.operation = httpHeaders.getFirst(SUBJECT);
|
||||
output.output = input.input != null ? input.input.toUpperCase() : "NO DATA";
|
||||
return output;
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloudEventHeaderEnricher attributesProvider() {
|
||||
return attributes -> attributes
|
||||
.setSpecVersion("1.0")
|
||||
.setId(UUID.randomUUID()
|
||||
.toString())
|
||||
.setSource("http://example.com/uppercase")
|
||||
.setType("com.redhat.faas.springboot.events");
|
||||
}
|
||||
|
||||
/**
|
||||
* Health checks
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Function<String, String> health() {
|
||||
return probe -> {
|
||||
if ("readiness".equals(probe)) {
|
||||
return "ready";
|
||||
} else if ("liveness".equals(probe)) {
|
||||
return "live";
|
||||
} else {
|
||||
return "OK";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package functions;
|
||||
package echo;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
@ -30,26 +30,21 @@ public class SpringCloudEventsApplicationTests {
|
|||
@Autowired
|
||||
private TestRestTemplate rest;
|
||||
|
||||
@Autowired
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
public void testUpperCaseJsonInput() throws Exception {
|
||||
public void testEchoInput() throws Exception {
|
||||
|
||||
Input input = new Input();
|
||||
|
||||
input.input = "hello";
|
||||
String input ="hello";
|
||||
|
||||
HttpHeaders ceHeaders = new HttpHeaders();
|
||||
ceHeaders.add(SPECVERSION, "1.0");
|
||||
ceHeaders.add(ID, UUID.randomUUID()
|
||||
.toString());
|
||||
ceHeaders.add(TYPE, "com.redhat.faas.springboot.test");
|
||||
ceHeaders.add(SOURCE, "http://localhost:8080/uppercase");
|
||||
ceHeaders.add(SUBJECT, "Convert to UpperCase");
|
||||
ceHeaders.add(TYPE, "MyEvent");
|
||||
ceHeaders.add(SOURCE, "http://localhost:8080/echo");
|
||||
ceHeaders.add(SUBJECT, "Echo content");
|
||||
|
||||
ResponseEntity<String> response = this.rest.exchange(
|
||||
RequestEntity.post(new URI("/uppercase"))
|
||||
RequestEntity.post(new URI("/echo"))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.headers(ceHeaders)
|
||||
.body(input),
|
||||
|
@ -59,12 +54,33 @@ public class SpringCloudEventsApplicationTests {
|
|||
.value(), equalTo(200));
|
||||
String body = response.getBody();
|
||||
assertThat(body, notNullValue());
|
||||
Output output = objectMapper.readValue(body,
|
||||
Output.class);
|
||||
assertThat(output, notNullValue());
|
||||
assertThat(output.input, equalTo("hello"));
|
||||
assertThat(output.operation, equalTo("Convert to UpperCase"));
|
||||
assertThat(output.output, equalTo("HELLO"));
|
||||
assertThat(output.error, nullValue());
|
||||
assertThat(body, equalTo(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEchoRoutingBasedOnType() throws Exception {
|
||||
|
||||
String input ="hello";
|
||||
|
||||
HttpHeaders ceHeaders = new HttpHeaders();
|
||||
ceHeaders.add(SPECVERSION, "1.0");
|
||||
ceHeaders.add(ID, UUID.randomUUID()
|
||||
.toString());
|
||||
ceHeaders.add(TYPE, "MyEvent");
|
||||
ceHeaders.add(SOURCE, "http://localhost:8080/echo");
|
||||
ceHeaders.add(SUBJECT, "Echo content");
|
||||
|
||||
ResponseEntity<String> response = this.rest.exchange(
|
||||
RequestEntity.post(new URI("/"))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.headers(ceHeaders)
|
||||
.body(input),
|
||||
String.class);
|
||||
|
||||
assertThat(response.getStatusCode()
|
||||
.value(), equalTo(200));
|
||||
String body = response.getBody();
|
||||
assertThat(body, notNullValue());
|
||||
assertThat(body, equalTo(input));
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue