Skip to content

Commit a276ade

Browse files
authored
fix: add support for imported cluster
1 parent e705d14 commit a276ade

6 files changed

Lines changed: 121 additions & 14 deletions

File tree

docs/resources/cluster.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ resource "rancher2_cluster" "foo-imported" {
2020
}
2121
```
2222

23-
Creating Rancher v2 RKE cluster
23+
### Creating Rancher v2 imported cluster with custom configuration. For Rancher v2.11.x and above.
24+
25+
```hcl
26+
# Create a new rancher2 imported Cluster with custom configuration
27+
resource "rancher2_cluster" "foo-imported" {
28+
name = "foo-imported"
29+
imported_config {
30+
private_registry_url = "test.io"
31+
}
32+
}
33+
```
34+
35+
### Creating Rancher v2 RKE cluster
2436

2537
```hcl
2638
# Create auditlog policy yaml file
@@ -629,6 +641,7 @@ The following arguments are supported:
629641
* `gke_config` - (Optional) The Google GKE configuration for `gke` Clusters. Conflicts with `aks_config`, `aks_config_v2`, `eks_config`, `eks_config_v2`, `gke_config_v2`, `oke_config`, `k3s_config` and `rke_config` (list maxitems:1)
630642
* `gke_config_v2` - (Optional) The Google GKE V2 configuration for `gke` Clusters. Conflicts with `aks_config`, `aks_config_v2`, `eks_config`, `eks_config_v2`, `gke_config`, `oke_config`, `k3s_config` and `rke_config`. For Rancher v2.5.8 and above (list maxitems:1)
631643
* `oke_config` - (Optional) The Oracle OKE configuration for `oke` Clusters. Conflicts with `aks_config`, `aks_config_v2`, `eks_config`, `eks_config_v2`, `gke_config`, `gke_config_v2`, `k3s_config` and `rke_config` (list maxitems:1)
644+
* `imported_config` - (Optional) The imported configuration for generic imported Clusters. Conflicts with `aks_config`,`aks_config_v2`, `eks_config`, `eks_config`, `eks_config_v2`, `gke_config`, `gke_config_v2`, `rke_config`, `rke2_config` and `k3s_config` (list maxitems:1)
632645
* `description` - (Optional) The description for Cluster (string)
633646
* `cluster_auth_endpoint` - (Optional/Computed) Enabling the [local cluster authorized endpoint](https://rancher.com/docs/rancher/v2.x/en/cluster-provisioning/rke-clusters/options/#local-cluster-auth-endpoint) allows direct communication with the cluster, bypassing the Rancher API proxy. (list maxitems:1)
634647
* `cluster_template_answers` - (Optional/Computed) Cluster template answers. For Rancher v2.3.x and above (list maxitems:1)
@@ -1959,6 +1972,12 @@ The following arguments are supported:
19591972
* `type` - (Optional) Variable type. `boolean`, `int`, `password`, and `string` are allowed. Default `string` (string)
19601973
* `variable` - (Optional) Variable name (string)
19611974

1975+
### `imported_config`
1976+
1977+
#### Arguments
1978+
1979+
* `private_registry_url` - (Optional) The URL for a cluster-level private registry (string)
1980+
19621981
### `cluster_registration_token`
19631982

19641983
#### Attributes
@@ -1976,7 +1995,6 @@ The following arguments are supported:
19761995
* `annotations` - (Computed) Annotations for cluster registration token object (map)
19771996
* `labels` - (Computed) Labels for cluster registration token object (map)
19781997

1979-
19801998
## Timeouts
19811999

19822000
`rancher2_cluster` provides the following

rancher2/resource_rancher2_cluster.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,22 @@ func resourceRancher2ClusterCreate(d *schema.ResourceData, meta interface{}) err
148148

149149
expectedState := []string{"active"}
150150

151-
if cluster.Driver == clusterDriverImported || (cluster.Driver == clusterDriverEKSV2 && cluster.EKSConfig.Imported) {
151+
if cluster.Driver == clusterDriverEKSV2 && cluster.EKSConfig.Imported {
152152
expectedState = append(expectedState, "pending")
153153
}
154154

155155
if cluster.Driver == clusterDriverRKE || cluster.Driver == clusterDriverK3S || cluster.Driver == clusterDriverRKE2 {
156156
expectedState = append(expectedState, "provisioning")
157157
}
158158

159+
if cluster.Driver == clusterDriverImported {
160+
if cluster.ImportedConfig != nil {
161+
expectedState = append(expectedState, "provisioning")
162+
} else {
163+
expectedState = append(expectedState, "pending")
164+
}
165+
}
166+
159167
// Creating cluster with monitoring disabled
160168
newCluster := &Cluster{}
161169
if cluster.EKSConfig != nil && !cluster.EKSConfig.Imported {
@@ -349,6 +357,8 @@ func resourceRancher2ClusterUpdate(d *schema.ResourceData, meta interface{}) err
349357
case clusterDriverRKE2:
350358
update["rke2Config"] = expandClusterRKE2Config(d.Get("rke2_config").([]interface{}))
351359
replace = d.HasChange("cluster_agent_deployment_customization")
360+
case clusterDriverImported:
361+
update["importedConfig"] = expandClusterImportedConfig(d.Get("imported_config").([]interface{}))
352362
}
353363

354364
// update the cluster; retry til timeout or non retryable error is returned. If api 500 error is received,

rancher2/schema_cluster.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func clusterFields() map[string]*schema.Schema {
332332
MaxItems: 1,
333333
Optional: true,
334334
Computed: true,
335-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke2_config"},
335+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke2_config", "imported_config"},
336336
Elem: &schema.Resource{
337337
Schema: clusterRKEConfigFields(),
338338
},
@@ -342,7 +342,7 @@ func clusterFields() map[string]*schema.Schema {
342342
MaxItems: 1,
343343
Optional: true,
344344
Computed: true,
345-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config"},
345+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "imported_config"},
346346
Elem: &schema.Resource{
347347
Schema: clusterRKE2ConfigFields(),
348348
},
@@ -352,7 +352,7 @@ func clusterFields() map[string]*schema.Schema {
352352
MaxItems: 1,
353353
Optional: true,
354354
Computed: true,
355-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "oke_config", "rke_config", "rke2_config"},
355+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "oke_config", "rke_config", "rke2_config", "imported_config"},
356356
Elem: &schema.Resource{
357357
Schema: clusterK3SConfigFields(),
358358
},
@@ -361,7 +361,7 @@ func clusterFields() map[string]*schema.Schema {
361361
Type: schema.TypeList,
362362
MaxItems: 1,
363363
Optional: true,
364-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config"},
364+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config", "imported_config"},
365365
Elem: &schema.Resource{
366366
Schema: clusterEKSConfigFields(),
367367
},
@@ -371,7 +371,7 @@ func clusterFields() map[string]*schema.Schema {
371371
MaxItems: 1,
372372
Optional: true,
373373
Computed: true,
374-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config"},
374+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config", "imported_config"},
375375
Elem: &schema.Resource{
376376
Schema: clusterEKSConfigV2Fields(),
377377
},
@@ -380,7 +380,7 @@ func clusterFields() map[string]*schema.Schema {
380380
Type: schema.TypeList,
381381
MaxItems: 1,
382382
Optional: true,
383-
ConflictsWith: []string{"eks_config", "aks_config_v2", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config"},
383+
ConflictsWith: []string{"eks_config", "aks_config_v2", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config", "imported_config"},
384384
Elem: &schema.Resource{
385385
Schema: clusterAKSConfigFields(),
386386
},
@@ -389,7 +389,7 @@ func clusterFields() map[string]*schema.Schema {
389389
Type: schema.TypeList,
390390
MaxItems: 1,
391391
Optional: true,
392-
ConflictsWith: []string{"aks_config", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config"},
392+
ConflictsWith: []string{"aks_config", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config", "imported_config"},
393393
Elem: &schema.Resource{
394394
Schema: clusterAKSConfigV2Fields(),
395395
},
@@ -398,7 +398,7 @@ func clusterFields() map[string]*schema.Schema {
398398
Type: schema.TypeList,
399399
MaxItems: 1,
400400
Optional: true,
401-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config"},
401+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config", "imported_config"},
402402
Elem: &schema.Resource{
403403
Schema: clusterGKEConfigFields(),
404404
},
@@ -407,7 +407,7 @@ func clusterFields() map[string]*schema.Schema {
407407
Type: schema.TypeList,
408408
MaxItems: 1,
409409
Optional: true,
410-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "k3s_config", "oke_config", "rke_config", "rke2_config"},
410+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "k3s_config", "oke_config", "rke_config", "rke2_config", "imported_config"},
411411
Elem: &schema.Resource{
412412
Schema: clusterGKEConfigV2Fields(),
413413
},
@@ -416,11 +416,20 @@ func clusterFields() map[string]*schema.Schema {
416416
Type: schema.TypeList,
417417
MaxItems: 1,
418418
Optional: true,
419-
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "rke_config", "rke2_config"},
419+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "rke_config", "rke2_config", "imported_config"},
420420
Elem: &schema.Resource{
421421
Schema: clusterOKEConfigFields(),
422422
},
423423
},
424+
"imported_config": {
425+
Type: schema.TypeList,
426+
MaxItems: 1,
427+
Optional: true,
428+
ConflictsWith: []string{"aks_config", "aks_config_v2", "eks_config", "eks_config_v2", "gke_config", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "rke2_config"},
429+
Elem: &schema.Resource{
430+
Schema: clusterImportedConfigFields(),
431+
},
432+
},
424433
"default_project_id": {
425434
Type: schema.TypeString,
426435
Computed: true,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package rancher2
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
)
6+
7+
//Schemas
8+
9+
func clusterImportedConfigFields() map[string]*schema.Schema {
10+
return map[string]*schema.Schema{
11+
"private_registry_url": {
12+
Type: schema.TypeString,
13+
Optional: true,
14+
Computed: true,
15+
Description: "Private registry URL",
16+
},
17+
}
18+
}

rancher2/structure_cluster.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rancher2
22

33
import (
44
"fmt"
5-
65
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
76
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
87
)
@@ -234,6 +233,19 @@ func flattenCluster(d *schema.ResourceData, in *Cluster, clusterRegToken *manage
234233
if err != nil {
235234
return err
236235
}
236+
case clusterDriverImported:
237+
v, ok := d.Get("imported_config").([]interface{})
238+
if !ok {
239+
v = []interface{}{}
240+
}
241+
importedConfig, err := flattenClusterImportedConfig(in.ImportedConfig, v)
242+
if err != nil {
243+
return err
244+
}
245+
err = d.Set("imported_config", importedConfig)
246+
if err != nil {
247+
return err
248+
}
237249
}
238250

239251
// Setting k3s_config, rke2_config and rke_config always as computed
@@ -560,6 +572,10 @@ func expandCluster(in *schema.ResourceData) (*Cluster, error) {
560572
obj.Driver = clusterDriverRKE2
561573
}
562574

575+
if v, ok := in.Get("imported_config").([]interface{}); ok && len(v) > 0 {
576+
obj.ImportedConfig = expandClusterImportedConfig(v)
577+
}
578+
563579
if len(obj.Driver) == 0 {
564580
obj.Driver = clusterDriverImported
565581
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package rancher2
2+
3+
import managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
4+
5+
func expandClusterImportedConfig(p []interface{}) *managementClient.ImportedConfig {
6+
obj := &managementClient.ImportedConfig{}
7+
if len(p) == 0 || p[0] == nil {
8+
return obj
9+
}
10+
in := p[0].(map[string]interface{})
11+
12+
if v, ok := in["private_registry_url"].(string); ok && len(v) > 0 {
13+
obj.PrivateRegistryURL = v
14+
}
15+
16+
return obj
17+
}
18+
19+
func flattenClusterImportedConfig(in *managementClient.ImportedConfig, p []interface{}) ([]interface{}, error) {
20+
var obj map[string]interface{}
21+
if len(p) == 0 || p[0] == nil {
22+
obj = make(map[string]interface{})
23+
} else {
24+
obj = p[0].(map[string]interface{})
25+
}
26+
27+
if in == nil {
28+
return []interface{}{}, nil
29+
}
30+
31+
if len(in.PrivateRegistryURL) > 0 {
32+
obj["private_registry_url"] = in.PrivateRegistryURL
33+
}
34+
35+
return []interface{}{obj}, nil
36+
}

0 commit comments

Comments
 (0)