Skip to main content

Generating Packages

Simplest way to generate a package with Anemos is to define a function that takes a builder and an optional options object in a JavaScript file. This function can then be used to generate Kubernetes manifests. It is also possible to create an NPM package with multiple files, but this is not the focus of this tutorial.

Let's create a file named my-package.js with the following content:

my-package.js
function add(builder, options) {
options ??= {};

const name = options.name ?? "my-package";
const namespace = options.namespace ?? "default";
const image = options.image ?? "nginx";

builder.addDocument({
documentGroup: name,
path: `pod.yaml`,
content: `
apiVersion: "v1"
kind: "Pod"
metadata:
name: ${name}
namespace: ${namespace}
spec:
containers:
- name: "echo"
image: "${image}"
ports:
- name: "http"
containerPort: 80
protocol: "TCP"
`
});
}

module.exports = {
add
}

Explanation

my-package.js
function add(builder, options) {

This line defines a function that takes a builder and an options object. Users will call this function to generate manifests. Anemos will also call this function when package is applied directly to a cluster with anemos apply command.

index.js
  options ??= {};

const name = options.name ?? "my-package";
const namespace = options.namespace ?? "default";
const image = options.image ?? "nginx";

Users may or may not provide an options object or the values in it. We set default values for the options that users do not provide.

my-package.js
  builder.addDocument({
documentGroup: name,
path: `pod.yaml`,
content: `
apiVersion: "v1"
kind: "Pod"
metadata:
name: ${name}
namespace: ${namespace}
spec:
containers:
- name: "echo"
image: "${image}"
ports:
- name: "http"
containerPort: 80
protocol: "TCP"
`
});

This code snippet is similar to the one we used in the previous section. The only difference is that we pass the name as the documentGroup parameter to the addDocument method, we set the path parameter to the desired file name, and we pass the manifest definition as the content parameter.

Document groups are used to organize documents. Documents in a group will be written into a folder with the same name as the group. Documents in the same group will also be applied together, and they will be deleted together when the package is removed.

Default file path of a document is its kind in lowercase concatenated with a hyphen and its metadata name. Here we override the default path to pod.yaml since this is the only pod in the package.

my-package.js
module.exports = {
add
}

Lastly, we export the add function so that it can be used by users and Anemos.

Using the Package

To use the package we just created, we can create another JavaScript file named index.js with the following content:

const anemos = require('@ohayocorp/anemos');
const myPackage = require("./my-package.js");
// It is also possible to import packages from remote URLs.
// const myPackage = require("https://example.com/my-package.js");

const builder = new anemos.Builder();

myPackage.add(builder, {
name: "custom-app",
namespace: "custom-namespace",
image: "hashicorp/http-echo:1.0"
});

builder.build();

This code imports the Anemos library and our package, creates a Builder instance, adds our package to the builder with custom options, and finally builds the manifests.