-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example extension hello world service
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
Showing
7 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/talos-systems/hello-world | ||
|
||
go 1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |