Skip to content

Commit

Permalink
Merge pull request #323 from kate-goldenring/component-filtering
Browse files Browse the repository at this point in the history
feat: support only running a subset of components of a Spin app
  • Loading branch information
michelleN authored Oct 24, 2024
2 parents c1b079c + 43dbb39 commit 9668cab
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sample-apps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
app: [cpu-load-gen, hello-world, outbound-http, variabletester, redis-sample]
app: [cpu-load-gen, hello-world, outbound-http, variabletester, redis-sample, salutations]
env:
IMAGE_NAME: ${{ github.repository }}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Create a k3d cluster:

```shell
k3d cluster create wasm-cluster \
--image ghcr.io/spinkube/containerd-shim-spin/k3d:v0.15.1 \
--image ghcr.io/spinkube/containerd-shim-spin/k3d:v0.16.0 \
-p "8081:80@loadbalancer" \
--agents 2
```
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha1/spinapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ type SpinAppSpec struct {

// Resources defines the resource requirements for this app.
Resources Resources `json:"resources,omitempty"`

// Components of the app to execute.
//
// If this is not provided all components are executed.
// +kubebuilder:validation:MinItems:=1
Components []string `json:"components,omitempty"`
}

// SpinAppStatus defines the observed state of SpinApp
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apps/salutations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main.wasm
.spin/
7 changes: 7 additions & 0 deletions apps/salutations/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/salutations

go 1.20

require github.com/fermyon/spin/sdk/go/v2 v2.2.0

require github.com/julienschmidt/httprouter v1.3.0 // indirect
4 changes: 4 additions & 0 deletions apps/salutations/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/fermyon/spin/sdk/go/v2 v2.2.0 h1:zHZdIqjbUwyxiwdygHItnM+vUUNSZ3CX43jbIUemBI4=
github.com/fermyon/spin/sdk/go/v2 v2.2.0/go.mod h1:kfJ+gdf/xIaKrsC6JHCUDYMv2Bzib1ohFIYUzvP+SCw=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
17 changes: 17 additions & 0 deletions apps/salutations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"net/http"

spinhttp "github.com/fermyon/spin/sdk/go/v2/http"
)

func init() {
spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Goodbye!")
})
}

func main() {}
29 changes: 29 additions & 0 deletions apps/salutations/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spin_manifest_version = 2

[application]
name = "salutations"
version = "0.1.0"
authors = ["Kate Goldenring <kate.goldenring@fermyon.com>"]
description = "An app that gives salutations"

[[trigger.http]]
route = "/hi"
component = "hello"

[component.hello]
source = "../hello-world/main.wasm"
allowed_outbound_hosts = []
[component.hello.build]
command = "pushd ../hello-world && tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go && popd"
watch = ["**/*.go", "go.mod"]

[[trigger.http]]
route = "/bye"
component = "goodbye"

[component.goodbye]
source = "main.wasm"
allowed_outbound_hosts = []
[component.goodbye.build]
command = "tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
watch = ["**/*.go", "go.mod"]
6 changes: 6 additions & 0 deletions config/crd/bases/core.spinoperator.dev_spinapps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ spec:
type: integer
type: object
type: object
components:
description: Components to be executed in this group.
items:
type: string
minItems: 1
type: array
deploymentAnnotations:
additionalProperties:
type: string
Expand Down
12 changes: 12 additions & 0 deletions config/samples/component-filtering.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.spinoperator.dev/v1alpha1
kind: SpinApp
metadata:
name: hello-salutation-spinapp
spec:
# TODO: change to image hosted at ghcr.io/spinkube/spin-operator/salutations when published
image: "ghcr.io/kate-goldenring/spin-operator/examples/spin-salutations:20241022-144454"
replicas: 1
executor: containerd-shim-spin
# Configure the application to only contain the "hello" component
# Who doesn't hate goodbyes?
components: ["hello"]
10 changes: 9 additions & 1 deletion internal/controller/spinapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"hash/adler32"
"maps"
"strings"

"github.com/pelletier/go-toml/v2"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -429,6 +430,12 @@ func constructDeployment(ctx context.Context, app *spinv1alpha1.SpinApp, config
}

env := ConstructEnvForApp(ctx, app, spinapp.DefaultHTTPPort, config.Otel)
if app.Spec.Components != nil {
env = append(env, corev1.EnvVar{
Name: "SPIN_COMPONENTS_TO_RETAIN",
Value: strings.Join(app.Spec.Components[:], ","),
})
}

readinessProbe, livenessProbe, err := ConstructPodHealthChecks(app)
if err != nil {
Expand Down Expand Up @@ -457,7 +464,8 @@ func constructDeployment(ctx context.Context, app *spinv1alpha1.SpinApp, config
container = corev1.Container{
Name: app.Name,
Image: *config.SpinImage,
Args: []string{"up", "--listen", fmt.Sprintf("0.0.0.0:%d", spinapp.DefaultHTTPPort), "-f", app.Spec.Image, "--runtime-config-file", "/runtime-config.toml"},
// TODO: add support for --component-id flags to set components to be retained once spintainer supports Spin v3.0
Args: []string{"up", "--listen", fmt.Sprintf("0.0.0.0:%d", spinapp.DefaultHTTPPort), "-f", app.Spec.Image, "--runtime-config-file", "/runtime-config.toml"},
Ports: []corev1.ContainerPort{{
Name: spinapp.HTTPPortName,
ContainerPort: spinapp.DefaultHTTPPort,
Expand Down

0 comments on commit 9668cab

Please sign in to comment.