Skip to content

Commit

Permalink
kgctl: make peer name argument optional
Browse files Browse the repository at this point in the history
This commit makes the peer name argument in the `kgctl connect` command
optional. Now, the computer's hostname will be used as the default peer
name when no argument is supplied. This is a good predictable feature
that makes it easier to integrate with containers and environments like
Kubernetes.

Signed-off-by: squat <[email protected]>
  • Loading branch information
squat committed May 14, 2024
1 parent 71430a0 commit 07f45d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
14 changes: 11 additions & 3 deletions cmd/kgctl/connect_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions docs/kgctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions e2e/kgctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 07f45d4

Please sign in to comment.