github.com/openshift/installer@v1.4.17/pkg/asset/ignition/bootstrap/baremetal/template.go (about)

     1  package baremetal
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strings"
     7  
     8  	utilsnet "k8s.io/utils/net"
     9  
    10  	"github.com/openshift/installer/pkg/types"
    11  	"github.com/openshift/installer/pkg/types/baremetal"
    12  )
    13  
    14  // TemplateData holds data specific to templates used for the baremetal platform.
    15  type TemplateData struct {
    16  	// ProvisioningInterfaceMAC holds the interface's MAC address that the bootstrap node will use to host the ProvisioningIP below.
    17  	// When the provisioning network is disabled, this is the external baremetal network MAC address.
    18  	ProvisioningInterfaceMAC string
    19  
    20  	// ProvisioningIP holds the IP the bootstrap node will use to service Ironic, TFTP, etc.
    21  	ProvisioningIP string
    22  
    23  	// ProvisioningIPv6 determines if we are using IPv6 or not.
    24  	ProvisioningIPv6 bool
    25  
    26  	// ProvisioningCIDR has the integer CIDR notation, e.g. 255.255.255.0 should be "24"
    27  	ProvisioningCIDR int
    28  
    29  	// ProvisioningDNSMasq determines if we start the dnsmasq service on the bootstrap node.
    30  	ProvisioningDNSMasq bool
    31  
    32  	// ProvisioningDHCPRange has the DHCP range, if DHCP is not external. Otherwise it
    33  	// should be blank.
    34  	ProvisioningDHCPRange string
    35  
    36  	// ProvisioningDHCPAllowList contains a space-separated list of all of the control plane's boot
    37  	// MAC addresses. Requests to bootstrap DHCP from other hosts will be ignored.
    38  	ProvisioningDHCPAllowList string
    39  
    40  	// IronicUsername contains the username for authentication to Ironic
    41  	IronicUsername string
    42  
    43  	// IronicUsername contains the password for authentication to Ironic
    44  	IronicPassword string
    45  
    46  	// BaremetalEndpointOverride contains the url for the baremetal endpoint
    47  	BaremetalEndpointOverride string
    48  
    49  	// BaremetalIntrospectionEndpointOverride contains the url for the baremetal introspection endpoint
    50  	BaremetalIntrospectionEndpointOverride string
    51  
    52  	// ClusterOSImage contains 4 URLs to download RHCOS live iso, kernel, rootfs and initramfs
    53  	ClusterOSImage string
    54  
    55  	// API VIP for use by ironic during bootstrap.
    56  	APIVIPs []string
    57  
    58  	// Hosts is the information needed to create the objects in Ironic.
    59  	Hosts []*baremetal.Host
    60  
    61  	// How many of the Hosts are control plane machines?
    62  	ControlPlaneReplicas int64
    63  
    64  	// ProvisioningNetwork displays the type of provisioning network being used
    65  	ProvisioningNetwork string
    66  
    67  	// ExternalStaticIP is the static IP of the bootstrap node
    68  	ExternalStaticIP string
    69  
    70  	// ExternalStaticIP is the static gateway of the bootstrap node
    71  	ExternalStaticGateway string
    72  
    73  	// ExternalStaticDNS is the static DNS of the bootstrap node
    74  	ExternalStaticDNS string
    75  
    76  	ExternalSubnetCIDR int
    77  
    78  	ExternalMACAddress string
    79  
    80  	// ExternalURLv6 is a callback URL for the node if the node and the BMC use different network families
    81  	ExternalURLv6 string
    82  }
    83  
    84  func externalURLs(apiVIPs []string) (externalURLv4 string, externalURLv6 string) {
    85  	if len(apiVIPs) > 1 {
    86  		// IPv6 BMCs may not be able to reach IPv4 servers, use the right callback URL for them.
    87  		// Warning: when backporting to 4.12 or earlier, change the port to 80!
    88  		externalURL := fmt.Sprintf("http://%s/", net.JoinHostPort(apiVIPs[1], "6180"))
    89  		if utilsnet.IsIPv6String(apiVIPs[1]) {
    90  			externalURLv6 = externalURL
    91  		}
    92  		if utilsnet.IsIPv4String(apiVIPs[1]) {
    93  			externalURLv4 = externalURL
    94  		}
    95  	}
    96  
    97  	return
    98  }
    99  
   100  // GetTemplateData returns platform-specific data for bootstrap templates.
   101  func GetTemplateData(config *baremetal.Platform, networks []types.MachineNetworkEntry, controlPlaneReplicaCount int64, ironicUsername, ironicPassword string) *TemplateData {
   102  	var templateData TemplateData
   103  
   104  	templateData.Hosts = config.Hosts
   105  	templateData.ControlPlaneReplicas = controlPlaneReplicaCount
   106  
   107  	templateData.ProvisioningIP = config.BootstrapProvisioningIP
   108  	templateData.ProvisioningNetwork = string(config.ProvisioningNetwork)
   109  	templateData.ExternalStaticIP = config.BootstrapExternalStaticIP
   110  	templateData.ExternalStaticGateway = config.BootstrapExternalStaticGateway
   111  	templateData.ExternalStaticDNS = config.BootstrapExternalStaticDNS
   112  	templateData.ExternalMACAddress = config.ExternalMACAddress
   113  
   114  	_, externalURLv6 := externalURLs(config.APIVIPs)
   115  
   116  	templateData.ExternalURLv6 = externalURLv6
   117  
   118  	if len(config.APIVIPs) > 0 {
   119  		templateData.APIVIPs = config.APIVIPs
   120  		templateData.BaremetalEndpointOverride = fmt.Sprintf("http://%s/v1", net.JoinHostPort(config.APIVIPs[0], "6385"))
   121  		templateData.BaremetalIntrospectionEndpointOverride = fmt.Sprintf("http://%s/v1", net.JoinHostPort(config.APIVIPs[0], "5050"))
   122  	}
   123  
   124  	if config.BootstrapExternalStaticIP != "" {
   125  		for _, network := range networks {
   126  			cidr, _ := network.CIDR.Mask.Size()
   127  			templateData.ExternalSubnetCIDR = cidr
   128  			break
   129  		}
   130  	}
   131  
   132  	if config.ProvisioningNetwork != baremetal.DisabledProvisioningNetwork {
   133  		cidr, _ := config.ProvisioningNetworkCIDR.Mask.Size()
   134  		templateData.ProvisioningCIDR = cidr
   135  		templateData.ProvisioningIPv6 = config.ProvisioningNetworkCIDR.IP.To4() == nil
   136  		templateData.ProvisioningInterfaceMAC = config.ProvisioningMACAddress
   137  		templateData.ProvisioningDNSMasq = true
   138  	}
   139  
   140  	switch config.ProvisioningNetwork {
   141  	case baremetal.ManagedProvisioningNetwork:
   142  		cidr, _ := config.ProvisioningNetworkCIDR.Mask.Size()
   143  
   144  		// When provisioning network is managed, we set a DHCP range including
   145  		// netmask for dnsmasq.
   146  		templateData.ProvisioningDHCPRange = fmt.Sprintf("%s,%d", config.ProvisioningDHCPRange, cidr)
   147  
   148  		var dhcpAllowList []string
   149  		for _, host := range config.Hosts {
   150  			if host.IsMaster() {
   151  				dhcpAllowList = append(dhcpAllowList, host.BootMACAddress)
   152  			}
   153  		}
   154  		templateData.ProvisioningDHCPAllowList = strings.Join(dhcpAllowList, " ")
   155  	case baremetal.DisabledProvisioningNetwork:
   156  		templateData.ProvisioningInterfaceMAC = config.ExternalMACAddress
   157  		templateData.ProvisioningDNSMasq = false
   158  
   159  		if templateData.ProvisioningIP != "" {
   160  			for _, network := range networks {
   161  				if network.CIDR.Contains(net.ParseIP(templateData.ProvisioningIP)) {
   162  					templateData.ProvisioningIPv6 = network.CIDR.IP.To4() == nil
   163  
   164  					cidr, _ := network.CIDR.Mask.Size()
   165  					templateData.ProvisioningCIDR = cidr
   166  				}
   167  			}
   168  		}
   169  	}
   170  
   171  	templateData.IronicUsername = ironicUsername
   172  	templateData.IronicPassword = ironicPassword
   173  	templateData.ClusterOSImage = config.ClusterOSImage
   174  
   175  	return &templateData
   176  }