Kubernetes (k8s)
A framework for synthesizing Kubernetes manifests using Winglang.
Installation
Install the Wing CLI:
npm i -g winglang
Create a new project and install this library:
mkdir wing-loves-k8s
cd wing-loves-k8s
npm i @winglibs/k8s
Usage
Let's define an app with a simple Kubernetes object:
// main.w
bring k8s;
new k8s.ApiObject(
apiVersion: "v1",
kind: "ConfigMap",
spec: {
data: {
key: "value",
},
}
);
Now, compile it to YAML:
$ wing compile -t @winglibs/k8s ubuntu.main.w
target/ubuntu.main.k8s
You
The output is a valid K8S YAML is in target/ubuntu.main.k8s
:
$ ls target/ubuntu.main.k8s
chart-c86185a7.k8s.yaml
Creating Helm charts
You can set WING_K8S_OUTPUT
to helm
in order to produce a helm chart instead of simple manifest.
This requires a Chart.yaml
file next in the current directory.
Default labels and namespace
You can use the WING_K8S_LABELS
environment variable to apply labels to all resources in an app.
The value is a JSON-encoded map.
The WING_K8S_NAMESPACE
variable can be used to specify the default namespace.
export WING_K8S_LABELS='{ "my-label": "123", "your-label": "444" }'
export WING_K8S_NAMESPACE='my-namespace'
wing compile -t @winglibs/k8s main.w
CDK8s Support
This library supports cdk8s and cdk8s-plus, so you can do stuff like this:
npm i cdk8s-plus-27
And then:
bring "cdk8s-plus-27" as k8s;
// lets create a volume that contains our app.
let appData = new k8s.ConfigMap();
appData.addDirectory("./nodejs-app");
let appVolume = k8s.Volume.fromConfigMap(this, "App", appData);
// lets create a deployment to run a few instances of a pod
let deployment = new k8s.Deployment(
replicas: 3,
);
// now we create a container that runs our app
let appPath = "/var/lib/app";
let port = 80;
let container = deployment.addContainer({
image: "node:14.4.0-alpine3.12",
command: ["node", "index.js", "{port}"],
port: port,
workingDir: appPath,
});
// make the app accessible to the container
container.mount(appPath, appVolume);
// finally, we expose the deployment as a load balancer service and make it run
deployment.exposeViaService(serviceType: k8s.ServiceType.LOAD_BALANCER);
Roadmap
- Support generating Wing bindings from K8S API specifications and CRDs (
cdk8s import
).
Maintainers
License
This library is licensed under the MIT License.
API Reference
Table of Contents
- Classes
- Structs
ApiObject (preflight class)
No description
Constructor
new(props: ApiObjectProps): ApiObject
Properties
Name | Type | Description |
---|---|---|
apiGroup | str | The group portion of the API version (e.g. authorization.k8s.io ). |
apiVersion | str | The object's API version (e.g. authorization.k8s.io/v1 ). |
chart | Chart | The chart in which this object is defined. |
kind | str | The object kind. |
metadata | ApiObjectMetadataDefinition | Metadata associated with this API object. |
name | str | The name of the API object. |
Methods
Signature | Description |
---|---|
| Create a dependency between this ApiObject and other constructs. |
| Applies a set of RFC-6902 JSON-Patch operations to the manifest synthesized for this API object. |
static isApiObject(o: any): bool | Return whether the given object is an ApiObject . |
static of(c: IConstruct): ApiObject | Returns the ApiObject named Resource which is a child of the given construct. |
toJson(): any | Renders the object to Kubernetes JSON. |
ApiObjectProps (struct)
No description
Properties
Name | Type | Description |
---|---|---|
apiVersion | str | API version. |
kind | str | Resource kind. |
metadata | ApiObjectMetadata? | Object metadata. |
spec | Json? | No description |