Add netmask to ip addresses

Change-Id: I067d93360c4384fc0b02234e75c311d105ea7b69
This commit is contained in:
Kostiantyn Kalynovskyi 2021-06-12 19:40:07 +00:00 committed by Kostyantyn Kalynovskyi
parent 0e709c0d27
commit 7737926909
4 changed files with 34 additions and 1 deletions

View File

@ -14,6 +14,7 @@ stringData:
template: |
{{ $netToIface := dict }}
{{ $netToIp := dict }}
{{ $netToNetmask := dict }}
links:
{{- range .BuilderDomain.Interfaces }}
- id: {{ .Name }}
@ -29,6 +30,7 @@ stringData:
{{- /* Save the network->interface mapping, needed below */ -}}
{{- $_ := set $netToIface .NetworkName .Name }}
{{- $_ := set $netToIp .NetworkName .IPAddress }}
{{- $_ := set $netToNetmask .NetworkName .NetMask }}
{{- end }}
networks:
{{- range .Networks }}
@ -36,7 +38,7 @@ stringData:
type: {{ .Type }}
link: {{ index $netToIface .Name }}
ip_address: {{ index $netToIp .Name }}
#netmask: "TODO - see if needed when ip has CIDR range"
netmask: {{ index $netToNetmask .Name }}
dns_nameservers: {{ .DNSServers }}
{{- if .Routes }}
routes:

View File

@ -287,6 +287,16 @@ string
</tr>
<tr>
<td>
<code>netMask</code><br>
<em>
string
</em>
</td>
<td>
</td>
</tr>
<tr>
<td>
<code>macAddress</code><br>
<em>
string

View File

@ -31,6 +31,7 @@ type Builder struct {
type BuilderNetworkInterface struct {
IPAddress string `json:"ipAddress,omitempty"`
NetMask string `json:"netMask,omitempty"`
MACAddress string `json:"macAddress,omitempty"`
NetworkInterface `json:",inline"`
}

View File

@ -18,6 +18,7 @@ import (
"bytes"
"context"
"fmt"
"net"
"text/template"
"time"
@ -322,10 +323,15 @@ func (r *BMHManager) domainSpecificNetValues(
if err != nil {
return networkTemplateValues{}, err
}
netmask, err := convertNetmask(subnet)
if err != nil {
return networkTemplateValues{}, err
}
domainInterfaces = append(domainInterfaces, vinov1.BuilderNetworkInterface{
IPAddress: ipAddress,
MACAddress: macAddress,
NetworkInterface: iface,
NetMask: netmask,
})
r.Logger.Info("Got MAC and IP for the network and node",
@ -507,3 +513,17 @@ func applyRuntimeObject(ctx context.Context, key client.ObjectKey, obj client.Ob
}
return err
}
func convertNetmask(subnet string) (string, error) {
_, network, err := net.ParseCIDR(subnet)
if err != nil {
return "", err
}
m := network.Mask
if len(m) != 4 {
return "", fmt.Errorf("Couldn't parse netmask for subnet %s", subnet)
}
return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]), nil
}