Skip to content

Commit

Permalink
Merge pull request #42 from uc-cdis/feat/default_pay_model
Browse files Browse the repository at this point in the history
[HP-651] Add default paymodel
  • Loading branch information
jawadqur authored Jan 25, 2022
2 parents 6e19cc7 + d0de479 commit 229e88a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions hatchery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type PayModel struct {
// HatcheryConfig is the root of all the configuration
type HatcheryConfig struct {
UserNamespace string `json:"user-namespace"`
DefaultPayModel PayModel `json:"default-pay-model"`
PayModels []PayModel `json:"pay-models"`
PayModelsDynamodbTable string `json:"pay-models-dynamodb-table"`
SubDir string `json:"sub-dir"`
Expand Down
29 changes: 22 additions & 7 deletions hatchery/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ func podStatus(ctx context.Context, userName string, accessToken string, payMode
_, serviceErr := podClient.Services(Config.Config.UserNamespace).Get(ctx, serviceName, metav1.GetOptions{})
if err != nil {
if isExternalClient && serviceErr == nil {
// only worry for service if podClient is external EKS
// only worry about service if podClient is external EKS
policy := metav1.DeletePropagationBackground
deleteOptions := metav1.DeleteOptions{
PropagationPolicy: &policy,
}
err := podClient.Services(Config.Config.UserNamespace).Delete(ctx, serviceName, deleteOptions)
if err != nil {
Config.Logger.Printf("Error deleting service. %s", err)
}
Config.Logger.Printf("Pod has been terminated, but service is still being terminated. Wait for service to be killed.")
// Pod has been terminated, but service is still being terminated. Wait for service to be killed
status.Status = "Terminating"
Expand Down Expand Up @@ -617,14 +625,20 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str
}

func getPayModelForUser(userName string) (result *PayModel, err error) {
if userName == "" {
return nil, fmt.Errorf("No username sent in header")
}
if Config.Config.PayModelsDynamodbTable == "" {
// fallback for backward compatibility
Config.Logger.Printf("Unable to query pay model data in DynamoDB: no 'pay-models-dynamodb-table' in config. Fallback on config.")
payModel := Config.Config.DefaultPayModel
for _, configPaymodel := range Config.PayModelMap {
if configPaymodel.User == userName {
return &configPaymodel, nil
payModel = configPaymodel
}
}
if (PayModel{} != payModel) {
return &payModel, nil
}
return nil, fmt.Errorf("No pay model data for username '%s'.", userName)
}
// query pay model data for this user from DynamoDB
Expand Down Expand Up @@ -659,12 +673,15 @@ func getPayModelForUser(userName string) (result *PayModel, err error) {
// in DynamoDB
// TODO: remove this block once we only rely on DynamoDB
Config.Logger.Printf("No pay model data for username '%s' in DynamoDB. Fallback on config.", userName)
payModel := Config.Config.DefaultPayModel
for _, configPaymodel := range Config.PayModelMap {
if configPaymodel.User == userName {
return &configPaymodel, nil
payModel = configPaymodel
}
}

if (PayModel{} != payModel) {
return &payModel, nil
}
return nil, fmt.Errorf("No pay model data for username '%s'.", userName)
}

Expand Down Expand Up @@ -765,8 +782,6 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access

_, err = podClient.Services(Config.Config.UserNamespace).Get(ctx, serviceName, metav1.GetOptions{})
if err == nil {
// This probably happened as the result of some error... there was no pod but was a service
// Lets just clean it up and proceed
policy := metav1.DeletePropagationBackground
deleteOptions := metav1.DeleteOptions{
PropagationPolicy: &policy,
Expand Down

0 comments on commit 229e88a

Please sign in to comment.