From b5efac7dbd8ec4b0a780e0a792b6d0d213b9f5fc Mon Sep 17 00:00:00 2001 From: k8swrangler <6446939+k8swrangler@users.noreply.github.com> Date: Wed, 1 Nov 2023 15:26:24 -0500 Subject: [PATCH] feat: call new api route (#230) * feat: call new api route * hotwire * fix: call environments * Apply suggestions from code review Co-authored-by: John Dietz --------- Co-authored-by: John Dietz --- internal/environments/defaultEnvironments.go | 78 +++++++++----------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/internal/environments/defaultEnvironments.go b/internal/environments/defaultEnvironments.go index df506dd8..4d656d0d 100644 --- a/internal/environments/defaultEnvironments.go +++ b/internal/environments/defaultEnvironments.go @@ -14,7 +14,6 @@ import ( "io" "net/http" "os" - "strconv" "time" "github.com/kubefirst/kubefirst-api/internal/db" @@ -42,7 +41,7 @@ func CreateDefaultEnvironments(mgmtCluster types.Cluster) error { }) log.SetReportCaller(false) log.SetOutput(os.Stdout) - + defaultClusterNames := []string{"development", "staging", "production"} defaultVclusterTemplate := types.WorkloadCluster{ @@ -63,7 +62,7 @@ func CreateDefaultEnvironments(mgmtCluster types.Cluster) error { MachineType: "", //left up to terraform NodeCount: 3, //defaulted here } - + defaultClusters := []types.WorkloadCluster{} for _, clusterName := range defaultClusterNames { @@ -99,57 +98,52 @@ func CreateDefaultEnvironments(mgmtCluster types.Cluster) error { func callApiEE(goPayload types.WorkloadClusterSet) error { + // in cluster url KubefirstApiEe := os.Getenv("ENTERPRISE_API_URL") + customTransport := http.DefaultTransport.(*http.Transport).Clone() customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} httpClient := http.Client{Transport: customTransport} - for i, cluster := range goPayload.Clusters { - log.Infof("sleeping 10 seconds per cluster") - time.Sleep(10 * time.Second) - log.Infof("sleeping period complete") - - log.Infof("creating cluster %s for %s", strconv.Itoa(i), cluster.ClusterName) - payload, err := json.Marshal(cluster) - if err != nil { - return err - } - - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/cluster/%s", KubefirstApiEe, os.Getenv("CLUSTER_ID")), bytes.NewReader(payload)) + payload, err := json.Marshal(goPayload) + if err != nil { + return err + } - if err != nil { - log.Errorf("error creating http request %s", err) - return err - } - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Accept", "application/json") - - res, err := httpClient.Do(req) - timer := 0 - for err != nil { - if timer > 12 { - log.Errorf("error in http call to api ee: api url (%s) did not come up within 2 minutes %s", req.URL, err.Error()) - } else { - res, err = httpClient.Do(req) - } - timer++ - time.Sleep(10 * time.Second) - } + req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/environments/%s", KubefirstApiEe, os.Getenv("CLUSTER_ID")), bytes.NewReader(payload)) - if res.StatusCode != http.StatusAccepted { - log.Errorf("unable to create default workload clusters and default environments %s: \n request: %s", res.Status, res.Request.URL) - return err + if err != nil { + log.Errorf("error creating http request %s", err) + return err + } + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Accept", "application/json") + + res, err := httpClient.Do(req) + timer := 0 + for err != nil { + if timer > 12 { + log.Errorf("error in http call to api ee: api url (%s) did not come up within 2 minutes %s", req.URL, err.Error()) + } else { + res, err = httpClient.Do(req) } + timer++ + time.Sleep(10 * time.Second) + } - body, err := io.ReadAll(res.Body) - if err != nil { - return err - } + if res.StatusCode != http.StatusAccepted { + log.Errorf("unable to create default workload clusters and default environments %s: \n request: %s", res.Status, res.Request.URL) + return err + } - log.Infof("cluster %s created. result: %s", cluster.ClusterName, string(body)) - time.Sleep(10 * time.Second) + body, err := io.ReadAll(res.Body) + if err != nil { + return err } + + log.Infof("Default environments initiatied", string(body)) + return nil }