Skip to content

Commit

Permalink
feat: add demo apps and workflows to build
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechmares committed Feb 19, 2024
1 parent e79b919 commit daedf91
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 0 deletions.
160 changes: 160 additions & 0 deletions .github/workflows/build-apps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
name: Build demo apps

on:
push:
branches:
- main
paths:
- 'apps/**'

jobs:
hello-world:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: checkout
uses: actions/checkout@v4

- name: setup qemu
uses: docker/setup-qemu-action@v3

- name: setup docker
uses: docker/setup-buildx-action@v3

- name: login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: docker metadata
uses: docker/metadata-action@v5
id: hello_world_meta
with:
images: |
ghcr.io/${{ github.repository }}/apps/hello-world
tags: |
type=sha,format=long
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
- name: build and push
uses: docker/build-push-action@v5
with:
context: /apps/hello-world
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.hello_world_meta.outputs.tags }}
labels: ${{ steps.hello_world_meta.outputs.labels }}

probes:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: checkout
uses: actions/checkout@v4

- name: setup qemu
uses: docker/setup-qemu-action@v3

- name: setup docker
uses: docker/setup-buildx-action@v3

- name: login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: docker metadata
uses: docker/metadata-action@v5
id: probe_meta
with:
images: |
ghcr.io/${{ github.repository }}/apps/probes
tags: |
type=sha,format=long
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
- name: build and push
uses: docker/build-push-action@v5
with:
context: /apps/probes
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.probe_meta.outputs.tags }}
labels: ${{ steps.probe_meta.outputs.labels }}

blue-green:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: checkout
uses: actions/checkout@v4

- name: setup qemu
uses: docker/setup-qemu-action@v3

- name: setup docker
uses: docker/setup-buildx-action@v3

- name: login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: docker metadata (blue)
uses: docker/metadata-action@v5
id: blue_meta
with:

images: |
ghcr.io/${{ github.repository }}/apps/blue-green
tags: |
type=sha,format=long
type=raw,value=blue
- name: docker metadata (green)
uses: docker/metadata-action@v5
id: green_meta
with:
images: |
ghcr.io/${{ github.repository }}/apps/blue-green
tags: |
type=sha,format=long
type=raw,value=green
- name: build and push (blue)
uses: docker/build-push-action@v5
with:
context: /apps/blue-green
platforms: linux/amd64,linux/arm64
build-args: |
IS_BLUE=true
push: true
tags: ${{ steps.green_meta.outputs.tags }}
labels: ${{ steps.green_meta.outputs.labels }}

- name: build and push (green)
uses: docker/build-push-action@v5
with:
context: /apps/blue-green
platforms: linux/amd64,linux/arm64
build-args: |
IS_BLUE=false
push: true
tags: ${{ steps.green_meta.outputs.tags }}
labels: ${{ steps.green_meta.outputs.labels }}

19 changes: 19 additions & 0 deletions apps/blue-green/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.22 as builder

ARG IS_BLUE=true

WORKDIR /go/src/app

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.IsBlue=${IS_BLUE}" -a -installsuffix cgo -o blue-green .

FROM debian:bookwork-slim as runtime

WORKDIR /app

COPY --from=builder /go/src/app/blue-green .

CMD [ "/app/blue-green" ]
5 changes: 5 additions & 0 deletions apps/blue-green/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/vojtechmares/kubernetes-training/apps/blue-green

go 1.22.0

require github.com/google/uuid v1.6.0 // indirect
2 changes: 2 additions & 0 deletions apps/blue-green/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
81 changes: 81 additions & 0 deletions apps/blue-green/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"text/template"

"github.com/google/uuid"
)

var IsBlue string = "true"

const html = `
<!DOCTYPE html>
<html>
<head>
<title>Hello, Kubernetes! | {{ .Hostname }}</title>
<style>
body {
font-family: Helvetica;
background-color: {{ .Color }};
color: #fff;
}
pre {
margin: 0.25rem 0;
font-size: 1.5rem;
}
</style>
</head>
<body>
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center;">
<h1 style="font-size: 4rem;">Hello, Kubernetes! 👋</h1>
<pre>hostname: {{ .Hostname }}</pre>
<pre>instanceID: {{ .InstanceID }}</pre>
</div>
</body>
</html>
`

func main() {
instanceID := uuid.New().String()
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}

var hexColor string = "#15803d" // green

isBlue, err := strconv.ParseBool(IsBlue)
if err != nil {
log.Fatal(err)
}

if isBlue {
hexColor = "#1d4ed8"
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tpl, err := template.New("index.html").Parse(html)
if err != nil {
log.Println(err)
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}

err = tpl.Execute(w, map[string]string{"Hostname": hostname, "InstanceID": instanceID, "Color": hexColor})
if err != nil {
log.Println(err)
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}

w.Header().Set("Content-Type", "text/html")
})

log.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}
17 changes: 17 additions & 0 deletions apps/hello-world/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.22 as builder

WORKDIR /go/src/app

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o hello-world .

FROM debian:bookwork-slim as runtime

WORKDIR /app

COPY --from=builder /go/src/app/hello-world .

CMD [ "/app/hello-world" ]
5 changes: 5 additions & 0 deletions apps/hello-world/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/vojtechmares/kubernetes-training/apps/probes

go 1.22.0

require github.com/google/uuid v1.6.0
2 changes: 2 additions & 0 deletions apps/hello-world/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
65 changes: 65 additions & 0 deletions apps/hello-world/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"text/template"

"github.com/google/uuid"
)

const html = `
<!DOCTYPE html>
<html>
<head>
<title>Hello, Kubernetes! | {{ .Hostname }}</title>
<style>
body {
font-family: Helvetica;
}
pre {
margin: 0.25rem 0;
font-size: 1.5rem;
}
</style>
</head>
<body style="font-family: Helvetica">
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center;">
<h1 style="font-size: 4rem;">Hello, Kubernetes! 👋</h1>
<pre>hostname: {{ .Hostname }}</pre>
<pre>instanceID: {{ .InstanceID }}</pre>
</div>
</body>
</html>
`

func main() {
instanceID := uuid.New().String()
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tpl, err := template.New("index.html").Parse(html)
if err != nil {
log.Println(err)
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}

err = tpl.Execute(w, map[string]string{"Hostname": hostname, "InstanceID": instanceID})
if err != nil {
log.Println(err)
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
return
}

w.Header().Set("Content-Type", "text/html")
})

log.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}
17 changes: 17 additions & 0 deletions apps/probes/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.22 as builder

WORKDIR /go/src/app

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o probes .

FROM debian:bookwork-slim as runtime

WORKDIR /app

COPY --from=builder /go/src/app/probes .

CMD [ "/app/probes" ]
4 changes: 4 additions & 0 deletions apps/probes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module github.com/vojtechmares/kubernetes-training/apps/probes

go 1.22.0

2 changes: 2 additions & 0 deletions apps/probes/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Loading

0 comments on commit daedf91

Please sign in to comment.