Skip to content

Commit 7b9d01c

Browse files
authored
Fix machine selector config to allow kubelet arg list (#1181)
1 parent 187a69c commit 7b9d01c

7 files changed

Lines changed: 388 additions & 21 deletions

rancher2/resource_rancher2_cluster_v2.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ func resourceRancher2ClusterV2() *schema.Resource {
2222
Importer: &schema.ResourceImporter{
2323
State: resourceRancher2ClusterV2Import,
2424
},
25-
Schema: clusterV2Fields(),
25+
Schema: clusterV2Fields(),
26+
SchemaVersion: 1,
27+
StateUpgraders: []schema.StateUpgrader{
28+
{
29+
Type: resourceRancher2ClusterV2Resource().CoreConfigSchema().ImpliedType(),
30+
Upgrade: resourceRancher2ClusterV2StateUpgradeV0,
31+
Version: 0,
32+
},
33+
},
2634
CustomizeDiff: func(d *schema.ResourceDiff, i interface{}) error {
2735
if d.HasChange("rke_config") {
2836
oldObj, newObj := d.GetChange("rke_config")
29-
//return fmt.Errorf("\n%#v\n%#v\n", oldObj, newObj)
3037
oldInterface, oldOk := oldObj.([]interface{})
3138
newInterface, newOk := newObj.([]interface{})
3239
if oldOk && newOk && len(newInterface) > 0 {
@@ -41,7 +48,6 @@ func resourceRancher2ClusterV2() *schema.Resource {
4148
}
4249
if d.HasChange("local_auth_endpoint") {
4350
oldObj, newObj := d.GetChange("local_auth_endpoint")
44-
//return fmt.Errorf("\n%#v\n%#v\n", oldObj, newObj)
4551
oldInterface, oldOk := oldObj.([]interface{})
4652
newInterface, newOk := newObj.([]interface{})
4753
if oldOk && newOk && len(newInterface) > 0 {
@@ -64,6 +70,39 @@ func resourceRancher2ClusterV2() *schema.Resource {
6470
}
6571
}
6672

73+
func resourceRancher2ClusterV2Resource() *schema.Resource {
74+
return &schema.Resource{
75+
Schema: clusterV2FieldsV0(),
76+
}
77+
}
78+
79+
func resourceRancher2ClusterV2StateUpgradeV0(rawState map[string]any, meta interface{}) (map[string]any, error) {
80+
if rkeConfigs, ok := rawState["rke_config"].([]any); ok && len(rkeConfigs) > 0 {
81+
for i := range rkeConfigs {
82+
if rkeConfig, ok := rkeConfigs[i].(map[string]any); ok && len(rkeConfig) > 0 {
83+
if machineSelectorConfigs, ok := rkeConfig["machine_selector_config"].([]any); ok && len(machineSelectorConfigs) > 0 {
84+
85+
// upgrade all machine selector configs
86+
for m := range machineSelectorConfigs {
87+
if machineSelectorConfig, ok := machineSelectorConfigs[m].(map[string]any); ok && len(machineSelectorConfig) > 0 {
88+
89+
// machine selector config data found. Migrate state from map -> string
90+
if config, ok := machineSelectorConfig["config"].(map[string]any); ok {
91+
newValue := ""
92+
if conf, err := mapInterfaceToYAML(config); err == nil {
93+
newValue = conf
94+
}
95+
rawState["rke_config"].([]interface{})[i].(map[string]any)["machine_selector_config"].([]any)[m].(map[string]any)["config"] = newValue
96+
}
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}
103+
return rawState, nil
104+
}
105+
67106
func resourceRancher2ClusterV2Create(d *schema.ResourceData, meta interface{}) error {
68107
name := d.Get("name").(string)
69108
cluster, err := expandClusterV2(d)

rancher2/schema_cluster_v2.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,126 @@ import (
44
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
55
)
66

7+
func clusterV2FieldsV0() map[string]*schema.Schema {
8+
s := map[string]*schema.Schema{
9+
"name": {
10+
Type: schema.TypeString,
11+
Required: true,
12+
ForceNew: true,
13+
Description: "Cluster V2 name",
14+
},
15+
"fleet_namespace": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
ForceNew: true,
19+
Default: "fleet-default",
20+
},
21+
"kubernetes_version": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
Description: "Cluster V2 kubernetes version",
25+
},
26+
"local_auth_endpoint": {
27+
Type: schema.TypeList,
28+
MaxItems: 1,
29+
Optional: true,
30+
Description: "Cluster V2 local auth endpoint",
31+
Elem: &schema.Resource{
32+
Schema: clusterV2LocalAuthEndpointFields(),
33+
},
34+
},
35+
"rke_config": {
36+
Type: schema.TypeList,
37+
MaxItems: 1,
38+
Optional: true,
39+
Computed: true,
40+
Description: "Cluster V2 rke config",
41+
Elem: &schema.Resource{
42+
Schema: clusterV2RKEConfigFieldsV0(),
43+
},
44+
},
45+
"agent_env_vars": {
46+
Type: schema.TypeList,
47+
Optional: true,
48+
Description: "Cluster V2 default agent env vars",
49+
Elem: &schema.Resource{
50+
Schema: envVarFields(),
51+
},
52+
},
53+
"cloud_credential_secret_name": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Description: "Cluster V2 cloud credential secret name",
57+
},
58+
"cluster_agent_deployment_customization": {
59+
Type: schema.TypeList,
60+
Optional: true,
61+
Description: "Optional customization for cluster agent",
62+
Elem: &schema.Resource{
63+
Schema: agentDeploymentCustomizationFields(),
64+
},
65+
},
66+
"fleet_agent_deployment_customization": {
67+
Type: schema.TypeList,
68+
Optional: true,
69+
Description: "Optional customization for fleet agent",
70+
Elem: &schema.Resource{
71+
Schema: agentDeploymentCustomizationFields(),
72+
},
73+
},
74+
"default_pod_security_policy_template_name": {
75+
Type: schema.TypeString,
76+
Optional: true,
77+
Description: "Cluster V2 default pod security policy template name",
78+
},
79+
"default_pod_security_admission_configuration_template_name": {
80+
Type: schema.TypeString,
81+
Optional: true,
82+
Description: "Cluster V2 default pod security admission configuration template name",
83+
},
84+
"default_cluster_role_for_project_members": {
85+
Type: schema.TypeString,
86+
Optional: true,
87+
Description: "Cluster V2 default cluster role for project members",
88+
},
89+
"enable_network_policy": {
90+
Type: schema.TypeBool,
91+
Optional: true,
92+
Computed: true,
93+
Description: "Enable k8s network policy",
94+
},
95+
// Computed attributes
96+
"cluster_registration_token": {
97+
Type: schema.TypeList,
98+
MaxItems: 1,
99+
Computed: true,
100+
Sensitive: true,
101+
Elem: &schema.Resource{
102+
Schema: clusterRegistrationTokenFields(),
103+
},
104+
},
105+
"kube_config": {
106+
Type: schema.TypeString,
107+
Computed: true,
108+
Sensitive: true,
109+
},
110+
"cluster_v1_id": {
111+
Type: schema.TypeString,
112+
Computed: true,
113+
},
114+
"resource_version": {
115+
Type: schema.TypeString,
116+
Computed: true,
117+
},
118+
}
119+
120+
for k, v := range commonAnnotationLabelFields() {
121+
s[k] = v
122+
}
123+
124+
return s
125+
}
126+
7127
func clusterV2Fields() map[string]*schema.Schema {
8128
s := map[string]*schema.Schema{
9129
"name": {

rancher2/schema_cluster_v2_rke_config.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,160 @@ import (
99

1010
//Types
1111

12+
func clusterV2RKEConfigFieldsV0() map[string]*schema.Schema {
13+
s := map[string]*schema.Schema{
14+
"additional_manifest": {
15+
Type: schema.TypeString,
16+
Optional: true,
17+
Description: "Cluster V2 additional manifest",
18+
},
19+
"local_auth_endpoint": {
20+
Type: schema.TypeList,
21+
MaxItems: 1,
22+
Optional: true,
23+
Deprecated: "Use rancher2_cluster_v2.local_auth_endpoint instead",
24+
Description: "Cluster V2 local auth endpoint",
25+
Elem: &schema.Resource{
26+
Schema: clusterV2LocalAuthEndpointFields(),
27+
},
28+
},
29+
"upgrade_strategy": {
30+
Type: schema.TypeList,
31+
MaxItems: 1,
32+
Optional: true,
33+
Description: "Cluster V2 upgrade strategy",
34+
Elem: &schema.Resource{
35+
Schema: clusterV2RKEConfigUpgradeStrategyFields(),
36+
},
37+
},
38+
"chart_values": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Description: "Cluster V2 chart values. It should be in YAML format",
42+
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
43+
v, ok := val.(string)
44+
if !ok || len(v) == 0 {
45+
return
46+
}
47+
_, err := ghodssyamlToMapInterface(v)
48+
if err != nil {
49+
errs = append(errs, fmt.Errorf("%q must be in yaml format, error: %v", key, err))
50+
return
51+
}
52+
return
53+
},
54+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
55+
if old == "" || new == "" {
56+
return false
57+
}
58+
oldMap, _ := ghodssyamlToMapInterface(old)
59+
newMap, _ := ghodssyamlToMapInterface(new)
60+
return reflect.DeepEqual(oldMap, newMap)
61+
},
62+
},
63+
"machine_global_config": {
64+
Type: schema.TypeString,
65+
Optional: true,
66+
Description: "Cluster V2 machine global config",
67+
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
68+
v, ok := val.(string)
69+
if !ok || len(v) == 0 {
70+
return
71+
}
72+
_, err := ghodssyamlToMapInterface(v)
73+
if err != nil {
74+
errs = append(errs, fmt.Errorf("%q must be in yaml format, error: %v", key, err))
75+
return
76+
}
77+
return
78+
},
79+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
80+
if old == "" || new == "" {
81+
return false
82+
}
83+
oldMap, _ := ghodssyamlToMapInterface(old)
84+
newMap, _ := ghodssyamlToMapInterface(new)
85+
return reflect.DeepEqual(oldMap, newMap)
86+
},
87+
},
88+
"machine_pools": {
89+
Type: schema.TypeList,
90+
Optional: true,
91+
Computed: true,
92+
Description: "Cluster V2 machine pools",
93+
Elem: &schema.Resource{
94+
Schema: clusterV2RKEConfigMachinePoolFields(),
95+
},
96+
},
97+
"machine_pool_defaults": {
98+
Type: schema.TypeList,
99+
Optional: true,
100+
Computed: true,
101+
Description: "Default values for machine pool configurations if unset",
102+
Elem: &schema.Resource{
103+
Schema: clusterV2RKEConfigMachinePoolDefaultFields(),
104+
},
105+
},
106+
"machine_selector_config": {
107+
Type: schema.TypeList,
108+
Optional: true,
109+
Computed: true,
110+
Description: "Cluster V2 machine selector config",
111+
Elem: &schema.Resource{
112+
Schema: clusterV2RKEConfigSystemConfigFieldsV0(),
113+
},
114+
},
115+
"registries": {
116+
Type: schema.TypeList,
117+
MaxItems: 1,
118+
Optional: true,
119+
Description: "Cluster V2 registries",
120+
Elem: &schema.Resource{
121+
Schema: clusterV2RKEConfigRegistryFields(),
122+
},
123+
},
124+
"etcd": {
125+
Type: schema.TypeList,
126+
MaxItems: 1,
127+
Optional: true,
128+
Computed: true,
129+
Description: "Cluster V2 etcd",
130+
Elem: &schema.Resource{
131+
Schema: clusterV2RKEConfigETCDFields(),
132+
},
133+
},
134+
"rotate_certificates": {
135+
Type: schema.TypeList,
136+
MaxItems: 1,
137+
Optional: true,
138+
Description: "Cluster V2 certificate rotation",
139+
Elem: &schema.Resource{
140+
Schema: clusterV2RKEConfigRotateCertificatesFields(),
141+
},
142+
},
143+
"etcd_snapshot_create": {
144+
Type: schema.TypeList,
145+
MaxItems: 1,
146+
Optional: true,
147+
Description: "Cluster V2 etcd snapshot create",
148+
Elem: &schema.Resource{
149+
Schema: clusterV2RKEConfigETCDSnapshotCreateFields(),
150+
},
151+
},
152+
"etcd_snapshot_restore": {
153+
Type: schema.TypeList,
154+
MaxItems: 1,
155+
Optional: true,
156+
Description: "Cluster V2 etcd snapshot restore",
157+
Elem: &schema.Resource{
158+
Schema: clusterV2RKEConfigETCDSnapshotRestoreFields(),
159+
},
160+
},
161+
}
162+
163+
return s
164+
}
165+
12166
func clusterV2RKEConfigFields() map[string]*schema.Schema {
13167
s := map[string]*schema.Schema{
14168
"additional_manifest": {

0 commit comments

Comments
 (0)