Skip to content

Commit ff14e09

Browse files
authored
Merge pull request #381 from squat/automatically_discover_hostname
kgctl: make peer name argument optional
2 parents 71430a0 + 07f45d4 commit ff14e09

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

cmd/kgctl/connect_linux.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func takeIPNet(_ net.IP, i *net.IPNet, err error) *net.IPNet {
6868
func connect() *cobra.Command {
6969
cmd := &cobra.Command{
7070
Use: "connect",
71-
Args: cobra.ExactArgs(1),
71+
Args: cobra.MaximumNArgs(1),
7272
RunE: runConnect,
7373
Short: "connect to a Kilo cluster as a peer over WireGuard",
7474
SilenceUsage: true,
@@ -118,7 +118,16 @@ func runConnect(cmd *cobra.Command, args []string) error {
118118
}
119119
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
120120
logger = log.With(logger, "caller", log.DefaultCaller)
121-
peerName := args[0]
121+
var peerName string
122+
var err error
123+
if len(args) > 0 {
124+
peerName = args[0]
125+
} else {
126+
level.Debug(logger).Log("msg", "no peer name provided; using hostname")
127+
if peerName, err = os.Hostname(); err != nil {
128+
return fmt.Errorf("could not determine hostname: %w", err)
129+
}
130+
}
122131

123132
for i := range allowedIPs {
124133
_, aip, err := net.ParseCIDR(allowedIPs[i])
@@ -129,7 +138,6 @@ func runConnect(cmd *cobra.Command, args []string) error {
129138
}
130139

131140
var privateKey wgtypes.Key
132-
var err error
133141
if connectOpts.privateKey == "" {
134142
privateKey, err = wgtypes.GeneratePrivateKey()
135143
if err != nil {

docs/kgctl.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ When the command exits, all of the configuration, including newly registered Pee
6868
Example:
6969

7070
```shell
71-
PEER_NAME=laptop
7271
SERVICECIDR=10.43.0.0/16
73-
kgctl connect $PEER_NAME --allowed-ips $SERVICECIDR
72+
kgctl connect --allowed-ips $SERVICECIDR
7473
```
7574

7675
The local host is now connected to the cluster and all IPs from the cluster and any registered Peers are fully routable.
76+
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.
7777
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.
7878

7979
With the service CIDR of the cluster routable from the local host, Kubernetes DNS names can now be resolved by the cluster DNS provider.

e2e/kgctl.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ test_connect() {
1414
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"
1515
assert "retry 10 5 '' check_ping --local" "should be able to ping Pods from host"
1616
docker stop "$PEER"
17+
18+
local PEER=test-hostname
19+
local ALLOWED_IP=10.5.0.1/32
20+
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"
21+
assert "retry 10 5 '' check_ping --local" "should be able to ping Pods from host using auto-discovered name"
22+
docker stop "$PEER"
1723
}

0 commit comments

Comments
 (0)