Skip to content

Commit

Permalink
Update tanzu-java-web-app for TP for K8s (#520)
Browse files Browse the repository at this point in the history
* Update tanzu-java-web-app for TP for K8s

- also update the imported fragments to use DSL
  • Loading branch information
trisberg authored Sep 12, 2024
1 parent 88a4d78 commit 3890d31
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 405 deletions.
7 changes: 7 additions & 0 deletions fragments/build-wrapper-gradle/accelerator.axl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
engine {

if (#includeBuildToolWrapper) {
Include({"gradlew*", "gradle/**"})
}

}
5 changes: 0 additions & 5 deletions fragments/build-wrapper-gradle/accelerator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ accelerator:
label: Include Build Tool wrapper support
dataType: boolean
defaultValue: true

engine:
merge:
- condition: "#includeBuildToolWrapper"
include: [ "gradlew*", "gradle/**" ]
Binary file modified fragments/build-wrapper-gradle/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
# 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.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
7 changes: 7 additions & 0 deletions fragments/build-wrapper-maven/accelerator.axl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
engine {

if (#includeBuildToolWrapper) {
Include({"mvnw", "mvnw.cmd", ".mvn/**"})
}

}
5 changes: 0 additions & 5 deletions fragments/build-wrapper-maven/accelerator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ accelerator:
label: Include Build Tool wrapper support
dataType: boolean
defaultValue: true

engine:
merge:
- condition: "#includeBuildToolWrapper"
include: [ "mvnw", "mvnw.cmd", ".mvn/**" ]
18 changes: 18 additions & 0 deletions fragments/java-version/accelerator.axl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
engine {
applyTo("README.md") {
UniquePath(strategy: FavorForeign)
ReplaceText(regex: {pattern: "set BP_JVM_VERSION=.*", with: 'set BP_JVM_VERSION=' + #javaVersion})
}

applyTo("pom.xml") {
ReplaceText(regex: {pattern: "<java.version>.*<", with: '<java.version>' + #javaVersion + '<'})
}

applyTo("build.gradle") {
ReplaceText(regex: {pattern: "sourceCompatibility = .*", with: 'sourceCompatibility = ''' + #javaVersion + ''''})
}

applyTo("build.gradle.kts") {
ReplaceText(regex: {pattern: "(?<unmodified>JavaVersion\.VERSION_)(\d+)", with: '${unmodified}' + #javaVersion})
}
}
53 changes: 0 additions & 53 deletions fragments/java-version/accelerator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,8 @@ accelerator:
inputType: select
label: Java version to use
choices:
- value: "1.8"
text: Java 8
- value: "11"
text: Java 11
- value: "17"
text: Java 17
- value: "21"
text: Java 21
defaultValue: "17"

engine:
let:
- name: workloadJavaVersion
expression: "#javaVersion == '1.8' ? '8' : #javaVersion"
merge:
# Maven
- include: [ "pom.xml" ]
chain:
- type: ReplaceText
regex:
pattern: "<java.version>.*<"
with: "'<java.version>' + #javaVersion + '<'"

# Gradle Groovy DSL
- include: [ "build.gradle" ]
chain:
- type: ReplaceText
regex:
pattern: "sourceCompatibility = .*"
with: "'sourceCompatibility = ''' + #javaVersion + ''''"

# Gradle Kotlin DSL
- include: [ "build.gradle.kts" ]
chain:
- type: ReplaceText
regex:
pattern: "(?<unmodified>JavaVersion\\.VERSION_)(\\d+)"
with: "'${unmodified}' + #javaVersion.replace('.', '_')"

# workload.yaml
# This requires that BP_JVM_VERSION is already defined in the workload.yaml
- include: [ "config/workload.yaml", "config/workload-native.yaml" ]
chain:
- type: OpenRewriteRecipe
recipe: org.openrewrite.yaml.ChangeValue
options:
oldKeyPath: '"$.spec.build.env[?(@.name == ''BP_JVM_VERSION'')].value"'
value: "'\"' + #workloadJavaVersion + '\"'"

# *.yaml
# Targeting Pipeline definitions. Update versions of OpenJdk image.
- include: [ "config/*.yaml" ]
exclude: [ "config/workload*.yaml" ]
chain:
- type: ReplaceText
regex:
pattern: "(?<unmodified>image: bellsoft/liberica-openjdk-\\w*:)(\\d+)"
with: "'${unmodified}' + #workloadJavaVersion"
55 changes: 0 additions & 55 deletions tanzu-java-web-app/.tanzuignore

This file was deleted.

13 changes: 0 additions & 13 deletions tanzu-java-web-app/README-native.md

This file was deleted.

157 changes: 124 additions & 33 deletions tanzu-java-web-app/README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,143 @@
# tanzu-java-web-app

This is a sample of a Java Spring app that works with Tilt and the Tanzu Application Platform.
This is a sample of a Java Spring web app for the Tanzu Platform for Kubernetes.

## Dependencies
1. [kubectl CLI](https://kubernetes.io/docs/tasks/tools/)
2. [Tilt version >= v0.23.2](https://docs.tilt.dev/install.html)
3. Tanzu CLI and the apps plugin v0.2.0 which are provided as part of [Tanzu Application Platform](https://network.tanzu.vmware.com/products/tanzu-application-platform)
4. A cluster with Tanzu Application Platform, and the "Default Supply Chain", plus its dependencies. This supply chains is part of [Tanzu Application Platform](https://network.tanzu.vmware.com/products/tanzu-application-platform).
## Building and running the app locally

## Application Live View
The workload is set up by default to autoconfigure the actuators. This results in that the Spring Actuators are available at TCP port 8081 and will be used by Application Live View.
Application Live View allows you see all health metrics in the TAP GUI. If you would like to have the Actuators available at TCP port 8080 you can set the
annotation `apps.tanzu.vmware.com/auto-configure-actuators` to `false`.
### Prerequisites
In order to further develop this application the following tools needs to be setup:
- Java Development Kit (https://bell-sw.com/libericajdk/)
- Visual Studio Code or IntelliJ IDEA as Integrated Development Environment (IDE)

## Building with local source
### Build the app

You can build using source from either a Git repository or from source on your local disk.
The instructions below use the latter option, where you build using the source from your local disk.
This is specified by adding a `--local-path` option providing the path for the source, and a `--source-image` option providing the OCI repository (e.g. `registry.io/user/tanzu-java-web-app-source`) to use for publishing the local source code.
In order to compile the application code:

You can set this as an environment variable before running any of the commands below using:
```sh
#IF(#buildTool == "gradle")
./gradlew build
#ENDIF
#IF(#buildTool == "maven")
./mvnw package
#ENDIF
```

### Run the app

```sh
#IF(#buildTool == "gradle")
./gradlew bootRun
#ENDIF
#IF(#buildTool == "maven")
./mvnw spring-boot:run
#ENDIF
```

### Accessing the running app

You can use [curl](https://curl.se/) command:

```sh
curl localhost:8080
```

Or, you can use [HTTPie](https://httpie.io/):

```sh
http -b :8080
```

You should see the following text:

```
Greetings from Spring Boot + Tanzu!
```

## Configuring your Tanzu Platform build environment

### Prerequisites

1. Tanzu CLI and the apps plugin v0.2.0 which are provided as part of [Tanzu Platform](https://docs.vmware.com/en/VMware-Tanzu-Platform/index.html). Installation instructions can be found at [VMware Tanzu Platform Product Documentation - Before you begin](https://docs.vmware.com/en/VMware-Tanzu-Platform/SaaS/create-manage-apps-tanzu-platform-k8s/getting-started-deploy-app-to-space.html#before-you-begin-0).

2. You have access to a space for your project, and you have used `tanzu login` to authenticate and configure your current Tanzu context, and you have set your project and space using `tanzu project use` and `tanzu space use` respectively.

### Configure an image registry to use for building the app

Before you can build your app, you need to specify the registry where the resulting image from the build can be stored.

```sh
tanzu build config --containerapp-registry REGISTRY
```

> Where `REGISTRY` is your container image registry location. For example, `my-registry.io/my-corp-apps/{name}` or `docker.io/<your-docker-id>/{name}` if you use Docker Hub. Note that use of literal `{name}` is required, and it will be replaced with your apps name when you build.
## Configuring your app environment

Change to the root directory of your generated app.

### Initialize the ContainerApp

```sh
tanzu app init
```

### Configure the JDK version

You need to specify the JDK version to be used for the app:

```sh
tanzu app config build non-secret-env set BP_JVM_VERSION=17
```

### Configure for native build with GraalVM

If you would like the build to use GraalVM for compipling native image then use the following comands:

```sh
tanzu app config build non-secret-env set BP_NATIVE_IMAGE=true
tanzu app config build non-secret-env set BP_MAVEN_ACTIVE_PROFILES=native
```

### Configure HTTP Ingress Routing

If you want to expose your application with a domain name and route traffic from the domain name to the deployed application, see [Adding HTTP Routing to an Application](https://docs.vmware.com/en/VMware-Tanzu-Platform/SaaS/create-manage-apps-tanzu-platform-k8s/how-to-ingress-to-app.html).

### Building and deploying the app to Tanzu Platform for Kubernetes in one step

Change to the root directory of your generated app.

Run this command to build and deploy the app:

```sh
export SOURCE_IMAGE=registry.io/user/tanzu-java-web-app-source
tanzu deploy
```

## Deploying the sample for TAP
### Using separate build and deploy commands

Change to the root directory of your generated app.

#### Building from local source

You can build using source on your local disk.

To build the app you can run this command:

```sh
tanzu build --output-dir ./prebuilt
```

#### Deploying the app to Tanzu Platform for Kubernetes

Start the app deployment by running:

```sh
tanzu apps workload create tanzu-java-web-app \
--file ./config/workload.yaml \
--local-path . \
--source-image "${SOURCE_IMAGE}"
tanzu deploy --from-build ./prebuilt
```

## Running the sample on TAP using Tilt for live update
### Scale the number of instances

You can use the IDE plugins for VSCode or IntelliJ IDEA to enable live update. You can also use the command line following these steps.
Run this command to scale to 1 instance:

1. Set the environment variable mentioned above specifying the repository to use for the source image. This is where the local source code will be written. As an example, use `export SOURCE_IMAGE=registry.io/user/tanzu-java-web-app-source`.
2. Start `Tilt` by running `tilt up`
> If you see an "Update error" message like the one below, then just follow the instructions and allow that context:
```
Stop! tap-beta2 might be production.
If you're sure you want to deploy there, add:
allow_k8s_contexts('tap-beta2')
to your Tiltfile. Otherwise, switch k8s contexts and restart Tilt.
```
3. You can hit the spacebar to open the Tilt UI in a browser.
```sh
tanzu app scale tanzu-java-web-app --instances=1
```
Loading

0 comments on commit 3890d31

Please sign in to comment.