Skip to content

Commit e28bd24

Browse files
authored
Merge pull request #1225 from jiaqiluo/machine-selector-files
2 parents 3127552 + b5df6c5 commit e28bd24

9 files changed

Lines changed: 486 additions & 11 deletions

docs/resources/cluster_v2.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,53 @@ EOF
580580
}
581581
```
582582

583+
### Creating Rancher V2 Cluster with Machine Selector Files. For Rancher v2.7.2 and above.
584+
585+
Machine selector files provides a means to deliver files to nodes, so that the files can be in place before initiating K3s server or agent processes.
586+
For more information, please refer to Rancher documentation:
587+
[RKE2 Cluster Configuration Reference](https://ranchermanager.docs.rancher.com/reference-guides/cluster-configuration/rancher-server-configuration/rke2-cluster-configuration#machineselectorconfig) or
588+
[K3s Cluster Configuration Reference](https://ranchermanager.docs.rancher.com/reference-guides/cluster-configuration/rancher-server-configuration/k3s-cluster-configuration#machineselectorfiles)
589+
590+
```hcl
591+
resource "rancher2_cluster_v2" "foo" {
592+
name = var.rke2_cluster_name
593+
kubernetes_version = "v1.25.13+rke2r1" // or a K3s version
594+
enable_network_policy = false
595+
rke_config {
596+
machine_pools {
597+
name = "pool1"
598+
cloud_credential_secret_name = rancher2_cloud_credential.foo.id
599+
control_plane_role = true
600+
etcd_role = true
601+
worker_role = true
602+
quantity = 1
603+
machine_config {
604+
kind = rancher2_machine_config_v2.foo.kind
605+
name = rancher2_machine_config_v2.foo.name
606+
}
607+
}
608+
machine_selector_files {
609+
machine_label_selector {
610+
match_labels = {
611+
"rke.cattle.io/control-plane-role" = "true"
612+
}
613+
}
614+
file_sources {
615+
secret {
616+
name = "config-file-v1"
617+
default_permissions = "644"
618+
items {
619+
key = "audit-policy"
620+
path ="/etc/rancher/rke2/custom/policy-v1.yaml"
621+
permissions = "666"
622+
}
623+
}
624+
}
625+
}
626+
}
627+
}
628+
```
629+
583630
## Argument Reference
584631

585632
The following arguments are supported:
@@ -659,6 +706,7 @@ The following attributes are exported:
659706
* `machine_global_config` - (Optional) Cluster V2 machine global config. Must be in YAML format (string)
660707
* `machine_pools` - (Optional/Computed) Cluster V2 machine pools (list)
661708
* `machine_selector_config` - (Optional/Computed) Cluster V2 machine selector config (list)
709+
* `machine_selector_files` - (Optional/Computed) Cluster V2 machine selector files (list)
662710
* `registries` - (Optional) Cluster V2 docker registries (list maxitems:1)
663711
* `etcd` - (Optional) Cluster V2 etcd (list maxitems:1)
664712
* `rotate_certificates` (Optional) Cluster V2 certificate rotation (list maxitems:1)
@@ -764,6 +812,46 @@ The following attributes are exported:
764812
* `operator` - (Optional) Machine selector label match expressions operator (string)
765813
* `values` - (Optional) Machine selector label match expressions values (List string)
766814

815+
#### `machine_selector_files`
816+
817+
##### Arguments
818+
819+
* `machine_label_selector` - (Optional) Machine selector label (list maxitems:1)
820+
* `files` - (Optional) Machine selector files (list)
821+
822+
#### `files`
823+
824+
##### Arguments
825+
826+
* `secret` - (Optional) The secret which is the source of files (list maxitems:1)
827+
* `configmap` - (Optional) The configmap which is the source of files (list maxitems:1)
828+
829+
#### `secret`
830+
831+
##### Arguments
832+
833+
* `name` - (Required) The name of the secret (string)
834+
* `default_permissions` - (Optional) The numeric representation of the file default permissions (string)
835+
* `items` - (Optional) Items to retrieve from the secret (list)
836+
837+
#### `configmap`
838+
839+
##### Arguments
840+
841+
* `name` - (Required) The name of the configmap (string)
842+
* `default_permissions` - (Optional) The numeric representation of the file default permissions (string)
843+
* `items` - (Optional) Items to retrieve from the configmap (list)
844+
845+
#### `items`
846+
847+
##### Arguments
848+
849+
* `key` - (Required) The key of the item to retrieve (string)
850+
* `path` - (Required) The path to put the file in the target node (string)
851+
* `dynamic` - (Optional) If true, the file is ignored when determining whether the node should be drained before updating the node plan (Boolean, default: true)
852+
* `permissions` - (Optional) The numeric representation of the file permission (string)
853+
* `hash` - (Optional) The base64 encoded value of the SHA256 checksum of the file's content (string)
854+
767855
#### `registries`
768856

769857
##### Arguments

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ require (
112112
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
113113
github.com/oklog/run v1.0.0 // indirect
114114
github.com/pkg/errors v0.9.1 // indirect
115-
github.com/pmezard/go-difflib v1.0.0 // indirect
116115
github.com/pkg/sftp v1.13.5 // indirect
117116
github.com/pmezard/go-difflib v1.0.0 // indirect
118117
github.com/posener/complete v1.2.3 // indirect

rancher2/schema_cluster_v2_rke_config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
88
)
99

10-
//Types
10+
// Types
1111

1212
func clusterV2RKEConfigFieldsV0() map[string]*schema.Schema {
1313
s := map[string]*schema.Schema{
@@ -112,6 +112,15 @@ func clusterV2RKEConfigFieldsV0() map[string]*schema.Schema {
112112
Schema: clusterV2RKEConfigSystemConfigFieldsV0(),
113113
},
114114
},
115+
"machine_selector_files": {
116+
Type: schema.TypeList,
117+
Optional: true,
118+
Computed: true,
119+
Description: "Cluster V2 machine selector files",
120+
Elem: &schema.Resource{
121+
Schema: clusterV2RKEConfigMachineSelectorFilesFields(),
122+
},
123+
},
115124
"registries": {
116125
Type: schema.TypeList,
117126
MaxItems: 1,
@@ -266,6 +275,15 @@ func clusterV2RKEConfigFields() map[string]*schema.Schema {
266275
Schema: clusterV2RKEConfigSystemConfigFields(),
267276
},
268277
},
278+
"machine_selector_files": {
279+
Type: schema.TypeList,
280+
Optional: true,
281+
Computed: true,
282+
Description: "Cluster V2 machine selector files",
283+
Elem: &schema.Resource{
284+
Schema: clusterV2RKEConfigMachineSelectorFilesFields(),
285+
},
286+
},
269287
"registries": {
270288
Type: schema.TypeList,
271289
MaxItems: 1,
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package rancher2
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
)
6+
7+
// Types
8+
9+
func clusterV2RKEConfigKeyToPathFields() map[string]*schema.Schema {
10+
s := map[string]*schema.Schema{
11+
"key": {
12+
Type: schema.TypeString,
13+
Required: true,
14+
Description: "The key of the item(file) to retrieve",
15+
},
16+
"path": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
Description: "The path to put the file in the target node",
20+
},
21+
"dynamic": {
22+
Type: schema.TypeBool,
23+
Optional: true,
24+
Default: true,
25+
Description: "If ture, the file is ignored when determining whether the node should be drained before updating the node plan (default: true).",
26+
},
27+
"permissions": {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Description: "The numeric representation of the file permissions",
31+
},
32+
"hash": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Description: "The base64 encoded value of the SHA256 checksum of the file's content",
36+
},
37+
}
38+
39+
return s
40+
}
41+
42+
func clusterV2RKEConfigK8sObjectFileSourceFields() map[string]*schema.Schema {
43+
s := map[string]*schema.Schema{
44+
"name": {
45+
Type: schema.TypeString,
46+
Required: true,
47+
Description: "The name of the K8s object",
48+
},
49+
"items": {
50+
Type: schema.TypeList,
51+
Optional: true,
52+
Description: "Items(files) to retrieve from the K8s object",
53+
Elem: &schema.Resource{
54+
Schema: clusterV2RKEConfigKeyToPathFields(),
55+
},
56+
},
57+
"default_permissions": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
Description: "The default permissions to be applied when they are not set at the item level",
61+
},
62+
}
63+
64+
return s
65+
}
66+
func clusterV2RKEConfigFileSourceFields() map[string]*schema.Schema {
67+
s := map[string]*schema.Schema{
68+
"secret": {
69+
Type: schema.TypeList,
70+
Optional: true,
71+
Description: "The secret which is the source of files",
72+
MaxItems: 1,
73+
Elem: &schema.Resource{
74+
Schema: clusterV2RKEConfigK8sObjectFileSourceFields(),
75+
},
76+
},
77+
"configmap": {
78+
Type: schema.TypeList,
79+
Optional: true,
80+
Description: "The configmap which is the source of files",
81+
MaxItems: 1,
82+
Elem: &schema.Resource{
83+
Schema: clusterV2RKEConfigK8sObjectFileSourceFields(),
84+
},
85+
},
86+
}
87+
88+
return s
89+
}
90+
91+
func clusterV2RKEConfigMachineSelectorFilesFields() map[string]*schema.Schema {
92+
s := map[string]*schema.Schema{
93+
"machine_label_selector": {
94+
Type: schema.TypeList,
95+
MaxItems: 1,
96+
Optional: true,
97+
Description: "Machine label selector",
98+
Elem: &schema.Resource{
99+
Schema: clusterV2RKEConfigSystemConfigLabelSelectorFields(),
100+
},
101+
},
102+
"file_sources": {
103+
Type: schema.TypeList,
104+
Optional: true,
105+
Description: "File sources",
106+
Elem: &schema.Resource{
107+
Schema: clusterV2RKEConfigFileSourceFields(),
108+
},
109+
},
110+
}
111+
112+
return s
113+
}

rancher2/structure_cluster_v2_rke_config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func flattenClusterV2RKEConfig(in *provisionv1.RKEConfig) []interface{} {
3636
if in.MachineSelectorConfig != nil && len(in.MachineSelectorConfig) > 0 {
3737
obj["machine_selector_config"] = flattenClusterV2RKEConfigSystemConfig(in.MachineSelectorConfig)
3838
}
39+
if in.MachineSelectorFiles != nil && len(in.MachineSelectorFiles) > 0 {
40+
obj["machine_selector_files"] = flattenClusterV2RKEConfigMachineSelectorFiles(in.MachineSelectorFiles)
41+
}
3942
if in.Registries != nil {
4043
obj["registries"] = flattenClusterV2RKEConfigRegistry(in.Registries)
4144
}
@@ -91,6 +94,9 @@ func expandClusterV2RKEConfig(p []interface{}) *provisionv1.RKEConfig {
9194
if v, ok := in["machine_selector_config"].([]interface{}); ok && len(v) > 0 {
9295
obj.MachineSelectorConfig = expandClusterV2RKEConfigSystemConfig(v)
9396
}
97+
if v, ok := in["machine_selector_files"].([]interface{}); ok && len(v) > 0 {
98+
obj.MachineSelectorFiles = expandClusterV2RKEConfigProvisioningFiles(v)
99+
}
94100
if v, ok := in["registries"].([]interface{}); ok && len(v) > 0 {
95101
obj.Registries = expandClusterV2RKEConfigRegistry(v)
96102
}

0 commit comments

Comments
 (0)