Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CIDR for microcluster Network Interface Configuration #449

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/_parts/commands/k8s_bootstrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ k8s bootstrap [flags]
### Options

```
--address string microcluster address, defaults to the node IP address
--address string microcluster address or CIDR, defaults to the node IP address
--file string path to the YAML file containing your custom cluster bootstrap configuration. Use '-' to read from stdin.
-h, --help help for bootstrap
--interactive interactively configure the most important cluster options
Expand Down
2 changes: 1 addition & 1 deletion docs/src/_parts/commands/k8s_join-cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ k8s join-cluster <join-token> [flags]
### Options

```
--address string microcluster address, defaults to the node IP address
--address string microcluster address or CIDR, defaults to the node IP address
--file string path to the YAML file containing your custom cluster join configuration. Use '-' to read from stdin.
-h, --help help for join-cluster
--name string node name, defaults to hostname
Expand Down
13 changes: 8 additions & 5 deletions src/k8s/cmd/k8s/k8s_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/canonical/k8s/pkg/config"
"github.com/canonical/k8s/pkg/utils"
"github.com/canonical/lxd/lxd/util"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -73,10 +72,14 @@ func newBootstrapCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
}
}

if opts.address == "" {
opts.address = util.NetworkInterfaceAddress()
address, err := utils.ParseAddressString(opts.address, config.DefaultPort)
if err != nil {
cmd.PrintErrf("Error: Failed to parse the address %q.\n\nThe error was: %v\n", opts.address, err)
env.Exit(1)
return
}
opts.address = util.CanonicalNetworkAddress(opts.address, config.DefaultPort)

opts.address = address
mateoflorido marked this conversation as resolved.
Show resolved Hide resolved

client, err := env.Client(cmd.Context())
if err != nil {
Expand Down Expand Up @@ -147,7 +150,7 @@ func newBootstrapCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
cmd.Flags().BoolVar(&opts.interactive, "interactive", false, "interactively configure the most important cluster options")
cmd.Flags().StringVar(&opts.configFile, "file", "", "path to the YAML file containing your custom cluster bootstrap configuration. Use '-' to read from stdin.")
cmd.Flags().StringVar(&opts.name, "name", "", "node name, defaults to hostname")
cmd.Flags().StringVar(&opts.address, "address", "", "microcluster address, defaults to the node IP address")
cmd.Flags().StringVar(&opts.address, "address", "", "microcluster address or CIDR, defaults to the node IP address")
cmd.Flags().StringVar(&opts.outputFormat, "output-format", "plain", "set the output format to one of plain, json or yaml")
cmd.Flags().DurationVar(&opts.timeout, "timeout", 90*time.Second, "the max time to wait for the command to execute")

Expand Down
14 changes: 9 additions & 5 deletions src/k8s/cmd/k8s/k8s_join_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
apiv1 "github.com/canonical/k8s/api/v1"
cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/canonical/k8s/pkg/config"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/k8s/pkg/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -55,10 +55,14 @@ func newJoinClusterCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
opts.name = hostname
}

if opts.address == "" {
opts.address = util.NetworkInterfaceAddress()
address, err := utils.ParseAddressString(opts.address, config.DefaultPort)
if err != nil {
cmd.PrintErrf("Error: Failed to parse the address %q.\n\nThe error was: %v\n", opts.address, err)
env.Exit(1)
return
}
opts.address = util.CanonicalNetworkAddress(opts.address, config.DefaultPort)

opts.address = address
mateoflorido marked this conversation as resolved.
Show resolved Hide resolved

client, err := env.Client(cmd.Context())
if err != nil {
Expand Down Expand Up @@ -110,7 +114,7 @@ func newJoinClusterCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
},
}
cmd.Flags().StringVar(&opts.name, "name", "", "node name, defaults to hostname")
cmd.Flags().StringVar(&opts.address, "address", "", "microcluster address, defaults to the node IP address")
cmd.Flags().StringVar(&opts.address, "address", "", "microcluster address or CIDR, defaults to the node IP address")
cmd.Flags().StringVar(&opts.configFile, "file", "", "path to the YAML file containing your custom cluster join configuration. Use '-' to read from stdin.")
cmd.Flags().StringVar(&opts.outputFormat, "output-format", "plain", "set the output format to one of plain, json or yaml")
cmd.Flags().DurationVar(&opts.timeout, "timeout", 90*time.Second, "the max time to wait for the command to execute")
Expand Down
50 changes: 50 additions & 0 deletions src/k8s/pkg/utils/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,32 @@ import (
"fmt"
"math/big"
"net"
"strconv"
"strings"

"github.com/canonical/lxd/lxd/util"
)

// FindMatchingNodeAddress returns the IP address of a network interface that belongs to the given CIDR.
func FindMatchingNodeAddress(cidr *net.IPNet) (net.IP, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("could not get interface addresses: %w", err)
}

for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if cidr.Contains(ipNet.IP) {
return ipNet.IP, nil
}
neoaggelos marked this conversation as resolved.
Show resolved Hide resolved
}

return nil, fmt.Errorf("could not find a matching address for CIDR %q", cidr.String())
}

// GetFirstIP returns the first IP address of a subnet. Use big.Int so that it can handle both IPv4 and IPv6 addreses.
func GetFirstIP(subnet string) (net.IP, error) {
_, cidr, err := net.ParseCIDR(subnet)
Expand Down Expand Up @@ -37,3 +60,30 @@ func GetKubernetesServiceIPsFromServiceCIDRs(serviceCIDR string) ([]net.IP, erro
}
return firstIPs, nil
}

// ParseAddressString parses an address string and returns a canonical network address.
func ParseAddressString(address string, port int64) (string, error) {
bschimke95 marked this conversation as resolved.
Show resolved Hide resolved
host, hostPort, err := net.SplitHostPort(address)
if err == nil {
address = host
port, err = strconv.ParseInt(hostPort, 10, 64)
if err != nil {
return "", fmt.Errorf("failed to parse the port from %q: %w", hostPort, err)
}
}

if address == "" {
address = util.NetworkInterfaceAddress()
}

if _, ipNet, err := net.ParseCIDR(address); err == nil {
mateoflorido marked this conversation as resolved.
Show resolved Hide resolved
matchingIP, err := FindMatchingNodeAddress(ipNet)
mateoflorido marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", fmt.Errorf("failed to find a matching node address for %q: %w", address, err)
}
address = matchingIP.String()
}

return util.CanonicalNetworkAddress(address, port), nil

}
Loading