Skip to content

Commit

Permalink
add route and fetcher function for instance size and node count for s… (
Browse files Browse the repository at this point in the history
#262)

* add route and fetcher function for instance size and node count for supported cloud providers

* make it 4 nodes

* reduce count to 6 (2 per az)

---------

Co-authored-by: Jared Edwards <[email protected]>
  • Loading branch information
D-B-Hawk and jarededwards authored Dec 8, 2023
1 parent d2fd02f commit 6ff94ad
Show file tree
Hide file tree
Showing 22 changed files with 295 additions and 255 deletions.
10 changes: 5 additions & 5 deletions extensions/civo/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions extensions/digitalocean/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
19 changes: 18 additions & 1 deletion internal/constants/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions internal/db/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}

Expand All @@ -87,19 +87,19 @@ 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()

if findError != nil {
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)
}
Expand All @@ -111,17 +111,17 @@ 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 {
return fmt.Errorf("error updating environment %v: %s", id, err)
}

return nil
}
}
8 changes: 4 additions & 4 deletions internal/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions internal/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
100 changes: 50 additions & 50 deletions internal/router/api/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down
14 changes: 14 additions & 0 deletions internal/router/api/v1/defaults.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 6ff94ad

Please sign in to comment.