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 package.js with the following content:

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

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

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

// Use an object to define Service.
builder.addDocument(
name,
`service.yaml`,
{
apiVersion: "v1",
kind: "Service",
metadata: {
name: name,
namespace: namespace
},
spec: {
ports: [
{
name: "http",
port: 80,
targetPort: "http"
}
]
}
});
}

module.exports = {
add
}

Explanation

index.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 = options ?? {};

const name = options.name ?? "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.

index.js
  builder.addDocument(
name,
`pod.yaml`,
`
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 the same as the one we used in the previous section. The only difference is that we pass the name as the first argument to the addDocument method. This is the name of the document group that the document will belong to.

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.

index.js
  // Use an object to define Service.
builder.addDocument(
name,
`service.yaml`,
{
apiVersion: "v1",
kind: "Service",
metadata: {
name: name,
namespace: namespace
},
spec: {
ports: [
{
name: "http",
port: 80,
targetPort: "http"
}
]
}
});
}

In this code snippet, we define a Service manifest. Instead of using a YAML string, we use an object to define the Service. The object will be converted to a YAML string by Anemos. Using this approach, you can take advantage of JavaScript's features, such as functions and classes, to dynamically generate your Kubernetes manifests.

index.js
module.exports = {
add
}

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