Skip to content

Commit

Permalink
simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
johndietz committed Oct 23, 2023
1 parent 69cf088 commit 2f70b09
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 111 deletions.
112 changes: 57 additions & 55 deletions internal/environments/defaultEnvironments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io"
"net/http"
"os"
"strconv"
"time"

"github.com/kubefirst/kubefirst-api/internal/db"
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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"
}
Expand All @@ -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
}
61 changes: 5 additions & 56 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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: "[email protected]", //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)

Expand Down

0 comments on commit 2f70b09

Please sign in to comment.