Modify a YAML Document
Modifying YAML documents is a simple but powerful feature of Anemos. It allows you to change the content of a document anywhere in the code.
The following example shows various ways to modify a YAML document. See the API reference for more ways to modify a document.
- TypeScript
- JavaScript
index.ts
import * as anemos from "@ohayocorp/anemos";
const builder = new anemos.Builder("1.31", anemos.KubernetesDistribution.Minikube, anemos.EnvironmentType.Development);
const document = new anemos.Document("modified.yaml");
// Set simple values.
const root = document.getRoot();
root.set("apiVersion", "apps/v1");
root.set("kind", "Deployment");
// Set objects as values.
root.set("metadata", {
name: "modified",
namespace: "default",
});
// Create chain of objects. Here "spec", "template" and "spec" are all created and the last mapping
// is returned.
const templateSpec = root.ensureMapping(["spec", "template", "spec"]);
// Set an array as YAML sequence.
templateSpec.set("containers", [
{
name: "modified",
image: "nginx:latest",
},
]);
// Anemos has some convenience methods for Kubernetes objects. For example, `getContainer` returns the
// container of a workload with given name.
const container = document.getContainer("modified");
container?.set("imagePullPolicy", "Always");
builder.addDocument(document);
builder.build();
index.js
const anemos = require("@ohayocorp/anemos");
const builder = new anemos.Builder("1.31", anemos.KubernetesDistribution.Minikube, anemos.EnvironmentType.Development);
const document = new anemos.Document("modified.yaml");
// Set simple values.
const root = document.getRoot();
root.set("apiVersion", "apps/v1");
root.set("kind", "Deployment");
// Set objects as values.
root.set("metadata", {
name: "modified",
namespace: "default",
});
// Create chain of objects. Here "spec", "template" and "spec" are all created and the last mapping
// is returned.
const templateSpec = root.ensureMapping(["spec", "template", "spec"]);
// Set an array as YAML sequence.
templateSpec.set("containers", [
{
name: "modified",
image: "nginx:latest",
},
]);
// Anemos has some convenience methods for Kubernetes objects. For example, `getContainer` returns the
// container of a workload with given name.
const container = document.getContainer("modified");
container?.set("imagePullPolicy", "Always");
builder.addDocument(document);
builder.build();
This example creates the following YAML document:
modified.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: modified
namespace: default
spec:
template:
spec:
containers:
- name: modified
image: nginx:latest
imagePullPolicy: Always