Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cmd/grus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type config struct {
DB string
TemplatesDir string
TemplateSeedDir string
TemplatesBackend string
S3Endpoint string
S3Bucket string
S3Prefix string
S3Insecure bool
S3AccessKey string
S3SecretKey string
Driver string
Namespace string
PublicURL string
Expand Down Expand Up @@ -48,6 +55,11 @@ func loadConfig(args []string, getenv func(string) string) (config, error) {
db := str("db", "GRUS_DB", "./grus.db", "SQLite database file")
templatesDir := str("templates-dir", "GRUS_TEMPLATES_DIR", "./templates", "template store root")
seedDir := str("template-seed-dir", "GRUS_TEMPLATE_SEED_DIR", "", "directory of template seeds, one subdirectory per template")
templatesBackend := str("templates-backend", "GRUS_TEMPLATES_BACKEND", "fs", "template store backend: fs or s3")
s3Endpoint := str("s3-endpoint", "GRUS_S3_ENDPOINT", "", "S3 endpoint for the s3 template backend: host:port or http(s) URL")
s3Bucket := str("s3-bucket", "GRUS_S3_BUCKET", "", "S3 bucket of the s3 template backend")
s3Prefix := str("s3-prefix", "GRUS_S3_PREFIX", "", "key prefix inside the bucket, optional")
s3Insecure := str("s3-insecure", "GRUS_S3_INSECURE", "false", "use plain HTTP for a host:port S3 endpoint (true/false)")
driverName := str("driver", "GRUS_DRIVER", "kubernetes", "instance driver: kubernetes or process")
namespace := str("namespace", "GRUS_NAMESPACE", "", "kubernetes namespace of the instances")
publicURL := str("public-url", "GRUS_PUBLIC_URL", "", "URL instances use to reach the controller (required)")
Expand All @@ -74,6 +86,12 @@ func loadConfig(args []string, getenv func(string) string) (config, error) {
DB: *db,
TemplatesDir: *templatesDir,
TemplateSeedDir: *seedDir,
TemplatesBackend: *templatesBackend,
S3Endpoint: *s3Endpoint,
S3Bucket: *s3Bucket,
S3Prefix: *s3Prefix,
S3AccessKey: getenv("AWS_ACCESS_KEY_ID"),
S3SecretKey: getenv("AWS_SECRET_ACCESS_KEY"),
Driver: *driverName,
Namespace: *namespace,
PublicURL: *publicURL,
Expand Down Expand Up @@ -101,6 +119,9 @@ func loadConfig(args []string, getenv func(string) string) (config, error) {
if cfg.ReconcileInterval, err = time.ParseDuration(*interval); err != nil {
return config{}, fmt.Errorf("GRUS_RECONCILE_INTERVAL: %w", err)
}
if cfg.S3Insecure, err = strconv.ParseBool(*s3Insecure); err != nil {
return config{}, fmt.Errorf("GRUS_S3_INSECURE: %w", err)
}

if err := cfg.validate(); err != nil {
return config{}, err
Expand All @@ -124,6 +145,15 @@ func (c config) validate() error {
if c.PublicURL == "" {
return errors.New("GRUS_PUBLIC_URL is required")
}
switch c.TemplatesBackend {
case "fs":
case "s3":
if err := c.validateS3(); err != nil {
return err
}
default:
return fmt.Errorf("GRUS_TEMPLATES_BACKEND must be fs or s3, got %q", c.TemplatesBackend)
}
if len(c.AdminToken) < minAdminTokenLen {
return fmt.Errorf("GRUS_ADMIN_TOKEN must be at least %d characters", minAdminTokenLen)
}
Expand All @@ -136,6 +166,24 @@ func (c config) validate() error {
return nil
}

// validateS3 checks that the s3 template backend has everything it needs. Credentials are checked for
// presence only: their values never appear in an error.
func (c config) validateS3() error {
if c.S3Endpoint == "" {
return errors.New("GRUS_S3_ENDPOINT is required for the s3 template backend")
}
if c.S3Bucket == "" {
return errors.New("GRUS_S3_BUCKET is required for the s3 template backend")
}
if c.S3AccessKey == "" {
return errors.New("AWS_ACCESS_KEY_ID is required for the s3 template backend")
}
if c.S3SecretKey == "" {
return errors.New("AWS_SECRET_ACCESS_KEY is required for the s3 template backend")
}
return nil
}

func defaultHostID(driverName, namespace string) string {
if driverName == "kubernetes" {
return "k8s-" + namespace
Expand Down
70 changes: 70 additions & 0 deletions cmd/grus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,73 @@ func TestSplitListDropsEmptyEntries(t *testing.T) {
t.Errorf("splitList = %v, want [a b]", got)
}
}

func s3Env() map[string]string {
env := validEnv()
env["GRUS_TEMPLATES_BACKEND"] = "s3"
env["GRUS_S3_ENDPOINT"] = "minio.local:9000"
env["GRUS_S3_BUCKET"] = "grus-templates-dev"
env["GRUS_S3_PREFIX"] = "envs/dev"
env["GRUS_S3_INSECURE"] = "true"
env["AWS_ACCESS_KEY_ID"] = "access-key-id"
env["AWS_SECRET_ACCESS_KEY"] = "secret-access-key-value"
return env
}

func TestLoadConfigTemplatesDefaultToFS(t *testing.T) {
cfg, err := loadConfig(nil, fakeEnv(validEnv()))
if err != nil {
t.Fatalf("loadConfig: %v", err)
}
if cfg.TemplatesBackend != "fs" {
t.Errorf("TemplatesBackend = %q, want fs", cfg.TemplatesBackend)
}
}

func TestLoadConfigReadsS3Settings(t *testing.T) {
cfg, err := loadConfig(nil, fakeEnv(s3Env()))
if err != nil {
t.Fatalf("loadConfig: %v", err)
}
if cfg.TemplatesBackend != "s3" || cfg.S3Endpoint != "minio.local:9000" || cfg.S3Bucket != "grus-templates-dev" {
t.Errorf("s3 settings = %q %q %q", cfg.TemplatesBackend, cfg.S3Endpoint, cfg.S3Bucket)
}
if cfg.S3Prefix != "envs/dev" || !cfg.S3Insecure {
t.Errorf("S3Prefix = %q, S3Insecure = %v, want envs/dev and true", cfg.S3Prefix, cfg.S3Insecure)
}
if cfg.S3AccessKey != "access-key-id" || cfg.S3SecretKey != "secret-access-key-value" {
t.Error("S3 credentials were not read from AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY")
}
}

func TestLoadConfigRejectsInvalidTemplateBackends(t *testing.T) {
cases := map[string]func(env map[string]string){
"unknown backend": func(env map[string]string) { env["GRUS_TEMPLATES_BACKEND"] = "gcs" },
"s3 without endpoint": func(env map[string]string) { delete(env, "GRUS_S3_ENDPOINT") },
"s3 without bucket": func(env map[string]string) { delete(env, "GRUS_S3_BUCKET") },
"s3 without access key": func(env map[string]string) { delete(env, "AWS_ACCESS_KEY_ID") },
"s3 without secret key": func(env map[string]string) { delete(env, "AWS_SECRET_ACCESS_KEY") },
"invalid insecure flag": func(env map[string]string) { env["GRUS_S3_INSECURE"] = "sometimes" },
}
for name, mutate := range cases {
t.Run(name, func(t *testing.T) {
env := s3Env()
mutate(env)
if _, err := loadConfig(nil, fakeEnv(env)); err == nil {
t.Fatalf("loadConfig accepted %s", name)
}
})
}
}

func TestLoadConfigErrorsNeverContainS3Secret(t *testing.T) {
env := s3Env()
delete(env, "GRUS_S3_BUCKET")
_, err := loadConfig(nil, fakeEnv(env))
if err == nil {
t.Fatal("loadConfig accepted a missing bucket")
}
if strings.Contains(err.Error(), "secret-access-key-value") {
t.Fatalf("error leaks the secret key: %v", err)
}
}
25 changes: 24 additions & 1 deletion cmd/grus/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newStack(ctx context.Context, cfg config, logger *slog.Logger) (*stack, err
if err != nil {
return nil, fmt.Errorf("open store: %w", err)
}
tpl, err := templates.NewFS(cfg.TemplatesDir)
tpl, err := openTemplates(cfg)
if err != nil {
_ = st.Close()
return nil, fmt.Errorf("open templates: %w", err)
Expand Down Expand Up @@ -85,6 +85,29 @@ func newStack(ctx context.Context, cfg config, logger *slog.Logger) (*stack, err
return &stack{store: st, tpl: tpl, drv: drv, hub: hub, ctrl: ctrl, handler: handler}, nil
}

// openTemplates opens the template store of the configured backend: fs (default) or s3.
func openTemplates(cfg config) (templates.Store, error) {
if cfg.TemplatesBackend == "s3" {
s, err := templates.NewS3(templates.S3Config{
Endpoint: cfg.S3Endpoint,
Bucket: cfg.S3Bucket,
Prefix: cfg.S3Prefix,
Insecure: cfg.S3Insecure,
AccessKey: cfg.S3AccessKey,
SecretKey: cfg.S3SecretKey,
})
if err != nil {
return nil, err
}
return s, nil
}
s, err := templates.NewFS(cfg.TemplatesDir)
if err != nil {
return nil, err
}
return s, nil
}

// runController is the `grus controller` subcommand. SIGINT or SIGTERM stops the HTTP server and the
// reconcile loop. It never stops instances: the next start adopts them.
func runController(args []string) error {
Expand Down
55 changes: 55 additions & 0 deletions deploy/helm/grus/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,61 @@ spec:
- name: GRUS_TEMPLATE_SEED_DIR
value: /etc/grus/seed
{{- end }}
{{- $s3 := .Values.templates.s3 }}
{{- if eq .Values.templates.backend "s3" }}
{{- $_ := required "templates.s3.existingSecret is required when templates.backend is s3" $s3.existingSecret }}
{{- if not (or $s3.endpoint $s3.existingConfigMap) }}
{{- fail "templates.s3.endpoint or templates.s3.existingConfigMap is required when templates.backend is s3" }}
{{- end }}
{{- if not (or $s3.bucket $s3.existingConfigMap) }}
{{- fail "templates.s3.bucket or templates.s3.existingConfigMap is required when templates.backend is s3" }}
{{- end }}
- name: GRUS_TEMPLATES_BACKEND
value: s3
{{- if $s3.existingConfigMap }}
- name: BUCKET_HOST
valueFrom:
configMapKeyRef:
name: {{ $s3.existingConfigMap | quote }}
key: BUCKET_HOST
- name: BUCKET_PORT
valueFrom:
configMapKeyRef:
name: {{ $s3.existingConfigMap | quote }}
key: BUCKET_PORT
{{- end }}
- name: GRUS_S3_ENDPOINT
{{- if $s3.endpoint }}
value: {{ $s3.endpoint | quote }}
{{- else }}
value: "$(BUCKET_HOST):$(BUCKET_PORT)"
{{- end }}
- name: GRUS_S3_BUCKET
{{- if $s3.bucket }}
value: {{ $s3.bucket | quote }}
{{- else }}
valueFrom:
configMapKeyRef:
name: {{ $s3.existingConfigMap | quote }}
key: BUCKET_NAME
{{- end }}
- name: GRUS_S3_PREFIX
value: {{ $s3.prefix | quote }}
- name: GRUS_S3_INSECURE
value: {{ ternary "true" "false" $s3.insecure | quote }}
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: {{ $s3.existingSecret | quote }}
key: AWS_ACCESS_KEY_ID
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: {{ $s3.existingSecret | quote }}
key: AWS_SECRET_ACCESS_KEY
{{- else if ne .Values.templates.backend "fs" }}
{{- fail "templates.backend must be fs or s3" }}
{{- end }}
- name: GRUS_DRIVER
value: kubernetes
- name: GRUS_NAMESPACE
Expand Down
16 changes: 16 additions & 0 deletions deploy/helm/grus/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,20 @@ web:
# content: |
# motd=Lobby
templates:
# Template store backend: fs (the file store on the data volume, default) or s3 (an S3 bucket, for
# example a Rook ObjectBucketClaim). The database stays on the data volume either way.
backend: fs
s3:
# Endpoint as host:port or an http(s) URL. Empty takes BUCKET_HOST:BUCKET_PORT from existingConfigMap.
endpoint: ""
# Bucket name. Empty takes BUCKET_NAME from existingConfigMap.
bucket: ""
# Key prefix inside the bucket, optional.
prefix: ""
# Plain HTTP for a host:port endpoint, for example the in-cluster Rook RGW on port 80.
insecure: false
# Existing Secret with AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY (the Rook OBC Secret).
existingSecret: ""
# Existing ConfigMap with BUCKET_NAME, BUCKET_HOST and BUCKET_PORT (the Rook OBC ConfigMap).
existingConfigMap: ""
seed: {}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.26.10

require (
github.com/danielgtaylor/huma/v2 v2.41.0
github.com/minio/minio-go/v7 v7.4.1
github.com/pressly/goose/v3 v3.30.1
k8s.io/api v0.36.6
k8s.io/apimachinery v0.36.6
Expand All @@ -12,6 +13,7 @@ require (
)

require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.1.0 // indirect
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
Expand All @@ -25,20 +27,30 @@ require (
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.20.4 // indirect
github.com/klauspost/cpuid/v2 v2.4.2 // indirect
github.com/klauspost/crc32 v1.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/mfridman/interpolate v0.2.0 // indirect
github.com/minio/crc64nvme v1.1.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/moby/spdystream v0.5.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/go-strftime v1.1.0 // indirect
github.com/philhofer/fwd v1.2.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/sethvargo/go-retry v0.5.0 // indirect
github.com/tinylib/msgp v1.6.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/zeebo/xxh3 v1.1.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.6 // indirect
golang.org/x/crypto v0.59.0 // indirect
golang.org/x/net v0.63.0 // indirect
golang.org/x/oauth2 v0.34.0 // indirect
golang.org/x/sync v0.25.0 // indirect
Expand All @@ -49,6 +61,7 @@ require (
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.140.0 // indirect
k8s.io/kube-openapi v0.0.0-20260317180543-43fb72c5454a // indirect
Expand Down
Loading