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
+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
|
||||
}
|
||||
Reference in new issue
Block a user