mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-06 13:18:58 +00:00
feat(dns): allow setting an externally managed cluster domain
This commit is contained in:
1 parent
b28c458029
commit
9ee3ca15eb
21 files changed
+233
-136
No files matched your search
@@ -202,6 +202,10 @@ func UpdateDomainRecords(ctx context.Context, clusterClient *client.Client, prog
|
||||
}
|
||||
return fmt.Errorf("get cluster domain: %w", err)
|
||||
}
|
||||
if !domain.Reserved {
|
||||
fmt.Printf("Skipping DNS records update as cluster domain '%s' is managed externally.\n", domain.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("Updating cluster domain records in Uncloud DNS to point to machines running caddy service...")
|
||||
// TODO: split the method into two: one to get the records and one to update them to ask for update confirmation.
|
||||
@@ -216,7 +220,7 @@ func UpdateDomainRecords(ctx context.Context, clusterClient *client.Client, prog
|
||||
if errors.Is(err, client.ErrNoReachableMachines) {
|
||||
fmt.Println()
|
||||
fmt.Printf("DNS records for domain '%s' could not be updated as there are no internet-reachable "+
|
||||
"machines running caddy containers.\n", domain)
|
||||
"machines running caddy containers.\n", domain.Name)
|
||||
fmt.Println()
|
||||
fmt.Println("Possible solutions:")
|
||||
fmt.Println("- Ensure your machines have public IP addresses")
|
||||
|
||||
@@ -37,6 +37,7 @@ func release(ctx context.Context, uncli *cli.CLI) error {
|
||||
if status.Convert(err).Code() == codes.NotFound {
|
||||
return errors.New("no domain reserved")
|
||||
}
|
||||
return fmt.Errorf("release cluster domain: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Released cluster domain: %s\n", domain.Name)
|
||||
|
||||
@@ -49,7 +49,8 @@ func reserve(ctx context.Context, uncli *cli.CLI, opts reserveOptions) error {
|
||||
domain, err := clusterClient.ReserveDomain(ctx, &pb.ReserveDomainRequest{Endpoint: opts.endpoint})
|
||||
if err != nil {
|
||||
if status.Convert(err).Code() == codes.AlreadyExists {
|
||||
return errors.New("domain already reserved")
|
||||
return errors.New("cluster domain already configured, unset it with 'uc dns set \"\"' " +
|
||||
"or release a reservation with 'uc dns release' first")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
+5
-3
@@ -7,11 +7,13 @@ import (
|
||||
func NewRootCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "dns",
|
||||
Short: "Manage cluster domain in Uncloud DNS.",
|
||||
Long: "Manage cluster domain in Uncloud DNS.\n" +
|
||||
Short: "Manage the cluster domain.",
|
||||
Long: "Manage the cluster domain.\n" +
|
||||
"DNS commands allow you to reserve or release a unique 'xxxxxx.uncld.dev' domain for your " +
|
||||
"cluster. When reserved, Caddy service deployments will automatically update DNS records to route " +
|
||||
"traffic to the services in the cluster.",
|
||||
"traffic to the services in the cluster.\n\n" +
|
||||
"EXPERIMENTAL: Use 'uc dns set' to configure an externally managed domain instead. " +
|
||||
"Uncloud does not manage DNS records for manually set domains.",
|
||||
}
|
||||
cmd.AddCommand(
|
||||
NewReleaseCommand(),
|
||||
|
||||
+25
-13
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/miekg/dns"
|
||||
"github.com/psviderski/uncloud/api/pb"
|
||||
"github.com/psviderski/uncloud/internal/cli"
|
||||
"github.com/psviderski/uncloud/internal/cli/tui"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@@ -17,9 +18,14 @@ func NewSetCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "set DOMAIN_NAME",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "Set a cluster domain directly in the cluster.",
|
||||
Long: "Set a cluster domain directly in the cluster, bypassing Uncloud DNS. " +
|
||||
"This assumes the DNS is externally set up.",
|
||||
Short: "Set or unset an externally managed cluster domain (EXPERIMENTAL).",
|
||||
Long: "EXPERIMENTAL: Set the cluster domain used to generate ingress hostnames for services.\n" +
|
||||
"Configure wildcard DNS records for this domain with your DNS provider. " +
|
||||
"Uncloud will not create, verify, update, or delete external DNS records.\n" +
|
||||
"Pass an empty string to unset a manually set domain. " +
|
||||
"Use 'uc dns release' to release a domain reserved in Uncloud DNS. " +
|
||||
"Setting or unsetting the domain does not change existing service hostnames.",
|
||||
Example: " uc dns set apps.example.com\n uc dns set \"\"",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||
return set(cmd.Context(), uncli, args[0])
|
||||
@@ -30,10 +36,12 @@ func NewSetCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
func set(ctx context.Context, uncli *cli.CLI, name string) error {
|
||||
labels, ok := dns.IsDomainName(name)
|
||||
if !ok || labels < 3 {
|
||||
return fmt.Errorf("domain '%s' is not a valid name", name)
|
||||
if name != "" {
|
||||
if labels, ok := dns.IsDomainName(name); !ok || labels < 2 {
|
||||
return fmt.Errorf("invalid cluster domain %q: must be a valid domain name with at least two labels", name)
|
||||
}
|
||||
}
|
||||
tui.PrintWarning("Setting an externally managed cluster domain is experimental.")
|
||||
|
||||
clusterClient, err := uncli.ConnectCluster(ctx)
|
||||
if err != nil {
|
||||
@@ -41,17 +49,21 @@ func set(ctx context.Context, uncli *cli.CLI, name string) error {
|
||||
}
|
||||
defer clusterClient.Close()
|
||||
|
||||
domain, err := clusterClient.SetDomain(ctx, &pb.SetDomainRequest{Name: name})
|
||||
_, err = clusterClient.SetDomain(ctx, &pb.SetDomainRequest{Name: name})
|
||||
if err != nil {
|
||||
if status.Convert(err).Code() == codes.AlreadyExists {
|
||||
return errors.New("domain already reserved")
|
||||
return errors.New("cluster domain already configured, unset it with 'uc dns set \"\"' or " +
|
||||
"release a reservation with 'uc dns release' first")
|
||||
}
|
||||
return err
|
||||
return fmt.Errorf("set cluster domain: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Set cluster domain: %s\n", domain.Name)
|
||||
|
||||
// No need to update anything as the domain records pointing to this cluster should be wildcard, so all
|
||||
// names already exist.
|
||||
if name == "" {
|
||||
fmt.Println("Unset cluster domain. External DNS records have not been changed.")
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("Set cluster domain: %s\n", name)
|
||||
fmt.Println("DNS is managed externally. " +
|
||||
"Configure wildcard DNS records pointing to machines running the Caddy service.")
|
||||
return nil
|
||||
}
|
||||
+2
-2
@@ -33,11 +33,11 @@ func show(ctx context.Context, uncli *cli.CLI) error {
|
||||
domain, err := clusterClient.GetDomain(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, api.ErrNotFound) {
|
||||
return errors.New("no domain reserved")
|
||||
return errors.New("no cluster domain configured")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(domain)
|
||||
fmt.Println(domain.Name)
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user