Skip to main content

NodeJS

An example implementation in NodeJS of a kubernetes client to interact with a Virtual Server resource on CoreWeave Cloud.

Implementation

The example is broken down into two parts:

Both parts are described below, and examples can be found on CoreWeave's GitHub.

Client

The client (client.js) provides an interface to create a Kubernetes client using your Kubernetes credentials (token). client.js may be dropped into any application and used to interface with the Virtual Server resource.

A set of functions specific to creating, modifying, and checking the status of a Virtual Server are additionally provided, which operate as follows:

Function nameDescription
init()Initializes the client ⚠️ init() must be called prior to using the client
namespaceNamespace in which the Virtual Server is deployed
nameName of the Virtual Server
virtualServer.start({namespace, name})Starts a stopped Virtual Server
virtualServer.stop({namespace, name})Stops a running Virtual Server
virtualServer.get({namespace, name})Retrieves a Virtual Server
virtualServer.ready({namespace, name})Stopping function that waits for the VirtualMachineReady status condition of a Virtual Server
virtualServer.delete({namespace, name})Deletes a Virtual Server
manifestA Virtual Server manifest (JSON/Object) - See Examples
virtualServer.list(\{namespace\})Lists all Virtual Servers
virtualServer.create(manifest)Creates a Virtual Server
virtualServer.update(manifest)Update an existing Virtual Server

Application

The application (main.js) implements client.js in order to create, watch, get, and update an example Virtual Server.

The example application serves as an example of how to implement client.js. Additionally, the example application illustrates how one might retrieve status information from a running Virtual Server, retrieving its run state and network information.

Finally, the example shows how to simply interface with the kubevirt __ subresource API in order to start and stop a Virtual Server.

Example

{
apiVersion: "virtualservers.coreweave.com/v1alpha1",
kind: "VirtualServer",
metadata: {
name: "example-vs",
namespace: "my-namespace"
},
spec: {
region: "ORD1",
os: {
type: "linux"
},
resources: {
gpu: {
type: "Quadro_RTX_4000",
count: 1
},
cpu: {
count: 3
},
memory: "16Gi"
},
storage: {
root: {
size: "40Gi",
storageClassName: "ceph-ssd-2-replica",
source: {
pvc: {
namespace: "vd-images",
name: "ubuntu2004-docker-master-20210323-ord1"
}
}
}
},
users: [
{
username, #Set from environment variable
password #Set from environment variable
}
],
network: {
public: true,
tcp: {
ports: [
22,
443,
60443,
4172,
3389,
]
},
udp: {
ports: [
4172,
3389
]
}
},
initializeRunning: true
}
}