Skip to content

Commit

Permalink
Merge pull request #211 from kubefirst/feat-one_route_to_rule_them_all
Browse files Browse the repository at this point in the history
Feat one route to rule them all
  • Loading branch information
claywd authored Oct 20, 2023
2 parents 538fad9 + 432cde9 commit d94f8ee
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tmp
launch.json
__debug_*
.env
kubefirst-api
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/route53 v1.27.5
github.com/charmbracelet/bubbles v0.15.0
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.7.1
github.com/charmbracelet/lipgloss v0.8.0
github.com/civo/civogo v0.3.28
github.com/cloudflare/cloudflare-go v0.73.0
github.com/gin-contrib/cors v1.4.0
Expand Down Expand Up @@ -175,7 +175,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
Expand All @@ -194,7 +194,7 @@ require (
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
Expand Down
12 changes: 7 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ github.com/charmbracelet/bubbletea v0.23.2 h1:vuUJ9HJ7b/COy4I30e8xDVQ+VRDUEFykIj
github.com/charmbracelet/bubbletea v0.23.2/go.mod h1:FaP3WUivcTM0xOKNmhciz60M6I+weYLF76mr1JyI7sM=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU=
github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -773,8 +773,9 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand Down Expand Up @@ -848,8 +849,8 @@ github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKt
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/muesli/termenv v0.14.0/go.mod h1:kG/pF1E7fh949Xhe156crRUrHNyK221IuGO7Ez60Uc8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
Expand Down Expand Up @@ -1505,6 +1506,7 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
5 changes: 3 additions & 2 deletions internal/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool) (pkgtypes.Cluster,
// Create if entry does not exist
_, err := mdbcl.ClustersCollection.InsertOne(mdbcl.Context, clusterFromSecret)
if err != nil {
return pkgtypes.Cluster{}, fmt.Errorf("error inserting cluster %s: %s", clusterName, err)
return pkgtypes.Cluster{}, fmt.Errorf("error inserting cluster %v: %s", clusterFromSecret, err)
}
log.Info("inserted cluster record to db. adding default services.")
// log clusterFromSecret
log.Infof("inserted cluster record to db. adding default services. %v", clusterFromSecret)

return clusterFromSecret, nil
} else {
Expand Down
149 changes: 149 additions & 0 deletions internal/environments/defaultEnvironments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package environments

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/kubefirst/kubefirst-api/internal/db"
"github.com/kubefirst/kubefirst-api/pkg/types"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func NewEnvironment(envDef types.Environment) (types.Environment, error) {
// Create new environment
envDef.CreationTimestamp = fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC()))

newEnv, err := db.Client.InsertEnvironment(envDef)

return newEnv, err
}

func CreateDefaultEnvironments( mgmtCluster types.Cluster) error {

// Logging handler
// Logs to stdout to maintain compatibility with event streaming
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "",
})
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
}


defaultClusters := []types.WorkloadCluster{}

for _, clusterName := range defaultClusterNames {
vcluster:= defaultVclusterTemplate
vcluster.ClusterName = clusterName
vcluster.Environment.Name = clusterName
vcluster.DomainName = fmt.Sprintf("%s.%s", clusterName, mgmtCluster.DomainName)
vcluster.Environment.Description = fmt.Sprintf("Default %s environment", clusterName)
switch clusterName {
case "development":
vcluster.Environment.Color = "green"
case "staging":
vcluster.Environment.Color = "yellow"
case "production":
vcluster.Environment.Color = "pink"
}

var err error
vcluster.Environment, err = NewEnvironment(vcluster.Environment)
if err != nil {
log.Errorf("error creating default environment in db for env %s", err)
}
defaultClusters = append(defaultClusters, vcluster)
}

defaultEnvironmentSet := types.WorkloadClusterSet{
Clusters: defaultClusters,
}

// call api-ee to create clusters
return callApiEE(defaultEnvironmentSet)
}

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
}

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)
}
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
}

body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

log.Infof("Default environments initiatied", string(body))

return nil
}
11 changes: 5 additions & 6 deletions internal/router/api/v1/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package api

import (
"fmt"
"time"

"net/http"

"github.com/gin-gonic/gin"
"github.com/kubefirst/kubefirst-api/internal/db"
environments "github.com/kubefirst/kubefirst-api/internal/environments"
"github.com/kubefirst/kubefirst-api/internal/types"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func GetEnvironments(c *gin.Context) {
Expand All @@ -26,6 +25,8 @@ func GetEnvironments(c *gin.Context) {
c.JSON(http.StatusOK, environments)
}



func CreateEnvironment(c *gin.Context) {

// Bind to variable as application/json, handle error
Expand All @@ -38,9 +39,7 @@ func CreateEnvironment(c *gin.Context) {
return
}

environmentDefinition.CreationTimestamp = fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC()))

newEnv, err := db.Client.InsertEnvironment(environmentDefinition)
newEnv, err := environments.NewEnvironment(environmentDefinition)

if err != nil {
c.JSON(http.StatusConflict, types.JSONFailureResponse{
Expand Down Expand Up @@ -117,4 +116,4 @@ func UpdateEnvironment(c *gin.Context) {
Message: fmt.Sprintf("successfully updated environment with id: %v", envId),
})

}
}
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"

"github.com/kubefirst/kubefirst-api/internal/environments"
"github.com/kubefirst/kubefirst-api/internal/services"

"github.com/joho/godotenv"
Expand Down Expand Up @@ -84,8 +85,20 @@ func main() {
log.Fatal(err)
}


if importedCluster.ClusterName != "" {
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())
}
}()
}
defer db.Client.Client.Disconnect(db.Client.Context)

Expand Down

0 comments on commit d94f8ee

Please sign in to comment.