mirror of
https://github.com/psviderski/uncloud.git
synced 2026-11-10 20:54:17 +00:00
feat(caddy): make cluster storage for Caddy default, remember 'uc caddy deploy' options
This commit is contained in:
+192
-20
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/psviderski/uncloud/internal/cli"
|
||||
"github.com/psviderski/uncloud/internal/cli/completion"
|
||||
"github.com/psviderski/uncloud/internal/cli/tui"
|
||||
"github.com/psviderski/uncloud/internal/version"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/psviderski/uncloud/pkg/client"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -25,6 +26,10 @@ type deployOptions struct {
|
||||
caddyfile string
|
||||
image string
|
||||
machines []string
|
||||
|
||||
caddyfileChanged bool
|
||||
imageChanged bool
|
||||
machinesChanged bool
|
||||
}
|
||||
|
||||
func NewDeployCommand() *cobra.Command {
|
||||
@@ -33,21 +38,62 @@ func NewDeployCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "deploy",
|
||||
Short: "Deploy or upgrade Caddy reverse proxy across all machines in the cluster.",
|
||||
Long: "Deploy or upgrade Caddy reverse proxy across all machines in the cluster.\n" +
|
||||
"A rolling update is performed when updating existing containers to minimise disruption.",
|
||||
Long: `Deploy or upgrade Caddy reverse proxy across all machines in the cluster.
|
||||
A rolling update is performed when updating existing containers to minimise disruption.
|
||||
|
||||
By default, this deploys the latest version of the Uncloud Caddy image with a global config that enables cluster
|
||||
storage for TLS certificates. See https://github.com/unlabs-dev/caddy-uncloud for more details.
|
||||
|
||||
Values set with --image, --caddyfile, and --machine are kept for later deployments. Pass an empty value to reset
|
||||
a flag to its default.
|
||||
|
||||
If this command didn't deploy the current caddy service, for example, it was deployed with 'uc deploy'
|
||||
from a Compose file or an older version of uc, the deployment replaces its configuration.
|
||||
|
||||
Requires Uncloud daemon ` + client.MinCaddyDaemonVersion + ` or newer on all machines.`,
|
||||
Example: ` # Deploy Caddy or upgrade it to the latest version of the Uncloud Caddy image.
|
||||
uc caddy deploy
|
||||
|
||||
# Deploy a custom image and keep it for later deployments.
|
||||
uc caddy deploy --image ghcr.io/unlabs-dev/caddy-uncloud:0.1.3
|
||||
|
||||
# Go back to the latest version of the Uncloud Caddy image.
|
||||
uc caddy deploy --image ""
|
||||
|
||||
# Deploy a custom image build and a custom global Caddy config and keep both for later deployments.
|
||||
uc caddy deploy --image myorg/caddy-uncloud:latest --caddyfile global.Caddyfile
|
||||
|
||||
# Go back to the default global Caddy config but keep the custom image.
|
||||
uc caddy deploy --caddyfile ""
|
||||
|
||||
# Deploy only to specific machines and keep them for later deployments.
|
||||
uc caddy deploy --machine machine1,machine2
|
||||
|
||||
# Go back to deploying to all machines.
|
||||
uc caddy deploy --machine ""`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||
|
||||
opts.caddyfileChanged = cmd.Flag("caddyfile").Changed
|
||||
opts.imageChanged = cmd.Flag("image").Changed
|
||||
opts.machinesChanged = cmd.Flag("machine").Changed
|
||||
|
||||
return runDeploy(cmd.Context(), uncli, opts)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&opts.caddyfile, "caddyfile", "",
|
||||
"Path to a custom global Caddy config (Caddyfile) that will be prepended to the auto-generated Caddy config.")
|
||||
"Path to a custom global Caddy config (Caddyfile) that will be prepended to the auto-generated Caddy config.\n"+
|
||||
"An empty value resets it to the default config that enables cluster storage.\n"+
|
||||
"(default is the current custom config or the default config)")
|
||||
cmd.Flags().StringVar(&opts.image, "image", "",
|
||||
"Caddy Docker image to deploy. (default caddy:LATEST_VERSION)")
|
||||
"Caddy Docker image to deploy. It must include the Uncloud storage module if the Caddy config uses it.\n"+
|
||||
"An empty value resets it to the latest version of the Uncloud Caddy image.\n"+
|
||||
"(default is the current custom image or "+client.CaddyImage+":LATEST_VERSION)")
|
||||
cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil,
|
||||
"Machine names or IDs to deploy to. Can be specified multiple times or as a comma-separated "+
|
||||
"list. (default is all machines)")
|
||||
"Machine names or IDs to deploy to. Can be specified multiple times or as a comma-separated list.\n"+
|
||||
"An empty value resets it to all machines.\n"+
|
||||
"(default is the current machines or all machines)")
|
||||
|
||||
completion.MachinesFlag(cmd)
|
||||
|
||||
@@ -55,13 +101,13 @@ func NewDeployCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
caddyfile := ""
|
||||
config := ""
|
||||
if opts.caddyfile != "" {
|
||||
data, err := os.ReadFile(opts.caddyfile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read Caddyfile: %w", err)
|
||||
}
|
||||
caddyfile = strings.TrimSpace(string(data))
|
||||
config = strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
clusterClient, err := uncli.ConnectCluster(ctx)
|
||||
@@ -70,6 +116,10 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
}
|
||||
defer clusterClient.Close()
|
||||
|
||||
if err = clusterClient.Caddy.CheckDaemonVersions(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc, err := clusterClient.InspectService(ctx, client.CaddyServiceName)
|
||||
if err != nil {
|
||||
if !errors.Is(err, api.ErrNotFound) {
|
||||
@@ -100,26 +150,48 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
}
|
||||
}
|
||||
|
||||
if opts.image != "" {
|
||||
fmt.Println(tui.Faint.Render("target image: ") + tui.FormatImage(opts.image, tui.Green))
|
||||
current := svc.Spec()
|
||||
deployOpts := resolveDeploymentOptions(current, opts, config)
|
||||
configChanged := deployOpts.ConfigOrDefault() != current.CaddyConfig()
|
||||
|
||||
if svc.ID != "" && configChanged {
|
||||
printCaddyConfig("current global Caddy config:", current.CaddyConfig())
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("Preparing a deployment plan...")
|
||||
|
||||
placement := api.Placement{
|
||||
Machines: cli.ExpandCommaSeparatedValues(opts.machines),
|
||||
}
|
||||
d, err := clusterClient.Caddy.NewDeployment(ctx, client.CaddyDeploymentOptions{
|
||||
Image: opts.image, Config: caddyfile, Placement: placement,
|
||||
})
|
||||
d, err := clusterClient.Caddy.NewDeployment(ctx, deployOpts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create caddy deployment: %w", err)
|
||||
}
|
||||
target := d.Spec
|
||||
|
||||
if opts.image == "" {
|
||||
fmt.Println(tui.Faint.Render("target image: ") + tui.FormatImage(d.Spec.Container.Image,
|
||||
tui.Green) + tui.Faint.Render(" (latest stable)"))
|
||||
// The Uncloud Caddy image resolves to the latest version allowed by this CLI, which is older than the running image
|
||||
// if a newer CLI deployed it. Continuing would downgrade the image and replace its default config.
|
||||
if deployOpts.Image == "" && client.IsNewerCaddyImage(current.Container.Image, target.Container.Image) {
|
||||
return fmt.Errorf("caddy service runs image '%s' which is newer than the latest version '%s' supported by "+
|
||||
"this uc version: upgrade uc or specify the image with --image",
|
||||
current.Container.Image, target.Container.Image)
|
||||
}
|
||||
|
||||
imageNote := " (latest)"
|
||||
if deployOpts.Image != "" {
|
||||
imageNote = " (custom)"
|
||||
if !opts.imageChanged {
|
||||
imageNote = " (custom, use --image \"\" for the latest version)"
|
||||
}
|
||||
}
|
||||
fmt.Println(tui.Faint.Render("target image: ") + tui.FormatImage(target.Container.Image, tui.Green) +
|
||||
tui.Faint.Render(imageNote))
|
||||
|
||||
if configChanged {
|
||||
if deployOpts.Config != nil {
|
||||
// The user provided this config so there is no need to print its content.
|
||||
fmt.Println(tui.Faint.Render("target global Caddy config: ") + opts.caddyfile)
|
||||
} else {
|
||||
printCaddyConfig("target global Caddy config: (default)", deployOpts.ConfigOrDefault())
|
||||
}
|
||||
}
|
||||
|
||||
plan, err := d.Plan(ctx)
|
||||
@@ -131,7 +203,7 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
fmt.Printf("%s service is up to date.\n", client.CaddyServiceName)
|
||||
} else {
|
||||
if svc.ID == "" {
|
||||
if len(opts.machines) > 0 {
|
||||
if len(deployOpts.Placement.Machines) > 0 {
|
||||
fmt.Println("This will run a Caddy container on each selected machine.")
|
||||
} else {
|
||||
fmt.Println("This will run a Caddy container on each machine.")
|
||||
@@ -163,6 +235,14 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
fmt.Println(summary)
|
||||
fmt.Println()
|
||||
|
||||
warnings := deploymentWarnings(current, target, opts)
|
||||
for _, warning := range warnings {
|
||||
tui.PrintWarning(warning)
|
||||
}
|
||||
if len(warnings) > 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
title := "Proceed with deployment?"
|
||||
// Include the direct connection or context name in the confirmation prompt to avoid accidentally
|
||||
// deploying to the wrong cluster.
|
||||
@@ -195,6 +275,98 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
return UpdateDomainRecords(ctx, clusterClient, uncli.ProgressOut())
|
||||
}
|
||||
|
||||
// resolveDeploymentOptions returns the deployment options for the flags and the current service spec, which is zero if
|
||||
// the service doesn't exist. Each flag works on its own: a specified flag overrides the value and an empty one resets
|
||||
// it to the default. A flag that isn't specified keeps the explicitly set value.
|
||||
func resolveDeploymentOptions(
|
||||
current api.ServiceSpec, opts deployOptions, config string,
|
||||
) client.CaddyDeploymentOptions {
|
||||
var resolved client.CaddyDeploymentOptions
|
||||
|
||||
if opts.imageChanged {
|
||||
resolved.Image = opts.image
|
||||
} else if _, custom := current.Labels[api.LabelCaddyImageCustom]; custom {
|
||||
resolved.Image = current.Container.Image
|
||||
}
|
||||
|
||||
if opts.caddyfileChanged {
|
||||
if opts.caddyfile != "" {
|
||||
resolved.Config = new(config)
|
||||
}
|
||||
} else if _, custom := current.Labels[api.LabelCaddyConfigCustom]; custom {
|
||||
resolved.Config = new(current.CaddyConfig())
|
||||
}
|
||||
|
||||
if opts.machinesChanged {
|
||||
resolved.Placement.Machines = cli.ExpandCommaSeparatedValues(opts.machines)
|
||||
} else if _, managed := current.Labels[api.LabelCaddyManaged]; managed {
|
||||
resolved.Placement = current.Placement
|
||||
}
|
||||
|
||||
return resolved
|
||||
}
|
||||
|
||||
// deploymentWarnings returns the consequences of deploying the target spec over the current one, which is zero if
|
||||
// the service doesn't exist.
|
||||
func deploymentWarnings(current, target api.ServiceSpec, opts deployOptions) []string {
|
||||
var warnings []string
|
||||
exists := current.Name != ""
|
||||
|
||||
if _, managed := current.Labels[api.LabelCaddyManaged]; exists && !managed {
|
||||
warnings = append(warnings, "'uc caddy deploy' doesn't manage this caddy service yet, so this deployment "+
|
||||
"replaces its configuration, including any custom global Caddy config.\n"+
|
||||
"If you manage it with a Compose file, cancel and deploy with 'uc deploy' instead.")
|
||||
}
|
||||
|
||||
currentStorage := client.CaddyStorageModule(current.CaddyConfig())
|
||||
targetStorage := client.CaddyStorageModule(target.CaddyConfig())
|
||||
if exists && currentStorage != targetStorage {
|
||||
warning := fmt.Sprintf("Caddy certificate storage changes from %s to %s.\n"+
|
||||
"Caddy doesn't automatically migrate certificates, so all of them will be reissued and HTTPS may fail "+
|
||||
"for those domains until that finishes.\n"+
|
||||
"To migrate them first, see %s/concepts/ingress/cluster-storage-for-caddy",
|
||||
caddyStorageName(currentStorage), caddyStorageName(targetStorage), version.DocsURL)
|
||||
if !opts.caddyfileChanged {
|
||||
warning += "\nTo keep the current storage, specify a custom Caddyfile that uses it with --caddyfile."
|
||||
}
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
|
||||
imageOrConfigChanged := current.Container.Image != target.Container.Image ||
|
||||
current.CaddyConfig() != target.CaddyConfig()
|
||||
if targetStorage == client.CaddyStorageUncloud && imageOrConfigChanged &&
|
||||
!client.IsDefaultCaddyImage(target.Container.Image) {
|
||||
warnings = append(warnings, fmt.Sprintf("Image '%s' must include the Uncloud storage module to use "+
|
||||
"'storage uncloud'. Otherwise, Caddy won't start or apply the new config.\n"+
|
||||
"Check 'uc caddy config' after the deployment for errors.", target.Container.Image))
|
||||
}
|
||||
|
||||
return warnings
|
||||
}
|
||||
|
||||
// caddyStorageName returns a user-friendly name of the Caddy storage module.
|
||||
func caddyStorageName(module string) string {
|
||||
switch module {
|
||||
case client.CaddyStorageFileSystem:
|
||||
return "local"
|
||||
case client.CaddyStorageUncloud:
|
||||
return "cluster"
|
||||
default:
|
||||
return "'" + module + "'"
|
||||
}
|
||||
}
|
||||
|
||||
// printCaddyConfig prints the title and the indented config or "none" if it's empty.
|
||||
func printCaddyConfig(title, config string) {
|
||||
if config == "" {
|
||||
lipgloss.Println(tui.Faint.Render(title) + " none")
|
||||
return
|
||||
}
|
||||
|
||||
lipgloss.Println(tui.Faint.Render(title))
|
||||
fmt.Println(config)
|
||||
}
|
||||
|
||||
func UpdateDomainRecords(ctx context.Context, clusterClient *client.Client, progressOut *streams.Out) error {
|
||||
domain, err := clusterClient.GetDomain(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user