forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirt.go
66 lines (56 loc) · 1.6 KB
/
libvirt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package libvirt
import (
"fmt"
"net"
"github.com/apparentlymart/go-cidr/cidr"
)
// Libvirt encompasses configuration specific to libvirt.
type Libvirt struct {
URI string `json:"libvirt_uri,omitempty"`
Image string `json:"os_image,omitempty"`
Network `json:",inline"`
MasterIPs []string `json:"libvirt_master_ips,omitempty"`
BootstrapIP string `json:"libvirt_bootstrap_ip,omitempty"`
}
// Network describes a libvirt network configuration.
type Network struct {
IfName string `json:"libvirt_network_if"`
IPRange string `json:"libvirt_ip_range"`
}
// TFVars fills in computed Terraform variables.
func (l *Libvirt) TFVars(masterCount int) error {
_, network, err := net.ParseCIDR(l.Network.IPRange)
if err != nil {
return fmt.Errorf("failed to parse libvirt network ipRange: %v", err)
}
if l.BootstrapIP == "" {
ip, err := cidr.Host(network, 10)
if err != nil {
return fmt.Errorf("failed to generate bootstrap IP: %v", err)
}
l.BootstrapIP = ip.String()
}
if len(l.MasterIPs) > 0 {
if len(l.MasterIPs) != masterCount {
return fmt.Errorf("length of MasterIPs doesn't match master count")
}
} else {
if ips, err := generateIPs("master", network, masterCount, 11); err == nil {
l.MasterIPs = ips
} else {
return err
}
}
return nil
}
func generateIPs(name string, network *net.IPNet, count int, offset int) ([]string, error) {
var ips []string
for i := 0; i < count; i++ {
ip, err := cidr.Host(network, offset+i)
if err != nil {
return nil, fmt.Errorf("failed to generate %s IPs: %v", name, err)
}
ips = append(ips, ip.String())
}
return ips, nil
}