Skip to content

Commit

Permalink
feat: add example extension hello world service
Browse files Browse the repository at this point in the history
This is just an example of a system extension providing Talos extension
service.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
smira committed Feb 16, 2022
1 parent 0757dc6 commit a05f558
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ COMMON_ARGS += --build-arg=https_proxy=$(https_proxy)
empty :=
space = $(empty) $(empty)

TARGETS = amd-ucode bnx2-bnx2x gvisor intel-ucode
TARGETS = amd-ucode bnx2-bnx2x gvisor hello-world-service intel-ucode
NONFREE_TARGETS =

all: $(TARGETS) ## Builds all known pkgs.
Expand Down
70 changes: 70 additions & 0 deletions examples/hello-world-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Example Talos Extension Service

This repository is an example of an extension service.

## Usage

Enable the extension in the machine configuration before installing Talos:

```yaml
machine:
install:
extensions:
- image: ghcr.io/talos-systems/hello-world-service:<VERSION>
```
Once this example extension is installed, it will provide simple HTTP server which responds with a message on port 80:
```bash
$ curl http://<IP>/
Hello from Talos Linux Extension Service!
```

Extension service appears in the service list (please note the `ext-` prefix):

```bash
$ talosctl services
NODE SERVICE STATE HEALTH LAST CHANGE LAST EVENT
172.20.0.5 apid Running OK 1m37s ago Health check successful
172.20.0.5 containerd Running OK 1m38s ago Health check successful
172.20.0.5 cri Running OK 1m37s ago Health check successful
172.20.0.5 ext-hello-world Running ? 1m38s ago Started task ext-hello-world (PID 1100) for container ext-hello-world
172.20.0.5 kubelet Running OK 1m30s ago Health check successful
172.20.0.5 machined Running ? 1m40s ago Service started as goroutine
172.20.0.5 udevd Running OK 1m38s ago Health check successful
```

Run `talosctl service ext-hello-world` to see the detailed service state:

```bash
$ talosctl service ext-hello-world
NODE 172.20.0.5
ID ext-hello-world
STATE Running
HEALTH ?
EVENTS [Running]: Started task ext-hello-world (PID 1100) for container ext-hello-world (2m47s ago)
[Preparing]: Creating service runner (2m47s ago)
[Preparing]: Running pre state (2m47s ago)
[Waiting]: Waiting for service "containerd" to be "up" (2m48s ago)
[Waiting]: Waiting for service "containerd" to be "up", network (2m49s ago)
```

The service can be started and stopped via `talosctl`:

```bash
$ talosctl service ext-hello-world stop
NODE RESPONSE
172.20.0.5 Service "ext-hello-world" stopped
$ talosctl service ext-hello-world start
NODE RESPONSE
172.20.0.5 Service "ext-hello-world" started
```

Use `talosctl logs` to access service logs:

```bash
$ talosctl logs ext-hello-world
172.20.0.5: 2022/02/16 18:45:21 starting the hello world service
172.20.0.5: 2022/02/16 18:52:33 stopping the hello world service
172.20.0.5: 2022/02/16 18:52:35 starting the hello world service
```
10 changes: 10 additions & 0 deletions examples/hello-world-service/hello-world.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: hello-world
container:
entrypoint: ./hello-world
args:
- --msg
- Talos Linux Extension Service
depends:
- network:
- addresses
restart: always
10 changes: 10 additions & 0 deletions examples/hello-world-service/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v1alpha1
metadata:
name: hello-world-service
version: v1.0.0
author: Andrey Smirnov
description: |
This system extension provides an example Talos extension service.
compatibility:
talos:
version: "> v0.15.0-alpha.2"
29 changes: 29 additions & 0 deletions examples/hello-world-service/pkg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: hello-world-service
variant: scratch
shell: /toolchain/bin/bash
dependencies:
- stage: base
steps:
- env:
GOPATH: /go
prepare:
- |
build:
- |
export PATH=${PATH}:${TOOLCHAIN}/go/bin
cd /pkg/src
CGO_ENABLED=0 go build -o ./hello-world .
install:
- |
mkdir -p /rootfs/usr/local/etc/containers
mkdir -p /rootfs/usr/local/lib/containers/hello-world
cp -p /pkg/src/hello-world /rootfs/usr/local/lib/containers/hello-world/
finalize:
- from: /rootfs
to: /rootfs
- from: /pkg/manifest.yaml
to: /
- from: /pkg/hello-world.yaml
to: /rootfs/usr/local/etc/containers
3 changes: 3 additions & 0 deletions examples/hello-world-service/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/talos-systems/hello-world

go 1.17
53 changes: 53 additions & 0 deletions examples/hello-world-service/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

var msg string

func main() {
flag.StringVar(&msg, "msg", "", "hello message")
flag.Parse()

log.Printf("starting the hello world service")
defer log.Printf("stopping the hello world service")

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

router := http.NewServeMux()
router.HandleFunc("/", HelloServer)

srv := &http.Server{
Addr: ":80",
Handler: router,
}

go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen error: %s", err)
}
}()

<-done

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("server shutdown failed: %s", err)
}
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello from %s!", msg)
}

0 comments on commit a05f558

Please sign in to comment.