forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirt.go
61 lines (51 loc) · 1.68 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
// Package libvirt contains libvirt-specific Terraform-variable logic.
package libvirt
import (
"encoding/json"
"fmt"
"net"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1alpha1"
"github.com/pkg/errors"
)
type config struct {
URI string `json:"libvirt_uri,omitempty"`
Image string `json:"os_image,omitempty"`
IfName string `json:"libvirt_network_if"`
MasterIPs []string `json:"libvirt_master_ips,omitempty"`
BootstrapIP string `json:"libvirt_bootstrap_ip,omitempty"`
}
// TFVars generates libvirt-specific Terraform variables.
func TFVars(masterConfig *v1alpha1.LibvirtMachineProviderConfig, osImage string, machineCIDR *net.IPNet, bridge string, masterCount int) ([]byte, error) {
bootstrapIP, err := cidr.Host(machineCIDR, 10)
if err != nil {
return nil, errors.Errorf("failed to generate bootstrap IP: %v", err)
}
masterIPs, err := generateIPs("master", machineCIDR, masterCount, 11)
if err != nil {
return nil, err
}
osImage, err = cachedImage(osImage)
if err != nil {
return nil, errors.Wrap(err, "failed to use cached libvirt image")
}
cfg := &config{
URI: masterConfig.URI,
Image: osImage,
IfName: bridge,
BootstrapIP: bootstrapIP.String(),
MasterIPs: masterIPs,
}
return json.MarshalIndent(cfg, "", " ")
}
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
}