feat(dns): reserve cluster domain when initialising cluster by default + update dns records

This commit is contained in:
Pavel Sviderski
2025-03-03 19:12:21 +10:00
parent c3123820b2
commit 69d3a511d6
2 changed files with 53 additions and 18 deletions
+9 -3
View File
@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/charmbracelet/huh" "github.com/charmbracelet/huh"
"github.com/docker/cli/cli/streams"
"github.com/docker/compose/v2/pkg/progress" "github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"maps" "maps"
@@ -173,7 +174,11 @@ func deploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
} }
fmt.Println() fmt.Println()
if _, err = clusterClient.GetDomain(ctx); err != nil { return UpdateDomainRecords(ctx, clusterClient, uncli.ProgressOut())
}
func UpdateDomainRecords(ctx context.Context, clusterClient *client.Client, progressOut *streams.Out) error {
if _, err := clusterClient.GetDomain(ctx); err != nil {
if errors.Is(err, client.ErrNotFound) { if errors.Is(err, client.ErrNotFound) {
fmt.Println("Skipping DNS records update as no cluster domain is reserved (see 'uc dns').") fmt.Println("Skipping DNS records update as no cluster domain is reserved (see 'uc dns').")
return nil return nil
@@ -185,10 +190,11 @@ func deploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
// TODO: split the method into two: one to get the records and one to update them to ask for update confirmation. // TODO: split the method into two: one to get the records and one to update them to ask for update confirmation.
var records []*pb.DNSRecord var records []*pb.DNSRecord
err = progress.RunWithTitle(ctx, func(ctx context.Context) error { err := progress.RunWithTitle(ctx, func(ctx context.Context) error {
var err error
records, err = clusterClient.CreateIngressRecords(ctx, client.CaddyServiceName) records, err = clusterClient.CreateIngressRecords(ctx, client.CaddyServiceName)
return err return err
}, uncli.ProgressOut(), "Verifying internet access to caddy service") }, progressOut, "Verifying internet access to caddy service")
if err != nil { if err != nil {
if errors.Is(err, client.ErrNoReachableMachines) { if errors.Is(err, client.ErrNoReachableMachines) {
fmt.Println() fmt.Println()
+44 -15
View File
@@ -6,18 +6,23 @@ import (
"github.com/docker/compose/v2/pkg/progress" "github.com/docker/compose/v2/pkg/progress"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"net/netip" "net/netip"
"uncloud/cmd/uncloud/caddy"
"uncloud/cmd/uncloud/dns"
"uncloud/internal/cli" "uncloud/internal/cli"
"uncloud/internal/cli/config" "uncloud/internal/cli/config"
"uncloud/internal/machine/api/pb"
"uncloud/internal/machine/cluster" "uncloud/internal/machine/cluster"
) )
type initOptions struct { type initOptions struct {
name string dnsEndpoint string
network string name string
noCaddy bool network string
publicIP string noCaddy bool
sshKey string noDNS bool
cluster string publicIP string
sshKey string
cluster string
} }
func NewInitCommand() *cobra.Command { func NewInitCommand() *cobra.Command {
@@ -47,6 +52,8 @@ func NewInitCommand() *cobra.Command {
return initCluster(cmd.Context(), uncli, remoteMachine, opts) return initCluster(cmd.Context(), uncli, remoteMachine, opts)
}, },
} }
cmd.Flags().StringVar(&opts.dnsEndpoint, "dns-endpoint", dns.DefaultUncloudDNSAPIEndpoint,
"API endpoint for the Uncloud DNS service.")
cmd.Flags().StringVarP( cmd.Flags().StringVarP(
&opts.name, "name", "n", "", &opts.name, "name", "n", "",
"Assign a name to the machine.", "Assign a name to the machine.",
@@ -59,6 +66,10 @@ func NewInitCommand() *cobra.Command {
&opts.noCaddy, "no-caddy", false, &opts.noCaddy, "no-caddy", false,
"Don't deploy Caddy reverse proxy service to the machine.", "Don't deploy Caddy reverse proxy service to the machine.",
) )
cmd.Flags().BoolVar(
&opts.noDNS, "no-dns", false,
"Don't reserve a cluster domain in Uncloud DNS.",
)
cmd.Flags().StringVar( cmd.Flags().StringVar(
&opts.publicIP, "public-ip", "auto", &opts.publicIP, "public-ip", "auto",
"Public IP address of the machine for ingress configuration. Use 'auto' for automatic detection, "+ "Public IP address of the machine for ingress configuration. Use 'auto' for automatic detection, "+
@@ -102,7 +113,7 @@ func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteM
} }
defer client.Close() defer client.Close()
if opts.noCaddy { if opts.noCaddy && opts.noDNS {
return nil return nil
} }
@@ -111,15 +122,33 @@ func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteM
// after cluster initialisation, we keep the user informed during this wait. // after cluster initialisation, we keep the user informed during this wait.
fmt.Println("Waiting for the machine to be ready...") fmt.Println("Waiting for the machine to be ready...")
d, err := client.NewCaddyDeployment("", nil) if !opts.noDNS {
if err != nil { domain, err := client.ReserveDomain(ctx, &pb.ReserveDomainRequest{Endpoint: opts.dnsEndpoint})
return fmt.Errorf("create caddy deployment: %w", err) if err != nil {
return fmt.Errorf("reserve cluster domain in Uncloud DNS: %w", err)
}
fmt.Printf("Reserved cluster domain: %s\n", domain.Name)
} }
return progress.RunWithTitle(ctx, func(ctx context.Context) error { if !opts.noCaddy {
if _, err = d.Run(ctx); err != nil { d, err := client.NewCaddyDeployment("", nil)
return fmt.Errorf("deploy caddy: %w", err) if err != nil {
return fmt.Errorf("create caddy deployment: %w", err)
} }
return nil
}, uncli.ProgressOut(), fmt.Sprintf("Deploying service %s", d.Spec.Name)) err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
if _, err = d.Run(ctx); err != nil {
return fmt.Errorf("deploy caddy: %w", err)
}
return nil
}, uncli.ProgressOut(), fmt.Sprintf("Deploying service %s", d.Spec.Name))
if err != nil {
return err
}
fmt.Println()
return caddy.UpdateDomainRecords(ctx, client, uncli.ProgressOut())
}
return nil
} }