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

View File

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

View File

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

View File

@ -18,6 +18,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"net"
"text/template" "text/template"
"time" "time"
@ -322,10 +323,15 @@ func (r *BMHManager) domainSpecificNetValues(
if err != nil { if err != nil {
return networkTemplateValues{}, err return networkTemplateValues{}, err
} }
netmask, err := convertNetmask(subnet)
if err != nil {
return networkTemplateValues{}, err
}
domainInterfaces = append(domainInterfaces, vinov1.BuilderNetworkInterface{ domainInterfaces = append(domainInterfaces, vinov1.BuilderNetworkInterface{
IPAddress: ipAddress, IPAddress: ipAddress,
MACAddress: macAddress, MACAddress: macAddress,
NetworkInterface: iface, NetworkInterface: iface,
NetMask: netmask,
}) })
r.Logger.Info("Got MAC and IP for the network and node", 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 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
}