diff --git a/api/bases/baremetal.openstack.org_openstackbaremetalsets.yaml b/api/bases/baremetal.openstack.org_openstackbaremetalsets.yaml index 26ad150..1a620f3 100644 --- a/api/bases/baremetal.openstack.org_openstackbaremetalsets.yaml +++ b/api/bases/baremetal.openstack.org_openstackbaremetalsets.yaml @@ -70,7 +70,7 @@ spec: description: InstanceSpec Instance specific attributes properties: ctlPlaneIP: - description: CtlPlaneIP - Control Plane IP + description: CtlPlaneIP - Control Plane IP in CIDR notation type: string networkData: description: NetworkData - Host Network Data diff --git a/api/v1beta1/openstackbaremetalset_types.go b/api/v1beta1/openstackbaremetalset_types.go index e2641b2..6b00809 100644 --- a/api/v1beta1/openstackbaremetalset_types.go +++ b/api/v1beta1/openstackbaremetalset_types.go @@ -29,7 +29,7 @@ type AutomatedCleaningMode string // InstanceSpec Instance specific attributes type InstanceSpec struct { // +kubebuilder:validation:Optional - // CtlPlaneIP - Control Plane IP + // CtlPlaneIP - Control Plane IP in CIDR notation CtlPlaneIP string `json:"ctlPlaneIP"` // +kubebuilder:validation:Optional // UserData - Host User Data diff --git a/config/crd/bases/baremetal.openstack.org_openstackbaremetalsets.yaml b/config/crd/bases/baremetal.openstack.org_openstackbaremetalsets.yaml index 26ad150..1a620f3 100644 --- a/config/crd/bases/baremetal.openstack.org_openstackbaremetalsets.yaml +++ b/config/crd/bases/baremetal.openstack.org_openstackbaremetalsets.yaml @@ -70,7 +70,7 @@ spec: description: InstanceSpec Instance specific attributes properties: ctlPlaneIP: - description: CtlPlaneIP - Control Plane IP + description: CtlPlaneIP - Control Plane IP in CIDR notation type: string networkData: description: NetworkData - Host Network Data diff --git a/config/samples/baremetal_v1beta1_openstackbaremetalset.yaml b/config/samples/baremetal_v1beta1_openstackbaremetalset.yaml index 0cdfed1..85d0324 100644 --- a/config/samples/baremetal_v1beta1_openstackbaremetalset.yaml +++ b/config/samples/baremetal_v1beta1_openstackbaremetalset.yaml @@ -6,8 +6,8 @@ spec: # Define how many BaremetalHosts are desired to be provisioned and assign # a control plane IP to them baremetalHosts: - compute-0: 172.22.0.100 - compute-1: 172.22.0.101 + compute-0: 172.22.0.100/24 + compute-1: 172.22.0.101/24 # The image to install on the provisioned nodes osImage: edpm-hardened-uefi.qcow2 # provisionServerName: openstack # uncomment if you pre-deploy a separate OsProvServer (use its name for the value) @@ -17,7 +17,6 @@ spec: # The interface on the nodes that will be assigned an IP from the mgmtCidr ctlplaneInterface: enp1s0 ctlplaneGateway: 172.22.0.3 - ctlplaneNetmask: 255.255.255.0 # An optional secret holding a data entry called "NodeRootPassword" # This will be set as the root password on all provisioned BaremetalHosts passwordSecret: baremetalset-password-secret diff --git a/pkg/openstackbaremetalset/baremetalhost.go b/pkg/openstackbaremetalset/baremetalhost.go index 22a8a0b..13d4e9a 100644 --- a/pkg/openstackbaremetalset/baremetalhost.go +++ b/pkg/openstackbaremetalset/baremetalhost.go @@ -3,6 +3,7 @@ package openstackbaremetalset import ( "context" "fmt" + "net" "strings" metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1" @@ -99,11 +100,39 @@ func BaremetalHostProvision( } if networkDataSecret == nil { + + // Check IP version and set template variables accordingly + ipAddr, ipNet, err := net.ParseCIDR(ctlPlaneIP) + if err != nil { + // TODO: Remove this conversion once all usage sets ctlPlaneIP in CIDR format. + ipAddr = net.ParseIP(ctlPlaneIP) + if ipAddr == nil { + return err + } + + var ipPrefix int + if ipAddr.To4() != nil { + ipPrefix, _ = net.IPMask(net.ParseIP(instance.Spec.CtlplaneNetmask).To4()).Size() + } else { + ipPrefix, _ = net.IPMask(net.ParseIP(instance.Spec.CtlplaneNetmask).To16()).Size() + } + _, ipNet, err = net.ParseCIDR(fmt.Sprintf("%s/%d", ipAddr, ipPrefix)) + if err != nil { + return err + } + } + + CtlplaneIPVersion := "ipv6" + if ipAddr.To4() != nil { + CtlplaneIPVersion = "ipv4" + } + templateParameters := make(map[string]interface{}) - templateParameters["CtlplaneIp"] = ctlPlaneIP + templateParameters["CtlplaneIpVersion"] = CtlplaneIPVersion + templateParameters["CtlplaneIp"] = ipAddr templateParameters["CtlplaneInterface"] = instance.Spec.CtlplaneInterface templateParameters["CtlplaneGateway"] = instance.Spec.CtlplaneGateway - templateParameters["CtlplaneNetmask"] = instance.Spec.CtlplaneNetmask + templateParameters["CtlplaneNetmask"] = net.IP(ipNet.Mask) if len(instance.Spec.BootstrapDNS) > 0 { templateParameters["CtlplaneDns"] = instance.Spec.BootstrapDNS } else { diff --git a/templates/openstackbaremetalset/cloudinit/networkdata b/templates/openstackbaremetalset/cloudinit/networkdata index ff22756..6a27819 100644 --- a/templates/openstackbaremetalset/cloudinit/networkdata +++ b/templates/openstackbaremetalset/cloudinit/networkdata @@ -3,13 +3,16 @@ links: id: {{ .CtlplaneInterface }} type: vif networks: -- netmask: {{ .CtlplaneNetmask }} - link: {{ .CtlplaneInterface }} +- link: {{ .CtlplaneInterface }} id: {{ .CtlplaneInterface }} + type: {{ .CtlplaneIpVersion }} ip_address: {{ .CtlplaneIp }} - type: ipv4 + netmask: {{ .CtlplaneNetmask }} {{- if (index . "CtlplaneGateway") }} - gateway: {{ .CtlplaneGateway }} + routes: + - network: {{ if eq .CtlplaneIpVersion "ipv6" }}::{{ else }}0.0.0.0{{ end }} + netmask: {{ if eq .CtlplaneIpVersion "ipv6" }}::{{ else }}0.0.0.0{{ end }} + gateway: {{ .CtlplaneGateway }} {{- end }} {{- if not (eq (len .CtlplaneDns) 0) }} services: