|
5 | 5 | [](https://npmjs.com/package/kubernetes-fluent-client)
|
6 | 6 | [](https://npmjs.com/package/kubernetes-fluent-client)
|
7 | 7 |
|
| 8 | +The Kubernetes Fluent Client for Node is a fluent API for the [Kubernetes JavaScript Client](https://github.com/kubernetes-client/javascript) with some additional logic for [Server Side Apply](https://kubernetes.io/docs/reference/using-api/server-side-apply/), [Watch](https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes) with retry/signal control, and [Field Selectors](https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/). In addition to providing a human-friendly API, it also provides a simple way to create and manage resources in the cluster and integrate with K8s in a type-safe way. |
| 9 | + |
| 10 | +See below for some example uses of the library. |
| 11 | + |
8 | 12 | ```typescript
|
9 | 13 | import { K8s, kind } from "kubernetes-fluent-client";
|
10 | 14 |
|
11 |
| -async function main() { |
12 |
| - const pods = await K8s(kind.Pod).Get(); |
| 15 | +// Let's create a random namespace to work in |
| 16 | +const namespace = "my-namespace" + Math.floor(Math.random() * 1000); |
| 17 | + |
| 18 | +// This will be called after the resources are created in the cluster |
| 19 | +async function demo() { |
| 20 | + // Now, we can use the fluent API to query for the resources we just created |
| 21 | + |
| 22 | + // You can use watch to monitor resources in the cluster and react to changes |
| 23 | + // This will run until the process is terminated or the watch is aborted |
| 24 | + const ctrl = await K8s(kind.Pod).Watch((pod, phase) => { |
| 25 | + console.log(`Pod ${pod.metadata?.name} is ${phase}`); |
| 26 | + }); |
| 27 | + |
| 28 | + // Let's abort the watch after 5 seconds |
| 29 | + setTimeout(ctrl.abort, 5 * 1000); |
| 30 | + |
| 31 | + // Passing the name to Get() will return a single resource |
| 32 | + const ns = await K8s(kind.Namespace).Get(namespace); |
| 33 | + console.log(ns); |
| 34 | + |
| 35 | + // This time we'll use the InNamespace() method to filter the results by namespace and name |
| 36 | + const cm = await K8s(kind.ConfigMap).InNamespace(namespace).Get("my-configmap"); |
| 37 | + console.log(cm); |
| 38 | + |
| 39 | + // If we don't pass a name to Get(), we'll get a list of resources as KubernetesListObject |
| 40 | + // The matching resources will be in the items property |
| 41 | + const pods = await K8s(kind.Pod).InNamespace(namespace).Get(); |
13 | 42 | console.log(pods);
|
| 43 | + |
| 44 | + // Now let's delete the resources we created, you can pass the name to Delete() or the resource itself |
| 45 | + await K8s(kind.Namespace).Delete(namespace); |
| 46 | + |
| 47 | + // Let's use the field selector to find all the running pods in the cluster |
| 48 | + const runningPods = await K8s(kind.Pod).WithField("status.phase", "Running").Get(); |
| 49 | + runningPods.items.forEach(pod => { |
| 50 | + console.log(`${pod.metadata?.namespace}/${pod.metadata?.name} is running`); |
| 51 | + }); |
14 | 52 | }
|
| 53 | + |
| 54 | +// Create a few resources to work with: Namespace, ConfigMap, and Pod |
| 55 | +Promise.all([ |
| 56 | + // Create the namespace |
| 57 | + K8s(kind.Namespace).Apply({ |
| 58 | + metadata: { |
| 59 | + name: namespace, |
| 60 | + }, |
| 61 | + }), |
| 62 | + |
| 63 | + // Create the ConfigMap in the namespace |
| 64 | + K8s(kind.ConfigMap).Apply({ |
| 65 | + metadata: { |
| 66 | + name: "my-configmap", |
| 67 | + namespace, |
| 68 | + }, |
| 69 | + data: { |
| 70 | + "my-key": "my-value", |
| 71 | + }, |
| 72 | + }), |
| 73 | + |
| 74 | + // Create the Pod in the namespace |
| 75 | + K8s(kind.Pod).Apply({ |
| 76 | + metadata: { |
| 77 | + name: "my-pod", |
| 78 | + namespace, |
| 79 | + }, |
| 80 | + spec: { |
| 81 | + containers: [ |
| 82 | + { |
| 83 | + name: "my-container", |
| 84 | + image: "nginx", |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + }), |
| 89 | +]) |
| 90 | + .then(demo) |
| 91 | + .catch(err => { |
| 92 | + console.error(err); |
| 93 | + }); |
15 | 94 | ```
|
0 commit comments