Skip to content

Commit 192f486

Browse files
fix: [v8] add support for imported cluster
Co-authored-by: Matt Trachier <matt.trachier@suse.com>
1 parent e0eaca9 commit 192f486

6 files changed

Lines changed: 118 additions & 11 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
@@ -626,6 +638,7 @@ The following arguments are supported:
626638
* `eks_config_v2` - (Optional/Computed) The Amazon EKS V2 configuration to create or import `eks` Clusters. Conflicts with `gke_config_v2`, `k3s_config`, `oke_config` and `rke_config`. For Rancher v2.5.x and above (list maxitems:1)
627639
* `gke_config_v2` - (Optional) The Google GKE V2 configuration for `gke` Clusters. Conflicts with `aks_config_v2`, `eks_config_v2`, `k3s_config`, `oke_config` and `rke_config`. For Rancher v2.5.8 and above (list maxitems:1)
628640
* `oke_config` - (Optional) The Oracle OKE configuration for `oke` Clusters. Conflicts with `aks_config_v2`, `eks_config_v2`, `gke_config_v2`, `k3s_config` and `rke_config` (list maxitems:1)
641+
* `imported_config` - (Optional) The imported configuration for generic imported Clusters. Conflicts with `aks_config_v2`, `eks_config_v2`, `gke_config_v2`, `rke_config`, `rke2_config` and `k3s_config` (list maxitems:1)
629642
* `description` - (Optional) The description for Cluster (string)
630643
* `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)
631644
* `cluster_template_answers` - (Optional/Computed) Cluster template answers. For Rancher v2.3.x and above (list maxitems:1)
@@ -1824,6 +1837,12 @@ The following arguments are supported:
18241837
* `type` - (Optional) Variable type. `boolean`, `int`, `password`, and `string` are allowed. Default `string` (string)
18251838
* `variable` - (Optional) Variable name (string)
18261839

1840+
### `imported_config`
1841+
1842+
#### Arguments
1843+
1844+
* `private_registry_url` - (Optional) The URL for a cluster-level private registry (string)
1845+
18271846
### `cluster_registration_token`
18281847

18291848
#### Attributes
@@ -1841,7 +1860,6 @@ The following arguments are supported:
18411860
* `annotations` - (Computed) Annotations for cluster registration token object (map)
18421861
* `labels` - (Computed) Labels for cluster registration token object (map)
18431862

1844-
18451863
## Timeouts
18461864

18471865
`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 {
@@ -331,6 +339,8 @@ func resourceRancher2ClusterUpdate(d *schema.ResourceData, meta interface{}) err
331339
case clusterDriverRKE2:
332340
update["rke2Config"] = expandClusterRKE2Config(d.Get("rke2_config").([]interface{}))
333341
replace = d.HasChange("cluster_agent_deployment_customization")
342+
case clusterDriverImported:
343+
update["importedConfig"] = expandClusterImportedConfig(d.Get("imported_config").([]interface{}))
334344
}
335345

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

rancher2/schema_cluster.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func clusterFields() map[string]*schema.Schema {
305305
MaxItems: 1,
306306
Optional: true,
307307
Computed: true,
308-
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "oke_config", "rke2_config"},
308+
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "oke_config", "rke2_config", "imported_config"},
309309
Elem: &schema.Resource{
310310
Schema: clusterRKEConfigFields(),
311311
},
@@ -315,7 +315,7 @@ func clusterFields() map[string]*schema.Schema {
315315
MaxItems: 1,
316316
Optional: true,
317317
Computed: true,
318-
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "oke_config", "rke_config"},
318+
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "oke_config", "rke_config", "imported_config"},
319319
Elem: &schema.Resource{
320320
Schema: clusterRKE2ConfigFields(),
321321
},
@@ -325,7 +325,7 @@ func clusterFields() map[string]*schema.Schema {
325325
MaxItems: 1,
326326
Optional: true,
327327
Computed: true,
328-
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "rke_config", "oke_config", "rke2_config"},
328+
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "rke_config", "oke_config", "rke2_config", "imported_config"},
329329
Elem: &schema.Resource{
330330
Schema: clusterK3SConfigFields(),
331331
},
@@ -335,7 +335,7 @@ func clusterFields() map[string]*schema.Schema {
335335
MaxItems: 1,
336336
Optional: true,
337337
Computed: true,
338-
ConflictsWith: []string{"aks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config"},
338+
ConflictsWith: []string{"aks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config", "imported_config"},
339339
Elem: &schema.Resource{
340340
Schema: clusterEKSConfigV2Fields(),
341341
},
@@ -344,7 +344,7 @@ func clusterFields() map[string]*schema.Schema {
344344
Type: schema.TypeList,
345345
MaxItems: 1,
346346
Optional: true,
347-
ConflictsWith: []string{"eks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config"},
347+
ConflictsWith: []string{"eks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config", "imported_config"},
348348
Elem: &schema.Resource{
349349
Schema: clusterAKSConfigV2Fields(),
350350
},
@@ -353,7 +353,7 @@ func clusterFields() map[string]*schema.Schema {
353353
Type: schema.TypeList,
354354
MaxItems: 1,
355355
Optional: true,
356-
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config"},
356+
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config", "imported_config"},
357357
Elem: &schema.Resource{
358358
Schema: clusterGKEConfigV2Fields(),
359359
},
@@ -362,11 +362,20 @@ func clusterFields() map[string]*schema.Schema {
362362
Type: schema.TypeList,
363363
MaxItems: 1,
364364
Optional: true,
365-
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "rke2_config"},
365+
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "rke2_config", "imported_config"},
366366
Elem: &schema.Resource{
367367
Schema: clusterOKEConfigFields(),
368368
},
369369
},
370+
"imported_config": {
371+
Type: schema.TypeList,
372+
MaxItems: 1,
373+
Optional: true,
374+
ConflictsWith: []string{"aks_config_v2", "eks_config_v2", "gke_config_v2", "k3s_config", "rke_config", "oke_config", "rke2_config"},
375+
Elem: &schema.Resource{
376+
Schema: clusterImportedConfigFields(),
377+
},
378+
},
370379
"default_project_id": {
371380
Type: schema.TypeString,
372381
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
)
@@ -195,6 +194,19 @@ func flattenCluster(d *schema.ResourceData, in *Cluster, clusterRegToken *manage
195194
if err != nil {
196195
return err
197196
}
197+
case clusterDriverImported:
198+
v, ok := d.Get("imported_config").([]interface{})
199+
if !ok {
200+
v = []interface{}{}
201+
}
202+
importedConfig, err := flattenClusterImportedConfig(in.ImportedConfig, v)
203+
if err != nil {
204+
return err
205+
}
206+
err = d.Set("imported_config", importedConfig)
207+
if err != nil {
208+
return err
209+
}
198210
}
199211

200212
// Setting k3s_config, rke2_config and rke_config always as computed
@@ -494,6 +506,10 @@ func expandCluster(in *schema.ResourceData) (*Cluster, error) {
494506
obj.Driver = clusterDriverRKE2
495507
}
496508

509+
if v, ok := in.Get("imported_config").([]interface{}); ok && len(v) > 0 {
510+
obj.ImportedConfig = expandClusterImportedConfig(v)
511+
}
512+
497513
if len(obj.Driver) == 0 {
498514
obj.Driver = clusterDriverImported
499515
}
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)