Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eliecharra committed Mar 12, 2024
0 parents commit 93abdd3
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build docker image
on:
push:
branches:
- main
permissions:
contents: read
packages: write
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 }}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Deployment

```shell
# Create a stack
kubectl apply -f infra/spacelift/stack.yaml &&\
kubectl wait --for=jsonpath='{.status.ready}'=true stack/demo-stack --timeout 1h

# Trigger a run
kubectl delete --ignore-not-found=true -f infra/spacelift/run.yaml &&\
kubectl apply -f infra/spacelift/run.yaml &&\
kubectl wait --for=jsonpath='{.status.argo.health}'=Healthy run/spacelift-operator-demo --timeout 1h

# Deploy the app
helm upgrade --install operator-demo ./infra/helm/ --set 'image.tag=58de3fcc53659909a9779d3a5ed71aef1959b5a8'
```
12 changes: 12 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.21.7 as builder
WORKDIR /build
COPY . .
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o demo .

FROM scratch
USER 1000
COPY --from=builder /build/demo /bin/demo
ENTRYPOINT ["/bin/demo"]



3 changes: 3 additions & 0 deletions app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/spacelift-io/spacelift-operator-demo

go 1.21.7
32 changes: 32 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"errors"
"fmt"
"log"
"net/http"
"os"
)

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
}
secrets := os.Environ()
body := ""
for i := 0; i < len(secrets); i++ {
body += fmt.Sprintf("%s\n", secrets[i])
}
_, _ = writer.Write([]byte(body))
})
log.Printf("Listening on %s\n", listenAddr)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}
}
23 changes: 23 additions & 0 deletions infra/helm/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions infra/helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: operator-demo
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.0.0"
47 changes: 47 additions & 0 deletions infra/helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "operator-demo.name" -}}
{{- default .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "operator-demo.fullname" -}}
{{- $name := default .Chart.Name }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "operator-demo.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "operator-demo.labels" -}}
helm.sh/chart: {{ include "operator-demo.chart" . }}
{{ include "operator-demo.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "operator-demo.selectorLabels" -}}
app.kubernetes.io/name: {{ include "operator-demo.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
32 changes: 32 additions & 0 deletions infra/helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "operator-demo.fullname" . }}
labels:
{{- include "operator-demo.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "operator-demo.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "operator-demo.labels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
livenessProbe:
{{- toYaml .Values.livenessProbe | nindent 12 }}
readinessProbe:
{{- toYaml .Values.readinessProbe | nindent 12 }}
envFrom:
- secretRef:
optional: false
name: {{ .Values.app.envFromSecret }}
15 changes: 15 additions & 0 deletions infra/helm/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "operator-demo.fullname" . }}
labels:
{{- include "operator-demo.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "operator-demo.selectorLabels" . | nindent 4 }}
18 changes: 18 additions & 0 deletions infra/helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
replicaCount: 1
image:
repository: ghcr.io/spacelift-io/spacelift-operator-demo
pullPolicy: IfNotPresent
tag: ""
service:
type: ClusterIP
port: 8888
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
app:
envFromSecret: stack-output-spacelift-operator-demo
10 changes: 10 additions & 0 deletions infra/spacelift/run.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: app.spacelift.io/v1beta1
kind: Run
metadata:
name: spacelift-operator-demo
annotations:
argocd.argoproj.io/hook: Sync
argocd.argoproj.io/sync-wave: "-1"
spec:
stackName: demo-stack
27 changes: 27 additions & 0 deletions infra/spacelift/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: app.spacelift.io/v1beta1
kind: Stack
metadata:
name: demo-stack
annotations:
argocd.argoproj.io/sync-wave: "-2"
spec:
name: spacelift-operator-demo
commitSHA: 58de3fcc53659909a9779d3a5ed71aef1959b5a8
settings:
administrative: false
space: spacelift-operator-01HR9KQ590MFT6H6ETFJ657KR6
namespace: spacelift-io
repository: spacelift-operator-demo
projectRoot: infra/tf
branch: main
managesStateFile: true
awsIntegration:
id: 01HRQ4YMJQTP5R6K8Q9N1NB3XM
read: true
write: true
vendorConfig:
terraform:
version: 1.6.2
workflowTool: OPEN_TOFU
labels:
- argo
36 changes: 36 additions & 0 deletions infra/tf/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions infra/tf/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "BUCKET_URL" {
value = "https://${aws_s3_bucket.bucket.bucket_regional_domain_name}"
}
3 changes: 3 additions & 0 deletions infra/tf/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "eu-west-1"
}
10 changes: 10 additions & 0 deletions infra/tf/s3.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "random_string" "random" {
length = 6
special = false
numeric = false
upper = false
}

resource "aws_s3_bucket" "bucket" {
bucket = "bucket-${random_string.random.result}"
}

0 comments on commit 93abdd3

Please sign in to comment.