Skip to content

Commit 3cda5cc

Browse files
committed
chore: caddy client to get caddy config
1 parent 75fdbaf commit 3cda5cc

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

cmd/uncloud/caddy/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
9191
placement := api.Placement{
9292
Machines: cli.ExpandCommaSeparatedValues(opts.machines),
9393
}
94-
d, err := clusterClient.NewCaddyDeployment(opts.image, placement)
94+
d, err := clusterClient.NewCaddyDeployment(opts.image, "", placement)
9595
if err != nil {
9696
return fmt.Errorf("create caddy deployment: %w", err)
9797
}

cmd/uncloud/machine/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func add(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteMachine,
146146

147147
// TODO: scale the existing Caddy service to the new machine instead of running a new deployment
148148
// that may cause a small downtime.
149-
d, err := clusterClient.NewCaddyDeployment(caddyImage, api.Placement{})
149+
d, err := clusterClient.NewCaddyDeployment(caddyImage, "", api.Placement{})
150150
if err != nil {
151151
return fmt.Errorf("create caddy deployment: %w", err)
152152
}

cmd/uncloud/machine/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteM
147147
}
148148

149149
if !opts.noCaddy {
150-
d, err := client.NewCaddyDeployment("", api.Placement{})
150+
d, err := client.NewCaddyDeployment("", "", api.Placement{})
151151
if err != nil {
152152
return fmt.Errorf("create caddy deployment: %w", err)
153153
}

pkg/client/caddy.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var caddyImageTagRegex = regexp.MustCompile(`^2\.\d+\.\d+$`)
2323
// NewCaddyDeployment creates a new deployment for a Caddy reverse proxy service.
2424
// The service is deployed in global mode to all machines in the cluster. If the image is not provided, the latest
2525
// version of the official Caddy Docker image is used.
26-
func (cli *Client) NewCaddyDeployment(image string, placement api.Placement) (*deploy.Deployment, error) {
26+
func (cli *Client) NewCaddyDeployment(image, config string, placement api.Placement) (*deploy.Deployment, error) {
2727
if image == "" {
2828
latest, err := LatestCaddyImage()
2929
if err != nil {
@@ -72,6 +72,12 @@ func (cli *Client) NewCaddyDeployment(image string, placement api.Placement) (*d
7272
},
7373
}
7474

75+
if config != "" {
76+
spec.Caddy = &api.CaddySpec{
77+
Config: config,
78+
}
79+
}
80+
7581
return cli.NewDeployment(spec, nil), nil
7682
}
7783

pkg/client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Client struct {
2323
// Methods such as Reset or Inspect are ambiguous in the context of a machine+cluster client.
2424
pb.MachineClient
2525
pb.ClusterClient
26+
Caddy pb.CaddyClient
2627
// Docker is a namespaced client for the Docker service to distinguish Uncloud-specific service container operations
2728
// from generic Docker operations.
2829
Docker *docker.Client
@@ -50,6 +51,7 @@ func New(ctx context.Context, connector Connector) (*Client, error) {
5051

5152
c.MachineClient = pb.NewMachineClient(c.conn)
5253
c.ClusterClient = pb.NewClusterClient(c.conn)
54+
c.Caddy = pb.NewCaddyClient(c.conn)
5355
c.Docker = docker.NewClient(c.conn)
5456
return c, nil
5557
}

test/e2e/service_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func TestDeployment(t *testing.T) {
271271
}
272272
})
273273

274-
deployment, err := cli.NewCaddyDeployment("", api.Placement{})
274+
deployment, err := cli.NewCaddyDeployment("", "", api.Placement{})
275275
require.NoError(t, err)
276276

277277
_, err = deployment.Run(ctx)
@@ -284,6 +284,12 @@ func TestDeployment(t *testing.T) {
284284

285285
ctr := svc.Containers[0].Container
286286
assert.Regexp(t, `^caddy:2\.\d+\.\d+$`, ctr.Config.Image)
287+
288+
config, err := cli.Caddy.GetConfig(ctx, nil)
289+
require.NoError(t, err)
290+
291+
assert.Contains(t, config.Caddyfile, "# This file is autogenerated by Uncloud")
292+
assert.Contains(t, config.Caddyfile, "handle /.uncloud-verify")
287293
})
288294

289295
t.Run("caddy with machine placement", func(t *testing.T) {
@@ -295,7 +301,7 @@ func TestDeployment(t *testing.T) {
295301
})
296302

297303
// Deploy to machine #0.
298-
deployment, err := cli.NewCaddyDeployment("", api.Placement{
304+
deployment, err := cli.NewCaddyDeployment("", "", api.Placement{
299305
Machines: []string{c.Machines[0].Name},
300306
})
301307
require.NoError(t, err)
@@ -313,7 +319,7 @@ func TestDeployment(t *testing.T) {
313319
// initialContainerID := svc.Containers[0].Container.ID
314320

315321
// Deploy to all machines without a placement constraint.
316-
deployment, err = cli.NewCaddyDeployment(image, api.Placement{})
322+
deployment, err = cli.NewCaddyDeployment(image, "", api.Placement{})
317323
require.NoError(t, err)
318324

319325
_, err = deployment.Run(ctx)
@@ -332,6 +338,40 @@ func TestDeployment(t *testing.T) {
332338
// assert.True(t, containers.Contains(initialContainerID), "Expected initial container to remain")
333339
})
334340

341+
t.Run("caddy with custom config", func(t *testing.T) {
342+
t.Cleanup(func() {
343+
err := cli.RemoveService(ctx, client.CaddyServiceName)
344+
if !errors.Is(err, api.ErrNotFound) {
345+
require.NoError(t, err)
346+
}
347+
})
348+
349+
caddyfile := `{
350+
debug
351+
}
352+
353+
myapp.example.com {
354+
reverse_proxy myapp:8000
355+
}`
356+
deployment, err := cli.NewCaddyDeployment("", caddyfile, api.Placement{})
357+
require.NoError(t, err)
358+
359+
_, err = deployment.Run(ctx)
360+
require.NoError(t, err)
361+
362+
svc, err := cli.InspectService(ctx, client.CaddyServiceName)
363+
require.NoError(t, err)
364+
assertServiceMatchesSpec(t, svc, deployment.Spec)
365+
366+
config, err := cli.Caddy.GetConfig(ctx, nil)
367+
require.NoError(t, err)
368+
369+
assert.Contains(t, config.Caddyfile, "# This file is autogenerated by Uncloud")
370+
assert.Contains(t, config.Caddyfile, "handle /.uncloud-verify")
371+
assert.Contains(t, config.Caddyfile, caddyfile,
372+
"Expected user-defined global Caddy config to be included in the Caddyfile")
373+
})
374+
335375
t.Run("replicated", func(t *testing.T) {
336376
t.Parallel()
337377

0 commit comments

Comments
 (0)