Skip to content

Commit 20d0682

Browse files
committed
Implement a cache daemon that can move the cache into place quickly on demand
- a long running background process we'll deploy as a daemonset on k8s nodes - moves the cache into place via hardlinking the golden copy from a shared location - operates using the host filesystem, and thus has pretty priviledged access
1 parent 44d2d0f commit 20d0682

26 files changed

+1126
-37
lines changed

Contributing.md

+4
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,7 @@ We also need to build the server docker image and push it to Gadget's container
182182
```bash
183183
make upload-container-image version=0.0.x
184184
```
185+
186+
### Getting PASETO tokens locally
187+
188+
You can sign PASETO tokens locally with this handy online tool: https://token.dev/paseto/. Ensure you use the V2 algorithm in the public mode, and copy the PASTEO public and private key from the `development` folder.

Dockerfile

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ EOF
88

99
WORKDIR /app
1010

11+
RUN GRPC_HEALTH_PROBE_VERSION=v0.4.23 \
12+
&& curl -Lfso /bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-${TARGETARCH} \
13+
&& chmod +x /bin/grpc_health_probe
14+
1115
COPY flake.nix flake.lock ./
1216
COPY development ./development
1317

@@ -20,15 +24,11 @@ RUN nix develop -c go mod download
2024

2125
# copy everything else and build the project
2226
COPY . ./
23-
RUN nix develop -c make release/server_linux_$TARGETARCH
27+
RUN nix develop -c make release/server_linux_$TARGETARCH release/cached_linux_$TARGETARCH
2428

25-
FROM buildpack-deps:bullseye AS build-release-stage
29+
FROM debian:bullseye-slim AS release-stage
2630
ARG TARGETARCH
2731

28-
RUN GRPC_HEALTH_PROBE_VERSION=v0.4.23 \
29-
&& curl -Lfso /bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-${TARGETARCH} \
30-
&& chmod +x /bin/grpc_health_probe
31-
3232
RUN useradd -ms /bin/bash main
3333
USER main
3434
WORKDIR /home/main
@@ -37,11 +37,15 @@ RUN mkdir -p /home/main/secrets
3737
VOLUME /home/main/secrets/tls
3838
VOLUME /home/main/secrets/paseto
3939

40+
COPY --from=build-stage /bin/grpc_health_probe /bin/grpc_health_probe
41+
COPY --from=build-stage /app/release/cached_linux_${TARGETARCH} cached
4042
COPY --from=build-stage /app/release/server_linux_${TARGETARCH} server
43+
4144
COPY migrations migrations
4245
COPY entrypoint.sh entrypoint.sh
4346

44-
# smoke test -- ensure the server command can run
47+
# smoke test -- ensure the commands can run
4548
RUN ./server --help
49+
RUN ./cached --help
4650

4751
ENTRYPOINT ["./entrypoint.sh"]

Makefile

+27-13
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ DB_USER ?= postgres
88
DB_PASS ?= password
99
DB_URI := postgres://$(DB_USER):$(DB_PASS)@$(DB_HOST):5432/dl
1010

11-
GRPC_PORT ?= 5051
1211
GRPC_HOST ?= localhost
12+
GRPC_PORT ?= 5051
13+
GRPC_CACHED_PORT ?= 5053
1314

14-
DEV_TOKEN_ADMIN ?= v2.public.eyJzdWIiOiJhZG1pbiIsImlhdCI6IjIwMjEtMTAtMTVUMTE6MjA6MDAuMDM0WiJ9WtEey8KfQQRy21xoHq1C5KQatEevk8RxS47k4bRfMwVCPHumZmVuk6ADcfDHTmSnMtEGfFXdxnYOhRP6Clb_Dw
15-
DEV_TOKEN_PROJECT_1 ?= v2.public.eyJzdWIiOiIxIiwiaWF0IjoiMjAyMS0xMC0xNVQxMToyMDowMC4wMzVaIn2MQ14RfIGpoEycCuvRu9J3CZp6PppUXf5l5w8uKKydN3C31z6f6GgOEPNcnwODqBnX7Pjarpz4i2uzWEqLgQYD
15+
DEV_TOKEN_ADMIN ?= v2.public.eyJzdWIiOiJhZG1pbiJ9yt40HNkcyOUtDeFa_WPS6vi0WiE4zWngDGJLh17TuYvssTudCbOdQEkVDRD-mSNTXLgSRDXUkO-AaEr4ZLO4BQ
16+
DEV_TOKEN_PROJECT_1 ?= v2.public.eyJzdWIiOiIxIn2jV7FOdEXafKDtAnVyDgI4fmIbqU7C1iuhKiL0lDnG1Z5-j6_ObNDd75sZvLZ159-X98_mP4qvwzui0w8pjt8F
17+
DEV_SHARED_READER_TOKEN ?= v2.public.eyJzdWIiOiJzaGFyZWQtcmVhZGVyIn1CxWdB02s9el0Wt7qReARZ-7JtIb4Zj3D4Oiji1yXHqj0orkpbcVlswVUiekECJC16d1NrHwD2FWSwRORZn8gK
1618

1719
PKG_GO_FILES := $(shell find pkg/ -type f -name '*.go')
1820
INTERNAL_GO_FILES := $(shell find internal/ -type f -name '*.go')
@@ -63,7 +65,7 @@ development/server.key:
6365

6466
development/server.crt: development/server.key
6567

66-
build: internal/pb/fs.pb.go internal/pb/fs_grpc.pb.go bin/server bin/client development/server.crt
68+
build: internal/pb/fs.pb.go internal/pb/fs_grpc.pb.go internal/pb/cache.pb.go internal/pb/cache_grpc.pb.go bin/server bin/client bin/cached development/server.crt
6769

6870
lint:
6971
golangci-lint run
@@ -86,8 +88,9 @@ release/migrations.tar.gz: migrations/*
8688
tar -zcf $@ migrations
8789

8890
release: build
89-
release: release/server_linux_amd64 release/server_macos_amd64 release/server_macos_arm64
90-
release: release/client_linux_amd64 release/client_macos_amd64 release/client_macos_arm64
91+
release: release/server_linux_amd64 release/server_macos_amd64 release/server_macos_arm64 release/server_linux_arm64
92+
release: release/client_linux_amd64 release/client_macos_amd64 release/client_macos_arm64 release/client_linux_arm64
93+
release: release/cached_linux_amd64 release/cached_macos_amd64 release/cached_macos_arm64 release/cached_linux_arm64
9194
release: release/migrations.tar.gz
9295

9396
test: export DB_URI = postgres://$(DB_USER):$(DB_PASS)@$(DB_HOST):5432/dl_tests
@@ -121,6 +124,11 @@ server-profile: export DL_ENV=dev
121124
server-profile: internal/pb/fs.pb.go internal/pb/fs_grpc.pb.go
122125
go run cmd/server/main.go --dburi $(DB_URI) --port $(GRPC_PORT) --profile cpu.prof --log-level info
123126

127+
cached: export DL_ENV=dev
128+
cached: export DL_TOKEN=$(DEV_SHARED_READER_TOKEN)
129+
cached: internal/pb/cache.pb.go internal/pb/cache_grpc.pb.go
130+
go run cmd/cached/main.go --upstream-host $(GRPC_HOST) --upstream-port $(GRPC_PORT) --port $(GRPC_CACHED_PORT) --staging-path tmp/cache-stage
131+
124132
client-update: export DL_TOKEN=$(DEV_TOKEN_PROJECT_1)
125133
client-update: export DL_SKIP_SSL_VERIFICATION=1
126134
client-update:
@@ -169,6 +177,11 @@ client-getcache: export DL_SKIP_SSL_VERIFICATION=1
169177
client-getcache:
170178
go run cmd/client/main.go getcache --host $(GRPC_HOST) --path input/cache
171179

180+
client-getcache-from-daemon: export DL_TOKEN=$(DEV_TOKEN_ADMIN)
181+
client-getcache-from-daemon: export DL_SKIP_SSL_VERIFICATION=1
182+
client-getcache-from-daemon:
183+
mkdir -p tmp/pods/test-pod/volumes/example && go run cmd/client/main.go getcache-from-daemon --host $(GRPC_HOST) --port $(GRPC_CACHED_PORT) input/cache
184+
172185
client-gc-contents: export DL_TOKEN=$(DEV_TOKEN_ADMIN)
173186
client-gc-contents: export DL_SKIP_SSL_VERIFICATION=1
174187
client-gc-contents:
@@ -197,15 +210,14 @@ else
197210
docker push gcr.io/gadget-core-production/dateilager:latest
198211
endif
199212

200-
upload-prerelease-container-image: release
201-
docker build -t gcr.io/gadget-core-production/dateilager:$(GIT_COMMIT) .
202-
docker push gcr.io/gadget-core-production/dateilager:$(GIT_COMMIT)
213+
upload-prerelease-container-image:
214+
docker build --platform linux/arm64,linux/amd64 --push -t us-central1-docker.pkg.dev/gadget-core-production/core-production/dateilager:pre-$(GIT_COMMIT) .
203215

204216
build-local-container:
205-
docker build -t dl-local:latest .
217+
docker build --load -t dl-local:dev .
206218

207219
run-container: release build-local-container
208-
docker run --rm -it -p 127.0.0.1:$(GRPC_PORT):$(GRPC_PORT)/tcp -v ./development:/home/main/secrets/tls -v ./development:/home/main/secrets/paseto dl-local:latest $(GRPC_PORT) "postgres://$(DB_USER):$(DB_PASS)@host.docker.internal:5432" dl
220+
docker run --rm -it -p 127.0.0.1:$(GRPC_PORT):$(GRPC_PORT)/tcp -v ./development:/home/main/secrets/tls -v ./development:/home/main/secrets/paseto dl-local:dev $(GRPC_PORT) "postgres://$(DB_USER):$(DB_PASS)@host.docker.internal:5432" dl
209221

210222
gen-docs:
211223
go run cmd/gen-docs/main.go
@@ -243,8 +255,10 @@ else
243255
cd js && npm install
244256
endif
245257

246-
js/src/pb: $(PROTO_FILES)
247-
cd js && mkdir -p ./src/pb && npx protoc --experimental_allow_proto3_optional --ts_out ./src/pb --ts_opt long_type_bigint,ts_nocheck,eslint_disable,add_pb_suffix --proto_path ../internal/pb/ ../$^
258+
js/src/pb: js/node_modules $(PROTO_FILES)
259+
cd js && mkdir -p ./src/pb && for proto in $^; do \
260+
npx protoc --experimental_allow_proto3_optional --ts_out ./src/pb --ts_opt long_type_bigint,ts_nocheck,eslint_disable,add_pb_suffix --proto_path ../internal/pb/ ../$$proto; \
261+
done
248262

249263
js/dist: js/node_modules js/src/pb
250264
cd js && npm run build

cmd/cached/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/gadget-inc/dateilager/pkg/cli"
4+
5+
func main() {
6+
cli.CacheDaemonExecute()
7+
}

development/paseto.key

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MC4CAQAwBQYDK2VwBCIEILTL+0PfTOIQcn2VPkpxMwf6Gbt9n4UEFDjZ4RuUKjd0
3+
-----END PRIVATE KEY-----

development/paseto.pub

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-----BEGIN PUBLIC KEY-----
2-
MCowBQYDK2VwAyEASKQkA/AxlNCdOHTnp5McesmQ+y756VTtGz8Xrt1G0fs=
3-
-----END PUBLIC KEY-----
2+
MCowBQYDK2VwAyEAHrnbu7wEfAP9cGBOAHHwmH4Wsot1ciXBHwBBXQ4gsaI=
3+
-----END PUBLIC KEY-----

flake.nix

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
postgresql = pkgs.postgresql_14;
5656

57+
glibcLocales = pkgs.glibcLocales;
5758
## DateiLager outputs
5859

5960
dateilager = callPackage ./. {
@@ -72,6 +73,7 @@
7273
flake.packages.postgresql
7374
flake.packages.dev
7475
flake.packages.clean
76+
flake.packages.glibcLocales
7577
git
7678
protobuf
7779
protoc-gen-go

internal/key/key.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package key
22

33
import (
4+
"time"
5+
46
"github.com/gadget-inc/dateilager/pkg/stringutil"
57
"go.opentelemetry.io/otel/attribute"
68
"go.uber.org/zap"
@@ -36,7 +38,9 @@ const (
3638
Worker = IntKey("dl.worker")
3739
WorkerCount = IntKey("dl.worker_count")
3840
Ignores = StringSliceKey("dl.ignores")
41+
DurationMS = DurationKey("dl.duration_ms")
3942
CloneToProject = Int64Key("dl.clone_to_project")
43+
CachePath = StringKey("dl.cache_path")
4044
)
4145

4246
var (
@@ -148,3 +152,13 @@ func (isk Int64SliceKey) Field(value []int64) zap.Field {
148152
func (isk Int64SliceKey) Attribute(value []int64) attribute.KeyValue {
149153
return attribute.Int64Slice(string(isk), value)
150154
}
155+
156+
type DurationKey string
157+
158+
func (dk DurationKey) Field(value time.Duration) zap.Field {
159+
return zap.Duration(string(dk), value)
160+
}
161+
162+
func (dk DurationKey) Attribute(value time.Duration) attribute.KeyValue {
163+
return attribute.Float64(string(dk), float64(value.Milliseconds()))
164+
}

0 commit comments

Comments
 (0)