Skip to content

Commit 43f2840

Browse files
authored
Merge pull request #487 from rawmind0/clustersync
sync fixes
2 parents 0a2a672 + 919a08b commit 43f2840

12 files changed

Lines changed: 104 additions & 57 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ ENHANCEMENTS:
2121
* Updated golang to v1.14.9 and removing vendor folder
2222
* Updated go mod to support Rancher `v2.5.0`
2323
* Added dingtal_config and msteams_config arguments at rancher2_notifier resource. go code and docs
24+
* Improved `rancher2_cluster_sync` wait for cluster monitoring
25+
* Improved `rancher2_bootstrap` on resource creation. `bootstrapDoLogin` function will retry 3 times user/pass login before fail
2426

2527
BUG FIXES:
2628

rancher2/config.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const (
3232
// Client are the client kind for a Rancher v3 API
3333
type Client struct {
3434
Management *managementClient.Client
35-
Cluster *clusterClient.Client
36-
Project *projectClient.Client
35+
Cluster map[string]*clusterClient.Client
36+
Project map[string]*projectClient.Client
3737
Catalog map[string]catalogController.ClusterRepoController
3838
Factory map[string]*k8sFactory
3939
}
@@ -289,19 +289,23 @@ func (c *Config) UpdateToken(token string) error {
289289
return err
290290
}
291291

292-
if c.Client.Cluster != nil {
293-
c.Client.Cluster = nil
294-
_, err := c.ClusterClient(c.ClusterID)
295-
if err != nil {
296-
return err
297-
}
292+
for i := range c.Client.Cluster {
293+
if c.Client.Cluster[i] != nil {
294+
c.Client.Cluster[i] = nil
295+
_, err := c.ClusterClient(i)
296+
if err != nil {
297+
return err
298+
}
298299

300+
}
299301
}
300-
if c.Client.Project != nil {
301-
c.Client.Project = nil
302-
_, err := c.ProjectClient(c.ProjectID)
303-
if err != nil {
304-
return err
302+
for i := range c.Client.Project {
303+
if c.Client.Project[i] != nil {
304+
c.Client.Project[i] = nil
305+
_, err := c.ProjectClient(i)
306+
if err != nil {
307+
return err
308+
}
305309
}
306310
}
307311

@@ -344,15 +348,19 @@ func (c *Config) ManagementClient() (*managementClient.Client, error) {
344348

345349
// ClusterClient creates a Rancher client scoped to a Cluster API
346350
func (c *Config) ClusterClient(id string) (*clusterClient.Client, error) {
351+
if id == "" {
352+
return nil, fmt.Errorf("[ERROR] Rancher Cluster Client: cluster ID is nil")
353+
}
354+
347355
c.Sync.Lock()
348356
defer c.Sync.Unlock()
349357

350-
if id == "" {
351-
return nil, fmt.Errorf("[ERROR] Rancher Cluster Client: cluster ID is nil")
358+
if c.Client.Cluster == nil {
359+
c.Client.Cluster = map[string]*clusterClient.Client{}
352360
}
353361

354-
if c.Client.Cluster != nil && id == c.ClusterID {
355-
return c.Client.Cluster, nil
362+
if c.Client.Cluster[id] != nil {
363+
return c.Client.Cluster[id], nil
356364
}
357365

358366
err := c.isRancherReady()
@@ -367,23 +375,26 @@ func (c *Config) ClusterClient(id string) (*clusterClient.Client, error) {
367375
if err != nil {
368376
return nil, err
369377
}
370-
c.Client.Cluster = cClient
371-
c.ClusterID = id
378+
c.Client.Cluster[id] = cClient
372379

373-
return c.Client.Cluster, nil
380+
return c.Client.Cluster[id], nil
374381
}
375382

376383
// ProjectClient creates a Rancher client scoped to a Project API
377384
func (c *Config) ProjectClient(id string) (*projectClient.Client, error) {
385+
if id == "" {
386+
return nil, fmt.Errorf("[ERROR] Rancher Project Client: project ID is nil")
387+
}
388+
378389
c.Sync.Lock()
379390
defer c.Sync.Unlock()
380391

381-
if id == "" {
382-
return nil, fmt.Errorf("[ERROR] Rancher Project Client: project ID is nil")
392+
if c.Client.Project == nil {
393+
c.Client.Project = map[string]*projectClient.Client{}
383394
}
384395

385-
if c.Client.Project != nil && id == c.ProjectID {
386-
return c.Client.Project, nil
396+
if c.Client.Project[id] != nil {
397+
return c.Client.Project[id], nil
387398
}
388399

389400
err := c.isRancherReady()
@@ -399,10 +410,9 @@ func (c *Config) ProjectClient(id string) (*projectClient.Client, error) {
399410
return nil, err
400411
}
401412

402-
c.Client.Project = pClient
403-
c.ProjectID = id
413+
c.Client.Project[id] = pClient
404414

405-
return c.Client.Project, nil
415+
return c.Client.Project[id], nil
406416
}
407417

408418
func (c *Config) NormalizeURL() {
@@ -418,7 +428,6 @@ func (c *Config) CreateClientOpts() *clientbase.ClientOpts {
418428
CACerts: c.CACerts,
419429
Insecure: c.Insecure,
420430
}
421-
422431
return options
423432
}
424433

rancher2/data_source_rancher2_catalog_v2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ data "` + testAccRancher2CatalogV2Type + `" "foo" {
2323
Check: resource.ComposeTestCheckFunc(
2424
resource.TestCheckResourceAttr(name, "name", "foo"),
2525
resource.TestCheckResourceAttr(name, "git_repo", "https://git.rancher.io/charts"),
26-
resource.TestCheckResourceAttr(name, "git_branch", "v2.5.0"),
26+
resource.TestCheckResourceAttr(name, "git_branch", "dev-v2.5"),
2727
),
2828
},
2929
},

rancher2/resource_rancher2_bootstrap.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"strings"
7+
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
910
)
@@ -258,7 +259,17 @@ func bootstrapDoLogin(d *schema.ResourceData, meta interface{}) error {
258259
}
259260
tokenID, token, err := DoUserLogin(meta.(*Config).URL, bootstrapDefaultUser, currentPass, bootstrapDefaultTTL, bootstrapDefaultSessionDesc, meta.(*Config).CACerts, meta.(*Config).Insecure)
260261
if err != nil {
261-
return fmt.Errorf("[ERROR] Login with %s user: %v", bootstrapDefaultUser, err)
262+
// 3 login retries waiting 5 seconds if user/pass login fails
263+
for i := 0; i < 3; i++ {
264+
time.Sleep(5 * time.Second)
265+
tokenID, token, err = DoUserLogin(meta.(*Config).URL, bootstrapDefaultUser, currentPass, bootstrapDefaultTTL, bootstrapDefaultSessionDesc, meta.(*Config).CACerts, meta.(*Config).Insecure)
266+
if err == nil {
267+
break
268+
}
269+
}
270+
if err != nil {
271+
return fmt.Errorf("[ERROR] Login with %s user: %v", bootstrapDefaultUser, err)
272+
}
262273
}
263274

264275
// Update config token

rancher2/resource_rancher2_catalog_v2_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ resource "` + testAccRancher2CatalogV2Type + `" "foo" {
2727
cluster_id = rancher2_cluster_sync.testacc.cluster_id
2828
name = "foo"
2929
git_repo = "https://git.rancher.io/charts"
30-
git_branch = "v2.5.0"
30+
git_branch = "dev-v2.5"
3131
}
3232
`
3333
testAccRancher2CatalogV2Update = `
@@ -56,7 +56,7 @@ func TestAccRancher2CatalogV2_basic(t *testing.T) {
5656
testAccCheckRancher2CatalogV2Exists(testAccRancher2CatalogV2Type+".foo", catalog),
5757
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "name", "foo"),
5858
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "git_repo", "https://git.rancher.io/charts"),
59-
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "git_branch", "v2.5.0"),
59+
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "git_branch", "dev-v2.5"),
6060
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "cluster_id", testAccRancher2ClusterID),
6161
),
6262
},
@@ -76,7 +76,7 @@ func TestAccRancher2CatalogV2_basic(t *testing.T) {
7676
testAccCheckRancher2CatalogV2Exists(testAccRancher2CatalogV2Type+".foo", catalog),
7777
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "name", "foo"),
7878
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "git_repo", "https://git.rancher.io/charts"),
79-
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "git_branch", "v2.5.0"),
79+
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "git_branch", "dev-v2.5"),
8080
resource.TestCheckResourceAttr(testAccRancher2CatalogV2Type+".foo", "cluster_id", testAccRancher2ClusterID),
8181
),
8282
},

rancher2/resource_rancher2_cluster_sync.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1010
)
1111

12+
const clusterSyncMonitoringEnabledCondition = "MonitoringEnabled"
13+
1214
func resourceRancher2ClusterSync() *schema.Resource {
1315
return &schema.Resource{
1416
Create: resourceRancher2ClusterSyncCreate,
@@ -28,6 +30,7 @@ func resourceRancher2ClusterSync() *schema.Resource {
2830
func resourceRancher2ClusterSyncCreate(d *schema.ResourceData, meta interface{}) error {
2931
clusterID := d.Get("cluster_id").(string)
3032

33+
start := time.Now()
3134
cluster, err := meta.(*Config).GetClusterByID(clusterID)
3235
if err != nil {
3336
return err
@@ -51,27 +54,26 @@ func resourceRancher2ClusterSyncCreate(d *schema.ResourceData, meta interface{})
5154
}
5255

5356
if cluster.EnableClusterMonitoring && d.Get("wait_monitoring").(bool) {
54-
_, systemProjectID, err := meta.(*Config).GetClusterSpecialProjectsID(clusterID)
55-
if err != nil {
56-
return err
57-
}
58-
client, err := meta.(*Config).ProjectClient(systemProjectID)
59-
if err != nil {
60-
return err
61-
}
62-
projectID := splitProjectIDPart(systemProjectID)
63-
appID := projectID + ":cluster-monitoring"
64-
stateCluster := &resource.StateChangeConf{
65-
Pending: []string{},
66-
Target: []string{"active"},
67-
Refresh: appStateRefreshFunc(client, appID),
68-
Timeout: d.Timeout(schema.TimeoutCreate),
69-
Delay: 1 * time.Second,
70-
MinTimeout: 5 * time.Second,
57+
enabled := false
58+
for cluster, err := meta.(*Config).GetClusterByID(clusterID); ; cluster, err = meta.(*Config).GetClusterByID(clusterID) {
59+
if err != nil {
60+
return err
61+
}
62+
for _, v := range cluster.Conditions {
63+
if v.Type == clusterSyncMonitoringEnabledCondition {
64+
if v.Status == "True" {
65+
enabled = true
66+
}
67+
break
68+
}
69+
}
70+
if time.Since(start) >= d.Timeout(schema.TimeoutCreate) || enabled {
71+
break
72+
}
73+
time.Sleep(5 * time.Second)
7174
}
72-
_, waitClusterErr := stateCluster.WaitForState()
73-
if waitClusterErr != nil {
74-
return fmt.Errorf("[ERROR] waiting for cluster ID (%s) monitoring to be running: %s", clusterID, waitClusterErr)
75+
if !enabled {
76+
return fmt.Errorf("[ERROR] waiting for cluster ID (%s) monitoring to be running: Timeout", clusterID)
7577
}
7678
}
7779

rancher2/resource_rancher2_multi_cluster_app.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,25 @@ func resourceRancher2MultiClusterAppDelete(d *schema.ResourceData, meta interfac
234234
return fmt.Errorf(
235235
"[ERROR] waiting for multi cluster app (%s) to be removed: %s", id, waitErr)
236236
}
237-
238237
d.SetId("")
238+
239+
for i := range multiClusterApp.Targets {
240+
client, err := meta.(*Config).ProjectClient(multiClusterApp.Targets[i].ProjectID)
241+
if err != nil {
242+
continue
243+
}
244+
mappID := splitProjectIDPart(multiClusterApp.Targets[i].ProjectID) + ":" + multiClusterApp.Targets[i].AppID
245+
stateConf = &resource.StateChangeConf{
246+
Pending: []string{"removing"},
247+
Target: []string{"removed"},
248+
Refresh: appStateRefreshFunc(client, mappID),
249+
Timeout: d.Timeout(schema.TimeoutDelete),
250+
Delay: 1 * time.Second,
251+
MinTimeout: 3 * time.Second,
252+
}
253+
stateConf.WaitForState()
254+
}
255+
239256
return nil
240257
}
241258

rancher2/resource_rancher2_multi_cluster_app_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func testAccRancher2MultiClusterAppDisappears(mca *managementClient.MultiCluster
159159
return fmt.Errorf(
160160
"[ERROR] waiting for multi cluster app (%s) to be removed: %s", rs.Primary.ID, waitErr)
161161
}
162+
time.Sleep(5 * time.Second)
162163
}
163164
return nil
164165
}

scripts/cleanup_testacc.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ fi
1313
if [ -d ${TEMP_DIR} ] && [ "${TEMP_DIR}" != "/tmp" ]; then
1414
echo Cleaning up testacc temporary dir ${TEMP_DIR}
1515
rm -rf ${TEMP_DIR}
16-
fi
16+
fi

scripts/gotestacc.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ RANCHER_VERSION=${RANCHER_VERSION:-"v2.3.6"}
2020

2121
source $(dirname $0)/start_rancher.sh
2222

23+
if [ "${DOCKERIZED}" == "true" ]; then
24+
echo "${RANCHER_IP} ${RANCHER_HOSTNAME}" >> /etc/hosts
25+
fi
26+
2327
RANCHER_URL=${RANCHER_URL:-""}
2428
RANCHER_TOKEN_KEY=${RANCHER_TOKEN_KEY:-""}
2529
RANCHER_INSECURE=${RANCHER_INSECURE:-true}

0 commit comments

Comments
 (0)