mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-06 13:18:58 +00:00
* feat: add uc dns set
This allows manual setting of the cluster's domain name. I use this to
make the local cluster admin aware that this has been done.
(and then manually update the external dns)
```
% uc dns set ctrl.u.science.ru.nl
Set cluster domain: ctrl.u.science.ru.nl
Deploy the Caddy reverse proxy service ('uc caddy deploy') to enable internet access to your services via the reserved or your custom domain.
% uc dns show
ctrl.u.science.ru.nl
% uc dns set ctrl.u.science.ru.nl
Error: domain already reserved
% uc dns release
Released cluster domain: ctrl.u.science.ru.nl
```
See: #37
Signed-off-by: Miek Gieben <miek@miek.nl>
* docs
Signed-off-by: Miek Gieben <miek@miek.nl>
* check validatity
Signed-off-by: Miek Gieben <miek@miek.nl>
* No need to call out to update anything, as set doesnt use uncloud dns
Signed-off-by: Miek Gieben <miek@miek.nl>
* Ignore domain when there is no endpoint
Prevents this error, when you know what you're doing:
Error: failed to update DNS records pointing to caddy service: create cluster domain records in Uncloud DNS: rpc error: code = Unknown desc = Post "/domains/ctrl.u.science.ru.nl/records": unsupported protocol scheme ""
Signed-off-by: Miek Gieben <miek@miek.nl>
* from main
Signed-off-by: Miek Gieben <miek@miek.nl>
* mise proto && make cli-docs
Signed-off-by: Miek Gieben <miek@miek.nl>
* Rebase and and generate protos again
Signed-off-by: Miek Gieben <miek@miek.nl>
* Rebase and fix
Signed-off-by: Miek Gieben <miek@miek.nl>
* feat(dns): allow setting an externally managed cluster domain
---------
Signed-off-by: Miek Gieben <miek@miek.nl>
Co-authored-by: Pasha Sviderski <me@psviderski.name>
72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package dns
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/psviderski/uncloud/api/pb"
|
|
"github.com/psviderski/uncloud/cmd/uc/caddy"
|
|
"github.com/psviderski/uncloud/internal/cli"
|
|
"github.com/psviderski/uncloud/pkg/api"
|
|
"github.com/psviderski/uncloud/pkg/client"
|
|
"github.com/spf13/cobra"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
const DefaultUncloudDNSAPIEndpoint = "https://dns.uncloud.run/v1"
|
|
|
|
type reserveOptions struct {
|
|
endpoint string
|
|
}
|
|
|
|
func NewReserveCommand() *cobra.Command {
|
|
opts := reserveOptions{}
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "reserve",
|
|
Short: "Reserve a cluster domain in Uncloud DNS.",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
|
return reserve(cmd.Context(), uncli, opts)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVar(&opts.endpoint, "endpoint", DefaultUncloudDNSAPIEndpoint,
|
|
"API endpoint for the Uncloud DNS service.")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func reserve(ctx context.Context, uncli *cli.CLI, opts reserveOptions) error {
|
|
clusterClient, err := uncli.ConnectCluster(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("connect to cluster: %w", err)
|
|
}
|
|
defer clusterClient.Close()
|
|
|
|
domain, err := clusterClient.ReserveDomain(ctx, &pb.ReserveDomainRequest{Endpoint: opts.endpoint})
|
|
if err != nil {
|
|
if status.Convert(err).Code() == codes.AlreadyExists {
|
|
return errors.New("cluster domain already configured, unset it with 'uc dns set \"\"' " +
|
|
"or release a reservation with 'uc dns release' first")
|
|
}
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Reserved cluster domain: %s\n", domain.Name)
|
|
|
|
// Update cluster domain records in Uncloud DNS to point to machines running caddy service if it has been deployed.
|
|
if _, err = clusterClient.InspectService(ctx, client.CaddyServiceName); err != nil {
|
|
if errors.Is(err, api.ErrNotFound) {
|
|
fmt.Println("Deploy the Caddy reverse proxy service ('uc caddy deploy') to enable internet access " +
|
|
"to your services via the reserved or your custom domain.")
|
|
return nil
|
|
}
|
|
return fmt.Errorf("inspect caddy service: %w", err)
|
|
}
|
|
|
|
return caddy.UpdateDomainRecords(ctx, clusterClient, uncli.ProgressOut())
|
|
}
|