feat: add scheduler-k3s:preview subcommand

Adds a `scheduler-k3s:preview <app>` command that renders a unified diff
between the manifests currently stored in the live Helm release for an app
and the manifests that the next deploy would roll out. The diff is produced
entirely via the Helm Go SDK that scheduler-k3s already uses, with no
external `helm` binary or `helm-diff` plugin install required. The
underlying diff and parse logic is vendored from `databus23/helm-diff`
(Apache 2.0) into a new internal package, trimmed of three-way-merge,
release-ownership tracking, and the non-default output formatters.

The chart-construction half of `TriggerSchedulerDeploy` has been extracted
into a reusable `BuildAppChart` helper so deploy and preview share the
same chart materialization without duplicating template logic. Secret
values are redacted by default in preview output; `--show-secrets` and
`--show-secrets-decoded` flags match the upstream `helm diff` UX. Only the
main app helm release is compared in this iteration; auxiliary releases
for config vars, image-pull secrets, and TLS certificates are out of scope
and documented as such.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-06-02 20:28:31 -04:00
1 parent db798dfe16
commit 496ede44b7
15 files changed
+2076 -561

No files matched your search

+18 -561
View File
@@ -3,7 +3,6 @@ package scheduler_k3s
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
@@ -11,20 +10,16 @@ import (
"os"
"os/signal"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
"time"
appjson "github.com/dokku/dokku/plugins/app-json"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
"github.com/dokku/dokku/plugins/cron"
nginxvhosts "github.com/dokku/dokku/plugins/nginx-vhosts"
"github.com/dokku/dokku/plugins/registry"
"github.com/fatih/color"
"github.com/gosimple/slug"
"github.com/kballard/go-shellquote"
"github.com/ryanuber/columnize"
corev1 "k8s.io/api/core/v1"
@@ -463,18 +458,6 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
if scheduler != "k3s" {
return nil
}
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "ps-current-scale",
Args: []string{appName},
})
if err != nil {
return err
}
processes, err := common.ParseScaleOutput(results.StdoutBytes())
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
signals := make(chan os.Signal, 1)
@@ -493,82 +476,27 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
return fmt.Errorf("Error creating kubernetes namespace for deployment: %w", err)
}
image, err := common.GetDeployingAppImageName(appName, imageTag, "")
chartResult, err := BuildAppChart(ctx, appName, imageTag)
if err != nil {
return fmt.Errorf("Error getting deploying app image name: %w", err)
}
deployTimeout := getComputedDeployTimeout(appName)
if _, err := strconv.Atoi(deployTimeout); err == nil {
deployTimeout = fmt.Sprintf("%ss", deployTimeout)
}
deployRollback := getComputedRollbackOnFailure(appName)
allowRollbacks, err := strconv.ParseBool(deployRollback)
if err != nil {
return fmt.Errorf("Error parsing rollback-on-failure value as boolean: %w", err)
}
imageSourceType := "dockerfile"
if common.IsImageCnbBased(image) {
imageSourceType = "pack"
} else if common.IsImageHerokuishBased(image, appName) {
imageSourceType = "herokuish"
return err
}
defer os.RemoveAll(chartResult.ChartDir)
env, err := config.LoadMergedAppEnv(appName)
if err != nil {
return fmt.Errorf("Error loading environment for deployment: %w", err)
}
// Check for imported TLS certificate first
importedCertExists := false
if HasImportedTLSCert(appName) {
exists, err := TLSSecretExists(ctx, appName)
if err == nil && exists {
importedCertExists = true
}
}
// Determine TLS configuration
tlsEnabled := false
issuerName := ""
useImportedCert := false
if importedCertExists {
tlsEnabled = true
useImportedCert = true
} else {
server := getComputedLetsencryptServer(appName)
letsencryptEmailStag := getGlobalLetsencryptEmailStag()
letsencryptEmailProd := getGlobalLetsencryptEmailProd()
switch server {
case "prod", "production":
issuerName = "letsencrypt-prod"
tlsEnabled = letsencryptEmailProd != ""
case "stag", "staging":
issuerName = "letsencrypt-stag"
tlsEnabled = letsencryptEmailStag != ""
case "false":
issuerName = ""
tlsEnabled = false
default:
return fmt.Errorf("Invalid letsencrypt server config: %s", server)
}
}
chartDir, err := os.MkdirTemp("", "dokku-chart-")
globalAnnotations, err := getGlobalAnnotations(appName)
if err != nil {
return fmt.Errorf("Error creating chart directory: %w", err)
return fmt.Errorf("Error getting global annotations: %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)
globalLabels, err := getGlobalLabel(appName)
if err != nil {
return fmt.Errorf("Error getting global labels: %w", err)
}
deploymentId := time.Now().Unix()
imagePullSecrets := getComputedImagePullSecrets(appName)
dokkuManagedPullSecret := false
var dokkuPullSecretBytes []byte
@@ -586,484 +514,18 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
}
}
globalTemplateFiles := []string{"service-account"}
for _, templateName := range globalTemplateFiles {
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
if err != nil {
return fmt.Errorf("Error reading %s template: %w", templateName, err)
}
filename := filepath.Join(chartDir, "templates", fmt.Sprintf("%s.yaml", templateName))
err = os.WriteFile(filename, b, os.FileMode(0644))
if err != nil {
return fmt.Errorf("Error writing %s template: %w", templateName, err)
}
if os.Getenv("DOKKU_TRACE") == "1" {
common.CatFile(filename)
}
}
portMaps, err := getPortMaps(appName)
if err != nil {
return fmt.Errorf("Error getting port mappings for deployment: %w", err)
}
primaryPort := int32(5000)
primaryServicePort := int32(80)
for _, portMap := range portMaps {
primaryPort = portMap.ContainerPort
primaryServicePort = portMap.HostPort
if primaryPort != 0 {
break
}
}
appJSON, err := appjson.GetAppJSON(appName)
if err != nil {
return fmt.Errorf("Error getting app.json for deployment: %w", err)
}
workingDir := common.GetWorkingDir(appName, image)
cronTasks, err := cron.FetchCronTasks(cron.FetchCronTasksInput{AppName: appName})
if err != nil {
return fmt.Errorf("Error fetching cron tasks: %w", err)
}
domains := []string{}
if _, ok := processes["web"]; ok {
_, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "domains-vhost-enabled",
Args: []string{appName},
StreamStdio: true,
})
if err == nil {
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "domains-list",
Args: []string{appName},
})
if err != nil {
return fmt.Errorf("Error getting domains for deployment: %w", err)
}
for _, domain := range strings.Split(results.StdoutContents(), "\n") {
domain = strings.TrimSpace(domain)
if domain != "" {
domains = append(domains, domain)
}
}
}
}
chart := &Chart{
ApiVersion: "v2",
AppVersion: "1.0.0",
Name: appName,
Icon: "https://dokku.com/assets/dokku-logo.svg",
Version: fmt.Sprintf("0.0.%d", deploymentId),
}
err = writeYaml(WriteYamlInput{
Object: chart,
Path: filepath.Join(chartDir, "Chart.yaml"),
})
if err != nil {
return fmt.Errorf("Error writing chart: %w", err)
}
globalAnnotations, err := getGlobalAnnotations(appName)
if err != nil {
return fmt.Errorf("Error getting global annotations: %w", err)
}
globalLabels, err := getGlobalLabel(appName)
if err != nil {
return fmt.Errorf("Error getting global labels: %w", err)
}
clientset, err := NewKubernetesClient()
if err != nil {
return fmt.Errorf("Error creating kubernetes client: %w", err)
}
if err := clientset.Ping(); err != nil {
return fmt.Errorf("kubernetes api not available: %w", err)
}
kedaValues, err := getKedaValues(ctx, clientset, appName)
if err != nil {
return fmt.Errorf("Error getting keda values: %w", err)
}
securityContext, err := getSecurityContext(appName, "deploy")
if err != nil {
return fmt.Errorf("Error getting security context: %w", err)
}
values := &AppValues{
Global: GlobalValues{
Annotations: globalAnnotations,
AppName: appName,
DeploymentID: fmt.Sprint(deploymentId),
Keda: kedaValues,
Image: GlobalImage{
ImagePullSecrets: imagePullSecrets,
Name: image,
Type: imageSourceType,
WorkingDir: workingDir,
},
Labels: globalLabels,
Namespace: namespace,
Network: GlobalNetwork{
IngressClass: getComputedIngressClass(),
PrimaryPort: primaryPort,
PrimaryServicePort: primaryServicePort,
},
SecurityContext: securityContext,
},
Processes: map[string]ProcessValues{},
}
if len(kedaValues.Authentications) > 0 {
templateFiles := []string{"keda-secret", "keda-trigger-authentication"}
for _, templateName := range templateFiles {
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
if err != nil {
return fmt.Errorf("Error reading %s template: %w", templateName, err)
}
filename := filepath.Join(chartDir, "templates", fmt.Sprintf("%s.yaml", templateName))
err = os.WriteFile(filename, b, os.FileMode(0644))
if err != nil {
return fmt.Errorf("Error writing %s template: %w", templateName, err)
}
if os.Getenv("DOKKU_TRACE") == "1" {
common.CatFile(filename)
}
}
}
processVolumes := []ProcessVolume{}
if shmSize := getComputedShmSize(appName); shmSize != "" {
processVolumes = append(processVolumes, ProcessVolume{
Name: "shmem",
MountPath: "/dev/shm",
EmptyDir: &ProcessVolumeEmptyDir{
Medium: "Memory",
SizeLimit: shmSize,
},
})
}
deployMountPairs, err := LoadAppMounts(appName, "deploy")
if err != nil {
return fmt.Errorf("error loading storage app mounts: %w", err)
}
deployVolumes, err := ToProcessVolumes(deployMountPairs)
if err != nil {
return err
}
processVolumes = append(processVolumes, deployVolumes...)
for processType, processCount := range processes {
// todo: implement deployment annotations
// todo: implement pod annotations
healthchecks, ok := appJSON.Healthchecks[processType]
if !ok {
healthchecks = []appjson.Healthcheck{}
}
processHealthchecks := getProcessHealtchecks(healthchecks, primaryPort)
startCommand, err := getStartCommand(StartCommandInput{
AppName: appName,
ProcessType: processType,
ImageSourceType: imageSourceType,
Port: primaryPort,
Env: env.Map(),
})
if err != nil {
return fmt.Errorf("Error getting start command for deployment: %w", err)
}
args := startCommand.Command
processResources, err := getProcessResources(appName, processType)
if err != nil {
return fmt.Errorf("Error getting process resources: %w", err)
}
annotations, err := getAnnotations(appName, processType)
if err != nil {
return fmt.Errorf("Error getting process annotations: %w", err)
}
labels, err := getLabels(appName, processType)
if err != nil {
return fmt.Errorf("Error getting process labels: %w", err)
}
autoscaling, err := getAutoscaling(GetAutoscalingInput{
AppName: appName,
ProcessType: processType,
Replicas: int(processCount),
KedaValues: kedaValues,
})
if err != nil {
return fmt.Errorf("Error getting autoscaling: %w", err)
}
processValues := ProcessValues{
Annotations: annotations,
Autoscaling: autoscaling,
Args: args,
Healthchecks: processHealthchecks,
Labels: labels,
ProcessType: ProcessType_Worker,
Replicas: int32(processCount),
Resources: processResources,
Volumes: processVolumes,
}
if processType == "web" {
sort.Strings(domains)
domainValues := []ProcessDomains{}
for _, domain := range domains {
domainValues = append(domainValues, ProcessDomains{
Name: domain,
Slug: slug.Make(domain),
})
}
processValues.Web = ProcessWeb{
Domains: domainValues,
PortMaps: []ProcessPortMap{},
TLS: ProcessTls{
Enabled: tlsEnabled,
IssuerName: issuerName,
UseImportedCert: useImportedCert,
},
}
processValues.ProcessType = ProcessType_Web
for _, portMap := range portMaps {
protocol := PortmapProtocol_TCP
if portMap.Scheme == "udp" {
protocol = PortmapProtocol_UDP
}
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
ContainerPort: portMap.ContainerPort,
HostPort: portMap.HostPort,
Name: portMap.String(),
Protocol: protocol,
Scheme: portMap.Scheme,
})
}
for _, portMap := range processValues.Web.PortMaps {
_, httpOk := portMaps[fmt.Sprintf("http-80-%d", portMap.ContainerPort)]
_, httpsOk := portMaps[fmt.Sprintf("https-443-%d", portMap.ContainerPort)]
if portMap.Scheme == "http" && !httpsOk && tlsEnabled {
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
ContainerPort: portMap.ContainerPort,
HostPort: 443,
Name: fmt.Sprintf("https-443-%d", portMap.ContainerPort),
Protocol: PortmapProtocol_TCP,
Scheme: "https",
})
}
if portMap.Scheme == "https" && !httpOk {
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
ContainerPort: portMap.ContainerPort,
HostPort: 80,
Name: fmt.Sprintf("http-80-%d", portMap.ContainerPort),
Protocol: PortmapProtocol_TCP,
Scheme: "http",
})
}
}
sort.Sort(NameSorter(processValues.Web.PortMaps))
} else if appJSON.Formation[processType].Service != nil && appJSON.Formation[processType].Service.Exposed {
processValues.Web = ProcessWeb{
Domains: []ProcessDomains{},
PortMaps: []ProcessPortMap{},
TLS: ProcessTls{
Enabled: false,
},
}
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
ContainerPort: 5000,
HostPort: 5000,
Name: "http-5000-5000",
Protocol: PortmapProtocol_TCP,
Scheme: "http",
})
}
values.Processes[processType] = processValues
templateFiles := []string{"deployment", "keda-scaled-object"}
if processType == "web" {
templateFiles = append(templateFiles, "service", "certificate", "ingress", "ingress-route", "compression-middleware", "https-redirect-middleware", "keda-http-scaled-object", "keda-interceptor-proxy-service")
}
for _, templateName := range templateFiles {
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
if err != nil {
return fmt.Errorf("Error reading %s template: %w", templateName, err)
}
filename := filepath.Join(chartDir, "templates", fmt.Sprintf("%s.yaml", templateName))
err = os.WriteFile(filename, b, os.FileMode(0644))
if err != nil {
return fmt.Errorf("Error writing %s template: %w", templateName, err)
}
if os.Getenv("DOKKU_TRACE") == "1" {
common.CatFile(filename)
}
}
}
cronJobs, err := clientset.ListCronJobs(ctx, ListCronJobsInput{
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
Namespace: namespace,
})
if err != nil {
return fmt.Errorf("Error listing cron jobs: %w", err)
}
for _, cronTask := range cronTasks {
// todo: implement deployment annotations
// todo: implement pod annotations
suffix := ""
for _, cronJob := range cronJobs {
if cronJob.Annotations["dokku.com/cron-id"] == cronTask.ID {
if value, ok := cronJob.Annotations["dokku.com/job-suffix"]; ok {
suffix = value
}
}
}
if suffix == "" {
n := 5
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic(err)
}
suffix = strings.ToLower(fmt.Sprintf("%X", b))
}
words, err := shellquote.Split(cronTask.Command)
if err != nil {
return fmt.Errorf("Error parsing cron task command: %w", err)
}
processResources, err := getProcessResources(appName, cronTask.ID)
if err != nil {
return fmt.Errorf("Error getting process resources: %w", err)
}
annotations, err := getAnnotations(appName, cronTask.ID)
if err != nil {
return fmt.Errorf("Error getting process annotations: %w", err)
}
labels, err := getLabels(appName, cronTask.ID)
if err != nil {
return fmt.Errorf("Error getting process labels: %w", err)
}
concurrencyPolicy := strings.ToUpper(cronTask.ConcurrencyPolicy)
switch concurrencyPolicy {
case "ALLOW":
concurrencyPolicy = "Allow"
case "FORBID":
concurrencyPolicy = "Forbid"
case "REPLACE":
concurrencyPolicy = "Replace"
default:
return fmt.Errorf("Invalid concurrency_policy specified: %v", concurrencyPolicy)
}
processValues := ProcessValues{
Args: words,
Annotations: annotations,
Cron: ProcessCron{
ID: cronTask.ID,
Hash: cronIDLabelValue(cronTask.ID),
Schedule: cronTask.Schedule,
Suffix: suffix,
Suspend: cronTask.Maintenance,
ConcurrencyPolicy: ProcessCronConcurrencyPolicy(concurrencyPolicy),
},
Labels: labels,
ProcessType: ProcessType_Cron,
Replicas: 1,
Resources: processResources,
Volumes: processVolumes,
}
values.Processes[cronTask.ID] = processValues
}
if len(cronTasks) > 0 {
b, err := templates.ReadFile("templates/chart/cron-job.yaml")
if err != nil {
return fmt.Errorf("Error reading cron job template: %w", err)
}
cronFile := filepath.Join(chartDir, "templates", "cron-job.yaml")
err = os.WriteFile(cronFile, b, os.FileMode(0644))
if err != nil {
return fmt.Errorf("Error writing cron job template: %w", err)
}
if os.Getenv("DOKKU_TRACE") == "1" {
common.CatFile(cronFile)
}
}
b, err := templates.ReadFile("templates/chart/_helpers.tpl")
if err != nil {
return fmt.Errorf("Error reading _helpers template: %w", err)
}
helpersFile := filepath.Join(chartDir, "templates", "_helpers.tpl")
err = os.WriteFile(helpersFile, b, os.FileMode(0644))
if err != nil {
return fmt.Errorf("Error writing _helpers template: %w", err)
}
if os.Getenv("DOKKU_TRACE") == "1" {
common.CatFile(helpersFile)
}
err = writeYaml(WriteYamlInput{
Object: values,
Path: filepath.Join(chartDir, "values.yaml"),
})
if err != nil {
return fmt.Errorf("Error writing chart: %w", err)
}
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
helmAgent, err := NewHelmAgent(chartResult.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)
}
timeoutDuration, err := time.ParseDuration(deployTimeout)
if err != nil {
return fmt.Errorf("Error parsing deploy timeout duration: %w", err)
}
ingresses, err := clientset.ListIngresses(ctx, ListIngressesInput{
Namespace: namespace,
Namespace: chartResult.Namespace,
LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s-web", appName),
})
if err != nil {
@@ -1086,7 +548,7 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
common.LogVerboseQuiet(fmt.Sprintf("Removing non-matching ingress resource: %s", ingressName))
err := clientset.DeleteIngress(ctx, DeleteIngressInput{
Name: ingressName,
Namespace: namespace,
Namespace: chartResult.Namespace,
})
if err != nil {
@@ -1122,23 +584,18 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
if imagePullSecrets != "" {
keepImagePullSecrets = []string{imagePullSecrets}
}
if err := pruneStaleImagePullSecretsFromDeployments(ctx, clientset, namespace, appName, keepImagePullSecrets); err != nil {
if err := pruneStaleImagePullSecretsFromDeployments(ctx, clientset, chartResult.Namespace, appName, keepImagePullSecrets); err != nil {
return fmt.Errorf("Error pruning stale imagePullSecrets entries: %w", err)
}
kustomizeRootPath := ""
if hasKustomizeDirectory(appName) {
kustomizeRootPath = getProcessSpecificKustomizeRootPath(appName)
}
common.LogInfo2(fmt.Sprintf("Installing %s", appName))
err = helmAgent.InstallOrUpgradeChart(ctx, ChartInput{
ChartPath: chartPath,
KustomizeRootPath: kustomizeRootPath,
Namespace: namespace,
ReleaseName: appName,
RollbackOnFailure: allowRollbacks,
Timeout: timeoutDuration,
ChartPath: chartResult.ChartPath,
KustomizeRootPath: chartResult.KustomizeRootPath,
Namespace: chartResult.Namespace,
ReleaseName: chartResult.ReleaseName,
RollbackOnFailure: chartResult.RollbackOnFailure,
Timeout: chartResult.Timeout,
Wait: true,
})
if err != nil {