Skip to content

Commit 23691a9

Browse files
authored
Merge pull request #663 from rawmind0/timeout
Deprecating retries argument in favour of timeout new argument
2 parents 86de757 + c7c1b41 commit 23691a9

14 files changed

Lines changed: 172 additions & 91 deletions

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
## 1.15.0 (Unreleased)
1+
## 1.15.0 (May 20, 2021)
22

33
FEATURES:
44

55
* **Deprecated Argument:** `rancher2_cluster.aks_config.tag` - (Deprecated) Use `tags` argument instead as []string
66
* **New Argument:** `rancher2_cluster.aks_config.tags` - (Optional/Computed) Tags for Kubernetes cluster. For example, `["foo=bar","bar=foo"]` (list)
77
* **New Argument:** `rancher2_cluster.agent_env_vars` - (Optional) Optional Agent Env Vars for Rancher agent. Just for Rancher v2.5.6 and above (list)
8+
* **Deprecated provider Argument:** `retries` - (Deprecated) Use timeout instead
9+
* **New provider Argument:** `timeout` - (Optional) Timeout duration to retry for Rancher connectivity and resource operations. Default: `"120s"`
10+
* **New Argument:** `rancher2_cluster.oke_config.pod_cidr` - (Optional) A CIDR IP range from which to assign Kubernetes Pod IPs (string)
11+
* **New Argument:** `rancher2_cluster.oke_config.service_cidr` - (Optional) A CIDR IP range from which to assign Kubernetes Service IPs (string)
812

913
ENHANCEMENTS:
1014

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ The following arguments are supported:
7777
* `ca_certs` - CA certificates used to sign Rancher server tls certificates. Mandatory if self signed tls and insecure option false. It can also be sourced from the `RANCHER_CA_CERTS` environment variable.
7878
* `insecure` - (Optional) Allow insecure connection to Rancher. Mandatory if self signed tls and not ca_certs provided. It can also be sourced from the `RANCHER_INSECURE` environment variable.
7979
* `bootstrap` - (Optional) Enable bootstrap mode to manage `rancher2_bootstrap` resource. It can also be sourced from the `RANCHER_BOOTSTRAP` environment variable. Default: `false`
80-
* `retries` - (Optional) Number of retries to check Rancher connectivity and `rancher2_node_template` resource deletion. Default: `10`
80+
* `retries` - (Deprecated) Use timeout instead
81+
* `timeout` - (Optional) Timeout duration to retry for Rancher connectivity and resource operations. Default: `"120s"`

rancher2/0_provider_upgrade_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ provider "rancher2" {
214214
` + testAccRancher2CloudCredentialConfigAmazonec2 + `
215215
` + testAccRancher2CloudCredentialConfigAzure + `
216216
` + testAccRancher2CloudCredentialConfigDigitalocean + `
217-
` + testAccRancher2CloudCredentialConfigGoogle + `
218217
` + testAccRancher2CloudCredentialConfigOpenstack + `
219218
` + testAccRancher2CloudCredentialConfigVsphere + `
220219
` + testAccRancher2ClusterConfigRKE + `
@@ -343,19 +342,11 @@ func testAccRancher2UpgradeRancher() resource.TestCheckFunc {
343342
if err != nil {
344343
return fmt.Errorf("Upgrading rancher to %s: %s\n%v", testAccCheckRancher2UpgradeVersion[testAccCheckRancher2RunningVersionIndex], out, err)
345344
}
346-
clusterActive, _, err := testAccProvider.Meta().(*Config).isClusterActive(testAccRancher2ClusterID)
347-
for retry := 0; retry < 10 && !clusterActive; clusterActive, _, err = testAccProvider.Meta().(*Config).isClusterActive(testAccRancher2ClusterID) {
348-
fmt.Printf("Waiting for cluster ID %s becomes active %d\n", testAccRancher2ClusterID, retry+1)
349-
time.Sleep(5 * time.Second)
350-
retry++
351-
}
352-
345+
_, err = testAccProvider.Meta().(*Config).WaitForClusterState(testAccRancher2ClusterID, clusterActiveCondition, (120 * time.Second))
353346
if err != nil {
354-
return fmt.Errorf("Getting cluster ID %s state: %s", testAccRancher2ClusterID, err)
355-
}
356-
if !clusterActive {
357-
return fmt.Errorf("Cluster ID %s is not active", testAccRancher2ClusterID)
347+
return fmt.Errorf("Waiting for cluster ID %s to be active: %v", testAccRancher2ClusterID, err)
358348
}
349+
time.Sleep(5 * time.Second)
359350
return nil
360351
}
361352
}

rancher2/config.go

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ const (
2727
rancher2ManagementV2TypePrefix = "management.cattle.io"
2828
rancher2ReadyAnswer = "pong"
2929
rancher2RetriesWait = 5
30-
rancher2RetriesOnServerError = 3
3130
rancher2RKEK8sSystemImageVersion = "2.3.0"
3231
rancher2NodeTemplateChangeVersion = "2.3.3" // Change node template id format
3332
rancher2TokeTTLMinutesVersion = "2.4.6" // ttl token is readed in minutes
3433
rancher2TokeTTLMilisVersion = "2.4.7" // ttl token is readed in miliseconds
3534
rancher2UILandingVersion = "2.5.0" // ui landing option
3635
rancher2NodeTemplateNewPrefix = "cattle-global-nt:nt-"
36+
rancher2DefaultTimeout = "120s"
3737
)
3838

3939
// Client are the client kind for a Rancher v3 API
@@ -53,7 +53,7 @@ type Config struct {
5353
Bootstrap bool `json:"bootstrap"`
5454
ClusterID string `json:"clusterId"`
5555
ProjectID string `json:"projectId"`
56-
Retries int
56+
Timeout time.Duration
5757
RancherVersion string
5858
K8SDefaultVersion string
5959
K8SSupportedVersions []string
@@ -87,40 +87,50 @@ func (c *Config) isRancherReady() error {
8787
var err error
8888
var resp []byte
8989
url := RootURL(c.URL) + "/ping"
90-
for i := 0; i <= c.Retries; i++ {
90+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
91+
defer cancel()
92+
for {
9193
resp, err = DoGet(url, "", "", "", c.CACerts, c.Insecure)
9294
if err == nil && rancher2ReadyAnswer == string(resp) {
9395
return nil
9496
}
95-
time.Sleep(rancher2RetriesWait * time.Second)
97+
select {
98+
case <-time.After(rancher2RetriesWait * time.Second):
99+
case <-ctx.Done():
100+
return fmt.Errorf("Rancher is not ready: %v", err)
101+
}
96102
}
97-
return fmt.Errorf("Rancher is not ready: %v", err)
98103
}
99104

100105
func (c *Config) getK8SDefaultVersion() (string, error) {
101106
if len(c.K8SDefaultVersion) > 0 {
102107
return c.K8SDefaultVersion, nil
103108
}
104-
109+
var err error
105110
if c.Client.Management == nil {
106-
_, err := c.ManagementClient()
111+
_, err = c.ManagementClient()
107112
if err != nil {
108113
return "", err
109114
}
110115
}
111116

112-
for i := 0; i < rancher2RetriesOnServerError; i++ {
117+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
118+
defer cancel()
119+
for {
113120
k8sVer, err := c.Client.Management.Setting.ByID("k8s-version")
114121
if err == nil {
115122
c.K8SDefaultVersion = k8sVer.Value
116-
break
123+
return c.K8SDefaultVersion, nil
117124
}
118-
if (!IsServerError(err) && !IsForbidden(err)) || (i+1) == rancher2RetriesOnServerError {
125+
if !IsServerError(err) && !IsForbidden(err) {
126+
return "", err
127+
}
128+
select {
129+
case <-time.After(rancher2RetriesWait * time.Second):
130+
case <-ctx.Done():
119131
return "", err
120132
}
121-
time.Sleep(rancher2RetriesWait * time.Second)
122133
}
123-
return c.K8SDefaultVersion, nil
124134
}
125135

126136
func (c *Config) getK8SVersions() ([]string, error) {
@@ -682,8 +692,9 @@ func (c *Config) WaitForClusterState(clusterID, state string, interval time.Dura
682692
return nil, fmt.Errorf("Cluster ID and/or state is nil")
683693
}
684694

685-
timeout := int(interval.Seconds())
686-
for i := 0; i <= timeout; i = i + rancher2RetriesWait {
695+
ctx, cancel := context.WithTimeout(context.Background(), interval)
696+
defer cancel()
697+
for {
687698
obj, err := c.GetClusterByID(clusterID)
688699
if err != nil {
689700
return nil, fmt.Errorf("Getting cluster ID (%s): %v", clusterID, err)
@@ -700,9 +711,12 @@ func (c *Config) WaitForClusterState(clusterID, state string, interval time.Dura
700711
return nil, fmt.Errorf("Cluster ID %s: %s", clusterID, obj.Conditions[i].Message)
701712
}
702713
}
703-
time.Sleep(rancher2RetriesWait * time.Second)
714+
select {
715+
case <-time.After(rancher2RetriesWait * time.Second):
716+
case <-ctx.Done():
717+
return nil, fmt.Errorf("Timeout waiting for cluster ID %s", clusterID)
718+
}
704719
}
705-
return nil, fmt.Errorf("Timeout waiting for cluster ID %s", clusterID)
706720
}
707721

708722
func (c *Config) getObjectV2ByID(clusterID, id, APIType string, resp interface{}) error {
@@ -716,27 +730,32 @@ func (c *Config) getObjectV2ByID(clusterID, id, APIType string, resp interface{}
716730
return fmt.Errorf("Object API V2 type is nil")
717731
}
718732

733+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
734+
defer cancel()
735+
719736
client, err := c.CatalogV2Client(clusterID)
720737
if err != nil {
721738
return err
722739
}
723-
for i := 0; i < rancher2RetriesOnServerError; i++ {
740+
for {
724741
err = client.ByID(APIType, id, resp)
725742
if err == nil {
726-
break
743+
return nil
727744
}
728-
if (!IsServerError(err) && !IsNotFound(err)) || (i+1) == rancher2RetriesOnServerError {
745+
if !IsServerError(err) && !IsUnknownSchemaType(err) {
746+
return err
747+
}
748+
select {
749+
case <-time.After(rancher2RetriesWait * time.Second):
750+
case <-ctx.Done():
729751
return err
730752
}
731-
time.Sleep(rancher2RetriesWait * time.Second)
732753
}
733-
734-
return nil
735754
}
736755

737-
func (c *Config) GetSettingV2ByID(clusterID, id string) (*SettingV2, error) {
756+
func (c *Config) GetSettingV2ByID(id string) (*SettingV2, error) {
738757
resp := &SettingV2{}
739-
err := c.getObjectV2ByID(clusterID, id, settingV2APIType, resp)
758+
err := c.getObjectV2ByID("local", id, settingV2APIType, resp)
740759
if err != nil {
741760
if !IsServerError(err) && !IsNotFound(err) && !IsForbidden(err) {
742761
return nil, fmt.Errorf("Getting Setting V2: %s", err)
@@ -806,8 +825,7 @@ func (c *Config) createObjectV2(clusterID string, APIType string, obj, resp inte
806825
if err != nil {
807826
return err
808827
}
809-
err = client.Create(APIType, obj, resp)
810-
return err
828+
return client.Create(APIType, obj, resp)
811829
}
812830

813831
func (c *Config) CreateCatalogV2(clusterID string, repo *ClusterRepo) (*ClusterRepo, error) {
@@ -843,28 +861,34 @@ func (c *Config) GetCatalogV2List(clusterID string) ([]ClusterRepo, error) {
843861
return nil, err
844862
}
845863

864+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
865+
defer cancel()
846866
listOpts := NewListOpts(nil)
847867
resp := &ClusterRepoCollection{}
848-
for i := 0; i < rancher2RetriesOnServerError; i++ {
868+
for {
849869
err = client.List(catalogV2APIType, listOpts, resp)
850870
if err == nil {
851-
break
871+
return resp.Data, nil
852872
}
853-
if (!IsServerError(err) && !IsNotFound(err)) || (i+1) == rancher2RetriesOnServerError {
873+
if !IsServerError(err) && !IsUnknownSchemaType(err) && !IsNotFound(err) {
874+
return nil, err
875+
}
876+
select {
877+
case <-time.After(rancher2RetriesWait * time.Second):
878+
case <-ctx.Done():
854879
return nil, err
855880
}
856-
time.Sleep(rancher2RetriesWait * time.Second)
857881
}
858-
859-
return resp.Data, nil
860882
}
861883

862884
func (c *Config) WaitCatalogV2Downloaded(clusterID, catalogID string) (*ClusterRepo, error) {
863885
if clusterID == "" || catalogID == "" {
864886
return nil, fmt.Errorf("Cluster ID and/or Catalog V2 ID is nil")
865887
}
866888

867-
for i := 0; i <= catalogV2Timeout; i = i + rancher2RetriesWait {
889+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
890+
defer cancel()
891+
for {
868892
obj, err := c.GetCatalogV2ByID(clusterID, catalogID)
869893
if err != nil {
870894
return nil, fmt.Errorf("Getting catalog V2 ID (%s): %v", catalogID, err)
@@ -881,9 +905,12 @@ func (c *Config) WaitCatalogV2Downloaded(clusterID, catalogID string) (*ClusterR
881905
return nil, fmt.Errorf("Catalog V2 ID %s: %s", catalogID, obj.Status.Conditions[i].Message)
882906
}
883907
}
884-
time.Sleep(rancher2RetriesWait * time.Second)
908+
select {
909+
case <-time.After(rancher2RetriesWait * time.Second):
910+
case <-ctx.Done():
911+
return nil, fmt.Errorf("Timeout waiting for catalog V2 ID %s", catalogID)
912+
}
885913
}
886-
return nil, fmt.Errorf("Timeout waiting for catalog V2 ID %s", catalogID)
887914
}
888915

889916
func (c *Config) WaitAllCatalogV2Downloaded(clusterID string) ([]ClusterRepo, error) {
@@ -892,7 +919,7 @@ func (c *Config) WaitAllCatalogV2Downloaded(clusterID string) ([]ClusterRepo, er
892919
return nil, fmt.Errorf("[ERROR] getting catalog V2 list at cluster ID (%s): %s", clusterID, err)
893920
}
894921

895-
ctx, cancel := context.WithTimeout(context.Background(), catalogV2Timeout*time.Second)
922+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
896923
defer cancel()
897924
g, ctx := errgroup.WithContext(ctx)
898925
for _, clusterRepo := range clusterRepos {
@@ -1060,23 +1087,27 @@ func (c *Config) InfoAppV2(clusterID, repoName, chartName, chartVersion string)
10601087
resource.Links[link] = resource.Links[link] + "&version=" + chartVersion
10611088
}
10621089

1090+
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
1091+
defer cancel()
10631092
client, err := c.CatalogV2Client(clusterID)
10641093
if err != nil {
10651094
return nil, nil, err
10661095
}
10671096
resp := &types2.ChartInfo{}
1068-
for i := 0; i < rancher2RetriesOnServerError; i++ {
1097+
for {
10691098
err = client.GetLink(resource, link, resp)
10701099
if err == nil {
1071-
break
1100+
return repo, resp, nil
10721101
}
1073-
if (!IsServerError(err) && !IsNotFound(err)) || (i+1) == rancher2RetriesOnServerError {
1102+
if !IsServerError(err) && !IsNotFound(err) {
10741103
return nil, nil, fmt.Errorf("failed to get chart info %s:%s from catalog v2 %s: %v", chartName, chartVersion, repoName, err)
10751104
}
1076-
time.Sleep(rancher2RetriesWait * time.Second)
1105+
select {
1106+
case <-time.After(rancher2RetriesWait * time.Second):
1107+
case <-ctx.Done():
1108+
return nil, nil, fmt.Errorf("Timeout getting chart info %s:%s from catalog v2 %s: %v", chartName, chartVersion, repoName, err)
1109+
}
10771110
}
1078-
1079-
return repo, resp, nil
10801111
}
10811112

10821113
func (c *Config) InstallAppV2(clusterID string, repo *ClusterRepo, chartIntall *types2.ChartInstallAction) (*types2.ChartActionOutput, error) {

0 commit comments

Comments
 (0)