diff --git a/internal/environments/defaultEnvironments.go b/internal/environments/defaultEnvironments.go index 149dcef6..4161e87f 100644 --- a/internal/environments/defaultEnvironments.go +++ b/internal/environments/defaultEnvironments.go @@ -14,6 +14,7 @@ import ( "io" "net/http" "os" + "strconv" "time" "github.com/kubefirst/kubefirst-api/internal/db" @@ -31,7 +32,7 @@ func NewEnvironment(envDef types.Environment) (types.Environment, error) { return newEnv, err } -func CreateDefaultEnvironments( mgmtCluster types.Cluster) error { +func CreateDefaultEnvironments(mgmtCluster types.Cluster) error { // Logging handler // Logs to stdout to maintain compatibility with event streaming @@ -41,33 +42,32 @@ func CreateDefaultEnvironments( mgmtCluster types.Cluster) error { }) log.SetReportCaller(false) log.SetOutput(os.Stdout) - + defaultClusterNames := []string{"development", "staging", "production"} - defaultVclusterTemplate := types.WorkloadCluster{ - AdminEmail: mgmtCluster.AlertsEmail, - CloudProvider: mgmtCluster.CloudProvider, - ClusterID: mgmtCluster.ClusterID, - ClusterName: "not so empty string which should be replaced", - ClusterType: "workload-vcluster", - CloudRegion: mgmtCluster.CloudRegion, - DomainName: "not so empty string which should be replaced", - DnsProvider: mgmtCluster.DnsProvider, - Environment: types.Environment{ - Name: "not so empty string which should be replaced", - Description: "not so empty string which should be replaced", - }, - GitAuth: mgmtCluster.GitAuth, - InstanceSize: "", // left up to terraform - MachineType: "", //left up to terraform - NodeCount: 3, //defaulted here + defaultVclusterTemplate := types.WorkloadCluster{ + AdminEmail: mgmtCluster.AlertsEmail, + CloudProvider: mgmtCluster.CloudProvider, + ClusterID: mgmtCluster.ClusterID, + ClusterName: "not so empty string which should be replaced", + ClusterType: "workload-vcluster", + CloudRegion: mgmtCluster.CloudRegion, + DomainName: "not so empty string which should be replaced", + DnsProvider: mgmtCluster.DnsProvider, + Environment: types.Environment{ + Name: "not so empty string which should be replaced", + Description: "not so empty string which should be replaced", + }, + GitAuth: mgmtCluster.GitAuth, + InstanceSize: "", // left up to terraform + MachineType: "", //left up to terraform + NodeCount: 3, //defaulted here } - defaultClusters := []types.WorkloadCluster{} for _, clusterName := range defaultClusterNames { - vcluster:= defaultVclusterTemplate + vcluster := defaultVclusterTemplate vcluster.ClusterName = clusterName vcluster.Environment.Name = clusterName vcluster.DomainName = fmt.Sprintf("%s.%s", clusterName, mgmtCluster.DomainName) @@ -76,7 +76,7 @@ func CreateDefaultEnvironments( mgmtCluster types.Cluster) error { case "development": vcluster.Environment.Color = "green" case "staging": - vcluster.Environment.Color = "yellow" + vcluster.Environment.Color = "gold" case "production": vcluster.Environment.Color = "pink" } @@ -99,51 +99,53 @@ 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} - payload, err := json.Marshal(goPayload) - if err != nil { - return err - } + for i, cluster := range goPayload.Clusters { - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/environments/%s", KubefirstApiEe, goPayload.Clusters[0].ClusterID), bytes.NewReader(payload)) - 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) + log.Infof("creating cluster %s for %s", strconv.Itoa(i), cluster.ClusterName) + payload, err := json.Marshal(cluster) + if err != nil { + return err } - timer++ - time.Sleep(10 * time.Second) - } - if res.StatusCode != http.StatusOK { - log.Errorf("unable to create default workload clusters and default environments %s: \n request: %s", res.Status, res.Request.URL) - return err - } + req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/cluster/%s", KubefirstApiEe, os.Getenv("CLUSTER_ID")), bytes.NewReader(payload)) - body, err := io.ReadAll(res.Body) - 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) + } + + if res.StatusCode != http.StatusOK { + log.Errorf("unable to create default workload clusters and default environments %s: \n request: %s", res.Status, res.Request.URL) + return err + } - log.Infof("Default environments initiatied", string(body)) + body, err := io.ReadAll(res.Body) + if err != nil { + return err + } + log.Infof("Default environments initiatied %s", string(body)) + } return nil } diff --git a/main.go b/main.go index c83c8899..abc64939 100644 --- a/main.go +++ b/main.go @@ -7,19 +7,24 @@ See the LICENSE file for more details. package main import ( + "bytes" + "encoding/json" "fmt" + "io" + "net/http" "os" - "github.com/kubefirst/kubefirst-api/internal/environments" - "github.com/kubefirst/kubefirst-api/internal/services" - "github.com/kubefirst/metrics-client/pkg/telemetry" - "github.com/joho/godotenv" "github.com/kubefirst/kubefirst-api/docs" "github.com/kubefirst/kubefirst-api/internal/db" + "github.com/kubefirst/kubefirst-api/internal/environments" api "github.com/kubefirst/kubefirst-api/internal/router" + "github.com/kubefirst/kubefirst-api/internal/services" apitelemetry "github.com/kubefirst/kubefirst-api/internal/telemetry" "github.com/kubefirst/kubefirst-api/internal/utils" + pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" + "github.com/kubefirst/metrics-client/pkg/telemetry" + "github.com/kubefirst/runtime/pkg" log "github.com/sirupsen/logrus" ) @@ -80,8 +85,6 @@ func main() { log.Infof("adding default services for cluster %s", importedCluster.ClusterName) services.AddDefaultServices(&importedCluster) - // Call default environment create code if we imported a cluster - // execute default environment creation concurrently go func() { log.Infof("adding default environments for cluster %s", importedCluster.ClusterName) err := environments.CreateDefaultEnvironments(importedCluster) @@ -131,3 +134,37 @@ func main() { log.Fatalf("Error starting API: %s", err) } } + +func postVcluster(workloadClusterDef pkgtypes.WorkloadCluster, mgmtClusterID string) (string, error) { + + payload, err := json.Marshal(&workloadClusterDef) + if err != nil { + return "", err + } + + clusterApi := fmt.Sprintf("http://kubefirst-api-ee.kubefirst.svc.cluster.local:8080/cluster/%s", mgmtClusterID) + + req, err := http.NewRequest(http.MethodPost, clusterApi, bytes.NewBuffer(payload)) + if err != nil { + log.Infof("error setting request") + } + + k1AccessToken := os.Getenv("") + req.Header.Add("Content-Type", pkg.JSONContentType) + req.Header.Add("Accept", "application/json") + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", k1AccessToken)) + + res, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + if err != nil { + return "", err + } + log.Infof(string(body)) + + return "yay", nil +}