How do I build a Kubernetes core namespacepatch?
In this guide, we will create and patch a Kubernetes namespace using Pulumi. We will define a namespace and then apply a patch to it. This is useful for updating metadata or other properties of an existing namespace without recreating it.
Key Points
- Create a Kubernetes namespace.
- Apply a patch to the namespace.
- Use Pulumi’s infrastructure as code to manage and update Kubernetes resources.
Detailed Steps
- Create a Kubernetes Namespace: Define a namespace with specific metadata.
- Patch the Namespace: Apply a patch to update properties of the namespace.
Below is the complete Pulumi program to achieve this:
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define the initial namespace
const myNamespace = new k8s.core.v1.Namespace("my-namespace", {
metadata: {
name: "my-namespace",
labels: {
"environment": "dev",
},
},
});
// Patch the namespace to add or modify metadata
const patchedNamespace = new k8s.core.v1.NamespacePatch("my-namespace-patch", {
metadata: {
name: "my-namespace",
annotations: {
"patched-by": "pulumi",
},
},
spec: {
finalizers: ["kubernetes"],
},
}, { dependsOn: [myNamespace] });
Summary
In this example, we created a Kubernetes namespace named my-namespace
and then applied a patch to add an annotation and a finalizer to it. This demonstrates how to manage and update Kubernetes resources using Pulumi’s infrastructure as code approach.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.