From 53b593f106165f13d436292d40b9c0a5f87c3361 Mon Sep 17 00:00:00 2001 From: John Dietz Date: Mon, 9 Oct 2023 14:09:57 -0400 Subject: [PATCH 1/2] adjustments to debugging local flow --- internal/db/mongo.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/db/mongo.go b/internal/db/mongo.go index 4b4ed3cd..438f4e5a 100644 --- a/internal/db/mongo.go +++ b/internal/db/mongo.go @@ -8,6 +8,7 @@ package db import ( "context" + "encoding/json" "fmt" "os" @@ -89,7 +90,6 @@ func (mdbcl *MongoDBClient) TestDatabaseConnection(silent bool) error { // ImportClusterIfEmpty func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider string) error { - log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "", @@ -99,11 +99,17 @@ 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 + // homeDir, err := os.UserHomeDir() + // if err != nil { + // log.Fatalf("error getting home path: %s", err) + // } + switch cloudProvider { case "aws": // kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName) case "civo", "digitalocean", "vultr": kcfg = k8s.CreateKubeConfig(true, "") + //kcfg = k8s.CreateKubeConfig(false, fmt.Sprintf("%s/basura/kubeconfig", homeDir)) // TODO! dont merge to main with this line case "google": // var err error // kcfg, err = clctrl.GoogleClient.GetContainerClusterAuth(clctrl.ClusterName, []byte(clctrl.GoogleAuth.KeyFile)) @@ -130,6 +136,9 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider stri err = mdbcl.ClustersCollection.FindOne(mdbcl.Context, filter).Decode(&result) if err != nil { // This error means your query did not match any documents. + + err := json.Unmarshal([]byte(importPayload), &result) + if err == mongo.ErrNoDocuments { // Create if entry does not exist insert, err := mdbcl.ClustersCollection.InsertOne(mdbcl.Context, importPayload) From 5dc001c3d34ab95469ed41b5450ac6239a43cdcd Mon Sep 17 00:00:00 2001 From: John Dietz Date: Mon, 9 Oct 2023 17:44:16 -0400 Subject: [PATCH 2/2] adding final import changes to the instance --- internal/db/mongo.go | 56 ++++++++++++++++++++++++-------------------- main.go | 18 +++++++++----- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/internal/db/mongo.go b/internal/db/mongo.go index 438f4e5a..2a215755 100644 --- a/internal/db/mongo.go +++ b/internal/db/mongo.go @@ -8,7 +8,6 @@ package db import ( "context" - "encoding/json" "fmt" "os" @@ -89,7 +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: "", @@ -104,25 +103,24 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool, cloudProvider stri // log.Fatalf("error getting home path: %s", err) // } - switch cloudProvider { - case "aws": - // kcfg = awsext.CreateEKSKubeconfig(&clctrl.AwsClient.Config, clctrl.ClusterName) - case "civo", "digitalocean", "vultr": - kcfg = k8s.CreateKubeConfig(true, "") - //kcfg = k8s.CreateKubeConfig(false, fmt.Sprintf("%s/basura/kubeconfig", homeDir)) // TODO! dont merge to main with this line - 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) + } + 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"] @@ -132,28 +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. - - err := json.Unmarshal([]byte(importPayload), &result) - + 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 { diff --git a/main.go b/main.go index 5a454707..7b0b3929 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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") } @@ -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) } @@ -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