Skip to content

Commit

Permalink
feat: call new api route (#230)
Browse files Browse the repository at this point in the history
* feat: call new api route

* hotwire

* fix: call environments

* Apply suggestions from code review

Co-authored-by: John Dietz <[email protected]>

---------

Co-authored-by: John Dietz <[email protected]>
  • Loading branch information
claywd and johndietz authored Nov 1, 2023
1 parent 2b39a48 commit b5efac7
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions internal/environments/defaultEnvironments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"io"
"net/http"
"os"
"strconv"
"time"

"github.com/kubefirst/kubefirst-api/internal/db"
Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit b5efac7

Please sign in to comment.