fix: split env config and image pull secret into separate helm releases

Bundling these Secrets in the app helm chart caused two bugs in the scheduler-k3s plugin: a chart rollback could delete Secrets that older ReplicaSets still referenced by exact timestamped name (`env-{app}.{ts}` and `ims-{app}.{ts}`), hard-crashing pods until manual intervention; and the strategic-merge `patchMergeKey` on `imagePullSecrets` let stale entries leak into the live Deployment until the list pointed at many nonexistent Secrets. Each Secret now lives in its own helm release with a stable name (`config-{app}` and `pull-secret-{app}`), installed before the app chart on every deploy. The deployment trigger also prunes any leaked `imagePullSecrets` entries from the live Deployment so the next deploy lands on a clean list, and the rename and destroy paths uninstall the new releases (and the previously-leaked TLS release on rename) under the old app name.
This commit is contained in:
Jose Diaz-Gonzalez
2026-04-29 09:46:29 -04:00
parent f06048a266
commit ef9bdc0379
18 changed files with 1086 additions and 56 deletions

View File

@@ -0,0 +1,170 @@
package scheduler_k3s
import (
"context"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"github.com/dokku/dokku/plugins/common"
)
// ImagePullSecretValues contains the values for the dokku-managed image pull secret helm chart
type ImagePullSecretValues struct {
Global ImagePullSecretGlobalValues `yaml:"global"`
}
// ImagePullSecretGlobalValues contains the global values for the image pull secret chart
type ImagePullSecretGlobalValues struct {
AppName string `yaml:"app_name"`
Namespace string `yaml:"namespace"`
PullSecretBase64 string `yaml:"pull_secret_base64"`
}
// GetImagePullSecretReleaseName returns the helm release name for the image pull secret
func GetImagePullSecretReleaseName(appName string) string {
return fmt.Sprintf("pull-secret-%s", appName)
}
// GetImagePullSecretName returns the kubernetes secret name for the dokku-managed image pull secret
func GetImagePullSecretName(appName string) string {
return fmt.Sprintf("pull-secret-%s", appName)
}
// CreateOrUpdateImagePullSecret creates or updates the image pull secret helm chart for an app
func CreateOrUpdateImagePullSecret(ctx context.Context, appName string, dockerConfigJSON []byte) error {
if err := isKubernetesAvailable(); err != nil {
common.LogDebug("kubernetes not available, skipping image pull secret creation")
return nil
}
scheduler := common.PropertyGetDefault("scheduler", appName, "selected", "")
globalScheduler := common.PropertyGetDefault("scheduler", "--global", "selected", "docker-local")
if scheduler == "" {
scheduler = globalScheduler
}
if scheduler != "k3s" {
common.LogDebug("app does not use k3s scheduler, skipping image pull secret creation")
return nil
}
chartDir, err := os.MkdirTemp("", "dokku-pull-secret-chart-")
if err != nil {
return fmt.Errorf("error creating chart directory: %w", err)
}
defer os.RemoveAll(chartDir)
if err := os.MkdirAll(filepath.Join(chartDir, "templates"), os.FileMode(0755)); err != nil {
return fmt.Errorf("error creating chart templates directory: %w", err)
}
namespace := getComputedNamespace(appName)
releaseName := GetImagePullSecretReleaseName(appName)
chart := &Chart{
ApiVersion: "v2",
AppVersion: "1.0.0",
Name: releaseName,
Icon: "https://dokku.com/assets/dokku-logo.svg",
Version: "0.0.1",
}
err = writeYaml(WriteYamlInput{
Object: chart,
Path: filepath.Join(chartDir, "Chart.yaml"),
})
if err != nil {
return fmt.Errorf("error writing chart: %w", err)
}
b, err := templates.ReadFile("templates/pull-secret-chart/templates/pull-secret.yaml")
if err != nil {
return fmt.Errorf("error reading pull-secret template: %w", err)
}
filename := filepath.Join(chartDir, "templates", "pull-secret.yaml")
err = os.WriteFile(filename, b, os.FileMode(0644))
if err != nil {
return fmt.Errorf("error writing pull-secret template: %w", err)
}
if os.Getenv("DOKKU_TRACE") == "1" {
common.CatFile(filename)
}
values := &ImagePullSecretValues{
Global: ImagePullSecretGlobalValues{
AppName: appName,
Namespace: namespace,
PullSecretBase64: base64.StdEncoding.EncodeToString(dockerConfigJSON),
},
}
err = writeYaml(WriteYamlInput{
Object: values,
Path: filepath.Join(chartDir, "values.yaml"),
})
if err != nil {
return fmt.Errorf("error writing values: %w", err)
}
if err := createKubernetesNamespace(ctx, namespace); err != nil {
return fmt.Errorf("error creating namespace: %w", err)
}
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
if err != nil {
return fmt.Errorf("error creating helm agent: %w", err)
}
chartPath, err := filepath.Abs(chartDir)
if err != nil {
return fmt.Errorf("error getting chart path: %w", err)
}
common.LogVerboseQuiet(fmt.Sprintf("Installing image pull secret for %s", appName))
err = helmAgent.InstallOrUpgradeChart(ctx, ChartInput{
ChartPath: chartPath,
Namespace: namespace,
ReleaseName: releaseName,
Wait: false,
})
if err != nil {
return fmt.Errorf("error installing image pull secret chart: %w", err)
}
return nil
}
// DeleteImagePullSecret deletes the image pull secret helm chart for an app
func DeleteImagePullSecret(ctx context.Context, appName string) error {
if err := isKubernetesAvailable(); err != nil {
common.LogDebug("kubernetes not available, skipping image pull secret deletion")
return nil
}
namespace := getComputedNamespace(appName)
releaseName := GetImagePullSecretReleaseName(appName)
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
if err != nil {
return fmt.Errorf("error creating helm agent: %w", err)
}
exists, err := helmAgent.ChartExists(releaseName)
if err != nil {
return fmt.Errorf("error checking if image pull secret chart exists: %w", err)
}
if !exists {
return nil
}
common.LogVerboseQuiet(fmt.Sprintf("Removing image pull secret for %s", appName))
if err := helmAgent.UninstallChart(releaseName); err != nil {
return fmt.Errorf("error uninstalling image pull secret chart: %w", err)
}
return nil
}