Skip to content

Commit fecc5ab

Browse files
Use Kubebuilder
1 parent ca9eb18 commit fecc5ab

File tree

168 files changed

+5577
-3316
lines changed

Some content is hidden

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

168 files changed

+5577
-3316
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
/report.html
2222
/shell.nix
2323
/testing/
24+
25+
bin/

.github/workflows/push-charts.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,21 @@ jobs:
6262
CHART_PACKAGE=$(ls $CHART_DIR/*.tgz)
6363
helm push $CHART_PACKAGE oci://${{ env.REGISTRY }}/${{ github.repository }}/charts/
6464
done
65+
- name: Get all changed reservations Chart.yaml files
66+
id: changed-chart-yaml-files-reservations
67+
uses: tj-actions/changed-files@v46
68+
with:
69+
files: |
70+
reservations/dist/chart/Chart.yaml
71+
- name: Push reservations charts to registry
72+
if: steps.changed-chart-yaml-files-reservations.outputs.all_changed_files != ''
73+
shell: bash
74+
env:
75+
ALL_CHANGED_FILES: ${{ steps.changed-chart-yaml-files-reservations.outputs.all_changed_files }}
76+
run: |
77+
for CHART_FILE in ${ALL_CHANGED_FILES}; do
78+
CHART_DIR=$(dirname $CHART_FILE)
79+
helm package $CHART_DIR --dependency-update --destination $CHART_DIR
80+
CHART_PACKAGE=$(ls $CHART_DIR/*.tgz)
81+
helm push $CHART_PACKAGE oci://${{ env.REGISTRY }}/${{ github.repository }}/charts/
82+
done

.github/workflows/push-images.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,44 @@ jobs:
9898
subject-name: ${{ env.REGISTRY }}/${{ github.repository }}
9999
subject-digest: ${{ steps.push_cortex.outputs.digest }}
100100
push-to-registry: true
101+
# Only build and push the reservations operator image if there are changes
102+
# in the reservations directory.
103+
- name: Get all changed reservations/ files
104+
id: changed_reservations_files
105+
uses: tj-actions/changed-files@v46
106+
with:
107+
files: |
108+
reservations/**
109+
- name: Docker Meta (Cortex Reservations)
110+
if: steps.changed_reservations_files.outputs.all_changed_files != ''
111+
id: meta_cortex_reservations
112+
uses: docker/metadata-action@v5
113+
with:
114+
images: ${{ env.REGISTRY }}/${{ github.repository }}-reservations-operator
115+
tags: |
116+
type=semver,pattern={{version}}
117+
type=semver,pattern={{major}}.{{minor}}
118+
type=sha
119+
latest
120+
- name: Build and Push Cortex Reservations Operator
121+
if: steps.changed_reservations_files.outputs.all_changed_files != ''
122+
id: push_cortex_reservations
123+
uses: docker/build-push-action@v6
124+
with:
125+
context: .
126+
file: Dockerfile.kubebuilder
127+
platforms: linux/amd64,linux/arm64
128+
push: true
129+
tags: ${{ steps.meta_cortex_reservations.outputs.tags }}
130+
labels: ${{ steps.meta_cortex_reservations.outputs.labels }}
131+
build-args: |
132+
GO_MOD_PATH=reservations
133+
GIT_TAG=${{ github.ref_name }}
134+
GIT_COMMIT=${{ github.sha }}
135+
- name: Generate Artifact Attestation for Cortex Reservations
136+
if: steps.changed_reservations_files.outputs.all_changed_files != ''
137+
uses: actions/attest-build-provenance@v2
138+
with:
139+
subject-name: ${{ env.REGISTRY }}/${{ github.repository }}-reservations-operator
140+
subject-digest: ${{ steps.push_cortex_reservations.outputs.digest }}
141+
push-to-registry: true
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Reservations E2E Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test-e2e:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v5
14+
path: reservations
15+
16+
- name: Get all changed files
17+
id: changed-files
18+
uses: tj-actions/changed-files@v46
19+
with:
20+
files: |
21+
**/*
22+
23+
- name: Setup Go
24+
if: steps.changed-files.outputs.files != ''
25+
uses: actions/setup-go@v5
26+
with:
27+
go-version-file: go.mod
28+
29+
- name: Install the latest version of kind
30+
if: steps.changed-files.outputs.files != ''
31+
run: |
32+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
33+
chmod +x ./kind
34+
sudo mv ./kind /usr/local/bin/kind
35+
36+
- name: Verify kind installation
37+
if: steps.changed-files.outputs.files != ''
38+
run: kind version
39+
40+
- name: Running Test e2e
41+
if: steps.changed-files.outputs.files != ''
42+
run: |
43+
go mod tidy
44+
make test-e2e
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Reservations Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
name: Run on Ubuntu
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Clone the code
13+
uses: actions/checkout@v5
14+
path: reservations
15+
16+
- name: Get all changed files
17+
id: changed-files
18+
uses: tj-actions/changed-files@v46
19+
with:
20+
files: |
21+
**/*
22+
23+
- name: Setup Go
24+
if: steps.changed-files.outputs.files != ''
25+
uses: actions/setup-go@v5
26+
with:
27+
go-version-file: go.mod
28+
29+
- name: Running Tests
30+
if: steps.changed-files.outputs.files != ''
31+
run: |
32+
go mod tidy
33+
make test

.github/workflows/update-appversion.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,17 @@ jobs:
5555
git add helm/library/cortex-postgres/Chart.yaml
5656
git commit -m "Bump cortex-postgres chart appVersions to ${{ steps.vars.outputs.sha }} [skip ci]" || echo "No changes to commit"
5757
git push origin HEAD:main
58+
59+
# Only bumped if there are changes in the reservations directory.
60+
- name: Update appVersion in cortex-reservations Chart.yaml
61+
if: steps.changed_reservations_files.outputs.all_changed_files != ''
62+
run: |
63+
sed -i 's/^\([ ]*appVersion:[ ]*\).*/\1"${{ steps.vars.outputs.sha }}"/' reservations/dist/chart/Chart.yaml
64+
- name: Commit and push changes for cortex-reservations
65+
if: steps.changed_reservations_files.outputs.all_changed_files != ''
66+
run: |
67+
git config user.name "github-actions[bot]"
68+
git config user.email "github-actions[bot]@users.noreply.github.com"
69+
git add reservations/dist/chart/Chart.yaml
70+
git commit -m "Bump cortex-reservations chart appVersions to ${{ steps.vars.outputs.sha }} [skip ci]" || echo "No changes to commit"
71+
git push origin HEAD:main

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ build/**
99
# Test binary, built with `go test -c`
1010
*.test
1111

12+
bin/
13+
1214
# Output of the go coverage tool, specifically when used with LiteIDE
1315
*.out
1416

Dockerfile.kubebuilder

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Build the manager binary
2+
FROM golang:1.24 AS builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
# Path of our go.mod
6+
ARG GO_MOD_PATH=.
7+
8+
WORKDIR /workspace
9+
# Copy shared library and api
10+
COPY api /api
11+
COPY lib /lib
12+
# Copy the Go Modules manifests
13+
COPY ${GO_MOD_PATH} .
14+
# cache deps before building and copying source so that we don't need to re-download as much
15+
# and so that source changes don't invalidate our downloaded layer
16+
RUN go mod download
17+
18+
# Build
19+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
20+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
21+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
22+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
23+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
24+
25+
# Use distroless as minimal base image to package the manager binary
26+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
27+
FROM gcr.io/distroless/static:nonroot
28+
WORKDIR /
29+
COPY --from=builder /workspace/manager .
30+
USER 65532:65532
31+
32+
ENTRYPOINT ["/manager"]

Tiltfile

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if not os.getenv('TILT_VALUES_PATH'):
1111
fail("TILT_VALUES_PATH is not set.")
1212
if not os.path.exists(os.getenv('TILT_VALUES_PATH')):
1313
fail("TILT_VALUES_PATH "+ os.getenv('TILT_VALUES_PATH') + " does not exist.")
14+
tilt_values = os.getenv('TILT_VALUES_PATH')
1415

1516
# The upgrade job may take a long time to run, so it is disabled by default.
1617
enable_postgres_upgrade = False
@@ -22,9 +23,21 @@ helm_repo(
2223
labels=['Repositories'],
2324
)
2425

25-
########### CRDs
26-
local('sh helm/sync.sh helm/library/cortex-crds')
27-
k8s_yaml(helm('./helm/library/cortex-crds', name='cortex-crds'))
26+
def kubebuilder_binary_files(path):
27+
"""
28+
Return all usual binary files in a kubebuilder operator path.
29+
Can be used to perform selective watching on code paths for docker builds.
30+
"""
31+
return [path + '/cmd', path + '/api', path + '/internal', path + '/go.mod', path + '/go.sum']
32+
33+
########### Reservations Operator & CRDs
34+
docker_build('ghcr.io/cobaltcore-dev/cortex-reservations-operator', '.',
35+
dockerfile='Dockerfile.kubebuilder',
36+
build_args={'GO_MOD_PATH': 'reservations'},
37+
only=kubebuilder_binary_files('reservations') + ['api/', 'lib/'],
38+
)
39+
local('sh helm/sync.sh reservations/dist/chart')
40+
k8s_yaml(helm('reservations/dist/chart', name='cortex-reservations', values=[tilt_values]))
2841

2942
########### Dev Dependencies
3043
local('sh helm/sync.sh helm/dev/cortex-prometheus-operator')
@@ -62,9 +75,9 @@ k8s_resource('cortex-plutono', port_forwards=[
6275
], labels=['Monitoring'])
6376

6477
########### Cortex Bundles
65-
tilt_values = os.getenv('TILT_VALUES_PATH')
6678
docker_build('ghcr.io/cobaltcore-dev/cortex', '.', only=[
6779
'internal/', 'commands/', 'main.go', 'go.mod', 'go.sum', 'Makefile',
80+
'lib/', 'api/', 'reservations/api/',
6881
])
6982
docker_build('ghcr.io/cobaltcore-dev/cortex-postgres', 'postgres')
7083

@@ -127,7 +140,6 @@ resources = [
127140
'cortex-nova-kpis',
128141
'cortex-nova-scheduler',
129142
'cortex-nova-descheduler',
130-
'cortex-nova-reservations-operator',
131143
],
132144
[(2112, 'metrics'), (8080, 'api')],
133145
),

0 commit comments

Comments
 (0)