forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfvars.go
126 lines (114 loc) · 3.75 KB
/
tfvars.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Package tfvars converts an InstallConfig to Terraform variables.
package tfvars
import (
"context"
"encoding/json"
"time"
"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/tfvars/aws"
"github.com/openshift/installer/pkg/tfvars/libvirt"
"github.com/openshift/installer/pkg/tfvars/openstack"
"github.com/openshift/installer/pkg/types"
"github.com/pkg/errors"
)
type config struct {
ClusterID string `json:"cluster_id,omitempty"`
Name string `json:"cluster_name,omitempty"`
BaseDomain string `json:"base_domain,omitempty"`
Masters int `json:"master_count,omitempty"`
IgnitionBootstrap string `json:"ignition_bootstrap,omitempty"`
IgnitionMaster string `json:"ignition_master,omitempty"`
aws.AWS `json:",inline"`
libvirt.Libvirt `json:",inline"`
openstack.OpenStack `json:",inline"`
}
// TFVars converts the InstallConfig and Ignition content to
// terraform.tfvar JSON.
func TFVars(cfg *types.InstallConfig, bootstrapIgn, masterIgn string) ([]byte, error) {
config := &config{
ClusterID: cfg.ClusterID,
Name: cfg.ObjectMeta.Name,
BaseDomain: cfg.BaseDomain,
IgnitionMaster: masterIgn,
IgnitionBootstrap: bootstrapIgn,
}
for _, m := range cfg.Machines {
switch m.Name {
case "master":
var replicas int
if m.Replicas == nil {
replicas = 1
} else {
replicas = int(*m.Replicas)
}
config.Masters += replicas
if m.Platform.AWS != nil {
config.AWS.Master = aws.Master{
EC2Type: m.Platform.AWS.InstanceType,
IAMRoleName: m.Platform.AWS.IAMRoleName,
MasterRootVolume: aws.MasterRootVolume{
IOPS: m.Platform.AWS.EC2RootVolume.IOPS,
Size: m.Platform.AWS.EC2RootVolume.Size,
Type: m.Platform.AWS.EC2RootVolume.Type,
},
}
}
case "worker":
if m.Platform.AWS != nil {
config.AWS.Worker = aws.Worker{
IAMRoleName: m.Platform.AWS.IAMRoleName,
}
}
default:
return nil, errors.Errorf("unrecognized machine pool %q", m.Name)
}
}
if cfg.Platform.AWS != nil {
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()
ami, err := rhcos.AMI(ctx, rhcos.DefaultChannel, cfg.Platform.AWS.Region)
if err != nil {
return nil, errors.Wrap(err, "failed to determine default AMI")
}
config.AWS = aws.AWS{
Region: cfg.Platform.AWS.Region,
ExtraTags: cfg.Platform.AWS.UserTags,
VPCCIDRBlock: func() string {
if cfg.Platform.AWS.VPCCIDRBlock == nil {
return ""
}
return cfg.Platform.AWS.VPCCIDRBlock.String()
}(),
EC2AMIOverride: ami,
}
} else if cfg.Platform.Libvirt != nil {
masterIPs := make([]string, len(cfg.Platform.Libvirt.MasterIPs))
for i, ip := range cfg.Platform.Libvirt.MasterIPs {
masterIPs[i] = ip.String()
}
config.Libvirt = libvirt.Libvirt{
URI: cfg.Platform.Libvirt.URI,
Network: libvirt.Network{
IfName: cfg.Platform.Libvirt.Network.IfName,
IPRange: cfg.Platform.Libvirt.Network.IPRange.String(),
},
Image: cfg.Platform.Libvirt.DefaultMachinePlatform.Image,
MasterIPs: masterIPs,
}
if err := config.Libvirt.TFVars(config.Masters); err != nil {
return nil, errors.Wrap(err, "failed to insert libvirt variables")
}
if err := config.Libvirt.UseCachedImage(); err != nil {
return nil, errors.Wrap(err, "failed to use cached libvirt image")
}
} else if cfg.Platform.OpenStack != nil {
config.OpenStack = openstack.OpenStack{
Region: cfg.Platform.OpenStack.Region,
NetworkCIDRBlock: cfg.Platform.OpenStack.NetworkCIDRBlock.String(),
BaseImage: cfg.Platform.OpenStack.BaseImage,
}
config.OpenStack.Credentials.Cloud = cfg.Platform.OpenStack.Cloud
config.OpenStack.ExternalNetwork = cfg.Platform.OpenStack.ExternalNetwork
}
return json.MarshalIndent(config, "", " ")
}