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

+157
View File
@@ -0,0 +1,157 @@
package scheduler_k3s
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/scheduler-k3s/internal/helmdiff"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/storage/driver"
)
// CommandPreview prints 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. Only the main app helm release is compared; the auxiliary
// config-secret, image-pull-secret, and TLS-secret releases are out of scope.
func CommandPreview(appName string, showSecrets bool, showSecretsDecoded bool) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
scheduler := common.PropertyGetDefault("scheduler", appName, "selected", "")
globalScheduler := common.PropertyGetDefault("scheduler", "--global", "selected", "docker-local")
if scheduler == "" {
scheduler = globalScheduler
}
if scheduler != "k3s" {
return fmt.Errorf("App %s is not configured to use the k3s scheduler", appName)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM)
go func() {
<-signals
cancel()
}()
chartResult, err := BuildAppChart(ctx, appName, "")
if err != nil {
return err
}
defer os.RemoveAll(chartResult.ChartDir)
helmAgent, err := NewHelmAgent(chartResult.Namespace, DevNullPrinter)
if err != nil {
return fmt.Errorf("Error creating helm agent: %w", err)
}
chartRequested, err := loadChartForPreview(chartResult.ChartPath)
if err != nil {
return err
}
currentManifest, releaseExists, err := fetchCurrentManifest(helmAgent, chartResult.ReleaseName)
if err != nil {
return err
}
var proposedManifest string
if releaseExists {
proposedManifest, err = renderUpgradeDryRun(ctx, helmAgent, chartResult, chartRequested)
} else {
proposedManifest, err = renderInstallDryRun(ctx, helmAgent, chartResult, chartRequested)
}
if err != nil {
return err
}
options := &helmdiff.Options{
OutputFormat: "diff",
OutputContext: 3,
ShowSecrets: showSecrets,
ShowSecretsDecoded: showSecretsDecoded,
}
current := helmdiff.Parse([]byte(currentManifest), chartResult.Namespace, true)
proposed := helmdiff.Parse([]byte(proposedManifest), chartResult.Namespace, true)
helmdiff.Manifests(current, proposed, options, os.Stdout)
return nil
}
func loadChartForPreview(chartPath string) (*chart.Chart, error) {
settings := cli.New()
opts := action.ChartPathOptions{}
located, err := opts.LocateChart(chartPath, settings)
if err != nil {
return nil, fmt.Errorf("Error locating chart: %w", err)
}
chartRequested, err := loader.Load(located)
if err != nil {
return nil, fmt.Errorf("Error loading chart: %w", err)
}
return chartRequested, nil
}
func fetchCurrentManifest(helmAgent *HelmAgent, releaseName string) (string, bool, error) {
getAction := action.NewGet(helmAgent.Configuration)
rel, err := getAction.Run(releaseName)
if err != nil {
if errors.Is(err, driver.ErrReleaseNotFound) {
return "", false, nil
}
return "", false, fmt.Errorf("Error fetching current release: %w", err)
}
return rel.Manifest, true, nil
}
func renderUpgradeDryRun(ctx context.Context, helmAgent *HelmAgent, chartResult BuildResult, chartRequested *chart.Chart) (string, error) {
kustomizeRenderer := KustomizeRenderer{
ReleaseName: chartResult.ReleaseName,
KustomizeRootPath: chartResult.KustomizeRootPath,
}
client := action.NewUpgrade(helmAgent.Configuration)
client.DryRun = true
client.DryRunOption = "client"
client.Namespace = chartResult.Namespace
client.PostRenderer = &kustomizeRenderer
rel, err := client.RunWithContext(ctx, chartResult.ReleaseName, chartRequested, map[string]interface{}{})
if err != nil {
return "", fmt.Errorf("Error rendering upgrade dry-run: %w", err)
}
return rel.Manifest, nil
}
func renderInstallDryRun(ctx context.Context, helmAgent *HelmAgent, chartResult BuildResult, chartRequested *chart.Chart) (string, error) {
kustomizeRenderer := KustomizeRenderer{
ReleaseName: chartResult.ReleaseName,
KustomizeRootPath: chartResult.KustomizeRootPath,
}
client := action.NewInstall(helmAgent.Configuration)
client.DryRun = true
client.ClientOnly = true
client.Namespace = chartResult.Namespace
client.ReleaseName = chartResult.ReleaseName
client.PostRenderer = &kustomizeRenderer
rel, err := client.RunWithContext(ctx, chartRequested, map[string]interface{}{})
if err != nil {
return "", fmt.Errorf("Error rendering install dry-run: %w", err)
}
return rel.Manifest, nil
}