Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/local_workspace_account #62

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@

# End of https://www.toptal.com/developers/gitignore/api/go
.dccache
hatchery_qabrh.json
1 change: 1 addition & 0 deletions hatchery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type PayModel struct {
User string `json:"user_id"`
AWSAccountId string `json:"account_id"`
Status string `json:"request_status"`
Local bool `json:"local"`
Region string `json:"region"`
Ecs bool `json:"ecs"`
Subnet int `json:"subnet"`
Expand Down
2 changes: 1 addition & 1 deletion hatchery/hatchery.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func launch(w http.ResponseWriter, r *http.Request) {
if err != nil {
Config.Logger.Printf(err.Error())
}
if payModel == nil {
if payModel == nil || payModel.Local {
err = createLocalK8sPod(r.Context(), hash, userName, accessToken)
} else if payModel.Ecs {

Expand Down
91 changes: 35 additions & 56 deletions hatchery/paymodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,52 +72,39 @@ func payModelFromConfig(userName string) (pm *PayModel, err error) {
}

func getCurrentPayModel(userName string) (result *PayModel, err error) {
// If no DynamoDB table is configured, fall back to config-only
if Config.Config.PayModelsDynamodbTable == "" {
// Attempt to fetch pay model from config file
pm, err := payModelFromConfig(userName)
if err != nil {
// If error occurs, attempt to get default pay model
pm, err = getDefaultPayModel()
if err != nil {
return nil, NopaymodelsError
}
}
return pm, nil

var pm *[]PayModel

if Config.Config.PayModelsDynamodbTable != "" {
// Fetch pay models from DynamoDB with current_pay_model as `true`
pm, err = payModelsFromDatabase(userName, true)
}

payModel := PayModel{}

// Fetch pay models from DynamoDB
pm, err := payModelsFromDatabase(userName, true)

// If no pay models are found in the database
if len(*pm) == 0 {
// Attempt to fetch pay model from config file
pm, err := payModelFromConfig(userName)
// If no dynamoDB or no current pay models in the DB,
// fallback to defaultPayModel from config
if pm == nil || len(*pm) == 0 {
pm, err := getDefaultPayModel()
if err != nil {
// If error occurs, attempt to get default pay model
pm, err = getDefaultPayModel()
if err != nil {
return nil, nil
}
return nil, nil
}
return pm, nil
}
// If exactly one pay model is found in the database
if len(*pm) == 1 {
payModel = (*pm)[0]
if err != nil {
Config.Logger.Printf("Got error unmarshalling: %s", err)
return nil, err
}
}

// If more than one current pay model is found in the database
if len(*pm) > 1 {
// TODO: Reset to zero current pay models here.
// We don't want to be in a situation with multiple current pay models
return nil, fmt.Errorf("multiple current pay models set")
}

// If exactly one current pay model is found in the database
payModel = (*pm)[0]
if err != nil {
Config.Logger.Printf("Got error unmarshalling: %s", err)
return nil, err
}
return &payModel, nil
}

Expand All @@ -134,42 +121,34 @@ func getPayModelsForUser(userName string) (result *AllPayModels, err error) {
return nil, fmt.Errorf("no username sent in header")
}
PayModels := AllPayModels{}
var payModelMap *[]PayModel

// Fallback to config-only if DynamoDB table is not configured
if Config.Config.PayModelsDynamodbTable == "" {
pm, err := payModelFromConfig(userName)
if Config.Config.PayModelsDynamodbTable != "" {
payModelMap, err = payModelsFromDatabase(userName, false)
if err != nil {
pm, err = getDefaultPayModel()
if err != nil {
return nil, nil
}
}
if pm == nil {
return nil, NopaymodelsError
return nil, err
}
PayModels.CurrentPayModel = pm
PayModels.PayModels = append(PayModels.PayModels, *pm)
return &PayModels, nil
}

payModelMap, err := payModelsFromDatabase(userName, false)
payModel, err := getCurrentPayModel(userName)
if err != nil {
return nil, err
}

PayModels.PayModels = *payModelMap
// If `getCurrentPayModel` returns nil,
// then there are no other paymodels to fallback to
if payModel == nil {
return nil, nil
}

payModel, err := getCurrentPayModel(userName)
if err != nil {
return nil, err
if payModelMap == nil {
*payModelMap = append(PayModels.PayModels, *payModel)
} else if len(*payModelMap) == 0 {
*payModelMap = append(*payModelMap, *payModel)
}

PayModels.CurrentPayModel = payModel
PayModels.PayModels = *payModelMap

// Check if PayModels is empty
if len(PayModels.PayModels) == 0 {
return nil, nil
}
PayModels.CurrentPayModel = payModel

return &PayModels, nil
}
Expand Down
10 changes: 5 additions & 5 deletions hatchery/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type WorkspaceStatus struct {
}

func getPodClient(ctx context.Context, userName string, payModelPtr *PayModel) (corev1.CoreV1Interface, bool, error) {
if payModelPtr != nil {
if payModelPtr != nil && !(*payModelPtr).Local {
podClient, err := NewEKSClientset(ctx, userName, *payModelPtr)
if err != nil {
Config.Logger.Printf("Error fetching EKS kubeconfig: %v", err)
Expand Down Expand Up @@ -509,8 +509,8 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str
Annotations: annotations,
},
Spec: k8sv1.PodSpec{
SecurityContext: &securityContext,
InitContainers: []k8sv1.Container{},
SecurityContext: &securityContext,
InitContainers: []k8sv1.Container{},
EnableServiceLinks: &falseVal,
Containers: []k8sv1.Container{
{
Expand Down Expand Up @@ -624,7 +624,7 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str

func createLocalK8sPod(ctx context.Context, hash string, userName string, accessToken string) error {
hatchApp := Config.ContainersMap[hash]

Config.Logger.Printf("Creating a Local K8s Pod")
var extraVars []k8sv1.EnvVar
apiKey, err := getAPIKeyWithContext(ctx, accessToken)
if err != nil {
Expand Down Expand Up @@ -747,7 +747,7 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access

func createExternalK8sPod(ctx context.Context, hash string, userName string, accessToken string, payModel PayModel) error {
hatchApp := Config.ContainersMap[hash]

Config.Logger.Printf("Creating a External K8s Pod")
podClient, err := NewEKSClientset(ctx, userName, payModel)
if err != nil {
Config.Logger.Printf("Failed to create pod client for user %v, Error: %v", userName, err)
Expand Down