github.com/openshift/installer@v1.4.17/pkg/utils/vmware.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  	"fmt"
     7  	"net"
     8  	"net/netip"
     9  	"strings"
    10  
    11  	"github.com/sirupsen/logrus"
    12  	ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
    13  
    14  	machinev1beta1 "github.com/openshift/api/machine/v1beta1"
    15  	"github.com/openshift/installer/pkg/types"
    16  )
    17  
    18  // ConstructNetworkKargsFromMachine does something.
    19  func ConstructNetworkKargsFromMachine(claims []ipamv1.IPAddressClaim, addresses []ipamv1.IPAddress, machine *machinev1beta1.Machine, network machinev1beta1.NetworkDeviceSpec) (string, error) {
    20  	var ipAddresses []string
    21  	var gateways []string
    22  	for idx := range network.AddressesFromPools {
    23  		for _, address := range addresses {
    24  			logrus.Debugf("Checking IPAdress %v.  Does it match? %v", address.Name, fmt.Sprintf("%s-claim-%d-%d", machine.Name, 0, idx))
    25  			if address.Name == fmt.Sprintf("%s-claim-%d-%d", machine.Name, 0, idx) {
    26  				ipAddresses = append(ipAddresses, fmt.Sprintf("%v/%v", address.Spec.Address, address.Spec.Prefix))
    27  				gateways = append(gateways, address.Spec.Gateway)
    28  				break
    29  			}
    30  		}
    31  	}
    32  	return ConstructKargsFromNetworkConfig(ipAddresses, network.Nameservers, gateways)
    33  }
    34  
    35  func getSubnetMask(prefix netip.Prefix) (string, error) {
    36  	prefixLength := net.IPv4len * 8
    37  	if prefix.Addr().Is6() {
    38  		prefixLength = net.IPv6len * 8
    39  	}
    40  	ipMask := net.CIDRMask(prefix.Masked().Bits(), prefixLength)
    41  	maskBytes, err := hex.DecodeString(ipMask.String())
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	ip := net.IP(maskBytes)
    46  	maskStr := ip.To16().String()
    47  	return maskStr, nil
    48  }
    49  
    50  // ConstructKargsForBootstrap constructs the kargs string for a bootstrap node.
    51  func ConstructKargsForBootstrap(installConfig *types.InstallConfig) (string, error) {
    52  	for _, host := range installConfig.VSphere.Hosts {
    53  		if host.Role != "bootstrap" {
    54  			continue
    55  		}
    56  		return ConstructKargsFromNetworkConfig(host.NetworkDevice.IPAddrs, host.NetworkDevice.Nameservers, []string{host.NetworkDevice.Gateway})
    57  	}
    58  	return "", errors.New("unable to find host with bootstrap role")
    59  }
    60  
    61  // ConstructKargsFromNetworkConfig constructs the kargs string from the network configuration.
    62  func ConstructKargsFromNetworkConfig(ipAddrs []string, nameservers []string, gateways []string) (string, error) {
    63  	outKargs := ""
    64  
    65  	for index, address := range ipAddrs {
    66  		var gatewayIP netip.Addr
    67  		gateway := gateways[index]
    68  		if len(gateway) > 0 {
    69  			ip, err := netip.ParseAddr(gateway)
    70  			if err != nil {
    71  				return "", err
    72  			}
    73  			if ip.Is6() {
    74  				gateway = fmt.Sprintf("[%s]", gateway)
    75  			}
    76  			gatewayIP = ip
    77  		}
    78  
    79  		prefix, err := netip.ParsePrefix(address)
    80  		if err != nil {
    81  			return "", err
    82  		}
    83  		var ipStr, gatewayStr, maskStr string
    84  		addr := prefix.Addr()
    85  		switch {
    86  		case addr.Is6():
    87  			maskStr = fmt.Sprintf("%d", prefix.Bits())
    88  			ipStr = fmt.Sprintf("[%s]", addr.String())
    89  			if len(gateway) > 0 && gatewayIP.Is6() {
    90  				gatewayStr = gateway
    91  			}
    92  		case addr.Is4():
    93  			maskStr, err = getSubnetMask(prefix)
    94  			if err != nil {
    95  				return "", err
    96  			}
    97  			if len(gateway) > 0 && gatewayIP.Is4() {
    98  				gatewayStr = gateway
    99  			}
   100  			ipStr = addr.String()
   101  		default:
   102  			return "", errors.New("IP address must adhere to IPv4 or IPv6 format")
   103  		}
   104  		outKargs += fmt.Sprintf("ip=%s::%s:%s:::none ", ipStr, gatewayStr, maskStr)
   105  	}
   106  
   107  	for _, nameserver := range nameservers {
   108  		ip := net.ParseIP(nameserver)
   109  		if ip.To4() == nil {
   110  			nameserver = fmt.Sprintf("[%s]", nameserver)
   111  		}
   112  		outKargs += fmt.Sprintf("nameserver=%s ", nameserver)
   113  	}
   114  
   115  	outKargs = strings.Trim(outKargs, " ")
   116  	logrus.Debugf("Generated karg: [%v].", outKargs)
   117  	return outKargs, nil
   118  }