Spin Operator

repository·main·Indexed 43 days ago

https://github.com/spinframework/spin-operator

A Kubernetes controller that manages the deployment and lifecycle of Spin applications by watching SpinApp Custom Resources. It coordinates the necessary Kubernetes resources to execute WebAssembly (Wasm) workloads and can be installed via Helm.

Tokens
3.9K
Snippets
16
Records
19
Agent score
54%

What's inside Spin Operator

  1. What is Spin Operator

    main
    Spin Operator is a Kubernetes controller built using the Kubebuilder framework. It enables the deployment of Spin applications to Kubernetes by watching SpinApp Custom Resources (CRDs) and realizing their desired state within the cluster. It manages the lifecycle of Spin applications by coordinating the necessary Kubernetes resources to execute WebAssembly (Wasm) workloads.
  2. Upgrade the spin-operator chart

    main

    To upgrade the spin-operator release, use helm upgrade. Note that you may also need to upgrade the CRDs in tandem with the Helm release to ensure compatibility:

    1. Upgrade CRDs:

    $ kubectl apply -f https://raw.githubusercontent.com/spinframework/spin-operator/main/config/crd/bases/core.spinkube.dev_spinapps.yaml
    $ kubectl apply -f https://raw.githubusercontent.com/spinframework/spin-operator/main/config/crd/bases/core.spinkube.dev_spinappexecutors.yaml

    2. Upgrade Helm release:

    $ helm upgrade spin-operator \
      --namespace spin-operator \
      --version {{ CHART_VERSION }} \
      oci://ghcr.io/spinframework/charts/spin-operator
  3. Push cpu-load-gen to a registry for SpinApp use

    main

    To make the cpu-load-gen image available for use by SpinApp within spin-operator, you must push the built image to a container registry. Use the spin registry push command followed by the desired image tag (e.g., using a TTL-based tag like ttl.sh).

    spin registry push ttl.sh/cpu-load-gen:1h
  4. Uninstall the spin-operator

    main

    To remove the spin-operator release and its associated Kubernetes resources, run:

    $ helm delete spin-operator --namespace spin-operator

    To perform a complete uninstallation including the SpinAppExecutor and the CRDs, run the following commands:

    $ kubectl delete -f https://raw.githubusercontent.com/spinframework/spin-operator/main/config/samples/spin-shim-executor.yaml
    $ kubectl delete -f https://raw.githubusercontent.com/spinframework/spin-operator/main/config/crd/bases/core.spinkube.dev_spinapps.yaml
    $ kubectl delete -f https://raw.githubusercontent.com/spinframework/spin-operator/main/config/crd/bases/core.spinkube.dev_spinappexecutors.yaml
  5. Deploy Spin Operator to a remote Kubernetes cluster

    main

    Deploying to a remote cluster allows for testing advanced features like webhooks. This requires cert-manager for webhook support.

    Prerequisites

    Steps

    1. Install cert-manager: Required for webhook support.
      kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.3/cert-manager.yaml
      kubectl wait --for=condition=available --timeout=300s deployment/cert-manager-webhook -n cert-manager
    2. Install CRDs: Run make install to install SpinApp and SpinAppExecutor CRDs.
    3. Configure Runtime: Apply the sample RuntimeClass and SpinAppExecutor configurations.
    4. Build/Push Image (Optional):
      export IMG_REPO=<some-registry>/spin-operator
      make docker-build docker-push
    5. Deploy: Run make deploy to install the operator into the cluster.
    6. Verify: Apply the sample application and port-forward to test via curl as described in the local setup guide.
    kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.3/cert-manager.yaml
    kubectl wait --for=condition=available --timeout=300s deployment/cert-manager-webhook -n cert-manager
    
    make install
    
    kubectl apply -f config/samples/spin-runtime-class.yaml
    kubectl apply -f config/samples/spin-shim-executor.yaml
    
    # Optional image build
    export IMG_REPO=<some-registry>/spin-operator
    make docker-build docker-push
    
    make deploy
    
    kubectl apply -f ./config/samples/simple.yaml
    
    kubectl port-forward svc/simple-spinapp 8083:80
    
    curl localhost:8083/hello
  6. Interact with the Redis Spin app via HTTP

    main

    After deploying both the Spin app and Redis, expose the Spin app locally using kubectl port-forward svc/redis-spinapp 3000:80. You can then interact with Redis using curl commands by passing the desired key in the x-key header.

    Place a key-value pair

    Use a PUT request with --data-binary to store a value.

    Retrieve a value

    Perform a standard GET request (default) to fetch the value associated with the key.

    Delete a value

    Use a DELETE request to remove the key.

    # 1. Port-forward the service
    kubectl port-forward svc/redis-spinapp 3000:80
    
    # 2. In a new terminal, perform operations:
    
    # Place a key-value pair
    curl --request PUT --data-binary "Hello, world\!" -H 'x-key: helloKey' localhost:3000
    
    # Retrieve a value
    curl -H 'x-key: helloKey' localhost:3000
    
    # Delete a value
    curl --request DELETE -H 'x-key: helloKey' localhost:3000
  7. Deploy the Spin app for Redis interaction

    main

    To deploy the Spin application that demonstrates Redis interaction, create a Kubernetes manifest named redis_client.yaml using the SpinApp resource type. Ensure the redis_endpoint variable is set to the correct Redis service address (e.g., redis://redis.default.svc.cluster.local:6379).

    Apply the manifest using kubectl apply -f redis_client.yaml.

    apiVersion: core.spinkube.dev/v1alpha1
    kind: SpinApp
    metadata:
      name: redis-spinapp
    spec:
      image: "ghcr.io/spinkube/redis-sample"
      replicas: 1
      executor: containerd-shim-spin
      variables:
        - name: redis_endpoint
          value: redis://redis.default.svc.cluster.local:6379
  8. Deploy Redis to Kubernetes

    main

    Deploy a Redis instance by creating a Kubernetes manifest named redis_db.yaml containing both a Deployment and a Service. The service must expose port 6379 to allow the Spin app to connect.

    Apply the manifest using kubectl apply -f redis_db.yaml.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis
      labels:
        app: redis
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: redis
      template:
        metadata:
          labels:
            app: redis
        spec:
          containers:
            - name: redis
              image: redis
              ports:
                - containerPort: 6379
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis
    spec:
      selector:
        app: redis
      ports:
        - protocol: TCP
          port: 6379
          targetPort: 6379
  9. Set up a local development environment with k3d

    main

    To test Spin Operator locally, use k3d (a lightweight Kubernetes distribution running on Docker).

    Prerequisites

    Steps

    1. Create a k3d cluster with the required containerd-shim-spin image:
      k3d cluster create wasm-cluster \
          --image ghcr.io/spinframework/containerd-shim-spin/k3d:v0.23.0 \
          -p "8081:80@loadbalancer" \
          --agents 2
    2. Install CRDs: Use make install to install SpinApp and SpinAppExecutor definitions.
    3. Configure Runtime: Apply the sample RuntimeClass and SpinAppExecutor configurations.
    4. Run Operator: Execute make run to start the operator locally.
    5. Deploy Sample: Apply the sample application configuration.
    6. Test: Port-forward the service and use curl to verify the application is running.
    k3d cluster create wasm-cluster \
        --image ghcr.io/spinframework/containerd-shim-spin/k3d:v0.23.0 \
        -p "8081:80@loadbalancer" \
        --agents 2
    
    make install
    
    kubectl apply -f config/samples/spin-runtime-class.yaml
    kubectl apply -f config/samples/spin-shim-executor.yaml
    
    make run
    
    kubectl apply -f ./config/samples/simple.yaml
    
    kubectl port-forward svc/simple-spinapp 8083:80
    
    curl localhost:8083/hello