Skip to content

Commit

Permalink
Merge pull request #7 from cofide/ping-pong-mesh
Browse files Browse the repository at this point in the history
Add ping-pong-mesh demo and build/lint CI workflow
  • Loading branch information
markgoddard authored Nov 19, 2024
2 parents b8b7240 + c6d038e commit 6a32571
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 4 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: ci
on:
push:
branches:
- main
pull_request:

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Install golangci-lint
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=5m

build-test:
name: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install just
uses: taiki-e/install-action@just
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Setup Ko
uses: ko-build/[email protected]
env:
KO_DOCKER_REPO: ko.local
- name: Build all demos
run: just build-demos
9 changes: 7 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ check-deps:
echo "All dependencies installed"

# Build all demo ping-pong applications
build-demos: build-ping-pong
build-demos: build-ping-pong build-ping-pong-mesh

# Build the ping-pong application
build-ping-pong:
ko build github.com/cofide/cofide-demos/workloads/ping-pong/server
ko build github.com/cofide/cofide-demos/workloads/ping-pong/client
ko build github.com/cofide/cofide-demos/workloads/ping-pong/client

# Build the HTTP ping-pong applications to be deployed in a service mesh
build-ping-pong-mesh:
ko build -L github.com/cofide/cofide-demos/workloads/ping-pong-mesh/server
ko build -L github.com/cofide/cofide-demos/workloads/ping-pong-mesh/client
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ This repository has example applications that are used to demonstrate Cofide's o

The examples include `ping-pong` that can be deployed in a single Cofide trust zone, or federated across trust zones with multiple clusters.

There are two flavours of `ping-pong`:

- `workloads/ping-pong`: SPIFFE mTLS-enabled HTTPS ping pong
- `workloads/ping-pong-mesh`: HTTP ping pong for use with a service mesh

## Deploy a single trust zone Cofide instance

See the [`cofidectl` docs](https://www.github.com/cofide/cofidectl/README.md#quickstart)
Expand Down
87 changes: 87 additions & 0 deletions workloads/ping-pong-mesh/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"context"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"strconv"
"time"
)

func main() {
if err := run(context.Background(), getEnv()); err != nil {
log.Fatal(err)
}
}

type Env struct {
ServerAddress string
ServerPort int
}

func getEnvOrPanic(variable string) string {
v, ok := os.LookupEnv(variable)
if !ok {
panic(fmt.Sprintf("expected environment variable %s not set", variable))
}
return v
}

func getEnvIntWithDefault(variable string, defaultValue int) int {
v, ok := os.LookupEnv(variable)
if !ok {
return defaultValue
}

intValue, err := strconv.Atoi(v)
if err != nil {
return defaultValue
}

return intValue
}

func getEnv() *Env {
return &Env{
ServerAddress: getEnvOrPanic("PING_PONG_SERVICE_HOST"),
ServerPort: getEnvIntWithDefault("PING_PONG_SERVICE_PORT", 80),
}
}

func run(ctx context.Context, env *Env) error {
client := &http.Client{
Transport: &http.Transport{},
}

for {
slog.Info("ping...")
if err := ping(client, env.ServerAddress, env.ServerPort); err != nil {
slog.Error("problem reaching server", "error", err)
}
time.Sleep(5 * time.Second)
}
}

func ping(client *http.Client, serverAddr string, serverPort int) error {
r, err := client.Get((&url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", serverAddr, serverPort),
}).String())

if err != nil {
return err
}
defer r.Body.Close()

body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
slog.Info(string(body))
return nil
}
61 changes: 61 additions & 0 deletions workloads/ping-pong-mesh/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

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

func main() {
if err := run(context.Background(), getEnv()); err != nil {
log.Fatal(err)
}
}

type Env struct {
Port string
}

func getEnvWithDefault(variable string, defaultValue string) string {
v, ok := os.LookupEnv(variable)
if !ok {
return defaultValue
}
return v
}

func getEnv() *Env {
return &Env{
Port: getEnvWithDefault("PORT", ":9090"),
}
}

func run(ctx context.Context, env *Env) error {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)

server := &http.Server{
Addr: env.Port,
Handler: mux,
ReadHeaderTimeout: time.Second * 10,
}

if err := server.ListenAndServe(); err != nil {
return fmt.Errorf("failed to serve: %w", err)
}

return nil
}

func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("...pong"))
if err != nil {
log.Printf("Error writing response: %v", err)
return
}
}
2 changes: 1 addition & 1 deletion workloads/ping-pong/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func main() {
if err := run(context.Background(), getEnv()); err != nil {
log.Fatal("", err)
log.Fatal(err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion workloads/ping-pong/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func main() {
if err := run(context.Background(), getEnv()); err != nil {
log.Fatal("", err)
log.Fatal(err)
}
}

Expand Down

0 comments on commit 6a32571

Please sign in to comment.