Răsfoiți Sursa

Merge pull request #381 from squat/automatically_discover_hostname

kgctl: make peer name argument optional
Lucas Servén Marín 2 ani în urmă
părinte
comite
ff14e09f64
3 a modificat fișierele cu 19 adăugiri și 5 ștergeri
  1. 11 3
      cmd/kgctl/connect_linux.go
  2. 2 2
      docs/kgctl.md
  3. 6 0
      e2e/kgctl.sh

+ 11 - 3
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 {

+ 2 - 2
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.

+ 6 - 0
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"
 }