diff --git a/cmd/kgctl/connect_linux.go b/cmd/kgctl/connect_linux.go index 707f533a..9031efa3 100644 --- a/cmd/kgctl/connect_linux.go +++ b/cmd/kgctl/connect_linux.go @@ -68,7 +68,7 @@ func takeIPNet(_ net.IP, i *net.IPNet, err error) *net.IPNet { func connect() *cobra.Command { cmd := &cobra.Command{ Use: "connect", - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: runConnect, Short: "connect to a Kilo cluster as a peer over WireGuard", SilenceUsage: true, @@ -118,7 +118,16 @@ func runConnect(cmd *cobra.Command, args []string) error { } logger = log.With(logger, "ts", log.DefaultTimestampUTC) logger = log.With(logger, "caller", log.DefaultCaller) - peerName := args[0] + var peerName string + var err error + if len(args) > 0 { + peerName = args[0] + } else { + level.Debug(logger).Log("msg", "no peer name provided; using hostname") + if peerName, err = os.Hostname(); err != nil { + return fmt.Errorf("could not determine hostname: %w", err) + } + } for i := range allowedIPs { _, aip, err := net.ParseCIDR(allowedIPs[i]) @@ -129,7 +138,6 @@ func runConnect(cmd *cobra.Command, args []string) error { } var privateKey wgtypes.Key - var err error if connectOpts.privateKey == "" { privateKey, err = wgtypes.GeneratePrivateKey() if err != nil { diff --git a/docs/kgctl.md b/docs/kgctl.md index f991da47..e5398f4d 100644 --- a/docs/kgctl.md +++ b/docs/kgctl.md @@ -68,12 +68,12 @@ When the command exits, all of the configuration, including newly registered Pee Example: ```shell -PEER_NAME=laptop SERVICECIDR=10.43.0.0/16 -kgctl connect $PEER_NAME --allowed-ips $SERVICECIDR +kgctl connect --allowed-ips $SERVICECIDR ``` The local host is now connected to the cluster and all IPs from the cluster and any registered Peers are fully routable. +By default, `kgctl` will use the local host's hostname as the Peer name in the mesh; this can be overridden by providing an additional argument for the preferred name. When combined with the `--clean-up false` flag, the configuration produced by the command is persistent and will remain in effect even after the process is stopped. With the service CIDR of the cluster routable from the local host, Kubernetes DNS names can now be resolved by the cluster DNS provider. diff --git a/e2e/kgctl.sh b/e2e/kgctl.sh index 752607cf..07e17692 100644 --- a/e2e/kgctl.sh +++ b/e2e/kgctl.sh @@ -14,4 +14,10 @@ test_connect() { docker run -d --name="$PEER" --rm --network=host --cap-add=NET_ADMIN -v "$KGCTL_BINARY":/kgctl -v "$PWD/$KUBECONFIG":/kubeconfig --entrypoint=/kgctl alpine --kubeconfig /kubeconfig connect "$PEER" --allowed-ip "$ALLOWED_IP" assert "retry 10 5 '' check_ping --local" "should be able to ping Pods from host" docker stop "$PEER" + + local PEER=test-hostname + local ALLOWED_IP=10.5.0.1/32 + docker run -d --name="$PEER" --rm --network=host --cap-add=NET_ADMIN -v "$KGCTL_BINARY":/kgctl -v "$PWD/$KUBECONFIG":/kubeconfig --entrypoint=/kgctl alpine --kubeconfig /kubeconfig connect --allowed-ip "$ALLOWED_IP" + assert "retry 10 5 '' check_ping --local" "should be able to ping Pods from host using auto-discovered name" + docker stop "$PEER" }