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 350f6b38..4f6b37d6 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "net/http" "os" + "github.com/kubefirst/kubefirst-api/internal/environments" "github.com/kubefirst/kubefirst-api/internal/services" "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" @@ -96,63 +97,11 @@ 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) - // if err != nil { - // log.Infof("Error creating default environments %s", err.Error()) - // } - // }() - arrayOne := [3]string{"development", "staging", "production"} - - for _, env := range arrayOne { - - log.Infoln("creating cluster", env) - // - name: CLOUD_PROVIDER - // - name: CLUSTER_ID - // - name: CLUSTER_TYPE - // - name: DOMAIN_NAME - // - name: GIT_PROVIDER - // - name: INSTALL_METHOD - // - name: KUBEFIRST_CLIENT - // - name: KUBEFIRST_TEAM - // - name: KUBEFIRST_TEAM_INFO - // - name: KUBEFIRST_VERSION - // - name: USE_TELEMETRY - - var developmentCluster = pkgtypes.WorkloadCluster{ - AdminEmail: "alerts@kubefirst.io", //todo - CloudProvider: os.Getenv("CLOUD_PROVIDER"), - ClusterID: "", - ClusterName: env, - ClusterType: "workload-vcluster", - CloudRegion: "fra1", //todo - DomainName: fmt.Sprintf("%s.%s", env, os.Getenv("DOMAIN_NAME")), - DnsProvider: "civo", //todo - Environment: pkgtypes.Environment{ - ID: [12]byte{}, - Name: env, - Color: "fucia", //todo - Description: "pretty", - }, - GitAuth: pkgtypes.GitAuth{ - Token: "", - User: "", - Owner: "", - PublicKey: "", - PrivateKey: "", - PublicKeys: "", - }, - InstanceSize: "medium", - MachineType: "", - NodeCount: 1, - Status: "", - } - postVcluster(developmentCluster, os.Getenv("CLUSTER_ID")) + log.Infof("adding default environments for cluster %s", importedCluster.ClusterName) + err := environments.CreateDefaultEnvironments(importedCluster) + if err != nil { + log.Infof("Error creating default environments %s", err.Error()) } - } defer db.Client.Client.Disconnect(db.Client.Context)