diff --git a/extensions/civo/env.go b/extensions/civo/env.go index 47d54993..ecd09c92 100644 --- a/extensions/civo/env.go +++ b/extensions/civo/env.go @@ -36,7 +36,7 @@ func GetCivoTerraformEnvs(envs map[string]string, cl *pkgtypes.Cluster) map[stri envs["AWS_SECRET_ACCESS_KEY"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_aws_access_key_id"] = cl.StateStoreCredentials.AccessKeyID envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging //envs["TF_LOG"] = "debug" @@ -52,7 +52,7 @@ func GetGithubTerraformEnvs(envs map[string]string, cl *pkgtypes.Cluster) map[st envs["AWS_SECRET_ACCESS_KEY"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_aws_access_key_id"] = cl.StateStoreCredentials.AccessKeyID envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging return envs @@ -70,7 +70,7 @@ func GetGitlabTerraformEnvs(envs map[string]string, gid int, cl *pkgtypes.Cluste envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_owner_group_id"] = strconv.Itoa(gid) envs["TF_VAR_gitlab_owner"] = cl.GitAuth.Owner - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging return envs @@ -81,7 +81,7 @@ func GetUsersTerraformEnvs(clientset *kubernetes.Clientset, cl *pkgtypes.Cluster envs["VAULT_ADDR"] = providerConfigs.VaultPortForwardURL envs[fmt.Sprintf("%s_TOKEN", strings.ToUpper(cl.GitProvider))] = cl.GitAuth.Token envs[fmt.Sprintf("%s_OWNER", strings.ToUpper(cl.GitProvider))] = cl.GitAuth.Owner - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging return envs @@ -103,7 +103,7 @@ func GetVaultTerraformEnvs(clientset *kubernetes.Clientset, cl *pkgtypes.Cluster envs["TF_VAR_kbot_ssh_public_key"] = cl.GitAuth.PublicKey envs["TF_VAR_cloudflare_origin_ca_api_key"] = cl.CloudflareAuth.OriginCaIssuerKey envs["TF_VAR_cloudflare_api_key"] = cl.CloudflareAuth.APIToken - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging switch cl.GitProvider { diff --git a/extensions/digitalocean/env.go b/extensions/digitalocean/env.go index 4ce12d88..432cbced 100644 --- a/extensions/digitalocean/env.go +++ b/extensions/digitalocean/env.go @@ -54,7 +54,7 @@ func GetGithubTerraformEnvs(envs map[string]string, cl *pkgtypes.Cluster) map[st envs["AWS_SECRET_ACCESS_KEY"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_aws_access_key_id"] = cl.StateStoreCredentials.AccessKeyID envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging return envs @@ -74,7 +74,7 @@ func GetGitlabTerraformEnvs(envs map[string]string, gid int, cl *pkgtypes.Cluste envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_owner_group_id"] = strconv.Itoa(gid) envs["TF_VAR_gitlab_owner"] = cl.GitAuth.Owner - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging return envs @@ -91,7 +91,7 @@ func GetUsersTerraformEnvs(clientset *kubernetes.Clientset, cl *pkgtypes.Cluster envs["AWS_SECRET_ACCESS_KEY"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_aws_access_key_id"] = cl.StateStoreCredentials.AccessKeyID envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging return envs @@ -116,7 +116,7 @@ func GetVaultTerraformEnvs(clientset *kubernetes.Clientset, cl *pkgtypes.Cluster envs["AWS_SECRET_ACCESS_KEY"] = cl.StateStoreCredentials.SecretAccessKey envs["TF_VAR_aws_access_key_id"] = cl.StateStoreCredentials.AccessKeyID envs["TF_VAR_aws_secret_access_key"] = cl.StateStoreCredentials.SecretAccessKey - envs["AWS_SESSION_TOKEN"] = "" // allows for debugging + envs["AWS_SESSION_TOKEN"] = "" // allows for debugging envs["TF_VAR_aws_session_token"] = "" // allows for debugging switch cl.GitProvider { diff --git a/internal/aws/aws.go b/internal/aws/aws.go index 0fd8f351..1bc7d050 100644 --- a/internal/aws/aws.go +++ b/internal/aws/aws.go @@ -37,7 +37,7 @@ var Conf AWSConfiguration = AWSConfiguration{ // NewAws instantiates a new AWS configuration func NewAws() aws.Config { env, _ := env.GetEnv(constants.SilenceGetEnv) - + awsClient, err := config.LoadDefaultConfig( context.Background(), config.WithRegion(env.AWSRegion), diff --git a/internal/constants/global.go b/internal/constants/global.go index 902f02e7..6f3cfb98 100644 --- a/internal/constants/global.go +++ b/internal/constants/global.go @@ -6,6 +6,10 @@ See the LICENSE file for more details. */ package constants +import ( + "github.com/kubefirst/kubefirst-api/internal/types" +) + const ( // The Namespace in which Kubefirst runs in-cluster KubefirstNamespace = "kubefirst" @@ -20,5 +24,18 @@ const ( ClusterStatusProvisioned = "provisioned" ClusterStatusProvisioning = "provisioning" - SilenceGetEnv = true + SilenceGetEnv = true ) + +var cloudProviderDefaults = types.CloudProviderDefaults{ + Aws: types.CloudDefault{InstanceSize: "m5.large", NodeCount: "6"}, + Civo: types.CloudDefault{InstanceSize: "g4s.kube.large", NodeCount: "6"}, + DigitalOcean: types.CloudDefault{InstanceSize: "s-4vcpu-8gb", NodeCount: "4"}, + Google: types.CloudDefault{InstanceSize: "e2-medium", NodeCount: "2"}, + Vultr: types.CloudDefault{InstanceSize: "vc2-4c-8gb", NodeCount: "4"}, + K3d: types.CloudDefault{InstanceSize: "", NodeCount: "3"}, +} + +func GetCloudDefaults() types.CloudProviderDefaults { + return cloudProviderDefaults +} diff --git a/internal/controller/controller.go b/internal/controller/controller.go index a9d88ce8..910e61b5 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -305,7 +305,7 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition) DigitaloceanAuth: clctrl.DigitaloceanAuth, VultrAuth: clctrl.VultrAuth, CloudflareAuth: clctrl.CloudflareAuth, - NodeType: clctrl.NodeType, + NodeType: clctrl.NodeType, NodeCount: clctrl.NodeCount, } err = clctrl.MdbCl.InsertCluster(cl) diff --git a/internal/db/environments.go b/internal/db/environments.go index c0f56ce4..7c5fca0e 100644 --- a/internal/db/environments.go +++ b/internal/db/environments.go @@ -42,7 +42,7 @@ func (mdbcl *MongoDBClient) GetEnvironments() ([]pkgtypes.Environment, error) { // GetEnvironment func (mdbcl *MongoDBClient) GetEnvironment(name string) (pkgtypes.Environment, error) { // Find - filter := bson.D{{Key: "name", Value: name }} + filter := bson.D{{Key: "name", Value: name}} var result pkgtypes.Environment err := mdbcl.EnvironmentsCollection.FindOne(mdbcl.Context, filter).Decode(&result) if err != nil { @@ -56,14 +56,14 @@ func (mdbcl *MongoDBClient) GetEnvironment(name string) (pkgtypes.Environment, e } // InsertEnvironment -func (mdbcl *MongoDBClient) InsertEnvironment(env pkgtypes.Environment) (pkgtypes.Environment ,error) { - filter := bson.D{{ Key: "name", Value: env.Name }} - - result := pkgtypes.Environment { - ID: primitive.NewObjectID(), - Name: env.Name, - Color: env.Color, - Description: env.Description, +func (mdbcl *MongoDBClient) InsertEnvironment(env pkgtypes.Environment) (pkgtypes.Environment, error) { + filter := bson.D{{Key: "name", Value: env.Name}} + + result := pkgtypes.Environment{ + ID: primitive.NewObjectID(), + Name: env.Name, + Color: env.Color, + Description: env.Description, CreationTimestamp: env.CreationTimestamp, } @@ -87,11 +87,11 @@ func (mdbcl *MongoDBClient) InsertEnvironment(env pkgtypes.Environment) (pkgtype func (mdbcl *MongoDBClient) DeleteEnvironment(envId string) error { objectId, idErr := primitive.ObjectIDFromHex(envId) - if idErr != nil{ + if idErr != nil { return fmt.Errorf("invalid id %v", envId) } - filter := bson.D{{Key: "_id", Value: objectId }} + filter := bson.D{{Key: "_id", Value: objectId}} findError := mdbcl.EnvironmentsCollection.FindOne(mdbcl.Context, filter).Err() @@ -99,7 +99,7 @@ func (mdbcl *MongoDBClient) DeleteEnvironment(envId string) error { return fmt.Errorf("no environment with id %v", envId) } - _,err := mdbcl.EnvironmentsCollection.DeleteOne(mdbcl.Context, filter) + _, err := mdbcl.EnvironmentsCollection.DeleteOne(mdbcl.Context, filter) if err != nil { return fmt.Errorf("error deleting environment with provided id %v: %s", envId, err) } @@ -111,12 +111,12 @@ func (mdbcl *MongoDBClient) DeleteEnvironment(envId string) error { func (mdbcl *MongoDBClient) UpdateEnvironment(id string, env types.EnvironmentUpdateRequest) error { objectId, idErr := primitive.ObjectIDFromHex(id) - if idErr != nil{ + if idErr != nil { return fmt.Errorf("invalid id %v", id) } - filter := bson.D{{ Key: "_id", Value: objectId }} - update := bson.D{{ "$set", env }} + filter := bson.D{{Key: "_id", Value: objectId}} + update := bson.D{{"$set", env}} _, err := mdbcl.EnvironmentsCollection.UpdateOne(mdbcl.Context, filter, update) if err != nil { @@ -124,4 +124,4 @@ func (mdbcl *MongoDBClient) UpdateEnvironment(id string, env types.EnvironmentUp } return nil -} \ No newline at end of file +} diff --git a/internal/db/mongo.go b/internal/db/mongo.go index 63f5c5ca..4b58c3a5 100644 --- a/internal/db/mongo.go +++ b/internal/db/mongo.go @@ -110,9 +110,9 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool) (pkgtypes.Cluster, // find the secret in mgmt cluster's kubefirst namespace and read import payload and clustername var kcfg *k8s.KubernetesClient - var isClusterZero bool = true + var isClusterZero bool = true if env.IsClusterZero == "false" { - isClusterZero = false + isClusterZero = false } if isClusterZero { @@ -126,9 +126,9 @@ func (mdbcl *MongoDBClient) ImportClusterIfEmpty(silent bool) (pkgtypes.Cluster, } clusterDir := fmt.Sprintf("%s/.k1/%s", homeDir, "") - var inCluster bool = false + var inCluster bool = false if env.InCluster == "true" { - inCluster = true + inCluster = true } kcfg = k8s.CreateKubeConfig(inCluster, fmt.Sprintf("%s/kubeconfig", clusterDir)) diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go index 082c872b..6c30ce7d 100644 --- a/internal/middleware/auth.go +++ b/internal/middleware/auth.go @@ -18,18 +18,18 @@ import ( // ValidateAPIKey determines whether or not a request is authenticated with a valid API key func ValidateAPIKey() gin.HandlerFunc { - + return func(c *gin.Context) { APIKey := strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer ") - + if APIKey == "" { c.JSON(http.StatusUnauthorized, gin.H{"status": 401, "message": "Authentication failed - no API key provided in request"}) c.Abort() - + log.Info().Msgf(" Request Status: 401; Authentication failed - no API key provided in request") return } - + env, _ := env.GetEnv(constants.SilenceGetEnv) if APIKey != env.K1AccessToken { diff --git a/internal/router/api/v1/cluster.go b/internal/router/api/v1/cluster.go index 7fb613ea..46317464 100644 --- a/internal/router/api/v1/cluster.go +++ b/internal/router/api/v1/cluster.go @@ -289,9 +289,9 @@ func PostCreateCluster(c *gin.Context) { env, _ := env.GetEnv(constants.SilenceGetEnv) - var inCluster bool = false + var inCluster bool = false if env.InCluster == "true" { - inCluster = true + inCluster = true } if inCluster { @@ -533,64 +533,64 @@ func GetClusterKubeconfig(c *gin.Context) { }) return } - + switch cloudProvider { - case "civo": - civoConfig := civoruntime.CivoConfiguration{ - Client: civoruntime.NewCivo(instanceSizesRequest.CivoAuth.Token, instanceSizesRequest.CloudRegion), - Context: context.Background(), - } - - kubeConfig, cfgError := civoConfig.GetKubeconfig(clusterName) - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: cfgError.Error(), - }) - return - } - - c.IndentedJSON(http.StatusOK, kubeConfig) - - case "digitalocean": - digitaloceanConf := digioceanruntime.DigitaloceanConfiguration{ - Client: digioceanruntime.NewDigitalocean(instanceSizesRequest.DigitaloceanAuth.Token), - Context: context.Background(), - } - - kubeConfig, err := digitaloceanConf.GetKubeconfig(clusterName) + case "civo": + civoConfig := civoruntime.CivoConfiguration{ + Client: civoruntime.NewCivo(instanceSizesRequest.CivoAuth.Token, instanceSizesRequest.CloudRegion), + Context: context.Background(), + } - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } + kubeConfig, cfgError := civoConfig.GetKubeconfig(clusterName) + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: cfgError.Error(), + }) + return + } - c.IndentedJSON(http.StatusOK, kubeConfig) + c.IndentedJSON(http.StatusOK, kubeConfig) - case "vultr": + case "digitalocean": + digitaloceanConf := digioceanruntime.DigitaloceanConfiguration{ + Client: digioceanruntime.NewDigitalocean(instanceSizesRequest.DigitaloceanAuth.Token), + Context: context.Background(), + } - vultrConf := vultrruntime.VultrConfiguration{ - Client: vultrruntime.NewVultr(instanceSizesRequest.VultrAuth.Token), - Context: context.Background(), - } - - kubeConfig, err := vultrConf.GetKubeconfig(clusterName) + kubeConfig, err := digitaloceanConf.GetKubeconfig(clusterName) - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: err.Error(), + }) + return + } + + c.IndentedJSON(http.StatusOK, kubeConfig) - c.IndentedJSON(http.StatusOK, kubeConfig) - - default: + case "vultr": + + vultrConf := vultrruntime.VultrConfiguration{ + Client: vultrruntime.NewVultr(instanceSizesRequest.VultrAuth.Token), + Context: context.Background(), + } + + kubeConfig, err := vultrConf.GetKubeconfig(clusterName) + + if err != nil { c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: fmt.Sprintf("provided cloud provider: %v not implemented", cloudProvider), + Message: err.Error(), }) return + } + + c.IndentedJSON(http.StatusOK, kubeConfig) + + default: + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: fmt.Sprintf("provided cloud provider: %v not implemented", cloudProvider), + }) + return } } diff --git a/internal/router/api/v1/defaults.go b/internal/router/api/v1/defaults.go new file mode 100644 index 00000000..4cdad5c4 --- /dev/null +++ b/internal/router/api/v1/defaults.go @@ -0,0 +1,14 @@ +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/kubefirst/kubefirst-api/internal/constants" +) + +func GetCloudProviderDefaults(c *gin.Context) { + cloudDefaults := constants.GetCloudDefaults() + + c.JSON(http.StatusOK, cloudDefaults) +} diff --git a/internal/router/api/v1/environments.go b/internal/router/api/v1/environments.go index 9dd5322d..2cd4286c 100644 --- a/internal/router/api/v1/environments.go +++ b/internal/router/api/v1/environments.go @@ -25,8 +25,6 @@ func GetEnvironments(c *gin.Context) { c.JSON(http.StatusOK, environments) } - - func CreateEnvironment(c *gin.Context) { // Bind to variable as application/json, handle error @@ -47,7 +45,7 @@ func CreateEnvironment(c *gin.Context) { }) return } - + c.JSON(http.StatusCreated, newEnv) } @@ -85,7 +83,7 @@ func UpdateEnvironment(c *gin.Context) { }) return } - + var environmentUpdate types.EnvironmentUpdateRequest err := c.Bind(&environmentUpdate) if err != nil { @@ -94,7 +92,7 @@ func UpdateEnvironment(c *gin.Context) { }) return } - + if environmentUpdate.Color == "" && environmentUpdate.Description == "" { c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ Message: "please provide a description and or color to update", @@ -102,7 +100,6 @@ func UpdateEnvironment(c *gin.Context) { return } - updateErr := db.Client.UpdateEnvironment(envId, environmentUpdate) if updateErr != nil { diff --git a/internal/router/api/v1/instanceSizes.go b/internal/router/api/v1/instanceSizes.go index 84428779..d198c9cb 100644 --- a/internal/router/api/v1/instanceSizes.go +++ b/internal/router/api/v1/instanceSizes.go @@ -14,7 +14,6 @@ import ( "github.com/kubefirst/runtime/pkg/vultr" ) - func ListInstanceSizesForRegion(c *gin.Context) { dnsProvider, param := c.Params.Get("cloud_provider") @@ -34,163 +33,161 @@ func ListInstanceSizesForRegion(c *gin.Context) { return } - var instanceSizesResponse types.InstanceSizesResponse - + switch dnsProvider { - case "civo": - if instanceSizesRequest.CivoAuth.Token == "" { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: "missing civo auth token, try again", - }) - return - } - - civoConfig := civo.CivoConfiguration{ - Client: civo.NewCivo(instanceSizesRequest.CivoAuth.Token, instanceSizesRequest.CloudRegion), - Context: context.Background(), - } - - instanceSizes, err := civoConfig.ListInstanceSizes() - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } - - instanceSizesResponse.InstanceSizes = instanceSizes - c.JSON(http.StatusOK, instanceSizesResponse) - return - - case "aws": - if instanceSizesRequest.AWSAuth.AccessKeyID == "" || + case "civo": + if instanceSizesRequest.CivoAuth.Token == "" { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: "missing civo auth token, try again", + }) + return + } + + civoConfig := civo.CivoConfiguration{ + Client: civo.NewCivo(instanceSizesRequest.CivoAuth.Token, instanceSizesRequest.CloudRegion), + Context: context.Background(), + } + + instanceSizes, err := civoConfig.ListInstanceSizes() + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: err.Error(), + }) + return + } + + instanceSizesResponse.InstanceSizes = instanceSizes + c.JSON(http.StatusOK, instanceSizesResponse) + return + + case "aws": + if instanceSizesRequest.AWSAuth.AccessKeyID == "" || instanceSizesRequest.AWSAuth.SecretAccessKey == "" || instanceSizesRequest.AWSAuth.SessionToken == "" { c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: "missing authentication credentials in request, please check and try again", - }) - return - } - - awsConf := &awsinternal.AWSConfiguration{ - Config: awsinternal.NewAwsV3( - instanceSizesRequest.CloudRegion, - instanceSizesRequest.AWSAuth.AccessKeyID, - instanceSizesRequest.AWSAuth.SecretAccessKey, - instanceSizesRequest.AWSAuth.SessionToken, - ), - } - - if err != nil { - fmt.Println("Error describing instance offerings:", err) - return - } - - instanceSizes, err := awsConf.ListInstanceSizesForRegion() - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } - - instanceSizesResponse.InstanceSizes = instanceSizes - c.JSON(http.StatusOK, instanceSizesResponse) - return - - case "digitalocean": - if instanceSizesRequest.DigitaloceanAuth.Token == "" { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: "missing authentication credentials in request, please check and try again", - }) - return - } - - digitaloceanConf := digitalocean.DigitaloceanConfiguration{ - Client: digitalocean.NewDigitalocean(instanceSizesRequest.DigitaloceanAuth.Token), - Context: context.Background(), - } - - instances, err := digitaloceanConf.ListInstances() - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } - - instanceSizesResponse.InstanceSizes = instances - c.JSON(http.StatusOK, instanceSizesResponse) - return - - case "vultr": - if instanceSizesRequest.VultrAuth.Token == "" { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: "missing authentication credentials in request, please check and try again", - }) - return - } - - vultrConf := vultr.VultrConfiguration{ - Client: vultr.NewVultr(instanceSizesRequest.VultrAuth.Token), - Context: context.Background(), - } - - instances, err := vultrConf.ListInstances() - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } - - instanceSizesResponse.InstanceSizes = instances - c.JSON(http.StatusOK, instanceSizesResponse) - return - - case "google": - - if instanceSizesRequest.CloudZone == "" { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: "missing cloud_zone arg, please check and try again", - }) - return - } - - if instanceSizesRequest.GoogleAuth.ProjectId == "" || - instanceSizesRequest.GoogleAuth.KeyFile == "" { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: "missing authentication credentials in request, please check and try again", - }) - return - } - - googleConf := google.GoogleConfiguration{ - Context: context.Background(), - Project: instanceSizesRequest.GoogleAuth.ProjectId, - Region: instanceSizesRequest.CloudRegion, - KeyFile: instanceSizesRequest.GoogleAuth.KeyFile, - } - - instances, err := googleConf.ListInstances(instanceSizesRequest.CloudZone) - - if err != nil { - c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: err.Error(), - }) - return - } - - instanceSizesResponse.InstanceSizes = instances - - default: + Message: "missing authentication credentials in request, please check and try again", + }) + return + } + + awsConf := &awsinternal.AWSConfiguration{ + Config: awsinternal.NewAwsV3( + instanceSizesRequest.CloudRegion, + instanceSizesRequest.AWSAuth.AccessKeyID, + instanceSizesRequest.AWSAuth.SecretAccessKey, + instanceSizesRequest.AWSAuth.SessionToken, + ), + } + + if err != nil { + fmt.Println("Error describing instance offerings:", err) + return + } + + instanceSizes, err := awsConf.ListInstanceSizesForRegion() + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: err.Error(), + }) + return + } + + instanceSizesResponse.InstanceSizes = instanceSizes + c.JSON(http.StatusOK, instanceSizesResponse) + return + + case "digitalocean": + if instanceSizesRequest.DigitaloceanAuth.Token == "" { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: "missing authentication credentials in request, please check and try again", + }) + return + } + + digitaloceanConf := digitalocean.DigitaloceanConfiguration{ + Client: digitalocean.NewDigitalocean(instanceSizesRequest.DigitaloceanAuth.Token), + Context: context.Background(), + } + + instances, err := digitaloceanConf.ListInstances() + if err != nil { c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ - Message: fmt.Sprintf("unsupported dns provider: %s", dnsProvider), + Message: err.Error(), }) return + } + + instanceSizesResponse.InstanceSizes = instances + c.JSON(http.StatusOK, instanceSizesResponse) + return + + case "vultr": + if instanceSizesRequest.VultrAuth.Token == "" { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: "missing authentication credentials in request, please check and try again", + }) + return + } + + vultrConf := vultr.VultrConfiguration{ + Client: vultr.NewVultr(instanceSizesRequest.VultrAuth.Token), + Context: context.Background(), + } + + instances, err := vultrConf.ListInstances() + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: err.Error(), + }) + return + } + + instanceSizesResponse.InstanceSizes = instances + c.JSON(http.StatusOK, instanceSizesResponse) + return + + case "google": + + if instanceSizesRequest.CloudZone == "" { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: "missing cloud_zone arg, please check and try again", + }) + return + } + + if instanceSizesRequest.GoogleAuth.ProjectId == "" || + instanceSizesRequest.GoogleAuth.KeyFile == "" { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: "missing authentication credentials in request, please check and try again", + }) + return + } + + googleConf := google.GoogleConfiguration{ + Context: context.Background(), + Project: instanceSizesRequest.GoogleAuth.ProjectId, + Region: instanceSizesRequest.CloudRegion, + KeyFile: instanceSizesRequest.GoogleAuth.KeyFile, + } + + instances, err := googleConf.ListInstances(instanceSizesRequest.CloudZone) + + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: err.Error(), + }) + return + } + + instanceSizesResponse.InstanceSizes = instances + + default: + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: fmt.Sprintf("unsupported dns provider: %s", dnsProvider), + }) + return } c.JSON(http.StatusOK, instanceSizesResponse) } - diff --git a/internal/router/api/v1/telemetry.go b/internal/router/api/v1/telemetry.go index d297e31d..79225eab 100644 --- a/internal/router/api/v1/telemetry.go +++ b/internal/router/api/v1/telemetry.go @@ -62,7 +62,7 @@ func PostTelemetry(c *gin.Context) { MachineID: cl.DomainName, ErrorMessage: "", UserId: cl.DomainName, - MetricName: "", + MetricName: "", } var req types.TelemetryRequest diff --git a/internal/router/api/v1/zones.go b/internal/router/api/v1/zones.go index 019cae21..8dfc4e4d 100644 --- a/internal/router/api/v1/zones.go +++ b/internal/router/api/v1/zones.go @@ -28,7 +28,6 @@ func ListZonesForRegion(c *gin.Context) { KeyFile: zonesListRequest.GoogleAuth.KeyFile, } - var zonesListResponse types.ZonesListResponse zones, err := googleConf.GetZones() @@ -43,4 +42,3 @@ func ListZonesForRegion(c *gin.Context) { c.JSON(http.StatusOK, zonesListResponse) } - diff --git a/internal/router/router.go b/internal/router/router.go index 962f38ed..26480bfc 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -80,6 +80,9 @@ func SetupRouter() *gin.Engine { // Instance Sizes v1.POST("/instance-sizes/:cloud_provider", middleware.ValidateAPIKey(), router.ListInstanceSizesForRegion) + // Default instance size and node count for supported cloud providers + v1.GET("/cloud-defaults", middleware.ValidateAPIKey(), router.GetCloudProviderDefaults) + // Environments v1.GET("/environment", middleware.ValidateAPIKey(), router.GetEnvironments) v1.POST("/environment", middleware.ValidateAPIKey(), router.CreateEnvironment) diff --git a/internal/services/services.go b/internal/services/services.go index ef2d0c3f..b77461e9 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -97,9 +97,9 @@ func CreateService(cl *pkgtypes.Cluster, serviceName string, appDef *types.Gitop env, _ := env.GetEnv(constants.SilenceGetEnv) - var inCluster bool = false + var inCluster bool = false if env.InCluster == "true" { - inCluster = true + inCluster = true } kcfg = k8s.CreateKubeConfig(inCluster, fmt.Sprintf("%s/kubeconfig", tmpGitopsDir)) diff --git a/internal/types/cloudDefaults.go b/internal/types/cloudDefaults.go new file mode 100644 index 00000000..a3aa6375 --- /dev/null +++ b/internal/types/cloudDefaults.go @@ -0,0 +1,15 @@ +package types + +type CloudDefault struct { + InstanceSize string `json:"instance_size"` + NodeCount string `json:"node_count"` +} + +type CloudProviderDefaults struct { + Aws CloudDefault `json:"aws"` + Civo CloudDefault `json:"civo"` + DigitalOcean CloudDefault `json:"do"` + Google CloudDefault `json:"google"` + Vultr CloudDefault `json:"vultr"` + K3d CloudDefault `json:"k3d"` +} diff --git a/internal/types/domain.go b/internal/types/domain.go index 2a49290f..f257dee2 100644 --- a/internal/types/domain.go +++ b/internal/types/domain.go @@ -12,7 +12,7 @@ import ( // DomainListRequest type DomainListRequest struct { - CloudRegion string `json:"cloud_region"` + CloudRegion string `json:"cloud_region"` AWSAuth pkgtypes.AWSAuth `json:"aws_auth,omitempty"` CivoAuth pkgtypes.CivoAuth `json:"civo_auth,omitempty"` DigitaloceanAuth pkgtypes.DigitaloceanAuth `json:"do_auth,omitempty"` @@ -25,4 +25,3 @@ type DomainListRequest struct { type DomainListResponse struct { Domains []string `json:"domains"` } - diff --git a/internal/types/environments.go b/internal/types/environments.go index fd00bf24..b833e3b7 100644 --- a/internal/types/environments.go +++ b/internal/types/environments.go @@ -7,6 +7,6 @@ See the LICENSE file for more details. package types type EnvironmentUpdateRequest struct { - Color string `bson:"color,omitempty" json:"color,omitempty"` - Description string `bson:"description,omitempty" json:"description,omitempty"` + Color string `bson:"color,omitempty" json:"color,omitempty"` + Description string `bson:"description,omitempty" json:"description,omitempty"` } diff --git a/internal/types/instanceSizes.go b/internal/types/instanceSizes.go index 00bb7350..669e7781 100644 --- a/internal/types/instanceSizes.go +++ b/internal/types/instanceSizes.go @@ -11,9 +11,9 @@ import ( ) type InstanceSizesRequest struct { - CloudRegion string `json:"cloud_region" binding:"required"` - CloudZone string `json:"cloud_zone,omitempty"` - CivoAuth pkgtypes.CivoAuth `json:"civo_auth,omitempty"` + CloudRegion string `json:"cloud_region" binding:"required"` + CloudZone string `json:"cloud_zone,omitempty"` + CivoAuth pkgtypes.CivoAuth `json:"civo_auth,omitempty"` AWSAuth pkgtypes.AWSAuth `json:"aws_auth,omitempty"` DigitaloceanAuth pkgtypes.DigitaloceanAuth `json:"do_auth,omitempty"` VultrAuth pkgtypes.VultrAuth `json:"vultr_auth,omitempty"` diff --git a/internal/types/region.go b/internal/types/region.go index 8bae0223..12e47e09 100644 --- a/internal/types/region.go +++ b/internal/types/region.go @@ -12,7 +12,7 @@ import ( // RegionListRequest type RegionListRequest struct { - CloudRegion string `json:"cloud_region,omitempty"` + CloudRegion string `json:"cloud_region,omitempty"` AWSAuth pkgtypes.AWSAuth `json:"aws_auth,omitempty"` CivoAuth pkgtypes.CivoAuth `json:"civo_auth,omitempty"` DigitaloceanAuth pkgtypes.DigitaloceanAuth `json:"do_auth,omitempty"` diff --git a/internal/types/zones.go b/internal/types/zones.go index 04cf5dda..5ae20435 100644 --- a/internal/types/zones.go +++ b/internal/types/zones.go @@ -11,7 +11,7 @@ import ( ) type ZonesListRequest struct { - CloudRegion string `json:"cloud_region" binding:"required"` + CloudRegion string `json:"cloud_region" binding:"required"` GoogleAuth pkgtypes.GoogleAuth `json:"google_auth" binding:"required"` }