Skip to content

Commit 3a6eef4

Browse files
committed
feat: add --caddyfile flag to 'uc caddy deploy' and 'uc run' commands
1 parent 9f5ca9a commit 3a6eef4

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

cmd/uncloud/caddy/deploy.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"maps"
8+
"os"
89
"slices"
910
"strings"
1011

@@ -18,9 +19,10 @@ import (
1819
)
1920

2021
type deployOptions struct {
21-
image string
22-
machines []string
23-
context string
22+
caddyfile string
23+
image string
24+
machines []string
25+
context string
2426
}
2527

2628
func NewDeployCommand() *cobra.Command {
@@ -37,6 +39,8 @@ func NewDeployCommand() *cobra.Command {
3739
},
3840
}
3941

42+
cmd.Flags().StringVar(&opts.caddyfile, "caddyfile", "",
43+
"Path to a custom global Caddy config (Caddyfile) that will be prepended to the auto-generated Caddy config.")
4044
cmd.Flags().StringVar(&opts.image, "image", "",
4145
"Caddy Docker image to deploy. (default caddy:LATEST_VERSION)")
4246
cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil,
@@ -51,6 +55,15 @@ func NewDeployCommand() *cobra.Command {
5155
}
5256

5357
func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
58+
caddyfile := ""
59+
if opts.caddyfile != "" {
60+
data, err := os.ReadFile(opts.caddyfile)
61+
if err != nil {
62+
return fmt.Errorf("read Caddyfile: %w", err)
63+
}
64+
caddyfile = strings.TrimSpace(string(data))
65+
}
66+
5467
clusterClient, err := uncli.ConnectCluster(ctx, opts.context)
5568
if err != nil {
5669
return fmt.Errorf("connect to cluster: %w", err)
@@ -91,7 +104,7 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
91104
placement := api.Placement{
92105
Machines: cli.ExpandCommaSeparatedValues(opts.machines),
93106
}
94-
d, err := clusterClient.NewCaddyDeployment(opts.image, "", placement)
107+
d, err := clusterClient.NewCaddyDeployment(opts.image, caddyfile, placement)
95108
if err != nil {
96109
return fmt.Errorf("create caddy deployment: %w", err)
97110
}

cmd/uncloud/service/run.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
type runOptions struct {
20+
caddyfile string
2021
command []string
2122
cpu dockeropts.NanoCPUs
2223
entrypoint string
@@ -57,6 +58,9 @@ func NewRunCommand() *cobra.Command {
5758
},
5859
}
5960

61+
cmd.Flags().StringVar(&opts.caddyfile, "caddyfile", "",
62+
"Path to a custom Caddy config (Caddyfile) for the service. "+
63+
"Cannot be used together with non-@host published ports.")
6064
cmd.Flags().VarP(&opts.cpu, "cpu", "",
6165
"Maximum number of CPU cores a service container can use. Fractional values are allowed: "+
6266
"0.5 for half a core or 2.25 for two and a quarter cores.")
@@ -161,6 +165,15 @@ func run(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
161165
func prepareServiceSpec(opts runOptions) (api.ServiceSpec, error) {
162166
var spec api.ServiceSpec
163167

168+
caddyfile := ""
169+
if opts.caddyfile != "" {
170+
data, err := os.ReadFile(opts.caddyfile)
171+
if err != nil {
172+
return spec, fmt.Errorf("read Caddyfile: %w", err)
173+
}
174+
caddyfile = strings.TrimSpace(string(data))
175+
}
176+
164177
env, err := parseEnv(opts.env)
165178
if err != nil {
166179
return spec, err
@@ -218,6 +231,12 @@ func prepareServiceSpec(opts runOptions) (api.ServiceSpec, error) {
218231
Volumes: volumes,
219232
}
220233

234+
if caddyfile != "" {
235+
spec.Caddy = &api.CaddySpec{
236+
Config: caddyfile,
237+
}
238+
}
239+
221240
// Overwrite the default ENTRYPOINT of the image or reset it if an empty string is passed.
222241
if opts.entrypoint != "" {
223242
spec.Container.Entrypoint = []string{opts.entrypoint}

0 commit comments

Comments
 (0)