Golang
An example implementation in Golang of a kubernetes client to interact with a Virtual Server resource on CoreWeave Cloud.
The provided Golang example illustrates the following:
- Spinning up a Virtual Server struct (
VirtualServer
). - Building a Service and PVC to be used as a FloatingIP and Additional Filesystem respectively.
- Removing an existing Virtual Server.
- Creating a new Virtual Server. (The instance is started automatically.)
- Waiting for Virtual Server ready status.
- Stopping the instance, then waiting until it is completely stopped.
- Deleting the Virtual Server once it is completely stopped.
Example code is available on CoreWeave's GitHub.
Build and Run
A makefile
is provided within the Golang example with the following directives:
Directive | Description |
---|---|
install | Download the module dependencies. |
run | Build and run the example executable. |
clean | Remove the executable. |
Implementation
The Go example employs the v1alpha1 package of CoreWeave's VirtualServer
in order to interface with the Virtual Server resource on CoreWeave Cloud. The Virtual Server struct is prepared using helper functions provided by package v1alpha1.
Additionally, a Persistent Volume Claim and a Service are created. They are then allocated to the Virtual Server as an additional file system and floating IP respectively. Once prepared, a simple sigs client is used to create, get, and delete the Virtual Server.
Client
The example application (main.go
) builds a Kubernetes client using the credentials from either a Kubernetes config file or from a service account mount.
Additional information on building a config can be found in the config
section of the Go package.
Persistent Volume Claim
A Persistent Volume Claim (PVC) is created and assigned to the Virtual Server as an additional file system. The PVC will then be accessible from within the Virtual Server as a mounted file system. The example function buildPVC
illustrates the creation of a PVC struct. The Kubernetes client is then used to deploy the PVC to the cluster.
Service
A Service is created and assigned to the Virtual Server as a floating IP service. The service must be type: LoadBalancer
, and have been assigned a load balancer IP in order to be used as a floating IP service. If the service is valid, the IP of the service will be assigned to the Virtual Server as a floating IP.
This example also illustrates creating a floating service when the FLOATING_SERVICE_NAME
environment variable is specified. The function buildFloatingIPService
illustrates the creation of a Service struct. The Kubernetes client is then used to deploy the Service to the cluster.
Virtual Server
Using utility functions provided by v1alpha1, the application creates a VirtualServer struct, and then deploys the Virtual Server to the cluster using the Kubernetes client.
For long-running, fine-grained and event-driven control of Virtual Servers, a sigs controller can be implemented for Virtual Servers.