From 5317d19febef73cb55f36081a88590c6bed93ec3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 19:03:42 +0000 Subject: [PATCH 1/2] feat(operator): let configSchema fields set the env var name A configSchema field was always exported as an env var named after the field. The minecraft-java "Enforce whitelist" field (WHITELIST_ENABLED) therefore never reached the itzg image, which only reads ENABLE_WHITELIST, and the whitelist stayed off. Add an optional envName to ConfigField. The value stays under the field name in spec.config and the config Secret; only the container env var name changes, so modules can fix a wrong mapping without orphaning saved values. Renamed fields add a part to the config hash so the pod rolls, while templates without envName keep their hash. A schema where an envName makes two env fields export the same variable is refused. The gp-module preview, web types and module authoring docs follow. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- changelog/unreleased/config-field-env-name.md | 7 ++ .../crds/gameplane.local_backups.yaml | 2 +- .../crds/gameplane.local_backupschedules.yaml | 2 +- .../crds/gameplane.local_clusters.yaml | 2 +- .../crds/gameplane.local_gameservers.yaml | 2 +- .../crds/gameplane.local_gametemplates.yaml | 15 +++- .../crds/gameplane.local_modules.yaml | 2 +- .../crds/gameplane.local_modulesources.yaml | 2 +- .../crds/gameplane.local_networkcaptures.yaml | 2 +- .../crds/gameplane.local_restores.yaml | 2 +- .../gameplane.local_backups.yaml | 2 +- .../gameplane.local_backupschedules.yaml | 2 +- .../gameplane.local_clusters.yaml | 2 +- .../gameplane.local_gameservers.yaml | 2 +- .../gameplane.local_gametemplates.yaml | 15 +++- .../gameplane.local_modules.yaml | 2 +- .../gameplane.local_modulesources.yaml | 2 +- .../gameplane.local_networkcaptures.yaml | 2 +- .../gameplane.local_restores.yaml | 2 +- docs/module-authoring.md | 15 ++++ gp-module/internal/preview/preview.go | 8 +- operator/api/v1alpha1/gametemplate_types.go | 13 +++- .../crd/gameplane.local_gametemplates.yaml | 13 +++- .../internal/controller/gameserver_config.go | 43 ++++++++++- .../controller/gameserver_config_test.go | 75 +++++++++++++++++++ web/src/types.ts | 2 + 26 files changed, 208 insertions(+), 30 deletions(-) create mode 100644 changelog/unreleased/config-field-env-name.md diff --git a/changelog/unreleased/config-field-env-name.md b/changelog/unreleased/config-field-env-name.md new file mode 100644 index 000000000..6dc44f18c --- /dev/null +++ b/changelog/unreleased/config-field-env-name.md @@ -0,0 +1,7 @@ +# Config fields can export a different env var name + +A module's `configSchema` field can set `envName` to export its value under a +different env var than the field name. The stored config key does not change, +so existing servers keep their saved values. The Minecraft module uses it to +make "Enforce whitelist" turn on the whitelist (the itzg image reads +`ENABLE_WHITELIST`, not `WHITELIST_ENABLED`). diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml index 63d29c4f4..b7d5c4711 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: backups.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml index 4beb31166..e6695977a 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: backupschedules.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml index 18b3b8f55..1467ee5e2 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: clusters.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml index c9f4e670b..260321878 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: gameservers.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml index fb1cca85b..f8fb8839b 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: gametemplates.gameplane.local spec: @@ -1130,6 +1130,14 @@ spec: items: type: string type: array + envName: + description: |- + EnvName, when set, is the env var name the value is exported as + instead of Name (Target "env" only). It lets a module keep a stable + config key while pointing it at the variable the game image reads. + maxLength: 253 + pattern: ^[A-Za-z_][A-Za-z0-9_]*$ + type: string max: description: |- Max is the maximum numeric value for int-typed fields. Bounds are @@ -1156,8 +1164,9 @@ spec: type: integer name: description: |- - Name is the field identifier (also used as an env var when - Target is "env"). + Name is the field identifier and the key in GameServer.spec.config. + It is also the env var name when Target is "env" and EnvName is + empty. minLength: 1 type: string required: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml index 5d74582b1..816dce8a0 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: modules.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml index 8cf33a5ec..f42edcdfa 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: modulesources.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml index f2cd36c44..82c98466e 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: networkcaptures.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml index d2f47f0ec..a798be25e 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: restores.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_backups.yaml b/charts/gameplane/crd-manifests/gameplane.local_backups.yaml index 63d29c4f4..b7d5c4711 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_backups.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_backups.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: backups.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml b/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml index 4beb31166..e6695977a 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: backupschedules.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml b/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml index 18b3b8f55..1467ee5e2 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: clusters.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml b/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml index c9f4e670b..260321878 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: gameservers.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml b/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml index fb1cca85b..f8fb8839b 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: gametemplates.gameplane.local spec: @@ -1130,6 +1130,14 @@ spec: items: type: string type: array + envName: + description: |- + EnvName, when set, is the env var name the value is exported as + instead of Name (Target "env" only). It lets a module keep a stable + config key while pointing it at the variable the game image reads. + maxLength: 253 + pattern: ^[A-Za-z_][A-Za-z0-9_]*$ + type: string max: description: |- Max is the maximum numeric value for int-typed fields. Bounds are @@ -1156,8 +1164,9 @@ spec: type: integer name: description: |- - Name is the field identifier (also used as an env var when - Target is "env"). + Name is the field identifier and the key in GameServer.spec.config. + It is also the env var name when Target is "env" and EnvName is + empty. minLength: 1 type: string required: diff --git a/charts/gameplane/crd-manifests/gameplane.local_modules.yaml b/charts/gameplane/crd-manifests/gameplane.local_modules.yaml index 5d74582b1..816dce8a0 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_modules.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_modules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: modules.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml b/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml index 8cf33a5ec..f42edcdfa 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: modulesources.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml b/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml index f2cd36c44..82c98466e 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: networkcaptures.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_restores.yaml b/charts/gameplane/crd-manifests/gameplane.local_restores.yaml index d2f47f0ec..a798be25e 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_restores.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_restores.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 3b2e34cfb50a4b2312502522937d45442c012cb313195cd2a556f69af01f3a96 + gameplane.local/crd-bundle-sha256: 54dcc2313c027593c0807ea53927179da7938ac305054f79289d82e222caddd7 controller-gen.kubebuilder.io/version: v0.22.0 name: restores.gameplane.local spec: diff --git a/docs/module-authoring.md b/docs/module-authoring.md index 3d998b2a8..eba683649 100644 --- a/docs/module-authoring.md +++ b/docs/module-authoring.md @@ -837,6 +837,21 @@ Materialization rules: GameServer `spec.env` — an explicit env override always wins. - **`target: file` fields feed `configFiles` templates** instead of becoming env vars — see the next section. +- **`envName` exports a field under another env var name.** By default + the env var is named after the field. Set `envName` when the image + reads a different variable than your config key, for example to fix a + wrong mapping without renaming the key (renaming orphans stored + values, see above). The value in `spec.config` stays under `name`; + only the container env var changes. Two env fields may not export the + same variable when either one sets `envName`. + + ```yaml + - name: WHITELIST_ENABLED # config key, unchanged + displayName: Enforce whitelist + type: bool + default: "false" + envName: ENABLE_WHITELIST # what the itzg image reads + ``` ### Config files diff --git a/gp-module/internal/preview/preview.go b/gp-module/internal/preview/preview.go index 280365af5..efb86763e 100644 --- a/gp-module/internal/preview/preview.go +++ b/gp-module/internal/preview/preview.go @@ -196,6 +196,10 @@ func GeneratePreview(opts Options) (*Result, error) { if target == "" { target = "env" } + envName, _ := sMap["envName"].(string) + if envName == "" { + envName = fieldName + } val := "" source := "" @@ -243,9 +247,9 @@ func GeneratePreview(opts Options) (*Result, error) { if target == "env" { if fieldType == "password" { - effectiveEnv[fieldName] = fmt.Sprintf("[secret: configSecret/%s]", fieldName) + effectiveEnv[envName] = fmt.Sprintf("[secret: configSecret/%s]", fieldName) } else { - effectiveEnv[fieldName] = val + effectiveEnv[envName] = val } } } diff --git a/operator/api/v1alpha1/gametemplate_types.go b/operator/api/v1alpha1/gametemplate_types.go index 2ebcceab1..84035faf0 100644 --- a/operator/api/v1alpha1/gametemplate_types.go +++ b/operator/api/v1alpha1/gametemplate_types.go @@ -1123,8 +1123,9 @@ type AgentSpec struct { // ConfigField is a single user-tunable setting surfaced in the wizard. type ConfigField struct { - // Name is the field identifier (also used as an env var when - // Target is "env"). + // Name is the field identifier and the key in GameServer.spec.config. + // It is also the env var name when Target is "env" and EnvName is + // empty. // +kubebuilder:validation:MinLength=1 Name string `json:"name"` @@ -1163,6 +1164,14 @@ type ConfigField struct { // +optional Target string `json:"target,omitempty"` + // EnvName, when set, is the env var name the value is exported as + // instead of Name (Target "env" only). It lets a module keep a stable + // config key while pointing it at the variable the game image reads. + // +kubebuilder:validation:Pattern=`^[A-Za-z_][A-Za-z0-9_]*$` + // +kubebuilder:validation:MaxLength=253 + // +optional + EnvName string `json:"envName,omitempty"` + // AutoFromMemoryLimit, when set, computes this field's value from the // game container's effective memory limit whenever neither // GameServer.spec.config nor Default provides one: floor(limit × diff --git a/operator/config/crd/gameplane.local_gametemplates.yaml b/operator/config/crd/gameplane.local_gametemplates.yaml index 762373e3e..a506241e7 100644 --- a/operator/config/crd/gameplane.local_gametemplates.yaml +++ b/operator/config/crd/gameplane.local_gametemplates.yaml @@ -1129,6 +1129,14 @@ spec: items: type: string type: array + envName: + description: |- + EnvName, when set, is the env var name the value is exported as + instead of Name (Target "env" only). It lets a module keep a stable + config key while pointing it at the variable the game image reads. + maxLength: 253 + pattern: ^[A-Za-z_][A-Za-z0-9_]*$ + type: string max: description: |- Max is the maximum numeric value for int-typed fields. Bounds are @@ -1155,8 +1163,9 @@ spec: type: integer name: description: |- - Name is the field identifier (also used as an env var when - Target is "env"). + Name is the field identifier and the key in GameServer.spec.config. + It is also the env var name when Target is "env" and EnvName is + empty. minLength: 1 type: string required: diff --git a/operator/internal/controller/gameserver_config.go b/operator/internal/controller/gameserver_config.go index 9e3f3aeb7..cf75ac852 100644 --- a/operator/internal/controller/gameserver_config.go +++ b/operator/internal/controller/gameserver_config.go @@ -103,6 +103,9 @@ func materializeConfig( return nil, fmt.Errorf("unknown config keys %s: template %q accepts [%s]", strings.Join(unknown, ", "), tmpl.Name, strings.Join(names, ", ")) } + if err := checkEnvNameCollisions(schema); err != nil { + return nil, err + } mc := &materializedConfig{} var hashParts []string @@ -165,6 +168,11 @@ func materializeConfig( } values[f.Name] = val hashParts = append(hashParts, f.Name+"="+val) + if f.EnvName != "" && f.Target != "file" { + // Only renamed fields add a part, so templates without + // envName keep their existing hash (no restarts on upgrade). + hashParts = append(hashParts, "env:"+f.Name+"="+f.EnvName) + } if f.Target == "file" { // File-target values exist only inside rendered // configFiles — no env var, and password values reach @@ -179,7 +187,7 @@ func materializeConfig( } mc.secretData[f.Name] = []byte(val) mc.env = append(mc.env, corev1.EnvVar{ - Name: f.Name, + Name: configEnvName(f), ValueFrom: &corev1.EnvVarSource{ SecretKeyRef: &corev1.SecretKeySelector{ LocalObjectReference: corev1.LocalObjectReference{Name: configSecretName(gs)}, @@ -188,7 +196,7 @@ func materializeConfig( }, }) } else { - mc.env = append(mc.env, corev1.EnvVar{Name: f.Name, Value: val}) + mc.env = append(mc.env, corev1.EnvVar{Name: configEnvName(f), Value: val}) } } if fileFieldSet && len(tmpl.Spec.ConfigFiles) == 0 { @@ -209,6 +217,37 @@ func materializeConfig( return mc, nil } +// configEnvName is the env var a Target "env" config field is exported as: +// EnvName when the template sets it, otherwise the field Name. +func configEnvName(f gameplanev1alpha1.ConfigField) string { + if f.EnvName != "" { + return f.EnvName + } + return f.Name +} + +// checkEnvNameCollisions refuses a schema where an envName makes two +// env-target fields export the same env var, which would let one value +// silently override the other. Schemas without envName are unaffected. +func checkEnvNameCollisions(schema []gameplanev1alpha1.ConfigField) error { + owner := make(map[string]string, len(schema)) + renamed := make(map[string]bool) + for _, f := range schema { + if f.Target == "file" { + continue + } + env := configEnvName(f) + if f.EnvName != "" { + renamed[env] = true + } + if prev, ok := owner[env]; ok && renamed[env] { + return fmt.Errorf("config fields %q and %q both export env var %q", prev, f.Name, env) + } + owner[env] = f.Name + } + return nil +} + // autoMemoryValue renders percent% of the game container's effective // memory limit as whole mebibytes ("6144M"), or "" when the resolved // resources declare no memory limit — the field then stays unset and the diff --git a/operator/internal/controller/gameserver_config_test.go b/operator/internal/controller/gameserver_config_test.go index b02e8c4c4..762149907 100644 --- a/operator/internal/controller/gameserver_config_test.go +++ b/operator/internal/controller/gameserver_config_test.go @@ -690,3 +690,78 @@ func TestMaterializeConfig_AutoFromMemoryLimit_HashTracksLimit(t *testing.T) { t.Errorf("a memory-limit change must change the config hash so the pod rolls") } } + +func TestMaterializeConfig_EnvNameRenamesEnvVar(t *testing.T) { + gs, tmpl := configFixtures([]gameplanev1alpha1.ConfigField{ + {Name: "WHITELIST_ENABLED", Type: "bool", Default: "false", EnvName: "ENABLE_WHITELIST"}, + {Name: "MOTD", Type: "string", Default: "hi"}, + }, map[string]string{"WHITELIST_ENABLED": "true"}) + + mc, err := materializeConfig(gs, tmpl) + if err != nil { + t.Fatalf("materializeConfig: %v", err) + } + got := envMap(mc.env) + if got["ENABLE_WHITELIST"] != "true" { + t.Errorf("ENABLE_WHITELIST = %q, want true", got["ENABLE_WHITELIST"]) + } + if _, ok := got["WHITELIST_ENABLED"]; ok { + t.Errorf("renamed field must not also export its config key as an env var") + } + if got["MOTD"] != "hi" { + t.Errorf("MOTD = %q, want hi", got["MOTD"]) + } +} + +func TestMaterializeConfig_EnvNamePasswordKeepsSecretKey(t *testing.T) { + gs, tmpl := configFixtures([]gameplanev1alpha1.ConfigField{ + {Name: "SERVER_PASS", Type: "password", EnvName: "GAME_PASSWORD"}, + }, map[string]string{"SERVER_PASS": "hunter22"}) + + mc, err := materializeConfig(gs, tmpl) + if err != nil { + t.Fatalf("materializeConfig: %v", err) + } + if len(mc.env) != 1 { + t.Fatalf("got %d env vars, want 1", len(mc.env)) + } + e := mc.env[0] + if e.Name != "GAME_PASSWORD" { + t.Errorf("env name = %q, want GAME_PASSWORD", e.Name) + } + if e.ValueFrom == nil || e.ValueFrom.SecretKeyRef == nil || e.ValueFrom.SecretKeyRef.Key != "SERVER_PASS" { + t.Errorf("password must still come from Secret key SERVER_PASS, got %+v", e) + } + if string(mc.secretData["SERVER_PASS"]) != "hunter22" { + t.Errorf("secretData[SERVER_PASS] = %q, want the password", mc.secretData["SERVER_PASS"]) + } +} + +func TestMaterializeConfig_EnvNameChangesHash(t *testing.T) { + plain := []gameplanev1alpha1.ConfigField{{Name: "WHITELIST_ENABLED", Type: "bool", Default: "true"}} + renamed := []gameplanev1alpha1.ConfigField{{Name: "WHITELIST_ENABLED", Type: "bool", Default: "true", EnvName: "ENABLE_WHITELIST"}} + gsA, tmplA := configFixtures(plain, nil) + gsB, tmplB := configFixtures(renamed, nil) + a, err := materializeConfig(gsA, tmplA) + if err != nil { + t.Fatalf("materializeConfig plain: %v", err) + } + b, err := materializeConfig(gsB, tmplB) + if err != nil { + t.Fatalf("materializeConfig renamed: %v", err) + } + if a.hash == b.hash { + t.Errorf("adding envName must change the config hash so the pod rolls") + } +} + +func TestMaterializeConfig_EnvNameCollision(t *testing.T) { + gs, tmpl := configFixtures([]gameplanev1alpha1.ConfigField{ + {Name: "ENABLE_WHITELIST", Type: "bool", Default: "false"}, + {Name: "WHITELIST_ENABLED", Type: "bool", Default: "true", EnvName: "ENABLE_WHITELIST"}, + }, nil) + _, err := materializeConfig(gs, tmpl) + if err == nil || !strings.Contains(err.Error(), "both export env var") { + t.Fatalf("want env var collision error, got %v", err) + } +} diff --git a/web/src/types.ts b/web/src/types.ts index 2091c1f42..9a37b9c82 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -338,6 +338,8 @@ export interface GameTemplate { enum?: string[]; required?: boolean; target?: "env" | "file"; + // Env var the value is exported as instead of name (target "env"). + envName?: string; autoFromMemoryLimit?: { percent: number }; // Bounds mirror the CRD ConfigField: min/max apply to int fields, // minLength/maxLength to string and password fields (bytes). From 0390534c99145b7ea0eb7fa1e39d74845983416b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 02:06:22 +0000 Subject: [PATCH 2/2] fix(operator,gp-module): catch envName misuse before it reaches a cluster Review follow-up. The CRD now refuses envName on a target file field, where it had no effect. gp-module validate mirrors the operator's checks: an envName must be a valid env var name, must not sit on a file field, and must not make two env fields export the same variable, so a module fails validation instead of failing at reconcile time. New tests cover the validator findings and pin the config hash of a schema without envName, which must not change on upgrade. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- .../crds/gameplane.local_backups.yaml | 2 +- .../crds/gameplane.local_backupschedules.yaml | 2 +- .../crds/gameplane.local_clusters.yaml | 2 +- .../crds/gameplane.local_gameservers.yaml | 2 +- .../crds/gameplane.local_gametemplates.yaml | 6 +- .../crds/gameplane.local_modules.yaml | 2 +- .../crds/gameplane.local_modulesources.yaml | 2 +- .../crds/gameplane.local_networkcaptures.yaml | 2 +- .../crds/gameplane.local_restores.yaml | 2 +- .../gameplane.local_backups.yaml | 2 +- .../gameplane.local_backupschedules.yaml | 2 +- .../gameplane.local_clusters.yaml | 2 +- .../gameplane.local_gameservers.yaml | 2 +- .../gameplane.local_gametemplates.yaml | 6 +- .../gameplane.local_modules.yaml | 2 +- .../gameplane.local_modulesources.yaml | 2 +- .../gameplane.local_networkcaptures.yaml | 2 +- .../gameplane.local_restores.yaml | 2 +- gp-module/internal/validator/rules.go | 101 ++++++++++++++++++ .../internal/validator/validator_test.go | 68 ++++++++++++ operator/api/v1alpha1/gametemplate_types.go | 1 + .../crd/gameplane.local_gametemplates.yaml | 4 + .../controller/gameserver_config_test.go | 17 +++ 23 files changed, 217 insertions(+), 18 deletions(-) diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml index 5351e0efe..797fe9169 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backups.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: backups.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml index 523246de6..1a5324135 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_backupschedules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: backupschedules.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml index 3b598305f..7062560b4 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_clusters.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: clusters.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml index d10c13fa2..71c5333df 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gameservers.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: gameservers.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml index 16b216738..44277c017 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_gametemplates.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: gametemplates.gameplane.local spec: @@ -1200,6 +1200,10 @@ spec: - name - type type: object + x-kubernetes-validations: + - message: envName applies only to target env fields + rule: '!has(self.envName) || !has(self.target) || self.target + == ''env''' type: array consoleMode: description: |- diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml index 43e1f8661..3c28a7995 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: modules.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml index e63fd0ab2..9c2f1ed33 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_modulesources.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: modulesources.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml index 58ed3896a..c1ac3905a 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_networkcaptures.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: networkcaptures.gameplane.local spec: diff --git a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml index 7106c17f5..28e7b6b62 100644 --- a/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml +++ b/charts/gameplane/charts/gameplane-crds/crds/gameplane.local_restores.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: restores.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_backups.yaml b/charts/gameplane/crd-manifests/gameplane.local_backups.yaml index 5351e0efe..797fe9169 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_backups.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_backups.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: backups.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml b/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml index 523246de6..1a5324135 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_backupschedules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: backupschedules.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml b/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml index 3b598305f..7062560b4 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_clusters.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: clusters.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml b/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml index d10c13fa2..71c5333df 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_gameservers.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: gameservers.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml b/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml index 16b216738..44277c017 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_gametemplates.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: gametemplates.gameplane.local spec: @@ -1200,6 +1200,10 @@ spec: - name - type type: object + x-kubernetes-validations: + - message: envName applies only to target env fields + rule: '!has(self.envName) || !has(self.target) || self.target + == ''env''' type: array consoleMode: description: |- diff --git a/charts/gameplane/crd-manifests/gameplane.local_modules.yaml b/charts/gameplane/crd-manifests/gameplane.local_modules.yaml index 43e1f8661..3c28a7995 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_modules.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_modules.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: modules.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml b/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml index e63fd0ab2..9c2f1ed33 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_modulesources.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: modulesources.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml b/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml index 58ed3896a..c1ac3905a 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_networkcaptures.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: networkcaptures.gameplane.local spec: diff --git a/charts/gameplane/crd-manifests/gameplane.local_restores.yaml b/charts/gameplane/crd-manifests/gameplane.local_restores.yaml index 7106c17f5..28e7b6b62 100644 --- a/charts/gameplane/crd-manifests/gameplane.local_restores.yaml +++ b/charts/gameplane/crd-manifests/gameplane.local_restores.yaml @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - gameplane.local/crd-bundle-sha256: 57c5559296bd196e2c065107f66b585f37e34dd82e263245522bffb92323b130 + gameplane.local/crd-bundle-sha256: 0f12a0d07eb5b4e6917b52da401eac7001c6946d13b567e81fb05146cd0caf5e controller-gen.kubebuilder.io/version: v0.22.0 name: restores.gameplane.local spec: diff --git a/gp-module/internal/validator/rules.go b/gp-module/internal/validator/rules.go index a0fdecf71..8f970077b 100644 --- a/gp-module/internal/validator/rules.go +++ b/gp-module/internal/validator/rules.go @@ -14,6 +14,9 @@ import ( var sha256DigestRegex = regexp.MustCompile(`@sha256:[0-9a-fA-F]{64}$`) +// envNameRegex mirrors the GameTemplate CRD pattern for configSchema envName. +var envNameRegex = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) + var allowedConfigTypes = map[string]bool{ "string": true, "int": true, @@ -262,6 +265,7 @@ func validateConfigSchemaRules(node *yaml.Node) []Finding { return nil } + var envFields []envFieldRef for i, item := range cfgNode.Content { if item.Kind != yaml.MappingNode { continue @@ -447,8 +451,105 @@ func validateConfigSchemaRules(node *yaml.Node) []Finding { } } } + + envFinding, ref := checkConfigEnvName(item, i, fieldName) + if envFinding != nil { + findings = append(findings, *envFinding) + } + if ref != nil { + envFields = append(envFields, *ref) + } } + return append(findings, envNameCollisions(envFields)...) +} + +// envFieldRef is one env-target configSchema field and the env var it +// exports, for the collision check. +type envFieldRef struct { + index int + name string + env string + renamed bool + line int + column int +} + +// checkConfigEnvName validates a field's optional envName the way the +// operator does: a valid env var name, and only on target env fields. +// It returns the field's env export for the collision check, or nil for +// file-target fields. +func checkConfigEnvName(item *yaml.Node, i int, fieldName string) (*Finding, *envFieldRef) { + target := "env" + if t := common.FindNode(item, "target"); t != nil && t.Value != "" { + target = t.Value + } + envNode := common.FindNode(item, "envName") + ref := &envFieldRef{index: i, name: fieldName, env: fieldName, line: item.Line, column: item.Column} + if target == "file" { + ref = nil + } + if envNode == nil || envNode.Value == "" { + return nil, ref + } + field := fmt.Sprintf("spec.configSchema[%d].envName", i) + if target == "file" { + return &Finding{ + Level: SeverityError, + RuleID: RuleTemplateSchemaViolation, + File: "template.yaml", + Line: envNode.Line, + Column: envNode.Column, + Field: field, + Message: fmt.Sprintf("envName on field %q has no effect because its target is file", fieldName), + Remediation: "Remove envName, or set 'target: env'.", + }, nil + } + if !envNameRegex.MatchString(envNode.Value) { + return &Finding{ + Level: SeverityError, + RuleID: RuleTemplateSchemaViolation, + File: "template.yaml", + Line: envNode.Line, + Column: envNode.Column, + Field: field, + Message: fmt.Sprintf("envName %q on field %q is not a valid env var name", envNode.Value, fieldName), + Remediation: "Use letters, digits and underscores, not starting with a digit.", + }, ref + } + ref.env = envNode.Value + ref.renamed = true + ref.line = envNode.Line + ref.column = envNode.Column + return nil, ref +} + +// envNameCollisions reports two env-target fields exporting the same env +// var when either one sets envName, which the operator refuses at +// reconcile time. Plain duplicate names are left to other checks. +func envNameCollisions(refs []envFieldRef) []Finding { + var findings []Finding + owner := make(map[string]envFieldRef, len(refs)) + renamed := make(map[string]bool) + for _, r := range refs { + if r.renamed { + renamed[r.env] = true + } + if prev, ok := owner[r.env]; ok && renamed[r.env] { + findings = append(findings, Finding{ + Level: SeverityError, + RuleID: RuleTemplateSchemaViolation, + File: "template.yaml", + Line: r.line, + Column: r.column, + Field: fmt.Sprintf("spec.configSchema[%d].envName", r.index), + Message: fmt.Sprintf("config fields %q and %q both export env var %q", prev.name, r.name, r.env), + Remediation: "Give each env field a distinct envName (or name).", + }) + continue + } + owner[r.env] = r + } return findings } diff --git a/gp-module/internal/validator/validator_test.go b/gp-module/internal/validator/validator_test.go index a8f979040..a1f8e5847 100644 --- a/gp-module/internal/validator/validator_test.go +++ b/gp-module/internal/validator/validator_test.go @@ -896,3 +896,71 @@ gameplaneMinVersion: 1.0.0 } }) } + +func TestValidate_ConfigEnvName(t *testing.T) { + t.Parallel() + files := map[string][]byte{ + "module.yaml": []byte(`apiVersion: gameplane.local/module/v1 +name: test-mod +displayName: Test Mod +version: 1.0.0 +game: test +summary: Test +`), + "template.yaml": []byte(`apiVersion: gameplane.io/v1alpha1 +kind: GameTemplate +metadata: + name: test-mod +spec: + image: "example.com/img@sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + configSchema: + - name: ENABLE_WHITELIST + type: bool + - name: WHITELIST_ENABLED + type: bool + envName: ENABLE_WHITELIST + - name: BAD_ENV + type: string + envName: 1-bad + - name: FILE_FIELD + type: string + target: file + envName: FILE_ENV + - name: GOOD + type: string + envName: GOOD_ENV +`), + "README.md": []byte("# Test\n"), + } + + report, err := ValidateFiles("test-mod", files, ValidateOptions{}) + if err != nil { + t.Fatal(err) + } + + var msgs []string + for _, f := range report.Findings { + if f.RuleID == RuleTemplateSchemaViolation && strings.Contains(f.Field, "envName") { + msgs = append(msgs, f.Message) + } + } + want := []string{ + `both export env var "ENABLE_WHITELIST"`, + `envName "1-bad" on field "BAD_ENV" is not a valid env var name`, + `envName on field "FILE_FIELD" has no effect because its target is file`, + } + for _, w := range want { + found := false + for _, m := range msgs { + if strings.Contains(m, w) { + found = true + } + } + if !found { + t.Errorf("missing envName finding %q in %v", w, msgs) + } + } + if len(msgs) != len(want) { + t.Errorf("got %d envName findings, want %d: %v", len(msgs), len(want), msgs) + } +} diff --git a/operator/api/v1alpha1/gametemplate_types.go b/operator/api/v1alpha1/gametemplate_types.go index 84035faf0..f79e4bd6d 100644 --- a/operator/api/v1alpha1/gametemplate_types.go +++ b/operator/api/v1alpha1/gametemplate_types.go @@ -1122,6 +1122,7 @@ type AgentSpec struct { } // ConfigField is a single user-tunable setting surfaced in the wizard. +// +kubebuilder:validation:XValidation:rule="!has(self.envName) || !has(self.target) || self.target == 'env'",message="envName applies only to target env fields" type ConfigField struct { // Name is the field identifier and the key in GameServer.spec.config. // It is also the env var name when Target is "env" and EnvName is diff --git a/operator/config/crd/gameplane.local_gametemplates.yaml b/operator/config/crd/gameplane.local_gametemplates.yaml index a506241e7..e77e92bcd 100644 --- a/operator/config/crd/gameplane.local_gametemplates.yaml +++ b/operator/config/crd/gameplane.local_gametemplates.yaml @@ -1199,6 +1199,10 @@ spec: - name - type type: object + x-kubernetes-validations: + - message: envName applies only to target env fields + rule: '!has(self.envName) || !has(self.target) || self.target + == ''env''' type: array consoleMode: description: |- diff --git a/operator/internal/controller/gameserver_config_test.go b/operator/internal/controller/gameserver_config_test.go index 762149907..983f40749 100644 --- a/operator/internal/controller/gameserver_config_test.go +++ b/operator/internal/controller/gameserver_config_test.go @@ -1,6 +1,8 @@ package controller import ( + "crypto/sha256" + "encoding/hex" "strings" "testing" @@ -765,3 +767,18 @@ func TestMaterializeConfig_EnvNameCollision(t *testing.T) { t.Fatalf("want env var collision error, got %v", err) } } + +func TestMaterializeConfig_NoEnvNameKeepsHash(t *testing.T) { + gs, tmpl := configFixtures([]gameplanev1alpha1.ConfigField{ + {Name: "MOTD", Type: "string", Default: "hi"}, + }, nil) + mc, err := materializeConfig(gs, tmpl) + if err != nil { + t.Fatalf("materializeConfig: %v", err) + } + // Same formula as before envName existed: sha256 of "MOTD=hi". + sum := sha256.Sum256([]byte("MOTD=hi")) + if want := hex.EncodeToString(sum[:]); mc.hash != want { + t.Errorf("hash = %s, want %s (templates without envName must keep their hash)", mc.hash, want) + } +}