-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
libvirt.go
67 lines (57 loc) · 2 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
67
// Package libvirt contains libvirt-specific Terraform-variable logic.
package libvirt
import (
"encoding/json"
"fmt"
"net"
"strconv"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1beta1"
"github.com/openshift/installer/pkg/tfvars/internal/cache"
"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"`
MasterMemory string `json:"libvirt_master_memory,omitempty"`
MasterVcpu string `json:"libvirt_master_vcpu,omitempty"`
}
// TFVars generates libvirt-specific Terraform variables.
func TFVars(masterConfig *v1beta1.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 = cache.DownloadImageFile(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,
MasterMemory: strconv.Itoa(masterConfig.DomainMemory),
MasterVcpu: strconv.Itoa(masterConfig.DomainVcpu),
}
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
}