diff --git a/internal/db/mongo.go b/internal/db/mongo.go index 4b4ed3cd..2a215755 100644 --- a/internal/db/mongo.go +++ b/internal/db/mongo.go @@ -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: "", @@ -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"] @@ -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 { 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