Skip to content

Commit 3552f49

Browse files
authored
Bump actions, fix cache, move to chainguard, simplify docker flow (#47)
* Update workflows and simplify docker flow Signed-off-by: clux <[email protected]> * bump all others as well Signed-off-by: clux <[email protected]> * need the image evar when not using meta action Signed-off-by: clux <[email protected]> * hm Signed-off-by: clux <[email protected]> * quoting Signed-off-by: clux <[email protected]> * anyhow, no unwraps in main Signed-off-by: clux <[email protected]> * try a full run to test cache Signed-off-by: clux <[email protected]> * ping hooks - 2023-03-12 Signed-off-by: clux <[email protected]> * bad cache numbers Signed-off-by: clux <[email protected]> * ping hooks - 2023-03-12 Signed-off-by: clux <[email protected]> * try splitting docker jobs.. Signed-off-by: clux <[email protected]> * fix tags on pr Signed-off-by: clux <[email protected]> * man that took forever, lets try it again Signed-off-by: clux <[email protected]> * why isn't this working :( Signed-off-by: clux <[email protected]> * ok apparently args prevent caching? Signed-off-by: clux <[email protected]> * ping hooks - 2023-03-12 Signed-off-by: clux <[email protected]> * out of ideas.. should try something else Signed-off-by: clux <[email protected]> * solve it with chef instead Signed-off-by: clux <[email protected]> * slash Signed-off-by: clux <[email protected]> * slash2 + copy paste errors Signed-off-by: clux <[email protected]> * ping hooks - 2023-03-13 Signed-off-by: clux <[email protected]> * comment to test rebuild Signed-off-by: clux <[email protected]> * screw this.. use builder directly.. Signed-off-by: clux <[email protected]> * justfile Signed-off-by: clux <[email protected]> * fix build Signed-off-by: clux <[email protected]> * get labels from actions inferred Signed-off-by: clux <[email protected]> * remove unnecessary env now Signed-off-by: clux <[email protected]> * simplify justfile Signed-off-by: clux <[email protected]> * last action bump Signed-off-by: clux <[email protected]> --------- Signed-off-by: clux <[email protected]>
1 parent b31fb25 commit 3552f49

File tree

8 files changed

+118
-150
lines changed

8 files changed

+118
-150
lines changed

Diff for: .github/workflows/ci.yml

+93-102
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,41 @@ on:
66
branches:
77
- main
88

9-
env:
10-
IMAGE: clux/controller
11-
129
jobs:
13-
docker:
10+
docker-base:
1411
runs-on: ubuntu-latest
1512
steps:
16-
- uses: actions/checkout@v2
17-
- name: Prepare image tags
18-
id: prep
19-
run: |
20-
TAG=$(grep -E "^version" Cargo.toml | awk -F"\"" '{print $2}' | head -n 1)
21-
IMAGE="${{ env.IMAGE }}"
22-
if curl -sSL https://registry.hub.docker.com/v1/repositories/${IMAGE}/tags | jq -r ".[].name" | grep -q ${TAG}; then
23-
echo "Semver tag ${TAG} already exists - not publishing"
24-
echo ::set-output name=tagsbase::${IMAGE}:latest
25-
echo ::set-output name=tagsotel::${IMAGE}:otel
26-
else
27-
echo "Semver tag ${TAG} not found - publishing"
28-
echo ::set-output name=tagsbase::${IMAGE}:latest,${IMAGE}:${TAG}
29-
echo ::set-output name=tagsotel::${IMAGE}:otel,${IMAGE}:otel-${TAG}
30-
fi
31-
echo ::set-output name=semver::${TAG}
13+
- uses: actions/checkout@v3
3214

33-
- uses: docker/setup-buildx-action@v1
34-
id: buildx
15+
# Build and push with docker buildx
16+
- name: Setup docker buildx
17+
uses: docker/setup-buildx-action@v2
3518

36-
- name: Inspect builder
37-
run: |
38-
echo "Name: ${{ steps.buildx.outputs.name }}"
39-
echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}"
40-
echo "Status: ${{ steps.buildx.outputs.status }}"
41-
echo "Flags: ${{ steps.buildx.outputs.flags }}"
42-
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
43-
44-
- uses: docker/metadata-action@v3
45-
id: docker_meta
46-
with:
47-
images: ${{ env.IMAGE }}
48-
labels: |
49-
org.opencontainers.image.version=${{ steps.prep.outputs.semver }}
50-
org.opencontainers.image.revision=${{ github.sha }}
51-
org.opencontainers.image.title=${{ env.IMAGE }}
52-
53-
- uses: docker/login-action@v1
19+
- name: Configure tags based on git tags + latest
20+
uses: docker/metadata-action@v4
21+
id: meta
22+
with:
23+
images: clux/controller
24+
tags: |
25+
type=pep440,pattern={{version}}
26+
type=raw,value=latest,enable={{is_default_branch}}
27+
type=ref,event=pr
28+
29+
- name: Docker login on main origin
30+
uses: docker/login-action@v2
5431
if: github.event_name != 'pull_request'
5532
with:
5633
username: ${{ secrets.DOCKERHUB_USERNAME }}
5734
password: ${{ secrets.DOCKERHUB_TOKEN }}
5835

59-
- name: Cache Docker layers
60-
uses: actions/cache@v2
61-
with:
62-
path: /tmp/.buildx-cache
63-
key: ${{ runner.os }}-dockerx6-${{ steps.prep.outputs.semver }}
64-
restore-keys: |
65-
${{ runner.os }}-dockerx6-
66-
67-
- uses: actions/cache@v2
36+
- uses: actions/cache@v3
6837
with:
6938
path: |
70-
~/.cargo/registry/index
71-
~/.cargo/registry/cache
72-
~/.cargo/git
73-
target
74-
key: musl-cargo-${{ hashFiles('**/Cargo.toml') }}
75-
39+
~/.cargo/registry/index/
40+
~/.cargo/registry/cache/
41+
~/.cargo/git/db/
42+
target/
43+
key: musl-cargo-${{ hashFiles('**/Cargo.lock') }}
7644

7745
- name: Compile base features
7846
run: |
@@ -82,38 +50,63 @@ jobs:
8250
--mount type=bind,source=$HOME/.cargo/registry,target=/root/.cargo/registry \
8351
--mount type=bind,source=$HOME/.cargo/git,target=/root/.cargo/git \
8452
clux/muslrust:stable \
85-
cargo build --release
53+
cargo build --release --bin controller
8654
cp target/x86_64-unknown-linux-musl/release/controller .
8755
88-
- name: Build and push controller image with base features
89-
uses: docker/build-push-action@v2
56+
- name: Docker buildx and push with base features
57+
uses: docker/build-push-action@v4
9058
with:
91-
builder: ${{ steps.buildx.outputs.name }}
9259
context: .
93-
platforms: linux/amd64
60+
cache-from: type=gha,scope=base
61+
cache-to: type=gha,scope=base,mode=max
9462
push: ${{ github.ref == 'refs/heads/main' }}
95-
tags: ${{ steps.prep.outputs.tagsbase }}
96-
labels: ${{ steps.docker_meta.outputs.labels }}
97-
cache-from: type=gha,scope=version5
98-
cache-to: type=gha,scope=version5,mode=max
99-
63+
tags: ${{ steps.meta.outputs.tags }}
64+
labels: ${{ steps.meta.outputs.labels }}
65+
platforms: linux/amd64
66+
10067
- name: Persist base image build to a tarball
101-
uses: docker/build-push-action@v2
68+
uses: docker/build-push-action@v4
10269
with:
103-
builder: ${{ steps.buildx.outputs.name }}
10470
context: .
10571
platforms: linux/amd64
106-
tags: ${{ steps.prep.outputs.tagsbase }}
107-
labels: ${{ steps.docker_meta.outputs.labels }}
108-
cache-from: type=gha,scope=version5
109-
cache-to: type=gha,scope=version5,mode=max
72+
tags: ${{ steps.meta.outputs.tags }}
73+
cache-from: type=gha,scope=base
11074
outputs: type=docker,dest=/tmp/image.tar
75+
11176
- name: Upload base docker image as artifact for e2e tests
11277
uses: actions/upload-artifact@v3
11378
with:
11479
name: controller-image
11580
path: /tmp/image.tar
11681

82+
docker-otel:
83+
runs-on: ubuntu-latest
84+
steps:
85+
- uses: actions/checkout@v3
86+
87+
# Build and push with docker buildx
88+
- name: Setup docker buildx
89+
uses: docker/setup-buildx-action@v2
90+
91+
- name: Configure tags based on git tags for otel
92+
uses: docker/metadata-action@v4
93+
id: meta
94+
with:
95+
images: clux/controller
96+
tags: |
97+
type=pep440,pattern={{version}},prefix=otel-
98+
type=raw,value=otel,enable={{is_default_branch}}
99+
type=ref,event=pr,prefix=otel-
100+
101+
- uses: actions/cache@v3
102+
with:
103+
path: |
104+
~/.cargo/registry/index/
105+
~/.cargo/registry/cache/
106+
~/.cargo/git/db/
107+
target/
108+
key: musl-cargo-otel-${{ hashFiles('**/Cargo.lock') }}
109+
117110
- name: Compile with telemetry
118111
run: |
119112
mkdir -p ~/.cargo/{git,registry}
@@ -122,26 +115,32 @@ jobs:
122115
--mount type=bind,source=$HOME/.cargo/registry,target=/root/.cargo/registry \
123116
--mount type=bind,source=$HOME/.cargo/git,target=/root/.cargo/git \
124117
clux/muslrust:stable \
125-
cargo build --features=telemetry --release
118+
cargo build --features=telemetry --release --bin controller
126119
cp target/x86_64-unknown-linux-musl/release/controller .
127120
128-
- name: Build and push controller image with telemetry
129-
uses: docker/build-push-action@v2
121+
- name: Docker login on main origin
122+
uses: docker/login-action@v2
123+
if: github.event_name != 'pull_request'
124+
with:
125+
username: ${{ secrets.DOCKERHUB_USERNAME }}
126+
password: ${{ secrets.DOCKERHUB_TOKEN }}
127+
128+
- name: Docker buildx and push with telemetry
129+
uses: docker/build-push-action@v4
130130
with:
131-
builder: ${{ steps.buildx.outputs.name }}
132131
context: .
133-
platforms: linux/amd64
132+
cache-from: type=gha,scope=otel
133+
cache-to: type=gha,scope=otel,mode=max
134134
push: ${{ github.ref == 'refs/heads/main' }}
135-
tags: ${{ steps.prep.outputs.tagsotel }}
136-
labels: ${{ steps.docker_meta.outputs.labels }}
137-
cache-from: type=gha,scope=version5
138-
cache-to: type=gha,scope=version5,mode=max
135+
tags: ${{ steps.meta.outputs.tags }}
136+
labels: ${{ steps.meta.outputs.labels }}
137+
platforms: linux/amd64
139138

140139
e2e:
141140
runs-on: ubuntu-latest
142-
needs: [docker]
141+
needs: [docker-base]
143142
steps:
144-
- uses: actions/checkout@v2
143+
- uses: actions/checkout@v3
145144
- uses: nolar/setup-k3d-k3s@v1
146145
with:
147146
version: v1.25
@@ -168,30 +167,26 @@ jobs:
168167
lint:
169168
runs-on: ubuntu-latest
170169
steps:
171-
- uses: actions/checkout@v2
170+
- uses: actions/checkout@v3
172171
- name: Install protoc
173172
run: sudo apt-get install -y protobuf-compiler
174-
- uses: actions-rs/toolchain@v1
173+
- uses: dtolnay/rust-toolchain@stable
175174
with:
176175
toolchain: nightly
177176
components: rustfmt,clippy
178-
override: true
179-
- name: Run rustfmt
180-
run: cargo +nightly fmt -- --check
181-
- uses: actions-rs/clippy-check@v1
177+
- run: cargo +nightly fmt -- --check
178+
179+
- uses: giraffate/clippy-action@v1
182180
with:
183-
args: --all-features
184-
token: ${{ secrets.GITHUB_TOKEN }}
181+
reporter: 'github-pr-review'
182+
github_token: ${{ secrets.GITHUB_TOKEN }}
183+
clippy_flags: --all-features
185184

186185
integration:
187186
runs-on: ubuntu-latest
188187
steps:
189-
- uses: actions/checkout@v2
190-
- uses: actions-rs/toolchain@v1
191-
with:
192-
override: true
193-
toolchain: stable
194-
profile: minimal
188+
- uses: actions/checkout@v3
189+
- uses: dtolnay/rust-toolchain@stable
195190
- uses: Swatinem/rust-cache@v2
196191
- uses: nolar/setup-k3d-k3s@v1
197192
with:
@@ -209,14 +204,10 @@ jobs:
209204
unit:
210205
runs-on: ubuntu-latest
211206
steps:
212-
- uses: actions/checkout@v2
207+
- uses: actions/checkout@v3
213208
with:
214209
fetch-depth: 2
215-
- uses: actions-rs/toolchain@v1
216-
with:
217-
override: true
218-
toolchain: stable
219-
profile: minimal
210+
- uses: dtolnay/rust-toolchain@stable
220211
- uses: Swatinem/rust-cache@v2
221212

222213
# Real CI work starts here

Diff for: .github/workflows/rustfmt.yml

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,20 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout sources
13-
uses: actions/checkout@v2
14-
15-
- name: Install nightly toolchain with rustfmt available
16-
uses: actions-rs/toolchain@v1
13+
uses: actions/checkout@v3
14+
- uses: dtolnay/rust-toolchain@stable
1715
with:
1816
toolchain: nightly
19-
override: true
2017
components: rustfmt
21-
22-
- run: rustfmt +nightly --edition 2018 $(find . -type f -iname *.rs)
18+
- run: cargo +nightly fmt
2319

2420
- name: Create Pull Request
2521
uses: peter-evans/create-pull-request@v3
2622
with:
2723
commit-message: rustfmt
2824
signoff: true
2925
title: rustfmt
30-
body: |
31-
Changes from `rustfmt +nightly --edition 2018 $(find . -type f -iname *.rs)`.
26+
body: Changes from `cargo +nightly fmt`.
3227
branch: rustfmt
3328
# Delete branch when merged
3429
delete-branch: true

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ opentelemetry = { version = "0.18.0", features = ["trace", "rt-tokio"] }
4343
opentelemetry-otlp = { version = "0.11.0", features = ["tokio"], optional = true }
4444
tonic = { version = "0.8.3", optional = true }
4545
thiserror = "1.0.37"
46+
anyhow = "1.0.69"
4647

4748
[dev-dependencies]
4849
assert-json-diff = "2.0.2"

Diff for: Dockerfile

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
FROM gcr.io/distroless/static:nonroot
2-
LABEL org.opencontainers.image.source=https://github.com/kube-rs/controller-rs
3-
LABEL org.opencontainers.image.description="Kube Example Controller"
4-
LABEL org.opencontainers.image.licenses="Apache-2.0"
1+
FROM cgr.dev/chainguard/static
52
COPY --chown=nonroot:nonroot ./controller /app/
63
EXPOSE 8080
74
ENTRYPOINT ["/app/controller"]

Diff for: README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ OPENTELEMETRY_ENDPOINT_URL=https://0.0.0.0:55680 RUST_LOG=info,kube=trace,contro
5050
```
5151

5252
### In-cluster
53-
Use either your locally built image or the one from dockerhub (using opentemetry features by default). Edit the [deployment](./yaml/deployment.yaml)'s image tag appropriately, and then:
53+
Use either your locally built image or the one from dockerhub (using opentelemetry features by default). Edit the [deployment](./yaml/deployment.yaml)'s image tag appropriately, and then:
5454

5555
```sh
5656
kubectl apply -f yaml/deployment.yaml
@@ -60,8 +60,6 @@ kubectl port-forward service/doc-controller 8080:80
6060

6161
To build and deploy the image quickly, we recommend using [tilt](https://tilt.dev/), via `tilt up` instead.
6262

63-
**NB**: namespace is assumed to be `default`. If you need a different namespace, you can replace `default` with whatever you want in the yaml and set the namespace in your current-context to get all the commands here to work.
64-
6563
## Usage
6664
In either of the run scenarios, your app is listening on port `8080`, and it will observe `Document` events.
6765

@@ -104,9 +102,9 @@ $ curl 0.0.0.0:8080/
104102
{"last_event":"2019-07-17T22:31:37.591320068Z"}
105103
```
106104

107-
The metrics will be auto-scraped if you have a standard [`PodMonitor` for `prometheus.io/scrape`](https://github.com/prometheus-community/helm-charts/blob/b69e89e73326e8b504102a75d668dc4351fcdb78/charts/prometheus/values.yaml#L1608-L1650).
105+
The metrics will be scraped by prometheus if you setup a `PodMonitor` or `ServiceMonitor` for it.
108106

109107
### Events
110-
The example `reconciler` only checks the `.spec.hidden` bool. If it does, it updates the `.status` object to reflect whether or not the instance `is_hidden`. It also sends a kubernetes event associated with the controller. It is visible at the bottom of `kubectl describe doc samuel`.
108+
The example `reconciler` only checks the `.spec.hidden` bool. If it does, it updates the `.status` object to reflect whether or not the instance `is_hidden`. It also sends a Kubernetes event associated with the controller. It is visible at the bottom of `kubectl describe doc samuel`.
111109

112-
While this controller has no child objects configured, there is a [`configmapgen_controller`](https://github.com/kube-rs/kube/blob/main/examples/configmapgen_controller.rs) example in [kube-rs](https://github.com/kube-rs/kube/).
110+
To extend this controller for a real-world setting. Consider looking at the [kube.rs controller guide](https://kube.rs/controllers/intro/).

0 commit comments

Comments
 (0)