Fix: cluster e2e test case

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
This commit is contained in:
barnettZQG
2022-07-28 15:13:49 +08:00
parent ad501b4582
commit 23cd84ba94
2 changed files with 21 additions and 10 deletions
+14 -3
View File
@@ -20,6 +20,8 @@ import (
"bytes"
"context"
"fmt"
"strings"
"time"
"github.com/briandowns/spinner"
prismclusterv1alpha1 "github.com/kubevela/prism/pkg/apis/cluster/v1alpha1"
@@ -89,9 +91,18 @@ func (clusterConfig *KubeClusterConfig) PostRegistration(ctx context.Context, cl
if clusterConfig.CreateNamespace == "" {
return nil
}
if err := ensureNamespaceExists(ctx, cli, clusterConfig.ClusterName, clusterConfig.CreateNamespace); err != nil {
_ = DetachCluster(ctx, cli, clusterConfig.ClusterName, DetachClusterManagedClusterKubeConfigPathOption(clusterConfig.FilePath))
return fmt.Errorf("failed to ensure %s namespace installed in cluster %s: %w", clusterConfig.CreateNamespace, clusterConfig.ClusterName, err)
// retry 3 times.
for i := 0; i < 3; i++ {
if err := ensureNamespaceExists(ctx, cli, clusterConfig.ClusterName, clusterConfig.CreateNamespace); err != nil {
// Cluster gateway discovers the cluster maybe be deferred, so we should retry.
if strings.Contains(err.Error(), "no such cluster") {
time.Sleep(time.Second * 1)
continue
}
_ = DetachCluster(ctx, cli, clusterConfig.ClusterName, DetachClusterManagedClusterKubeConfigPathOption(clusterConfig.FilePath))
return fmt.Errorf("failed to ensure %s namespace installed in cluster %s: %w", clusterConfig.CreateNamespace, clusterConfig.ClusterName, err)
}
break
}
return nil
}
+7 -7
View File
@@ -206,23 +206,23 @@ func delete(path string) *http.Response {
}
func decodeResponseBody(resp *http.Response, dst interface{}) error {
if resp.StatusCode != 200 {
return fmt.Errorf("response code is not 200: %d", resp.StatusCode)
}
if resp.Body == nil {
return fmt.Errorf("response body is nil")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if dst != nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, dst)
if err != nil {
return err
}
return nil
}
if resp.StatusCode != 200 {
return fmt.Errorf("response code is not 200: %d body: %s", resp.StatusCode, string(body))
}
return nil
}