Skip to content

Commit c1db4d8

Browse files
feat: Implement support for GCE machine configs
1 parent f9b095c commit c1db4d8

6 files changed

Lines changed: 445 additions & 7 deletions

docs/resources/machine_config_v2.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Provides a Rancher v2 Machine config v2 resource. This can be used to create Mac
88

99
The supported cloud providers includes `amazonec2`, `azure`, `digitalocean`, `harvester`, `linode`, `openstack`, and `vsphere`.
1010

11+
12+
Starting with Rancher v2.12.0 and above, `google` is also offered as a supported cloud provider.
13+
1114
## Example Usage
1215

1316
```hcl
@@ -87,13 +90,14 @@ The following arguments are supported:
8790

8891
* `generate_name` - (Required/ForceNew) Cluster V2 generate name. The pattern to generate machine config name. e.g generate_name=\"prod-pool1\" will generate \"nc-prod-pool1-?????\" name computed at `name` attribute (string)
8992
* `fleet_namespace` - (Optional/ForceNew) Cluster V2 fleet namespace
90-
* `amazonec2_config` - (Optional) AWS config for the Machine Config V2. Conflicts with `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `openstack_config` and `vsphere_config` (list maxitems:1)
91-
* `azure_config` - (Optional) Azure config for the Machine Config V2. Conflicts with `amazonec2_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `openstack_config` and `vsphere_config` (list maxitems:1)
92-
* `digitalocean_config` - (Optional) Digitalocean config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `harvester_config`, `linode_config`, `openstack_config` and `vsphere_config` (list maxitems:1)
93-
* `harvester_config` - (Optional) Harvester config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `linode_config`, `openstack_config` and `vsphere_config` (list maxitems:1)
94-
* `linode_config` - (Optional) Linode config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `openstack_config` and `vsphere_config` (list maxitems:1)
95-
* `openstack_config` - (Optional) Openstack config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config` and `vsphere_config` (list maxitems:1)
96-
* `vsphere_config` - (Optional) vSphere config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config` and `openstack_config` (list maxitems:1)
93+
* `amazonec2_config` - (Optional) AWS config for the Machine Config V2. Conflicts with `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `openstack_config`, `google_config` and `vsphere_config` (list maxitems:1)
94+
* `azure_config` - (Optional) Azure config for the Machine Config V2. Conflicts with `amazonec2_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `openstack_config`, `google_config` and `vsphere_config` (list maxitems:1)
95+
* `digitalocean_config` - (Optional) Digitalocean config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `harvester_config`, `linode_config`, `openstack_config`, `google_config` and `vsphere_config` (list maxitems:1)
96+
* `harvester_config` - (Optional) Harvester config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `linode_config`, `openstack_config`, `google_config` and `vsphere_config` (list maxitems:1)
97+
* `linode_config` - (Optional) Linode config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `openstack_config`, `google_config` and `vsphere_config` (list maxitems:1)
98+
* `openstack_config` - (Optional) Openstack config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `google_config` and `vsphere_config` (list maxitems:1)
99+
* `vsphere_config` - (Optional) vSphere config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `google_config` and `openstack_config` (list maxitems:1)
100+
* `google_config` - (Optional) Google config for the Machine Config V2. Conflicts with `amazonec2_config`, `azure_config`, `digitalocean_config`, `harvester_config`, `linode_config`, `openstack_config` and `vsphere_config` (list maxitems:1)
97101
* `annotations` - (Optional) Annotations for Machine Config V2 object (map)
98102
* `labels` - (Optional/Computed) Labels for Machine Config V2 object (map)
99103

@@ -328,6 +332,34 @@ The following attributes are exported:
328332
* `vcenter` - (Optional/Sensitive) vSphere IP/hostname for vCenter (string)
329333
* `vcenter_port` - (Optional/Sensitive) vSphere Port for vCenter Default `443` (string)
330334

335+
336+
### `google_config`
337+
338+
#### Arguments
339+
340+
* `address` - (Optional) GCE Instance External IP (string)
341+
* `auth_encoded_json` - (Optional/Sensitive) GCE service account auth json file path (string)
342+
* `disk_size` - (Required) GCE Instance Disk Size (in GB) (string)
343+
* `disk_type` - (Required) GCE Instance Disk Type (string)
344+
* `external_firewall_rule_prefix` - (Optional) A prefix to be added to firewall rules created when exposing ports publicly. Required if exposing ports publicly via the `open_port` field. (string)
345+
* `internal_firewall_rule_prefix` - (Optional) A prefix to be added to an internal firewall rule created to ensure virtual machines can communicate with one another. Omitting this field will result in an internal firewall rule not being created. (string)
346+
* `labels` - (Optional) A set of labels to be added to each VM, in the format of 'key1,value1,key2,value2' (string)
347+
* `machine_image` - (Required) GCE instance image absolute URL (string)
348+
* `machine_type` - (Required) GCE instance type (string)
349+
* `network` - (Required) The network to provision virtual machines within (string)
350+
* `open_port` - (Optional) A list of ports to be opened publicly (e.g. `123/tcp` or `456/udp`). The `external_firewall_rule_prefix` must also be set for ports to be exposed automatically. (list)
351+
* `preemptable` - (Optional) Indicates if the virtual machine can be preempted (bool)
352+
* `project` - (Required) The GCP project to create virtual machines within (string)
353+
* `scopes` - (Optional) Access scopes to be set on the virtual machine (string)
354+
* `sub_network` - (Optional) The subnetwork to provision virtual machines within (string)
355+
* `tags` - (Optional) A set of network tags to be added to each VM, in the format of 'tag1,tag2' (string)
356+
* `use_existing` - (Optional) Indicates if an existing VM should be used. This is not currently support in Rancher Node Drivers. (bool)
357+
* `use_internal_ip` - (Optional) Indicates if the virtual machines should use an internal IP (bool)
358+
* `use_internal_ip_only` - (Optional) Indicates if the virtual machines should use an internal IP only and not be assigned a public IP (bool)
359+
* `user_data` - (Optional) GCE user-data file path (string)
360+
* `username` - (Optional) The username to be set when logging into the virtual machines (string)
361+
* `zone` - (Required) The region and zone to create virtual machines within (e.g. us-east1-b) (string)
362+
331363
## Timeouts
332364

333365
`rancher2_machine_config_v2` provides the following

rancher2/resource_rancher2_machine_config_v2.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ func createMachineConfigV2(c *Config, obj *MachineConfigV2) (*MachineConfigV2, e
227227
out.ID = resp.ID
228228
out.TypeMeta = resp.TypeMeta
229229
out.ObjectMeta = resp.ObjectMeta
230+
case machineConfigV2GoogleGCEKind:
231+
resp := &MachineConfigV2GoogleGCE{}
232+
err = c.createObjectV2(rancher2DefaultLocalClusterID, machineConfigV2GoogleGCEAPIType, obj.GoogleGCEConfig, resp)
233+
out.GoogleGCEConfig = resp
234+
out.ID = resp.ID
235+
out.TypeMeta = resp.TypeMeta
236+
out.ObjectMeta = resp.ObjectMeta
230237
default:
231238
return nil, fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind)
232239
}
@@ -332,6 +339,16 @@ func getMachineConfigV2ByID(c *Config, id, kind string) (*MachineConfigV2, error
332339
out.Type = resp.Type
333340
out.TypeMeta = resp.TypeMeta
334341
out.ObjectMeta = resp.ObjectMeta
342+
case machineConfigV2GoogleGCEKind:
343+
resp := &MachineConfigV2GoogleGCE{}
344+
err = c.getObjectV2ByID(rancher2DefaultLocalClusterID, id, machineConfigV2GoogleGCEAPIType, resp)
345+
out.GoogleGCEConfig = resp
346+
out.ID = resp.ID
347+
out.Links = resp.Links
348+
out.Actions = resp.Actions
349+
out.Type = resp.Type
350+
out.TypeMeta = resp.TypeMeta
351+
out.ObjectMeta = resp.ObjectMeta
335352
default:
336353
return nil, fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind)
337354
}
@@ -404,6 +421,13 @@ func updateMachineConfigV2(c *Config, obj *MachineConfigV2) (*MachineConfigV2, e
404421
out.ID = resp.ID
405422
out.TypeMeta = resp.TypeMeta
406423
out.ObjectMeta = resp.ObjectMeta
424+
case machineConfigV2GoogleGCEKind:
425+
resp := &MachineConfigV2GoogleGCE{}
426+
err = c.updateObjectV2(rancher2DefaultLocalClusterID, obj.ID, machineConfigV2GoogleGCEAPIType, obj.GoogleGCEConfig, resp)
427+
out.GoogleGCEConfig = resp
428+
out.ID = resp.ID
429+
out.TypeMeta = resp.TypeMeta
430+
out.ObjectMeta = resp.ObjectMeta
407431
default:
408432
return nil, fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind)
409433
}

rancher2/schema_machine_config_v2.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var allMachineDriverConfigFields = []string{
1212
"linode_config",
1313
"openstack_config",
1414
"vsphere_config",
15+
"google_config",
1516
}
1617

1718
//Schemas
@@ -101,6 +102,15 @@ func machineConfigV2Fields() map[string]*schema.Schema {
101102
Schema: machineConfigV2VmwarevsphereFields(),
102103
},
103104
},
105+
"google_config": {
106+
Type: schema.TypeList,
107+
MaxItems: 1,
108+
Optional: true,
109+
ConflictsWith: getConflicts(allMachineDriverConfigFields, "google_config"),
110+
Elem: &schema.Resource{
111+
Schema: machineConfigV2GoogleGCEFields(),
112+
},
113+
},
104114
"resource_version": {
105115
Type: schema.TypeString,
106116
Computed: true,
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package rancher2
2+
3+
import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
4+
5+
func machineConfigV2GoogleGCEFields() map[string]*schema.Schema {
6+
return map[string]*schema.Schema{
7+
"address": {
8+
Type: schema.TypeString,
9+
Optional: true,
10+
Description: "GCE Instance External IP",
11+
},
12+
"auth_encoded_json": {
13+
Type: schema.TypeString,
14+
Optional: true,
15+
Sensitive: true,
16+
Description: "GCE service account auth json file path",
17+
},
18+
"disk_size": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "GCE Instance Disk Size (in GB)",
22+
},
23+
"disk_type": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
Description: "GCE Instance Disk Type",
27+
},
28+
"external_firewall_rule_prefix": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
Description: "A prefix to be added to firewall rules created when exposing ports publicly. Required if exposing ports publicly.",
32+
},
33+
"internal_firewall_rule_prefix": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Description: "A prefix to be added to an internal firewall rule created to ensure virtual machines can communicate with one another.",
37+
},
38+
"labels": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Description: "A set of labels to be added to each VM, in the format of 'key1,value1,key2,value2'",
42+
},
43+
"machine_image": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "GCE instance image absolute URL",
47+
},
48+
"machine_type": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
Description: "GCE instance type",
52+
},
53+
"network": {
54+
Type: schema.TypeString,
55+
Required: true,
56+
Description: "The network to provision virtual machines within",
57+
},
58+
"open_port": {
59+
Type: schema.TypeList,
60+
Optional: true,
61+
Elem: &schema.Schema{
62+
Type: schema.TypeString,
63+
},
64+
Description: "A list of ports to be opened publicly. 'external_firewall_rule_prefix' must also be set",
65+
},
66+
"preemptable": {
67+
Type: schema.TypeBool,
68+
Optional: true,
69+
Description: "Indicates if the virtual machine can be preempted",
70+
},
71+
"project": {
72+
Type: schema.TypeString,
73+
Required: true,
74+
Description: "The GCP project to create virtual machines within",
75+
},
76+
"scopes": {
77+
Type: schema.TypeString,
78+
Optional: true,
79+
Description: "Access scopes to be set on the virtual machine",
80+
},
81+
"sub_network": {
82+
Type: schema.TypeString,
83+
Optional: true,
84+
Description: "The subnetwork to provision virtual machines within",
85+
},
86+
"tags": {
87+
Type: schema.TypeString,
88+
Optional: true,
89+
Description: "A set of network tags to be added to each VM, in the format of 'tag1,tag2'",
90+
},
91+
"use_existing": {
92+
Type: schema.TypeBool,
93+
Optional: true,
94+
Description: "Indicates if an existing VM should be used. This is not currently support in Rancher.",
95+
},
96+
"use_internal_ip": {
97+
Type: schema.TypeBool,
98+
Optional: true,
99+
Description: "Indicates if the virtual machines should use an internal IP",
100+
},
101+
"use_internal_ip_only": {
102+
Type: schema.TypeBool,
103+
Optional: true,
104+
Description: "Indicates if the virtual machines should use an internal IP only and not be assigned a public IP",
105+
},
106+
"user_data": {
107+
Type: schema.TypeString,
108+
Optional: true,
109+
Description: "GCE user-data file path",
110+
},
111+
"username": {
112+
Type: schema.TypeString,
113+
Optional: true,
114+
Description: "The username to be set when logging into the virtual machines",
115+
},
116+
"zone": {
117+
Type: schema.TypeString,
118+
Required: true,
119+
Description: "The region and zone to create virtual machines within (e.g. us-east1-b)",
120+
},
121+
}
122+
}

rancher2/structure_machine_config_v2.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type machineConfigV2 struct {
2626
LinodeConfig *MachineConfigV2Linode `json:"linodeConfig,omitempty" yaml:"linodeConfig,omitempty"`
2727
OpenstackConfig *MachineConfigV2Openstack `json:"openstackConfig,omitempty" yaml:"openstackConfig,omitempty"`
2828
VmwarevsphereConfig *MachineConfigV2Vmwarevsphere `json:"vmwarevsphereConfig,omitempty" yaml:"vmwarevsphereConfig,omitempty"`
29+
GoogleGCEConfig *MachineConfigV2GoogleGCE `json:"googleConfig,omitempty" yaml:"googleConfig,omitempty"`
2930
}
3031

3132
type MachineConfigV2 struct {
@@ -77,6 +78,11 @@ func flattenMachineConfigV2(d *schema.ResourceData, in *MachineConfigV2) error {
7778
if err != nil {
7879
return err
7980
}
81+
case machineConfigV2GoogleGCEKind:
82+
err := d.Set("google_config", flattenMachineConfigV2GoogleGCE(in.GoogleGCEConfig))
83+
if err != nil {
84+
return err
85+
}
8086
default:
8187
return fmt.Errorf("[ERROR] Unsupported driver on node template: %s", kind)
8288
}
@@ -143,6 +149,9 @@ func expandMachineConfigV2(in *schema.ResourceData) *MachineConfigV2 {
143149
if v, ok := in.Get("vsphere_config").([]interface{}); ok && len(v) > 0 {
144150
obj.VmwarevsphereConfig = expandMachineConfigV2Vmwarevsphere(v, obj)
145151
}
152+
if v, ok := in.Get("google_config").([]interface{}); ok && len(v) > 0 {
153+
obj.GoogleGCEConfig = expandMachineConfigV2GoogleGCE(v, obj)
154+
}
146155

147156
return obj
148157
}

0 commit comments

Comments
 (0)