From ebd2314ac02fbf48c016dac78b39d8397d6dfb72 Mon Sep 17 00:00:00 2001 From: Elie CHARRA Date: Wed, 21 Feb 2024 11:24:22 +0100 Subject: [PATCH] Initial commit --- .github/workflows/build.yml | 29 ++++++++++++++++++++++++++ app/Dockerfile | 11 ++++++++++ app/go.mod | 3 +++ app/main.go | 41 +++++++++++++++++++++++++++++++++++++ infra/app/deployment.yaml | 29 ++++++++++++++++++++++++++ infra/app/service.yaml | 10 +++++++++ infra/app/spacelift.yaml | 10 +++++++++ infra/tf/main.tf | 9 ++++++++ 8 files changed, 142 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 app/Dockerfile create mode 100644 app/go.mod create mode 100644 app/main.go create mode 100644 infra/app/deployment.yaml create mode 100644 infra/app/service.yaml create mode 100644 infra/app/spacelift.yaml create mode 100644 infra/tf/main.tf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..5dfc216 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,29 @@ +name: Build docker image +on: + push: + branches: + - main +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: ./app + platforms: linux/amd64,linux/arm64 + push: true + tags: | + ghcr.io/spacelift-io/spacelift-operator-demo:latest + ghcr.io/spacelift-io/spacelift-operator-demo:${{ github.sha }} diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 0000000..3ce8635 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.21.7 as builder +WORKDIR /build +COPY . . +RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o demo . + +FROM scratch +COPY --from=builder /build/demo /bin/demo +ENTRYPOINT ["/bin/demo"] + + + diff --git a/app/go.mod b/app/go.mod new file mode 100644 index 0000000..7ffef20 --- /dev/null +++ b/app/go.mod @@ -0,0 +1,3 @@ +module github.com/spacelift-io/spacelift-operator-demo + +go 1.21.7 diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..3685264 --- /dev/null +++ b/app/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "errors" + "fmt" + "io" + "log" + "net/http" + "os" +) + +func readSecret() string { + f, err := os.Open("/secrets/RANDOM_STRING") + if err != nil { + return "" + } + data, err := io.ReadAll(f) + if err != nil { + return "" + } + return string(data) +} + +func main() { + listenAddr := ":8888" + http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { + log.Println(request.Method, request.URL.String()) + if request.URL.Path != "/" { + writer.WriteHeader(http.StatusNotFound) + return + } + resp := fmt.Sprintf("RANDOM_STRING=%s", readSecret()) + _, _ = writer.Write([]byte(resp)) + }) + log.Printf("Listening on %s\n", listenAddr) + if err := http.ListenAndServe(listenAddr, nil); err != nil { + if !errors.Is(err, http.ErrServerClosed) { + log.Fatal(err) + } + } +} diff --git a/infra/app/deployment.yaml b/infra/app/deployment.yaml new file mode 100644 index 0000000..6c198e8 --- /dev/null +++ b/infra/app/deployment.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: demo +spec: + replicas: 1 + revisionHistoryLimit: 3 + selector: + matchLabels: + app: demo + template: + metadata: + labels: + app: demo + spec: + containers: + - image: ghcr.io/spacelift-io/spacelift-operator-demo:latest + name: demo + ports: + - containerPort: 8888 + name: demo-port + volumeMounts: + - name: secrets + mountPath: /etc/secrets + readOnly: true + volumes: + - name: secrets + secret: + secretName: stack-output-spacelift-operator-demo diff --git a/infra/app/service.yaml b/infra/app/service.yaml new file mode 100644 index 0000000..e2c4c22 --- /dev/null +++ b/infra/app/service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: demo +spec: + ports: + - port: 80 + targetPort: demo-port + selector: + app: demo diff --git a/infra/app/spacelift.yaml b/infra/app/spacelift.yaml new file mode 100644 index 0000000..382769d --- /dev/null +++ b/infra/app/spacelift.yaml @@ -0,0 +1,10 @@ +--- +apiVersion: app.spacelift.io/v1beta1 +kind: Run +spec: + stackName: spacelift-operator-demo +metadata: + name: spacelift-operator-demo + annotations: + argocd.argoproj.io/hook: Sync + argocd.argoproj.io/sync-wave: "-1" diff --git a/infra/tf/main.tf b/infra/tf/main.tf new file mode 100644 index 0000000..fa5fc6b --- /dev/null +++ b/infra/tf/main.tf @@ -0,0 +1,9 @@ +resource "random_string" "random" { + length = 12 + special = true +} + +output "RANDOM_STRING" { + value = random_string.random.result + sensitive = true +}