Maven setup

Maven setup is likely the easiest, what you have to do is:

  1. Create a descriptor generation module,

  2. Add the needed dependencies in this pom module (see bindings page if needed),

  3. Write a main with the generation,

  4. Use exec-maven-plugin to run your main.

Here is a skeleton:

public class MyGenerator {
    public static void main(final String... args){
        final var output = Files.createDirectories(Path.of(args[0])); (1)

        final var deployment = new Deployment() (2)
                    .spec(new DeploymentSpec()
                    // ... complete the generation as needed
        );

        Files.writeString( (3)
                output.resolve("my-deployment.json"),
                deployment.asJson());
    }

    private MyGenerator() {
        // no-op
    }
}
  1. Get and create the output directory for this run (generally in target//${project.build.directory}),

  2. Create your in memory descriptor,

  3. Write your descriptor on the disk (or in a zip directly if you prefer).

Now to execute this main you can just use this exec-maven-plugin in your Apache Maven configuration:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <id>generate-k8s-descriptors</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>java</goal>
      </goals>
      <configuration>
        <stopUnresponsiveDaemonThreads>false</stopUnresponsiveDaemonThreads>
        <cleanupDaemonThreads>false</cleanupDaemonThreads>
        <mainClass>org.superbiz.MyGenerator</mainClass>
        <arguments>
          <argument>${project.build.directory}/k8s-descriptors</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

Now if you run mvn prepare-package you will get your descriptors dumped in target/k8s-descriptors folder.

JShell setup

You can use this project with JShell, you just need to ensure the JShell classpath is correct when executing your script. As of today, you must ensure it contains:

  • (optional for some setup but recommended) a JSON-P API and implementation,

  • (optional) a JSON-B API and implementation,

  • The kubernetes-java-xxx version you need,

  • (optional) bundlebee-java if you use it.

Here is how to run a custom JShell script (note it assumes it runs on Linux and the jar were downloaded in current folder):

jshell \
  --class-path \
  geronimo-json_1.1_spec-1.4-jakarta.jar:johnzon-core-1.2.18-jakarta.jar:kubernetes-java-1.24.3-1.0.0.jar \
  my-script.jsh

Then you just need to write your script almost as in plain java:

import io.yupiik.kubernetes.bindings.v1_24_3.v1.*; // (1)

{ // (2)
    final var output = Files.createDirectories(Path.of("target/k8s-descriptors"));

    final var deployment = new Deployment()
        .spec(new DeploymentSpec()
        /* ... */);

    Files.writeString(
       output.resolve("my-deployment.json"),
        deployment.asJson());
}

/exit // (3)
  1. Import the classes you need (here we use a wildcard import on the model because it is safe for our script but you can use explicit imports),

  2. A small trick to write a readable script is to enable multiline snippet by wrapping the code in a block (rest of the code is similar to previous ones),

  3. Finally when finished we exit JShell.

JBang Setup

JBang can also be used to generate your descriptors. Here is a script directly runnable with one of these runner:

  • From Maven/Gradle

  • From the command line if you installed jbang (either making your script executable - ///usr/bin/env jbang "$0" "$@" ; exit $? or using jbang $script),

  • From jbang-actions (Github Actions integration)

descriptors.generator.java
//DEPS org.apache.geronimo.specs:geronimo-json_1.1_spec:1.4:jakarta
//DEPS org.apache.johnzon:johnzon-core:1.2.18:jakarta
//DEPS io.yupiik.kubernetes:kubernetes-java-1.24.3:1.0-SNAPSHOT

import io.yupiik.kubernetes.bindings.v1_24_3.v1.*;
import java.io.*;
import java.nio.file.*;

class Generator {
    public static void main(final String... args) throws IOException {
        final var output = Files.createDirectories(Path.of("target/k8s-descriptors"));

        final var deployment = new Deployment()
                .spec(new DeploymentSpec()
                    /*...*/);

        Files.writeString(
                output.resolve("my-deployment.json"),
                deployment.asJson());
    }
}

It is pretty much the same script than before but we handled the classpath thanks to //DEPS directive which can be convenient if you are already using JBang.

To set it with Github Actions use:

on: [push]

jobs:
  jbang:
    runs-on: ubuntu-20.04
    name: Generate descriptors
    steps:
    - name: checkout
      uses: actions/checkout@v3
    - uses: actions/cache@v3
      with:
        path: /root/.jbang
        key: $-jbang-$
        restore-keys: |
            $-jbang-
    - name: jbang
      uses: jbangdev/jbang-action@v0.97.0
      with:
        script: descriptors.generator.java
      env:
        JBANG_REPO: /root/.jbang/repository