Skip to content

Commit 220ae87

Browse files
committed
Create a skeleton operator
Description: - Adds an operator to manage `platform.gov.uk/v1.JobRequest` custom resources - Developed from [kube-builder](https://book.kubebuilder.io/quick-start.html) - Further PRs will clear up unnecessary files in the repo - alphagov/govuk-infrastructure#4172
1 parent 786f4e4 commit 220ae87

53 files changed

Lines changed: 3446 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.custom-gcl.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file configures golangci-lint with module plugins.
2+
# When you run 'make lint', it will automatically build a custom golangci-lint binary
3+
# with all the plugins listed below.
4+
#
5+
# See: https://golangci-lint.run/plugins/module-plugins/
6+
version: v2.11.4
7+
plugins:
8+
# logcheck validates structured logging calls and parameters (e.g., balanced key-value pairs)
9+
- module: "sigs.k8s.io/logtools"
10+
import: "sigs.k8s.io/logtools/logcheck/gclplugin"
11+
version: latest

.devcontainer/devcontainer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.25",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {
6+
"moby": false,
7+
"dockerDefaultAddressPool": "base=172.30.0.0/16,size=24"
8+
},
9+
"ghcr.io/devcontainers/features/git:1": {},
10+
"ghcr.io/devcontainers/features/common-utils:2": {
11+
"upgradePackages": true
12+
}
13+
},
14+
15+
"runArgs": ["--privileged", "--init"],
16+
17+
"customizations": {
18+
"vscode": {
19+
"settings": {
20+
"terminal.integrated.shell.linux": "/bin/bash"
21+
},
22+
"extensions": [
23+
"ms-kubernetes-tools.vscode-kubernetes-tools",
24+
"ms-azuretools.vscode-docker"
25+
]
26+
}
27+
},
28+
29+
"remoteEnv": {
30+
"GO111MODULE": "on"
31+
},
32+
33+
"onCreateCommand": "bash .devcontainer/post-install.sh"
34+
}
35+

.devcontainer/post-install.sh

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
echo "===================================="
5+
echo "Kubebuilder DevContainer Setup"
6+
echo "===================================="
7+
8+
# Verify running as root (required for installing to /usr/local/bin and /etc)
9+
if [ "$(id -u)" -ne 0 ]; then
10+
echo "ERROR: This script must be run as root"
11+
exit 1
12+
fi
13+
14+
echo ""
15+
echo "Detecting system architecture..."
16+
# Detect architecture using uname
17+
MACHINE=$(uname -m)
18+
case "${MACHINE}" in
19+
x86_64)
20+
ARCH="amd64"
21+
;;
22+
aarch64|arm64)
23+
ARCH="arm64"
24+
;;
25+
*)
26+
echo "WARNING: Unsupported architecture ${MACHINE}, defaulting to amd64"
27+
ARCH="amd64"
28+
;;
29+
esac
30+
echo "Architecture: ${ARCH}"
31+
32+
echo ""
33+
echo "------------------------------------"
34+
echo "Setting up bash completion..."
35+
echo "------------------------------------"
36+
37+
BASH_COMPLETIONS_DIR="/usr/share/bash-completion/completions"
38+
39+
# Enable bash-completion in root's .bashrc (devcontainer runs as root)
40+
if ! grep -q "source /usr/share/bash-completion/bash_completion" ~/.bashrc 2>/dev/null; then
41+
echo 'source /usr/share/bash-completion/bash_completion' >> ~/.bashrc
42+
echo "Added bash-completion to .bashrc"
43+
fi
44+
45+
echo ""
46+
echo "------------------------------------"
47+
echo "Installing development tools..."
48+
echo "------------------------------------"
49+
50+
# Install kind
51+
if ! command -v kind &> /dev/null; then
52+
echo "Installing kind..."
53+
curl -Lo /usr/local/bin/kind "https://kind.sigs.k8s.io/dl/latest/kind-linux-${ARCH}"
54+
chmod +x /usr/local/bin/kind
55+
echo "kind installed successfully"
56+
fi
57+
58+
# Generate kind bash completion
59+
if command -v kind &> /dev/null; then
60+
if kind completion bash > "${BASH_COMPLETIONS_DIR}/kind" 2>/dev/null; then
61+
echo "kind completion installed"
62+
else
63+
echo "WARNING: Failed to generate kind completion"
64+
fi
65+
fi
66+
67+
# Install kubebuilder
68+
if ! command -v kubebuilder &> /dev/null; then
69+
echo "Installing kubebuilder..."
70+
curl -Lo /usr/local/bin/kubebuilder "https://go.kubebuilder.io/dl/latest/linux/${ARCH}"
71+
chmod +x /usr/local/bin/kubebuilder
72+
echo "kubebuilder installed successfully"
73+
fi
74+
75+
# Generate kubebuilder bash completion
76+
if command -v kubebuilder &> /dev/null; then
77+
if kubebuilder completion bash > "${BASH_COMPLETIONS_DIR}/kubebuilder" 2>/dev/null; then
78+
echo "kubebuilder completion installed"
79+
else
80+
echo "WARNING: Failed to generate kubebuilder completion"
81+
fi
82+
fi
83+
84+
# Install kubectl
85+
if ! command -v kubectl &> /dev/null; then
86+
echo "Installing kubectl..."
87+
KUBECTL_VERSION=$(curl -Ls https://dl.k8s.io/release/stable.txt)
88+
curl -Lo /usr/local/bin/kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl"
89+
chmod +x /usr/local/bin/kubectl
90+
echo "kubectl installed successfully"
91+
fi
92+
93+
# Generate kubectl bash completion
94+
if command -v kubectl &> /dev/null; then
95+
if kubectl completion bash > "${BASH_COMPLETIONS_DIR}/kubectl" 2>/dev/null; then
96+
echo "kubectl completion installed"
97+
else
98+
echo "WARNING: Failed to generate kubectl completion"
99+
fi
100+
fi
101+
102+
# Generate Docker bash completion
103+
if command -v docker &> /dev/null; then
104+
if docker completion bash > "${BASH_COMPLETIONS_DIR}/docker" 2>/dev/null; then
105+
echo "docker completion installed"
106+
else
107+
echo "WARNING: Failed to generate docker completion"
108+
fi
109+
fi
110+
111+
echo ""
112+
echo "------------------------------------"
113+
echo "Configuring Docker environment..."
114+
echo "------------------------------------"
115+
116+
# Wait for Docker to be ready
117+
echo "Waiting for Docker to be ready..."
118+
for i in {1..30}; do
119+
if docker info >/dev/null 2>&1; then
120+
echo "Docker is ready"
121+
break
122+
fi
123+
if [ "$i" -eq 30 ]; then
124+
echo "WARNING: Docker not ready after 30s"
125+
fi
126+
sleep 1
127+
done
128+
129+
# Create kind network (ignore if already exists)
130+
if ! docker network inspect kind >/dev/null 2>&1; then
131+
if docker network create kind >/dev/null 2>&1; then
132+
echo "Created kind network"
133+
else
134+
echo "WARNING: Failed to create kind network (may already exist)"
135+
fi
136+
fi
137+
138+
echo ""
139+
echo "------------------------------------"
140+
echo "Verifying installations..."
141+
echo "------------------------------------"
142+
kind version
143+
kubebuilder version
144+
kubectl version --client
145+
docker --version
146+
go version
147+
148+
echo ""
149+
echo "===================================="
150+
echo "DevContainer ready!"
151+
echo "===================================="
152+
echo "All development tools installed successfully."
153+
echo "You can now start building Kubernetes operators."

.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore everything by default and re-include only needed files
3+
**
4+
5+
# Re-include Go source files (but not *_test.go)
6+
!**/*.go
7+
**/*_test.go
8+
9+
# Re-include Go module files
10+
!go.mod
11+
!go.sum

.github/workflows/lint.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions: {}
8+
9+
jobs:
10+
lint:
11+
permissions:
12+
contents: read
13+
name: Run on Ubuntu
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Clone the code
17+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
18+
with:
19+
persist-credentials: false
20+
21+
- name: Setup Go
22+
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
23+
with:
24+
go-version-file: go.mod
25+
26+
- name: Check linter configuration
27+
run: make lint-config
28+
- name: Run linter
29+
run: make lint

.github/workflows/test-chart.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Test Chart
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions: {}
8+
9+
jobs:
10+
test-e2e:
11+
permissions:
12+
contents: read
13+
name: Run on Ubuntu
14+
runs-on: ubuntu-latest
15+
env:
16+
IMG: controller:latest
17+
steps:
18+
- name: Clone the code
19+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
20+
with:
21+
persist-credentials: false
22+
23+
- name: Setup Go
24+
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
25+
with:
26+
go-version-file: go.mod
27+
28+
- name: Install the latest version of kind
29+
run: |
30+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-$(go env GOARCH)
31+
chmod +x ./kind
32+
sudo mv ./kind /usr/local/bin/kind
33+
34+
- name: Verify kind installation
35+
run: kind version
36+
37+
- name: Create kind cluster
38+
run: kind create cluster
39+
40+
- name: Prepare govuk-job-request-operator
41+
run: |
42+
go mod tidy
43+
make docker-build
44+
kind load docker-image $IMG
45+
46+
- name: Install Helm
47+
run: make install-helm
48+
49+
- name: Lint Helm Chart
50+
run: |
51+
helm lint ./dist/chart
52+
53+
# TODO: Uncomment if cert-manager is enabled
54+
# - name: Install cert-manager via Helm (wait for readiness)
55+
# run: |
56+
# helm repo add jetstack https://charts.jetstack.io
57+
# helm repo update
58+
# helm install cert-manager jetstack/cert-manager \
59+
# --namespace cert-manager \
60+
# --create-namespace \
61+
# --set crds.enabled=true \
62+
# --wait \
63+
# --timeout 300s
64+
65+
# TODO: Uncomment if Prometheus is enabled
66+
# - name: Install Prometheus Operator CRDs
67+
# run: |
68+
# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
69+
# helm repo update
70+
# helm install prometheus-crds prometheus-community/prometheus-operator-crds
71+
72+
- name: Deploy manager via Helm
73+
run: |
74+
make helm-deploy
75+
76+
- name: Check Helm release status
77+
run: |
78+
make helm-status

.github/workflows/test-e2e.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: E2E Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions: {}
8+
9+
jobs:
10+
test-e2e:
11+
permissions:
12+
contents: read
13+
name: Run on Ubuntu
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Clone the code
17+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
18+
with:
19+
persist-credentials: false
20+
21+
- name: Setup Go
22+
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
23+
with:
24+
go-version-file: go.mod
25+
26+
- name: Install the latest version of kind
27+
run: |
28+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-$(go env GOARCH)
29+
chmod +x ./kind
30+
sudo mv ./kind /usr/local/bin/kind
31+
32+
- name: Verify kind installation
33+
run: kind version
34+
35+
- name: Running Test e2e
36+
run: |
37+
go mod tidy
38+
make test-e2e

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions: {}
8+
9+
jobs:
10+
test:
11+
permissions:
12+
contents: read
13+
name: Run on Ubuntu
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Clone the code
17+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
18+
with:
19+
persist-credentials: false
20+
21+
- name: Setup Go
22+
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
23+
with:
24+
go-version-file: go.mod
25+
26+
- name: Running Tests
27+
run: |
28+
go mod tidy
29+
make test

0 commit comments

Comments
 (0)