Skip to content

Commit

Permalink
Merge pull request #188 from kubefirst/logformat
Browse files Browse the repository at this point in the history
kubefirst-api adjustments for secret-based export import
  • Loading branch information
johndietz authored Oct 9, 2023
2 parents 59fde1e + 5dc001c commit 37b0dec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
57 changes: 36 additions & 21 deletions internal/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func (mdbcl *MongoDBClient) TestDatabaseConnection(silent bool) error {
}

// ImportClusterIfEmpty
func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider string) error {

func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider string) (pkgtypes.Cluster, error) {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "",
Expand All @@ -99,24 +98,29 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider stri
// find the secret in mgmt cluster's kubefirst namespace and read import payload and clustername
var kcfg *k8s.KubernetesClient

switch cloudProvider {
case "aws":
// kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName)
case "civo", "digitalocean", "vultr":
kcfg = k8s.CreateKubeConfig(true, "")
case "google":
// var err error
// kcfg, err = clctrl.GoogleClient.GetContainerClusterAuth(clctrl.ClusterName, []byte(clctrl.GoogleAuth.KeyFile))
// if err != nil {
// return err
// }
// homeDir, err := os.UserHomeDir()
// if err != nil {
// log.Fatalf("error getting home path: %s", err)
// }

homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("error getting home path: %s", err)
}
clusterDir := fmt.Sprintf("%s/.k1/%s", homeDir, "")

inCluster := false
if os.Getenv("IN_CLUSTER") == "true" {
inCluster = true
}

kcfg = k8s.CreateKubeConfig(inCluster, fmt.Sprintf("%s/kubeconfig", clusterDir))

log.Infof("reading secret mongo-state to determine if import is needed")
secData, err := k8s.ReadSecretV2(kcfg.Clientset, "kubefirst", "mongodb-state")
if err != nil {
log.Infof("error reading secret mongodb-state. %s", err)
return err
return pkgtypes.Cluster{}, err
}
clusterName := secData["cluster-name"]
importPayload := secData["cluster-0"]
Expand All @@ -126,25 +130,36 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider stri
// otherwise read the payload, import to db, bail

filter := bson.D{{Key: "cluster_name", Value: clusterName}}
var result pkgtypes.Cluster
err = mdbcl.ClustersCollection.FindOne(mdbcl.Context, filter).Decode(&result)
// var result1 pkgtypes.Cluster
var clusterFromSecret pkgtypes.Cluster
//err = mdbcl.ClustersCollection.FindOne(mdbcl.Context, filter).Decode(&result1)
err = mdbcl.ClustersCollection.FindOne(mdbcl.Context, filter).Decode(&clusterFromSecret)
if err != nil {
// This error means your query did not match any documents.
log.Infof("did not find preexisting record for cluster %s. importing record.", clusterName)
// clusterFromSecret := pkgtypes.Cluster{}
unmarshalErr := bson.UnmarshalExtJSON([]byte(importPayload), true, &clusterFromSecret)
if unmarshalErr != nil {
log.Info("error encountered unmarshaling secret data")
log.Error(unmarshalErr)
}
if err == mongo.ErrNoDocuments {
// Create if entry does not exist
insert, err := mdbcl.ClustersCollection.InsertOne(mdbcl.Context, importPayload)
_, err := mdbcl.ClustersCollection.InsertOne(mdbcl.Context, clusterFromSecret)
if err != nil {
return fmt.Errorf("error inserting cluster %s: %s", clusterName, err)
return pkgtypes.Cluster{}, fmt.Errorf("error inserting cluster %s: %s", clusterName, err)
}
log.Info(insert)
log.Info("inserted cluster record to db. adding default services.")

return clusterFromSecret, nil
} else {
return fmt.Errorf("error inserting record: %s", err)
return pkgtypes.Cluster{}, fmt.Errorf("error inserting record: %s", err)
}
} else {
log.Infof("cluster record for %s already exists - skipping", clusterName)
}

return nil
return pkgtypes.Cluster{}, nil
}

type EstablishConnectArgs struct {
Expand Down
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"os"

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

"github.com/joho/godotenv"
"github.com/kubefirst/kubefirst-api/docs"
"github.com/kubefirst/kubefirst-api/internal/db"
Expand All @@ -33,8 +35,8 @@ const (

func main() {

envError := godotenv.Load(".env");
envError := godotenv.Load(".env")

if envError != nil {
log.Info("error loading .env file, using local environment variables")
}
Expand All @@ -49,7 +51,7 @@ func main() {
if os.Getenv("MONGODB_HOST_TYPE") == "" {
log.Fatalf("the MONGODB_HOST_TYPE environment variable must be set to either: atlas, local")
}
for _, v := range []string{"MONGODB_HOST", "MONGODB_USERNAME", "MONGODB_PASSWORD"} {
for _, v := range []string{"MONGODB_HOST", "MONGODB_USERNAME", "MONGODB_PASSWORD", "CLOUD_PROVIDER"} {
if os.Getenv(v) == "" {
log.Fatalf("the %s environment variable must be set", v)
}
Expand All @@ -68,19 +70,23 @@ func main() {

// Verify database connectivity
err := db.Client.EstablishMongoConnection(db.EstablishConnectArgs{
Tries: 20,
Tries: 20,
Silent: false,
})
if err != nil {
log.Fatal(err)
}

log.Infof("checking for cluster import secret for %s management cluster", os.Getenv("CLOUD_PROVIDER"))
// Import if needed
err = db.Client.ImportClusterIfEmpty(false, os.Getenv("CLOUD_PROVIDER"))
importedCluster, err := db.Client.ImportClusterIfEmpty(false, os.Getenv("CLOUD_PROVIDER"))
if err != nil {
log.Fatal(err)
}

if importedCluster.ClusterName != "" {
services.AddDefaultServices(&importedCluster)
}
defer db.Client.Client.Disconnect(db.Client.Context)

// Programmatically set swagger info
Expand Down

0 comments on commit 37b0dec

Please sign in to comment.