@@ -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
100105func (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
126136func (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
708722func (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
813831func (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
862884func (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
889916func (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
10821113func (c * Config ) InstallAppV2 (clusterID string , repo * ClusterRepo , chartIntall * types2.ChartInstallAction ) (* types2.ChartActionOutput , error ) {
0 commit comments