From 8c6680cd578d3a7014a6eb179cbdc44b2efa2b1d Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Thu, 4 May 2023 23:28:47 +0200 Subject: [PATCH 01/16] Add structured logging via zap --- .secrets.baseline | 4 +- go.mod | 5 +- go.sum | 8 ++ hatchery/alb.go | 15 ++- hatchery/cloudwatch.go | 12 +- hatchery/config.go | 51 ++++++-- hatchery/ec2.go | 8 +- hatchery/ecs.go | 257 +++++++++++++++++++++++++++++-------- hatchery/efs.go | 46 ++++++- hatchery/hatchery.go | 74 +++++++++-- hatchery/paymodels.go | 16 ++- hatchery/pods.go | 204 +++++++++++++++++++++++------ hatchery/prisma.go | 28 +++- hatchery/ram.go | 53 ++++++-- hatchery/transitgateway.go | 148 ++++++++++++++++----- hatchery/vpc.go | 52 ++++++-- main.go | 30 +++-- 17 files changed, 806 insertions(+), 205 deletions(-) diff --git a/.secrets.baseline b/.secrets.baseline index 2ecab4ef..e2e4a088 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": "go.sum|^.secrets.baseline$", "lines": null }, - "generated_at": "2023-04-07T16:22:23Z", + "generated_at": "2023-05-04T21:28:41Z", "plugins_used": [ { "name": "AWSKeyDetector" @@ -99,7 +99,7 @@ "hashed_secret": "96e77458817d8486fab4c56c8d4c51b83f85c79d", "is_secret": false, "is_verified": false, - "line_number": 391, + "line_number": 465, "type": "Secret Keyword" } ], diff --git a/go.mod b/go.mod index e5952d06..c9b22e29 100644 --- a/go.mod +++ b/go.mod @@ -43,6 +43,9 @@ require ( github.com/prometheus/procfs v0.6.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/tinylib/msgp v1.1.6 // indirect + go.uber.org/atomic v1.11.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.24.0 // indirect golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect @@ -53,7 +56,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/klog/v2 v2.30.0 // indirect k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect sigs.k8s.io/structured-merge-diff/v4 v4.2.0 // indirect diff --git a/go.sum b/go.sum index ffa08552..0ce90799 100644 --- a/go.sum +++ b/go.sum @@ -430,10 +430,16 @@ go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16g go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= +go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= +go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= +go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -774,6 +780,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/hatchery/alb.go b/hatchery/alb.go index b87c8998..d2885bc0 100644 --- a/hatchery/alb.go +++ b/hatchery/alb.go @@ -223,7 +223,10 @@ func (creds *CREDS) terminateLoadBalancerTargetGroup(userName string) error { Region: aws.String("us-east-1"), }))) tgName := truncateString(strings.ReplaceAll(os.Getenv("GEN3_ENDPOINT"), ".", "-")+userToResourceName(userName, "service")+"tg", 32) - Config.Logger.Printf("Deleting target group: %s", tgName) + Config.Logger.Debug("Deleting target group", + "target-group", tgName, + "username", userName, + ) tgArn, err := svc.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ Names: []*string{aws.String(tgName)}, }) @@ -235,7 +238,10 @@ func (creds *CREDS) terminateLoadBalancerTargetGroup(userName string) error { return nil } } else { - Config.Logger.Printf("Error describing target group: %s", err.Error()) + Config.Logger.Error("Error describing target group", + "error", err.Error(), + "username", userName, + ) return err } } @@ -252,7 +258,10 @@ func (creds *CREDS) terminateLoadBalancerTargetGroup(userName string) error { return nil } } else { - Config.Logger.Printf("Error deleting target group: %s", err.Error()) + Config.Logger.Error("Error deleting target group", + "error", err.Error(), + "username", userName, + ) } } return nil diff --git a/hatchery/cloudwatch.go b/hatchery/cloudwatch.go index 27b94794..246ec722 100644 --- a/hatchery/cloudwatch.go +++ b/hatchery/cloudwatch.go @@ -7,7 +7,7 @@ import ( "github.com/aws/aws-sdk-go/service/cloudwatchlogs" ) -//Create CloudWatch LogGroup for hatchery containers +// Create CloudWatch LogGroup for hatchery containers func (sess *CREDS) CreateLogGroup(LogGroupName string, creds *credentials.Credentials) (string, error) { c := cloudwatchlogs.New(session.Must(session.NewSession(&aws.Config{ Credentials: creds, @@ -20,7 +20,10 @@ func (sess *CREDS) CreateLogGroup(LogGroupName string, creds *credentials.Creden logGroup, err := c.DescribeLogGroups(describeLogGroupIn) if err != nil { - Config.Logger.Printf("Error in DescribeLogGroup: %s", err) + Config.Logger.Error("Error in DescribeLogGroup", + "error", err, + "logGroupNamePrefix", LogGroupName, + ) return "", err } if len(logGroup.LogGroups) == 0 { @@ -29,7 +32,10 @@ func (sess *CREDS) CreateLogGroup(LogGroupName string, creds *credentials.Creden } newLogGroup, err := c.CreateLogGroup(createLogGroupIn) if err != nil { - Config.Logger.Printf("Error in CreateLogGroup: %s, %s", err, newLogGroup) + Config.Logger.Error("Error in CreateLogGroup", + "error", err, + "newLogGroup", newLogGroup, + ) return "", err } return newLogGroup.String(), nil diff --git a/hatchery/config.go b/hatchery/config.go index f6bef734..7a1f7df7 100644 --- a/hatchery/config.go +++ b/hatchery/config.go @@ -1,13 +1,13 @@ package hatchery import ( + "go.uber.org/zap" k8sv1 "k8s.io/api/core/v1" "crypto/md5" "encoding/json" "fmt" "io/ioutil" - "log" "os" ) @@ -102,14 +102,16 @@ type FullHatcheryConfig struct { Config HatcheryConfig ContainersMap map[string]Container PayModelMap map[string]PayModel - Logger *log.Logger + Logger *zap.SugaredLogger } // LoadConfig from a json file -func LoadConfig(configFilePath string, loggerIn *log.Logger) (config *FullHatcheryConfig, err error) { +func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *FullHatcheryConfig, err error) { logger := loggerIn if nil == loggerIn { - logger = log.New(os.Stdout, "", log.LstdFlags) + zapLogger, _ := zap.NewProduction() + defer zapLogger.Sync() + logger = zapLogger.Sugar() } plan, err := ioutil.ReadFile(configFilePath) @@ -119,10 +121,18 @@ func LoadConfig(configFilePath string, loggerIn *log.Logger) (config *FullHatche if nil != err { cwd, _ := os.Getwd() - data.Logger.Printf("failed to load %v from cwd %v got - %v", configFilePath, cwd, err) + // data.Logger.Printf("failed to load %v from cwd %v got - %v", configFilePath, cwd, err) + data.Logger.Error("Failed to load config file.", + "configFilePath", configFilePath, + "cwd", cwd, + "error", err, + ) return data, err } - data.Logger.Printf("loaded config: %v", string(plan)) + // data.Logger.Printf("loaded config: %v", string(plan)) + data.Logger.Debug("Loaded config.", + "config", string(plan), + ) data.ContainersMap = make(map[string]Container) data.PayModelMap = make(map[string]PayModel) _ = json.Unmarshal(plan, &data.Config) @@ -132,22 +142,38 @@ func LoadConfig(configFilePath string, loggerIn *log.Logger) (config *FullHatche if info.Name == "" { return nil, fmt.Errorf("Empty name for more-configs app at: %v", info.Path) } - data.Logger.Printf("loading config from %v", info.Path) + // data.Logger.Printf("loading config from %v", info.Path) + data.Logger.Debug("Loading config.", + "path", info.Path, + ) composeModel, err := DockstoreComposeFromFile(info.Path) if nil != err { - data.Logger.Printf("failed to load config from %v, got: %v", info.Path, err) + // data.Logger.Printf("failed to load config from %v, got: %v", info.Path, err) + data.Logger.Error("Failed to load config.", + "path", info.Path, + "error", err, + ) return nil, err } - data.Logger.Printf("%v", composeModel) + // data.Logger.Printf("%v", composeModel) + data.Logger.Debug("Loaded config.", + "composeModel", composeModel, + ) hatchApp, err := composeModel.BuildHatchApp() hatchApp.Name = info.Name if nil != err { - data.Logger.Printf("failed to translate app, got: %v", err) + // data.Logger.Printf("failed to translate app, got: %v", err) + data.Logger.Error("Failed to translate app.", + "error", err, + ) return nil, err } data.Config.Containers = append(data.Config.Containers, *hatchApp) } else { - data.Logger.Printf("ignoring config of unsupported type: %v", info.AppType) + // data.Logger.Printf("ignoring config of unsupported type: %v", info.AppType) + data.Logger.Warn("Ignoring config of unsupported type.", + "appType", info.AppType, + ) } } } @@ -158,7 +184,8 @@ func LoadConfig(configFilePath string, loggerIn *log.Logger) (config *FullHatche } if data.Config.PayModelsDynamodbTable == "" { - data.Logger.Printf("Warning: no 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") + // data.Logger.Printf("Warning: no 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") + data.Logger.Warn("No 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") } for _, payModel := range data.Config.PayModels { diff --git a/hatchery/ec2.go b/hatchery/ec2.go index e5308d6e..909359ad 100644 --- a/hatchery/ec2.go +++ b/hatchery/ec2.go @@ -108,7 +108,13 @@ func (creds *CREDS) describeWorkspaceNetwork(userName string) (*NetworkInfo, err if err != nil { return nil, err } - Config.Logger.Printf("Create Security Group: %s", *newSecurityGroup.GroupId) + Config.Logger.Info("Creating security group", + "security_group_id", *newSecurityGroup.GroupId, + "security_group_name", createSecurityGroupInput.GroupName, + "vpc_id", createSecurityGroupInput.VpcId, + "description", createSecurityGroupInput.Description, + "username", userName, + ) ingressRules := ec2.AuthorizeSecurityGroupIngressInput{ GroupId: newSecurityGroup.GroupId, diff --git a/hatchery/ecs.go b/hatchery/ecs.go index c445fbc7..e8238787 100644 --- a/hatchery/ecs.go +++ b/hatchery/ecs.go @@ -128,13 +128,19 @@ func (sess *CREDS) findEcsCluster() (*ecs.Cluster, error) { } else { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. - Config.Logger.Println(err.Error()) + // Config.Logger.Println(err.Error()) + Config.Logger.Error("Error describing ECS cluster", + "clusterName", clusterName, + "error", err.Error(), + ) } } if len(describeClusterResult.Failures) > 0 { for _, failure := range describeClusterResult.Failures { if *failure.Reason == "MISSING" { - Config.Logger.Printf("ECS cluster named %s not found, trying to create this ECS cluster", clusterName) + Config.Logger.Debug("Provisioning ECS cluster as it was not found", + "clusterName", clusterName, + ) input := &ecs.CreateClusterInput{ ClusterName: aws.String(clusterName), Tags: []*ecs.Tag{ @@ -166,8 +172,11 @@ func (sess *CREDS) findEcsCluster() (*ecs.Cluster, error) { return describeClusterResult.Clusters[0], nil } } - Config.Logger.Printf("ECS cluster named %s cannot be described", clusterName) - return nil, fmt.Errorf("ECS cluster named %s cannot be described", clusterName) + Config.Logger.Error("Failed to describe ECS cluster.", + "clusterName", clusterName, + ) + + return nil, fmt.Errorf("Failed to describe ECS cluster.") } else { return describeClusterResult.Clusters[0], nil } @@ -208,7 +217,9 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce if statusMessage == "ACTIVE" && (*service.Services[0].RunningCount == *service.Services[0].DesiredCount) { taskDefName = *service.Services[0].TaskDefinition if taskDefName == "" { - Config.Logger.Printf("No task definition found for user %s", userName) + Config.Logger.Error("No task definition found for user.", + "userName", userName, + ) } else { desTaskDefOutput, err := sess.svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ TaskDefinition: &taskDefName, @@ -227,22 +238,38 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce lastActivityTime, err := getKernelIdleTimeWithContext(ctx, accessToken) status.LastActivityTime = lastActivityTime if err != nil { - Config.Logger.Println(err.Error()) + // Config.Logger.Println(err.Error()) + Config.Logger.Debug("Cannot get kernel idle time.", + "error", err.Error(), + "userName", userName, + ) } } else { - Config.Logger.Println(err.Error()) + // Config.Logger.Println(err.Error()) + Config.Logger.Debug("Cannot convert idle time limit to int.", + "error", err.Error(), + "userName", userName, + ) } break } if i == len(args)-1 { - Config.Logger.Printf("Unable to find kernel idle shutdown time in args\n") + Config.Logger.Error("Unable to find kernel idle shutdown time in args.", + "userName", userName, + ) } } } else { - Config.Logger.Printf("No env vars found for task definition %s\n", taskDefName) + Config.Logger.Error("No env vars found for task definition.", + "taskDefName", taskDefName, + "userName", userName, + ) } } else { - Config.Logger.Printf("No container definition found for task definition %s\n", taskDefName) + Config.Logger.Error("No container definition found for task definition.", + "taskDefName", taskDefName, + "userName", userName, + ) } } } @@ -261,7 +288,9 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce // Terminate workspace running in ECS // TODO: Make this terminate ALB as well. func terminateEcsWorkspace(ctx context.Context, userName string, accessToken string, awsAcctID string) (string, error) { - Config.Logger.Printf("Terminating ECS workspace for user %s", userName) + Config.Logger.Info("Terminating ECS workspace.", + "username", userName, + ) roleARN := "arn:aws:iam::" + awsAcctID + ":role/csoc_adminvm" sess := session.Must(session.NewSession(&aws.Config{ // TODO: Make this configurable @@ -289,7 +318,9 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str return "", errors.New("No service found for " + userName) } if taskDefName == "" { - Config.Logger.Printf("No task definition found for user %s, skipping API key deletion", userName) + Config.Logger.Info("No task definition found for user, skipping API key deletion.", + "userName", userName, + ) } else { desTaskDefOutput, err := svc.svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ TaskDefinition: &taskDefName, @@ -303,26 +334,40 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str if len(envVars) > 0 { for i, ev := range envVars { if *ev.Name == "API_KEY_ID" { - Config.Logger.Printf("Found mounted API key. Attempting to delete API Key with ID %s for user %s\n", *ev.Value, userName) + Config.Logger.Debug("Found mounted API key. Attempting to delete API Key.", + "username", userName, + ) err := deleteAPIKeyWithContext(ctx, accessToken, *ev.Value) if err != nil { - Config.Logger.Printf("Error occurred when deleting API Key with ID %s for user %s: %s\n", *ev.Value, userName, err.Error()) + Config.Logger.Error("Error occurred when deleting API Key.", + "username", userName, + "error", err.Error(), + ) } break } if i == len(envVars)-1 { - Config.Logger.Printf("Unable to find API Key ID in env vars for user %s\n", userName) + Config.Logger.Error("Unable to find API Key ID in env vars.", + "username", userName, + ) } } } else { - Config.Logger.Printf("No env vars found for task definition %s, skipping API key deletion\n", taskDefName) + Config.Logger.Info("Skipping API key deletion as no environment variables were found.", + "taskDefName", taskDefName, + "username", userName) } } else { - Config.Logger.Printf("No container definition found for task definition %s, skipping API key deletion\n", taskDefName) + Config.Logger.Info("Skipping API key deletion as no environment variables were found.", + "taskDefName", taskDefName, + "username", userName) } } // Terminate ECS service - Config.Logger.Printf("Terminating ECS service %s for user %s\n", svcName, userName) + Config.Logger.Info("Terminating ECS service.", + "svcName", svcName, + "username", userName, + ) delServiceOutput, err := svc.svc.DeleteService(&ecs.DeleteServiceInput{ Cluster: cluster.ClusterName, Force: aws.Bool(true), @@ -333,22 +378,33 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str } // Terminate load balancer - Config.Logger.Printf("Terminating load balancer for user %s\n", userName) + Config.Logger.Info("Terminating load balancer.", + "username", userName, + ) err = svc.terminateLoadBalancer(userName) if err != nil { - Config.Logger.Printf("Error occurred when terminating load balancer for user %s: %s\n", userName, err.Error()) + Config.Logger.Error("Failed to terminate load balancer.", + "username", userName, + "error", err.Error(), + ) } // Terminate target group err = svc.terminateLoadBalancerTargetGroup(userName) if err != nil { - Config.Logger.Printf("Error occurred when terminating load balancer target group for user %s: %s\n", userName, err.Error()) + Config.Logger.Error("Failed to terminate load balancer target group.", + "username", userName, + "error", err.Error(), + ) } // Terminate transit gateway err = teardownTransitGateway(userName) if err != nil { - Config.Logger.Printf("Error occurred when terminating transit gateway resources for user %s: %s\n", userName, err.Error()) + Config.Logger.Error("Failed to terminate transit gateway resources.", + "username", userName, + "error", err.Error(), + ) } return fmt.Sprintf("Service '%s' is in status: %s", userToResourceName(userName, "pod"), *delServiceOutput.Service.Status), nil } @@ -367,30 +423,51 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod mem, err := mem(hatchApp.MemoryLimit) if err != nil { // Log error and return without launching workspace - Config.Logger.Printf("Failed to launch ECS workspace for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to launch ECS workspace.", + "username", userName, + "error", err.Error(), + ) return err } cpu, err := cpu(hatchApp.CPULimit) if err != nil { // Log error and return without launching workspace - Config.Logger.Printf("Failed to launch ECS workspace for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to launch ECS workspace.", + "username", userName, + "error", err.Error(), + ) } // Make sure ECS cluster exists _, err = svc.launchEcsCluster(userName) if err != nil { - Config.Logger.Printf("Failed to launch ECS cluster for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to launch ECS cluster.", + "username", userName, + "error", err.Error(), + ) return err } // Get Gen3 API key to be used in workspace - Config.Logger.Printf("Creating API key for user %s", userName) + Config.Logger.Info("Creating API key.", + "username", userName, + ) apiKey, err := getAPIKeyWithContext(ctx, accessToken) if err != nil { - Config.Logger.Printf("Failed to create API key for user %v, Error: %v. Moving on but workspace won't have API key", userName, err) + Config.Logger.Error("Failed to create API key.", + "username", userName, + "error", err.Error(), + ) + Config.Logger.Info( + "Moving on but workspace won't have API key.", + "username", userName, + ) apiKey = &APIKeyStruct{} } else { - Config.Logger.Printf("Created API key for user %v, key ID: %v", userName, apiKey.KeyID) + Config.Logger.Info("Created API key.", + "username", userName, + "keyID", apiKey.KeyID, + ) } envVars := []EnvVar{} @@ -418,30 +495,47 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod Value: os.Getenv("GEN3_ENDPOINT"), }) - Config.Logger.Printf("Settign up EFS for user %s", userName) + Config.Logger.Info("Setting up EFS.", + "username", userName, + ) volumes, err := svc.EFSFileSystem(userName) if err != nil { - Config.Logger.Printf("Failed to set up EFS for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to set up EFS.", + "username", userName, + "error", err.Error(), + ) return err } - Config.Logger.Printf("Setting up task role for user %s", userName) + Config.Logger.Info("Setting up task role.", + "username", userName, + ) taskRole, err := svc.taskRole(userName) if err != nil { // Log the error - Config.Logger.Printf("Failed to set up task role for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to set up task role.", + "username", userName, + "error", err.Error(), + ) return err } - Config.Logger.Printf("Setting up execution role for user %s", userName) + Config.Logger.Info("Setting up task execution role.", + "username", userName, + ) _, err = svc.CreateEcsTaskExecutionRole() if err != nil { // Log the error - Config.Logger.Printf("Failed to set up execution role for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to set up task execution role.", + "username", userName, + "error", err.Error(), + ) return err } - Config.Logger.Printf("Setting up ECS task definition for user %s", userName) + Config.Logger.Info("Setting up ECS task definition.", + "username", userName, + ) taskDef := CreateTaskDefinitionInput{ Image: hatchApp.Image, Cpu: cpu, @@ -513,35 +607,57 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod taskDefResult, err := svc.CreateTaskDefinition(&taskDef, userName, hash, payModel.AWSAccountId) if err != nil { // Log the error - Config.Logger.Printf("Failed to set up task definition for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to set up task definition.", + "username", userName, + "error", err.Error(), + ) aerr := deleteAPIKeyWithContext(ctx, accessToken, apiKey.KeyID) if aerr != nil { - Config.Logger.Printf("Error occurred when deleting API Key with ID %s for user %s: %s\n", apiKey.KeyID, userName, err.Error()) + Config.Logger.Error("Error occurred when deleting API Key.", + "username", userName, + "error", err.Error(), + ) } return err } - Config.Logger.Printf("Launching ECS workspace service for user %s", userName) + Config.Logger.Info("Launching ECS workspace service.", + "username", userName, + ) launchTask, err := svc.launchService(ctx, taskDefResult, userName, hash, payModel) if err != nil { // Log the error - Config.Logger.Printf("Failed to launch ECS workspace service for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to launch ECS workspace service.", + "username", userName, + "error", err.Error(), + ) aerr := deleteAPIKeyWithContext(ctx, accessToken, apiKey.KeyID) if aerr != nil { - Config.Logger.Printf("Error occurred when deleting API Key with ID %s for user %s: %s\n", apiKey.KeyID, userName, err.Error()) + Config.Logger.Error("Error occurred when deleting API Key.", + "username", userName, + "error", err.Error(), + ) } return err } - Config.Logger.Printf("Setting up Transit Gateway for user %s", userName) + Config.Logger.Info("Setting up Transit Gateway.", + "username", userName, + ) err = setupTransitGateway(userName) if err != nil { // Log the error - Config.Logger.Printf("Failed to set up Transit Gateway for user %v, Error: %v", userName, err) + Config.Logger.Error("Failed to set up Transit Gateway.", + "username", userName, + "error", err.Error(), + ) return err } - Config.Logger.Printf("Launched ECS workspace service at %s for user %s\n", launchTask, userName) + Config.Logger.Info("Launched ECS workspace service.", + "username", userName, + "launchTask", launchTask, + ) return nil } @@ -590,17 +706,33 @@ func (sess *CREDS) launchService(ctx context.Context, taskDefArn string, userNam switch aerr.Code() { case ecs.ErrCodeInvalidParameterException: if aerr.Error() == "InvalidParameterException: Creation of service was not idempotent." { - Config.Logger.Print("Service already exists.. ") + // Config.Logger.Print("Service already exists.. ") + Config.Logger.Info("ECS Service already exists. Moving on.", + "username", userName, + ) return "", nil } } } - Config.Logger.Println(err.Error()) + // Config.Logger.Println(err.Error()) + Config.Logger.Error("Error occurred when creating ECS service.", + "username", userName, + "error", err.Error(), + ) return "", err } - Config.Logger.Printf("Service launched: %s", *result.Service.ClusterArn) + Config.Logger.Info("Service launched.", + "username", userName, + "service", *result.Service.ServiceName, + ) err = createLocalService(ctx, userName, hash, *loadBalancer.LoadBalancers[0].DNSName, payModel) if err != nil { + Config.Logger.Error("Error occurred when creating local kubernetes service (used by ambassador).", + "username", userName, + "loadbalancer", *loadBalancer.LoadBalancers[0].DNSName, + "paymodel", payModel.Name, + "error", err.Error(), + ) return "", err } return *loadBalancer.LoadBalancers[0].DNSName, nil @@ -611,7 +743,11 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa creds := sess.creds LogGroup, err := sess.CreateLogGroup(fmt.Sprintf("/hatchery/%s/", awsAcctID), creds) if err != nil { - Config.Logger.Printf("Failed to create/get LogGroup. Error: %s", err) + Config.Logger.Error("Failed to create/get LogGroup.", + "username", userName, + "loggroup", "/hatchery/"+awsAcctID+"/", + "error", err.Error(), + ) return "", err } svc := ecs.New(session.Must(session.NewSession(&aws.Config{ @@ -619,7 +755,9 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa Region: aws.String("us-east-1"), }))) - Config.Logger.Printf("Creating ECS task definition") + Config.Logger.Info("Creating ECS task definition.", + "username", userName, + ) logConfiguration := &ecs.LogConfiguration{ LogDriver: aws.String(ecs.LogDriverAwslogs), @@ -664,13 +802,21 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa if Config.Config.PrismaConfig.Enable { installBundle, err := getInstallBundle() if err != nil { - Config.Logger.Print(err, " error getting prisma install bundle") + // Config.Logger.Print(err, " error getting prisma install bundle") + Config.Logger.Error("Error occurred when getting prisma install bundle.", + "username", userName, + "error", err.Error(), + ) return "", err } image, err := getPrismaImage() if err != nil { - Config.Logger.Print(err, " error getting prisma image") + // Config.Logger.Print(err, " error getting prisma image") + Config.Logger.Error("Error occurred when getting prisma image.", + "username", userName, + "error", err.Error(), + ) return "", err } @@ -738,13 +884,20 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa ) if err != nil { - Config.Logger.Print(err, " Couldn't register ECS task definition") + // Config.Logger.Print(err, " Couldn't register ECS task definition") + Config.Logger.Error("Couldn't register ECS task definition.", + "username", userName, + "error", err.Error(), + ) return "", err } td := resp.TaskDefinition - Config.Logger.Printf("Created ECS task definition [%s:%d]", aws.StringValue(td.Family), aws.Int64Value(td.Revision)) + Config.Logger.Info("Created ECS task definition.", + "username", userName, + "taskdefinition", fmt.Sprintf("%s:%d", aws.StringValue(td.Family), aws.Int64Value(td.Revision)), + ) return aws.StringValue(td.TaskDefinitionArn), nil } diff --git a/hatchery/efs.go b/hatchery/efs.go index f3c3b6d6..9aacf61a 100644 --- a/hatchery/efs.go +++ b/hatchery/efs.go @@ -134,7 +134,12 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { exisitingFS, _ = creds.getEFSFileSystem(userName, svc) for *exisitingFS.FileSystems[0].LifeCycleState != "available" { - Config.Logger.Printf("EFS filesystem is in state: %s ... Waiting for 2 seconds", *exisitingFS.FileSystems[0].LifeCycleState) + Config.Logger.Info("Waiting for EFS filesystem to be available", + "FilesystemID", *exisitingFS.FileSystems[0].FileSystemId, + "FileSystemName", fsName, + "CreationToken", *exisitingFS.FileSystems[0].CreationToken, + "LifeCycleState", *exisitingFS.FileSystems[0].LifeCycleState, + ) // sleep for 2 sec time.Sleep(2 * time.Second) exisitingFS, _ = creds.getEFSFileSystem(userName, svc) @@ -143,14 +148,40 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { // Create mount target mountTarget, err := creds.createMountTarget(*result.FileSystemId, svc, userName) if err != nil { + // Log error + Config.Logger.Error("Failed to create EFS MountTarget", + "username", userName, + "FileSystemID", *result.FileSystemId, + "MountTargetID", *mountTarget.MountTargetId, + "MountTargetIP", *mountTarget.IpAddress, + "LifeCycleState", *mountTarget.LifeCycleState, + "error", err, + ) return nil, fmt.Errorf("Failed to create EFS MountTarget: %s", err) } - Config.Logger.Printf("MountTarget created: %s", *mountTarget.MountTargetId) + Config.Logger.Info("MountTarget created", + "username", userName, + "MountTargetID", *mountTarget.MountTargetId, + "FileSystemID", *mountTarget.FileSystemId, + "MountTargetIP", *mountTarget.IpAddress, + "LifeCycleState", *mountTarget.LifeCycleState, + ) accessPoint, err := creds.createAccessPoint(*result.FileSystemId, userName, svc) if err != nil { + // Log error + Config.Logger.Error("Failed to create EFS AccessPoint", + "username", userName, + "FileSystemID", *result.FileSystemId, + "AccessPointID", *accessPoint, + "error", err, + ) return nil, fmt.Errorf("Failed to create EFS AccessPoint: %s", err) } - Config.Logger.Printf("AccessPoint created: %s", *accessPoint) + Config.Logger.Info("EFS AccessPoint created", + "username", userName, + "FileSystemID", *result.FileSystemId, + "AccessPointID", *accessPoint, + ) return &EFS{ EFSArn: *result.FileSystemArn, @@ -187,7 +218,14 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { if err != nil { return nil, fmt.Errorf("Failed to create EFS MountTarget: %s", err) } - Config.Logger.Printf("MountTarget created: %s", *mountTarget.MountTargetId) + Config.Logger.Info("EFS MountTarget created", + "username", userName, + "MountTargetID", *mountTarget.MountTargetId, + "FileSystemID", *mountTarget.FileSystemId, + "MountTargetIP", *mountTarget.IpAddress, + "LifeCycleState", *mountTarget.LifeCycleState, + ) + } return &EFS{ diff --git a/hatchery/hatchery.go b/hatchery/hatchery.go index fe840979..18a7922e 100644 --- a/hatchery/hatchery.go +++ b/hatchery/hatchery.go @@ -224,7 +224,10 @@ func launch(w http.ResponseWriter, r *http.Request) { userName := getCurrentUserName(r) payModel, err := getCurrentPayModel(userName) if err != nil { - Config.Logger.Printf(err.Error()) + Config.Logger.Error("Failed to get current paymodel", + "error", err.Error(), + "username", userName, + ) } if payModel == nil || payModel.Local { err = createLocalK8sPod(r.Context(), hash, userName, accessToken) @@ -233,12 +236,19 @@ func launch(w http.ResponseWriter, r *http.Request) { if payModel.Status != "active" { // send 500 response. // TODO: 403 is the correct code, but it triggers a 302 to the default 403 page in revproxy instead of showing error message. - Config.Logger.Printf("Paymodel is not active. Launch forbidden for user %s", userName) + Config.Logger.Error("Launch forbidden: pay model is not active", + "username", userName, + "paymodelstatus", payModel.Status, + "paymodel", payModel, + ) http.Error(w, "Paymodel is not active. Launch forbidden", http.StatusInternalServerError) return } - Config.Logger.Printf("Launching ECS workspace for user %s", userName) + Config.Logger.Info("Launching ECS workspace", + "username", userName, + "paymodel", payModel.Name, + ) // Sending a 200 response straight away, but starting the launch in a goroutine // TODO: Do more sanity checks before returning 200. w.WriteHeader(http.StatusOK) @@ -249,7 +259,11 @@ func launch(w http.ResponseWriter, r *http.Request) { err = createExternalK8sPod(r.Context(), hash, userName, accessToken, *payModel) } if err != nil { - Config.Logger.Printf("error during launch: %-v", err) + Config.Logger.Error("Error during launch", + "error", err.Error(), + "username", userName, + "paymodel", payModel, + ) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -263,18 +277,26 @@ func terminate(w http.ResponseWriter, r *http.Request) { } accessToken := getBearerToken(r) userName := getCurrentUserName(r) - Config.Logger.Printf("Terminating workspace for user %s", userName) + payModel, err := getCurrentPayModel(userName) if err != nil { - Config.Logger.Printf(err.Error()) - } + Config.Logger.Error("Failed to get current paymodel", + "error", err.Error(), + "username", userName, + ) + } + Config.Logger.Info("Terminating workspace", + "username", userName, + ) if payModel != nil && payModel.Ecs { _, err := terminateEcsWorkspace(r.Context(), userName, accessToken, payModel.AWSAccountId) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } else { - Config.Logger.Printf("Succesfully terminated all resources related to ECS workspace for user %s", userName) + Config.Logger.Info("Succesfully terminated all resources related to ECS workspace", + "username", userName, + ) fmt.Fprintf(w, "Terminated ECS workspace") } } else { @@ -283,7 +305,10 @@ func terminate(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - Config.Logger.Printf("Terminated workspace for user %s", userName) + Config.Logger.Info("Terminated workspace", + "username", userName, + "paymodel", payModel, + ) fmt.Fprintf(w, "Terminated workspace") } } @@ -326,13 +351,22 @@ func createECSCluster(w http.ResponseWriter, r *http.Request) { var reader *strings.Reader if err != nil { reader = strings.NewReader(err.Error()) - Config.Logger.Printf("Error: %s", err) + Config.Logger.Error("Error creating ECS cluster", + "error", err.Error(), + "username", userName, + "paymodel", payModel, + ) } else { reader = strings.NewReader(result.String()) } + // TODO: What's happening here? _, err = io.Copy(w, reader) if err != nil { - Config.Logger.Printf("Error: %s", err) + Config.Logger.Error("Error copying response", + "error", err.Error(), + "username", userName, + "paymodel", payModel, + ) } } @@ -346,7 +380,11 @@ func statusEcs(ctx context.Context, userName string, accessToken string, awsAcct svc := NewSVC(sess, roleARN) result, err := svc.statusEcsWorkspace(ctx, userName, accessToken) if err != nil { - Config.Logger.Printf("Error: %s", err) + Config.Logger.Error("Error getting status of ECS workspace", + "error", err.Error(), + "username", userName, + "awsAcctID", awsAcctID, + ) return nil, err } return result, nil @@ -358,11 +396,19 @@ func launchEcsWorkspaceWrapper(userName string, hash string, accessToken string, err := launchEcsWorkspace(userName, hash, accessToken, payModel) if err != nil { - Config.Logger.Printf("Error: %s", err) + Config.Logger.Error("Error launching ECS workspace", + "error", err.Error(), + "username", userName, + "paymodel", payModel, + ) // Terminate ECS workspace if launch fails. _, err = terminateEcsWorkspace(context.Background(), userName, accessToken, payModel.AWSAccountId) if err != nil { - Config.Logger.Printf("Error: %s", err) + Config.Logger.Error("Error terminating ECS workspace", + "error", err.Error(), + "username", userName, + "paymodel", payModel, + ) } } } diff --git a/hatchery/paymodels.go b/hatchery/paymodels.go index 68fc036b..b9fd21bb 100644 --- a/hatchery/paymodels.go +++ b/hatchery/paymodels.go @@ -31,7 +31,9 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel } expr, err := expression.NewBuilder().WithFilter(filt).Build() if err != nil { - Config.Logger.Printf("Got error building expression: %s", err) + Config.Logger.Error("Got error building dyamoDB expression", + "error", err, + ) return nil, err } @@ -43,7 +45,9 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel } res, err := dynamodbSvc.Scan(params) if err != nil { - Config.Logger.Printf("Query API call failed: %s", err) + Config.Logger.Error("Failed to query dynamoDB", + "error", err, + ) return nil, err } @@ -51,7 +55,9 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel var payModelMap []PayModel err = dynamodbattribute.UnmarshalListOfMaps(res.Items, &payModelMap) if err != nil { - Config.Logger.Printf("Got error unmarshalling paymodels: %s", err) + Config.Logger.Error("Failed to unmarshal dynamoDB paymodels", + "error", err, + ) return nil, err } @@ -101,10 +107,6 @@ func getCurrentPayModel(userName string) (result *PayModel, err error) { // 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 } diff --git a/hatchery/pods.go b/hatchery/pods.go index 8b8e1145..a0e2278e 100644 --- a/hatchery/pods.go +++ b/hatchery/pods.go @@ -72,7 +72,11 @@ func getPodClient(ctx context.Context, userName string, payModelPtr *PayModel) ( if payModelPtr != nil && !(*payModelPtr).Local { podClient, err := NewEKSClientset(ctx, userName, *payModelPtr) if err != nil { - Config.Logger.Printf("Error fetching EKS kubeconfig: %v", err) + Config.Logger.Error("Error fetching EKS kubeconfig", + "error", err, + "userName", userName, + "payModel", *payModelPtr, + ) return nil, true, err } else { return podClient, true, nil @@ -87,13 +91,19 @@ func getLocalPodClient() corev1.CoreV1Interface { config, err := rest.InClusterConfig() config.WrapTransport = kubernetestrace.WrapRoundTripper if err != nil { - Config.Logger.Printf("Error creating in-cluster config: %v", err) + + Config.Logger.Error("Error creating in-cluster kubeconfig", + "error", err, + ) return nil } // creates the clientset clientset, err := kubernetes.NewForConfig(config) if err != nil { - Config.Logger.Printf("Error creating in-cluster clientset: %v", err) + + Config.Logger.Error("Error creating in-cluster clientset", + "error", err, + ) return nil } // Access jobs. We can't do it all in one line, since we need to receive the @@ -116,7 +126,12 @@ func NewEKSClientset(ctx context.Context, userName string, payModel PayModel) (c } result, err := eksSvc.DescribeClusterWithContext(ctx, input) if err != nil { - Config.Logger.Printf("Error calling DescribeCluster: %v", err) + + Config.Logger.Error("Error calling DescribeCluster", + "error", err, + "userName", userName, + "payModel", payModel, + ) return nil, err } cluster := result.Cluster @@ -188,10 +203,14 @@ func podStatus(ctx context.Context, userName string, accessToken string, payMode } err := podClient.Services(Config.Config.UserNamespace).Delete(ctx, serviceName, deleteOptions) if err != nil { - Config.Logger.Printf("Error deleting service. %s", err) + + Config.Logger.Error("Error deleting service", + "error", err, + "userName", userName, + "serviceName", serviceName, + ) } - 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" return &status, nil } else { @@ -265,7 +284,11 @@ func statusK8sPod(ctx context.Context, userName string, accessToken string, payM status, err := podStatus(ctx, userName, accessToken, payModelPtr) if err != nil { status.Status = fmt.Sprintf("%v", err) - Config.Logger.Printf("Error getting status: %v", err) + + Config.Logger.Error("Error getting status", + "error", err, + "userName", userName, + ) } return status, nil } @@ -365,7 +388,12 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str var envVars []k8sv1.EnvVar // a null image indicates a dockstore app - always mount user volume mountUserVolume := hatchApp.UserVolumeLocation != "" - hatchConfig.Logger.Printf("building pod '%v' for user '%v'", hatchApp.Name, userName) + + Config.Logger.Info("building pod", + "podName", podName, + "userName", userName, + "appName", hatchApp.Name, + ) for key, value := range hatchApp.Env { envVar := k8sv1.EnvVar{ @@ -375,8 +403,6 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str envVars = append(envVars, envVar) } - //hatchConfig.Logger.Printf("environment configured") - var sidecarEnvVars []k8sv1.EnvVar for key, value := range hatchConfig.Config.Sidecar.Env { envVar := k8sv1.EnvVar{ @@ -399,8 +425,6 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str Value: os.Getenv("GEN3_ENDPOINT"), }) - //hatchConfig.Logger.Printf("sidecar configured") - var lifeCycle = k8sv1.Lifecycle{} if hatchApp.LifecyclePreStop != nil && len(hatchApp.LifecyclePreStop) > 0 { lifeCycle.PreStop = &k8sv1.Handler{ @@ -417,8 +441,6 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str } } - //hatchConfig.Logger.Printf("lifecycle configured") - var securityContext = k8sv1.PodSecurityContext{} if hatchApp.UserUID != 0 { @@ -467,8 +489,6 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str }) } - //hatchConfig.Logger.Printf("volumes configured") - var pullPolicy k8sv1.PullPolicy switch hatchApp.PullPolicy { case "IfNotPresent": @@ -618,20 +638,34 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str } pod.Spec.Containers = append(pod.Spec.Containers, hatchApp.Friends...) - //hatchConfig.Logger.Printf("friends added") + return pod, nil } func createLocalK8sPod(ctx context.Context, hash string, userName string, accessToken string) error { hatchApp := Config.ContainersMap[hash] - Config.Logger.Printf("Creating a Local K8s Pod") + + Config.Logger.Info("Creating a Local kubernetes pod for workspace.", + "userName", userName, + "image", hatchApp.Image, + "cpulimit", hatchApp.CPULimit, + "memorylimit", hatchApp.MemoryLimit, + ) var extraVars []k8sv1.EnvVar apiKey, err := getAPIKeyWithContext(ctx, accessToken) if err != nil { - Config.Logger.Printf("Failed to get API key for user %v, Error: %v", userName, err) + + Config.Logger.Error("Failed to get API key.", + "userName", userName, + "error", err, + ) return err } - Config.Logger.Printf("Created API key for user %v, key ID: %v", userName, apiKey.KeyID) + + Config.Logger.Info("Created API key.", + "userName", userName, + "keyID", apiKey.KeyID, + ) extraVars = append(extraVars, k8sv1.EnvVar{ Name: "API_KEY", @@ -644,13 +678,20 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access pod, err := buildPod(Config, &hatchApp, userName, extraVars) if err != nil { - Config.Logger.Printf("Failed to configure pod for launch for user %v, Error: %v", userName, err) + + Config.Logger.Error("Failed to configure pod for launch.", + "username", userName, + "error", err, + ) return err } podName := userToResourceName(userName, "pod") podClient, _, err := getPodClient(ctx, userName, nil) if err != nil { - Config.Logger.Panicf("Error in createLocalK8sPod: %v", err) + Config.Logger.Error("Failed to get pod client.", + "error", err, + "username", userName, + ) return err } // a null image indicates a dockstore app - always mount user volume @@ -660,7 +701,10 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Get(ctx, claimName, metav1.GetOptions{}) if err != nil { - Config.Logger.Printf("Creating PersistentVolumeClaim %s.\n", claimName) + Config.Logger.Debug("Creating PersistentVolumeClaim.", + "claimName", claimName, + "username", userName, + ) pvc := &k8sv1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ Name: claimName, @@ -678,7 +722,11 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access } _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Create(ctx, pvc, metav1.CreateOptions{}) if err != nil { - Config.Logger.Printf("Failed to create PVC %s. Error: %s\n", claimName, err) + + Config.Logger.Error("Failed to create PVC.", + "claimName", claimName, + "error", err, + ) return err } } @@ -686,11 +734,25 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access _, err = podClient.Pods(Config.Config.UserNamespace).Create(ctx, pod, metav1.CreateOptions{}) if err != nil { - Config.Logger.Printf("Failed to launch pod %s for user %s. Image: %s, CPU %s, Memory %s. Error: %s\n", hatchApp.Name, userName, hatchApp.Image, hatchApp.CPULimit, hatchApp.MemoryLimit, err) + + Config.Logger.Error("Failed to launch pod.", + "podName", hatchApp.Name, + "username", userName, + "image", hatchApp.Image, + "cpulimit", hatchApp.CPULimit, + "memorylimit", hatchApp.MemoryLimit, + "error", err, + ) return err } - Config.Logger.Printf("Launched pod %s for user %s. Image: %s, CPU %s, Memory %s\n", hatchApp.Name, userName, hatchApp.Image, hatchApp.CPULimit, hatchApp.MemoryLimit) + Config.Logger.Info("Launched pod.", + "podName", hatchApp.Name, + "username", userName, + "image", hatchApp.Image, + "cpulimit", hatchApp.CPULimit, + "memorylimit", hatchApp.MemoryLimit, + ) serviceName := userToResourceName(userName, "service") labelsService := make(map[string]string) @@ -747,19 +809,37 @@ 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") + + Config.Logger.Info("Creating a workspace pod in external kubernetes cluster.", + "username", userName, + "image", hatchApp.Image, + "cpulimit", hatchApp.CPULimit, + "memorylimit", hatchApp.MemoryLimit, + ) podClient, err := NewEKSClientset(ctx, userName, payModel) if err != nil { - Config.Logger.Printf("Failed to create pod client for user %v, Error: %v", userName, err) + + Config.Logger.Error("Failed to create external pod client.", + "username", userName, + "error", err, + ) return err } apiKey, err := getAPIKeyWithContext(ctx, accessToken) if err != nil { - Config.Logger.Printf("Failed to get API key for user %v, Error: %v", userName, err) + + Config.Logger.Error("Failed to get API key.", + "username", userName, + "error", err, + ) return err } - Config.Logger.Printf("Created API key for user %v, key ID: %v", userName, apiKey.KeyID) + + Config.Logger.Info("Created API key.", + "username", userName, + "keyID", apiKey.KeyID, + ) // Check if NS exists in external cluster, if not create it. ns, err := podClient.Namespaces().Get(ctx, Config.Config.UserNamespace, metav1.GetOptions{}) @@ -771,9 +851,18 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc } _, err = podClient.Namespaces().Create(ctx, nsName, metav1.CreateOptions{}) if err != nil { - Config.Logger.Printf("Error occurred when creating namespace: %s", err) + + Config.Logger.Error("Failed to create namespace.", + "namespace", Config.Config.UserNamespace, + "username", userName, + "error", err, + ) } else { - Config.Logger.Printf("Namespace created: %v", ns) + + Config.Logger.Info("Created namespace.", + "namespace", ns, + "username", userName, + ) } } @@ -799,7 +888,11 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc pod, err := buildPod(Config, &hatchApp, userName, extraVars) if err != nil { - Config.Logger.Printf("Failed to configure pod for launch for user %v, Error: %v", userName, err) + + Config.Logger.Error("Failed to configure pod for launch.", + "username", userName, + "error", err, + ) return err } podName := userToResourceName(userName, "pod") @@ -810,7 +903,11 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Get(ctx, claimName, metav1.GetOptions{}) if err != nil { - Config.Logger.Printf("Creating PersistentVolumeClaim %s.\n", claimName) + + Config.Logger.Info("Creating PersistentVolumeClaim.", + "claimName", claimName, + "username", userName, + ) pvc := &k8sv1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ Name: claimName, @@ -829,7 +926,12 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Create(ctx, pvc, metav1.CreateOptions{}) if err != nil { - Config.Logger.Printf("Failed to create PVC %s. Error: %s\n", claimName, err) + + Config.Logger.Error("Failed to create PVC.", + "claimName", claimName, + "username", userName, + "error", err, + ) return err } } @@ -837,11 +939,25 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err = podClient.Pods(Config.Config.UserNamespace).Create(ctx, pod, metav1.CreateOptions{}) if err != nil { - Config.Logger.Printf("Failed to launch pod %s for user %s. Image: %s, CPU %s, Memory %s. Error: %s\n", hatchApp.Name, userName, hatchApp.Image, hatchApp.CPULimit, hatchApp.MemoryLimit, err) + + Config.Logger.Error("Failed to launch pod.", + "podName", podName, + "username", userName, + "image", hatchApp.Image, + "cpu", hatchApp.CPULimit, + "memory", hatchApp.MemoryLimit, + "error", err, + ) return err } - Config.Logger.Printf("Launched pod %s for user %s. Image: %s, CPU %s, Memory %s\n", hatchApp.Name, userName, hatchApp.Image, hatchApp.CPULimit, hatchApp.MemoryLimit) + Config.Logger.Info("Launched pod.", + "podName", podName, + "username", userName, + "image", hatchApp.Image, + "cpu", hatchApp.CPULimit, + "memory", hatchApp.MemoryLimit, + ) serviceName := userToResourceName(userName, "service") labelsService := make(map[string]string) @@ -893,7 +1009,11 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc return err } - Config.Logger.Printf("Launched service %s for user %s forwarding port %d\n", serviceName, userName, hatchApp.TargetPort) + Config.Logger.Info("Launched service.", + "serviceName", serviceName, + "username", userName, + "port", hatchApp.TargetPort, + ) nodes, _ := podClient.Nodes().List(context.TODO(), metav1.ListOptions{}) NodeIP := nodes.Items[0].Status.Addresses[0].Address @@ -991,6 +1111,10 @@ tls: %s return err } - Config.Logger.Printf("Launched local service %s for user %s forwarding port %d\n", serviceName, userName, hatchApp.TargetPort) + Config.Logger.Info("Launched local service.", + "serviceName", serviceName, + "username", userName, + "port", hatchApp.TargetPort, + ) return nil } diff --git a/hatchery/prisma.go b/hatchery/prisma.go index dcf4eeea..51dc5f1a 100644 --- a/hatchery/prisma.go +++ b/hatchery/prisma.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io/ioutil" "net/http" "os" @@ -34,7 +33,9 @@ func getPrismaToken(username string, password string) (*string, error) { defer resp.Body.Close() if resp.StatusCode != 200 { b, _ := ioutil.ReadAll(resp.Body) - Config.Logger.Print(string(b)) + Config.Logger.Error("Error authenticating with Prisma Cloud", + "error", string(b), + ) return nil, errors.New("Error authenticating with Prisma Cloud: " + string(b)) } //We Read the response body on the line below. @@ -45,7 +46,11 @@ func getPrismaToken(username string, password string) (*string, error) { var result Token if err := json.Unmarshal(body, &result); err != nil { - fmt.Println("Invalid response from prisma auth endpoint: " + authEndpoint) + // fmt.Println("Invalid response from prisma auth endpoint: " + authEndpoint) + Config.Logger.Error("Invalid response from prisma auth endpoint", + "error", err, + "endpoint", authEndpoint, + ) } return &result.Token, nil @@ -78,7 +83,10 @@ func getInstallBundle() (*InstallBundle, error) { defer resp.Body.Close() if resp.StatusCode != 200 { b, _ := ioutil.ReadAll(resp.Body) - Config.Logger.Print(string(b)) + // Config.Logger.Print(string(b)) + Config.Logger.Error("Error getting prismacloud install bundle", + "error", string(b), + ) return nil, errors.New("Error getting install bundle: " + string(b)) } //We Read the response body on the line below. @@ -88,7 +96,11 @@ func getInstallBundle() (*InstallBundle, error) { } var result InstallBundle if err := json.Unmarshal(body, &result); err != nil { - fmt.Println("Invalid response from prisma install_bundle endpoint: " + installBundleEndpoint) + // fmt.Println("Invalid response from prisma install_bundle endpoint: " + installBundleEndpoint) + Config.Logger.Error("Invalid response from prisma install_bundle endpoint", + "error", err, + "endpoint", installBundleEndpoint, + ) } return &result, nil } @@ -120,8 +132,10 @@ func getPrismaImage() (*string, error) { defer resp.Body.Close() if resp.StatusCode != 200 { b, _ := ioutil.ReadAll(resp.Body) - Config.Logger.Print(string(b)) - return nil, errors.New("Error getting install bundle: " + string(b)) + Config.Logger.Error("Error getting prismacloud image name", + "error", string(b), + ) + return nil, errors.New("Error getting prismacloud image name: " + string(b)) } //We Read the response body on the line below. body, err := ioutil.ReadAll(resp.Body) diff --git a/hatchery/ram.go b/hatchery/ram.go index 7cbdb278..629f5542 100644 --- a/hatchery/ram.go +++ b/hatchery/ram.go @@ -16,7 +16,13 @@ func acceptTransitGatewayShare(pm *PayModel, userName string, sess *session.Sess err := svc.acceptTGWShare(ramArn) if err != nil { // Log error - Config.Logger.Printf(err.Error()) + + Config.Logger.Error("Failed to accept transitgateway share", + "error", err, + "username", userName, + "paymodel", pm, + "ram-share-arn", ramArn, + ) return err } return nil @@ -36,14 +42,17 @@ func (creds *CREDS) acceptTGWShare(ramArn *string) error { } resourceShareInvitation, err := svc.GetResourceShareInvitations(ramInvitationInput) if err != nil { - // Log error - Config.Logger.Printf(err.Error()) + Config.Logger.Error("Failed to get resource share invitation", + "error", err, + "ram-share-arn", ramArn, + ) return err } if len(resourceShareInvitation.ResourceShareInvitations) == 0 { - // Log that there are no invitations - Config.Logger.Printf("No invitations found something fishy is going on") + Config.Logger.Warn("No invitations found for RAM share", + "ram-share-arn", ramArn, + ) return nil } else { if *resourceShareInvitation.ResourceShareInvitations[0].Status != "ACCEPTED" { @@ -53,12 +62,17 @@ func (creds *CREDS) acceptTGWShare(ramArn *string) error { if err != nil { return err } - // Log that invitation was accepted - Config.Logger.Printf("Resource share invitation accepted") + + Config.Logger.Debug("Resource share invitation accepted", + "ram-share-arn", ramArn, + ) return nil } // Log that invitation was already accepted - Config.Logger.Printf("Resource share invitation already accepted") + + Config.Logger.Debug("Resource share invitation already accepted", + "ram-share-arn", ramArn, + ) return nil } } @@ -80,7 +94,11 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri return nil, err } if len(exRs.ResourceShares) == 0 { - Config.Logger.Printf("Did not find existing resource share, creating a resource share") + Config.Logger.Info("Resource share not found, creating new resource share", + "ramName", ramName, + "tgwArn", tgwArn, + "accountId", accountid) + resourceShareInput := &ram.CreateResourceShareInput{ // Indicates whether principals outside your organization in Organizations can // be associated with a resource share. @@ -105,7 +123,11 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri } return resourceShare.ResourceShare.ResourceShareArn, nil } else { - Config.Logger.Printf("Found existing resource share, associating resource share with account") + Config.Logger.Info("Existing resource share found. Associating resource share with account.", + "ramName", ramName, + "tgwArn", tgwArn, + "accountId", accountid) + listResourcesInput := &ram.ListResourcesInput{ ResourceOwner: aws.String("SELF"), ResourceArns: []*string{&tgwArn}, @@ -122,7 +144,11 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri } listPrincipals, err := svc.ListPrincipals(listPrincipalsInput) if err != nil { - Config.Logger.Printf("failed to ListPrincipals: %s", listPrincipalsInput) + + Config.Logger.Error("failed to ListPrincipals", + "listPrincipalsInput", listPrincipalsInput, + "error", err, + ) return nil, fmt.Errorf("failed to ListPrincipals: %s", err) } if len(listPrincipals.Principals) == 0 || len(listResources.Resources) == 0 { @@ -136,7 +162,10 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri return nil, err } } else { - Config.Logger.Printf("TransitGateway is already shared with AWS account %s ", *listPrincipals.Principals[0].Id) + + Config.Logger.Debug("TransitGateway is already shared with AWS account", + "accountId", *listPrincipals.Principals[0].Id, + ) } return exRs.ResourceShares[len(exRs.ResourceShares)-1].ResourceShareArn, nil } diff --git a/hatchery/transitgateway.go b/hatchery/transitgateway.go index 52480b9d..e01ce96b 100644 --- a/hatchery/transitgateway.go +++ b/hatchery/transitgateway.go @@ -25,7 +25,9 @@ func setupTransitGateway(userName string) error { return err } - Config.Logger.Printf("Setting up transit gateway in main account") + Config.Logger.Info("Setting up transit gateway in main account", + "username", userName, + ) tgwid, tgwarn, tgwRouteTableId, err := createTransitGateway(sess, userName) if err != nil { return fmt.Errorf("error creating transit gateway: %s", err.Error()) @@ -50,7 +52,10 @@ func setupTransitGateway(userName string) error { return err } - Config.Logger.Printf("Setting up remote account ") + Config.Logger.Info("Setting up remote account", + "username", userName, + "paymodel", pm.Name, + ) err = setupRemoteAccount(userName, false) if err != nil { return fmt.Errorf("failed to setup remote account: %s", err.Error()) @@ -69,18 +74,27 @@ func createLocalTransitGatewayAttachment(userName string, tgwid string, tgwRoute // ec2 session to main AWS account. ec2Local := ec2.New(sess) // Create Transit Gateway Attachment in local VPC - // Config.Logger.Printf("Creating tgw attachment in local VPC: %s", vpcid) + Config.Logger.Info("Creating transitgateway attachment in local VPC", + "username", userName, + "vpcid", vpcid, + ) tgwAttachment, err := createTransitGatewayAttachments(ec2Local, vpcid, tgwid, true, nil, userName) if err != nil { return err } - Config.Logger.Printf("Attachment created: %s", *tgwAttachment) + + Config.Logger.Info("Transit Gateway attachment created", + "username", userName, + "attachment", *tgwAttachment, + ) // Create Transit Gateway Route Table _, err = TGWRoutes(userName, tgwRouteTableId, tgwAttachment, ec2Local, true, false, nil) if err != nil { - // Log error - Config.Logger.Printf("Failed to create TGW route table: %s", err.Error()) + Config.Logger.Error("Failed to set up transit gateway route table", + "username", userName, + "error", err.Error(), + ) return err } @@ -88,7 +102,10 @@ func createLocalTransitGatewayAttachment(userName string, tgwid string, tgwRoute } func teardownTransitGateway(userName string) error { - Config.Logger.Printf("Terminating remote transit gateway attachment for user %s\n", userName) + + Config.Logger.Info("Terminating remote transit gateway attachment", + "username", userName, + ) err := setupRemoteAccount(userName, true) if err != nil { return err @@ -183,7 +200,8 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string // Create Transit Gateway if it doesn't exist if len(exTg.TransitGateways) == 0 { - Config.Logger.Printf("No transit gateway found. Creating one...") + + Config.Logger.Info("No existing transit gateway found. Creating a new transit gateway...") tgwName := strings.ReplaceAll(os.Getenv("GEN3_ENDPOINT"), ".", "-") + "-tgw" tg, err := ec2Local.CreateTransitGateway(&ec2.CreateTransitGatewayInput{ DryRun: aws.Bool(false), @@ -211,17 +229,29 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string if err != nil { return nil, nil, nil, err } - Config.Logger.Printf("Transit gateway created: %s", *tg.TransitGateway.TransitGatewayId) + + Config.Logger.Info("Transit gateway created", + "transit_gateway_id", *tg.TransitGateway.TransitGatewayId, + "username", userName, + ) return tg.TransitGateway.TransitGatewayId, tg.TransitGateway.TransitGatewayArn, tg.TransitGateway.Options.AssociationDefaultRouteTableId, nil } else { - Config.Logger.Print("Existing transit gateway found. Skipping creation...") + // Config.Logger.Print("Existing transit gateway found. Skipping creation...") + Config.Logger.Info("Existing transit gateway found. Skipping creation...", + "transit_gateway_id", *exTg.TransitGateways[len(exTg.TransitGateways)-1].TransitGatewayId, + "username", userName, + ) return exTg.TransitGateways[len(exTg.TransitGateways)-1].TransitGatewayId, exTg.TransitGateways[len(exTg.TransitGateways)-1].TransitGatewayArn, exTg.TransitGateways[len(exTg.TransitGateways)-1].Options.AssociationDefaultRouteTableId, nil } } func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, local bool, sess *CREDS, userName string) (*string, error) { - Config.Logger.Printf("Creating transit gateway attachment for VPC: %s", vpcid) + + Config.Logger.Info("Creating transit gateway attachment for VPC", + "vpc_id", vpcid, + "username", userName, + ) // Check for existing transit gateway tgInput := &ec2.DescribeTransitGatewaysInput{ TransitGatewayIds: []*string{aws.String(tgwid)}, @@ -250,7 +280,10 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l return nil, err } for *exTg.TransitGateways[0].State != "available" { - Config.Logger.Printf("TransitGateway is in state: %s ... Waiting for 10 seconds", *exTg.TransitGateways[0].State) + Config.Logger.Info("TransitGateway is not ready yet. Waiting for 10 seconds...", + "state", *exTg.TransitGateways[0].State, + "username", userName, + ) // sleep for 10 sec time.Sleep(10 * time.Second) exTg, _ = svc.DescribeTransitGateways(tgInput) @@ -296,7 +329,6 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l } if len(exTgwAttachment.TransitGatewayAttachments) == 0 { // Create the transit gateway attachment - Config.Logger.Printf("Local transitgateway attachment not found, creating new one") tgwAttachmentInput := &ec2.CreateTransitGatewayVpcAttachmentInput{ TransitGatewayId: exTg.TransitGateways[0].TransitGatewayId, VpcId: networkInfo.vpc.Vpcs[len(networkInfo.vpc.Vpcs)-1].VpcId, @@ -316,6 +348,9 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l }, }, } + Config.Logger.Info("Creating a new local transit gateway attachment as none was found.", + "username", userName, + ) for i := range networkInfo.subnets.Subnets { tgwAttachmentInput.SubnetIds = append(tgwAttachmentInput.SubnetIds, networkInfo.subnets.Subnets[i].SubnetId) } @@ -323,10 +358,18 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l if err != nil { return nil, fmt.Errorf("cannot create transitgatewayattachment: %s", err.Error()) } - Config.Logger.Printf("Created transitgatewayattachment: %s", *tgwAttachment.TransitGatewayVpcAttachment.TransitGatewayAttachmentId) + + Config.Logger.Info("Created transitgatewayattachment", + "username", userName, + "transitGatewayAttachment", *tgwAttachment.TransitGatewayVpcAttachment.TransitGatewayAttachmentId, + ) return tgwAttachment.TransitGatewayVpcAttachment.TransitGatewayAttachmentId, nil } else { - Config.Logger.Printf("Local transitgateway attachment found, using existing one") + + Config.Logger.Info("Using existing local transit gateway attachment", + "username", userName, + "transitGatewayAttachment", *exTgwAttachment.TransitGatewayAttachments[0].TransitGatewayAttachmentId, + ) return exTgwAttachment.TransitGatewayAttachments[0].TransitGatewayAttachmentId, nil } } @@ -359,7 +402,6 @@ func deleteTransitGatewayAttachment(svc *ec2.EC2, tgwid string, userName string) } exTgwAttachment, err := svc.DescribeTransitGatewayAttachments(exTgwAttachmentInput) if err != nil { - Config.Logger.Printf("Error: %s", err.Error()) if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case "InvalidTransitGatewayID.NotFound": @@ -367,13 +409,21 @@ func deleteTransitGatewayAttachment(svc *ec2.EC2, tgwid string, userName string) return nil, nil } } else { - Config.Logger.Printf("Error: %s", err.Error()) + + Config.Logger.Error("Error describing transit gateway attachment", + "username", userName, + "error", err.Error(), + ) return nil, err } } if len(exTgwAttachment.TransitGatewayAttachments) == 0 { // No transit gateway attachment found, we are happy :) - Config.Logger.Printf("No TGW attachment found, we are happy :)") + + Config.Logger.Debug("No transit gateway attachment found. We are happy :)", + "username", userName, + ) + return nil, nil } @@ -424,7 +474,10 @@ func setupRemoteAccount(userName string, teardown bool) error { return err } for len(exTg.TransitGateways) == 0 { - Config.Logger.Printf("Waiting to find ex_tgw") + + Config.Logger.Warn("Waiting to find ex_tgw", + "username", userName, + ) // err := svc.acceptTGWShare() // if err != nil { // return err @@ -454,9 +507,17 @@ func setupRemoteAccount(userName string, teardown bool) error { } else { tgw_attachment, err = createTransitGatewayAttachments(ec2Remote, *vpc.Vpcs[0].VpcId, *exTg.TransitGateways[0].TransitGatewayId, false, &svc, userName) if err != nil { + Config.Logger.Error("Cannot create remote TransitGatewayAttachment", + "username", userName, + "error", err.Error(), + ) return fmt.Errorf("Cannot create remote TransitGatewayAttachment: %s", err.Error()) } - Config.Logger.Printf("tgw_attachment: %s", *tgw_attachment) + + Config.Logger.Debug("tgw_attachment", + "username", userName, + "tgw_attachment", *tgw_attachment, + ) } // setup Transit Gateway Route Table @@ -515,7 +576,11 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string return delRouteInput.TransitGatewayRouteTableId, nil } else { for *tgwAttachment.TransitGatewayAttachments[0].State != "available" { - Config.Logger.Printf("Transit Gateway Attachment is not ready. State is: %s", *tgwAttachment.TransitGatewayAttachments[0].State) + + Config.Logger.Warn("Transit Gateway Attachment is not ready", + "username", userName, + "state", *tgwAttachment.TransitGatewayAttachments[0].State, + ) tgwAttachment, err = svc.DescribeTransitGatewayAttachments(tgwAttachmentInput) if err != nil { return nil, err @@ -535,7 +600,11 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string exRoutes, err := svc.SearchTransitGatewayRoutes(exRoutesInput) if err != nil { // log error - Config.Logger.Printf("error SearchTransitGatewayRoutes: %s", err.Error()) + + Config.Logger.Error("error SearchTransitGatewayRoutes", + "username", userName, + "error", err.Error(), + ) return nil, err } @@ -543,7 +612,12 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string if local { // Delete route only if it's blackhole route if *exRoutes.Routes[0].State == "blackhole" { - Config.Logger.Printf("Route is blackhole, deleting") + + Config.Logger.Info("Route is blackhole, deleting", + "username", userName, + "route", *exRoutes.Routes[0].DestinationCidrBlock, + "state", *exRoutes.Routes[0].State, + ) delRouteInput := &ec2.DeleteTransitGatewayRouteInput{ DestinationCidrBlock: networkInfo.vpc.Vpcs[0].CidrBlock, TransitGatewayRouteTableId: tgwRoutetableId, @@ -553,7 +627,10 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string return nil, err } } else { - Config.Logger.Printf("Route already exists for %s", *networkInfo.vpc.Vpcs[0].CidrBlock) + Config.Logger.Info("Route already exists", + "username", userName, + "route", *networkInfo.vpc.Vpcs[0].CidrBlock, + ) return exRoutesInput.TransitGatewayRouteTableId, nil } } else { @@ -614,11 +691,16 @@ func VPCRoutes(remote_network_info *NetworkInfo, main_network_info *NetworkInfo, TransitGatewayId: tgwId, } - remoteRoute, err := ec2_remote.CreateRoute(remoteCreateRouteInput) + _, err = ec2_remote.CreateRoute(remoteCreateRouteInput) if err != nil { return err } - Config.Logger.Printf("Route added to remote VPC. %s", remoteRoute) + + Config.Logger.Info("Route added to remote VPC", + "routeTableId", *remote_network_info.routeTable.RouteTables[0].RouteTableId, + "destinationCidrBlock", *remote_network_info.vpc.Vpcs[0].CidrBlock, + "transitGatewayId", *tgwId, + ) localCreateRouteInput := &ec2.CreateRouteInput{ DestinationCidrBlock: remote_network_info.vpc.Vpcs[0].CidrBlock, @@ -630,11 +712,19 @@ func VPCRoutes(remote_network_info *NetworkInfo, main_network_info *NetworkInfo, if err != nil { return err } - Config.Logger.Printf("Route added to local VPC. %s", localRoute) + + Config.Logger.Info("Route added to local VPC", + "routeTableId", *main_network_info.routeTable.RouteTables[0].RouteTableId, + "destinationCidrBlock", *remote_network_info.vpc.Vpcs[0].CidrBlock, + "transitGatewayId", *tgwId, + "localRoute", localRoute, + ) return nil } else { - // Delete Routes for VPC - Config.Logger.Printf("Deleting Routes for remote VPC %s", *remote_network_info.vpc.Vpcs[0].VpcId) + Config.Logger.Debug("Deleting Routes for remote VPC", + "vpcId", *remote_network_info.vpc.Vpcs[0].VpcId, + "cidrBlock", *remote_network_info.vpc.Vpcs[0].CidrBlock, + ) remoteDeleteRouteInput := &ec2.DeleteRouteInput{ DestinationCidrBlock: main_network_info.vpc.Vpcs[0].CidrBlock, RouteTableId: remote_network_info.routeTable.RouteTables[0].RouteTableId, diff --git a/hatchery/vpc.go b/hatchery/vpc.go index 21aa049e..d94c6ad4 100644 --- a/hatchery/vpc.go +++ b/hatchery/vpc.go @@ -12,12 +12,20 @@ import ( ) func setupVPC(userName string) (*string, error) { - Config.Logger.Printf("Setting up VPC for user %s", userName) + pm, err := getCurrentPayModel(userName) if err != nil { + Config.Logger.Error("Failed to get current paymodel", + "error", err, + "username", userName, + ) return nil, err } - + Config.Logger.Info("Setting up remote VPC", + "username", userName, + "paymodel", pm.Name, + "awsAccount", pm.AWSAccountId, + ) roleARN := "arn:aws:iam::" + pm.AWSAccountId + ":role/csoc_adminvm" sess := session.Must(session.NewSession(&aws.Config{ // TODO: Make this configurable @@ -41,7 +49,7 @@ func setupVPC(userName string) (*string, error) { } subnetString := subnet.String() - Config.Logger.Printf("Using subnet: %s for user %s. Make sure this does not overlap with other users", subnetString, userName) + Config.Logger.Warnf("Using subnet: %s for user %s. Make sure this does not overlap with other users in dynamodb, as that will cause routing issues.", subnetString, userName) // VPC stuff vpcname := userToResourceName(userName, "service") + "-" + strings.ReplaceAll(os.Getenv("GEN3_ENDPOINT"), ".", "-") + "-vpc" @@ -71,7 +79,11 @@ func setupVPC(userName string) (*string, error) { if err != nil { return nil, err } - Config.Logger.Printf("VPC created in remote account") + + Config.Logger.Info("VPC created in remote account", + "username", userName, + "vpcid", *vpc.Vpc.VpcId, + ) _, err = createInternetGW(vpcname, *vpc.Vpc.VpcId, ec2Remote) if err != nil { return nil, err @@ -148,7 +160,6 @@ func createSubnet(vpccidr string, vpcid string, svc *ec2.EC2) error { panic(err) } - Config.Logger.Print(cidrs) createSubnet1Input := &ec2.CreateSubnetInput{ CidrBlock: aws.String(subnet1Cidr.String()), //TODO: Make this configurable ? @@ -163,10 +174,20 @@ func createSubnet(vpccidr string, vpcid string, svc *ec2.EC2) error { } _, err = svc.CreateSubnet(createSubnet1Input) if err != nil { + Config.Logger.Error("Error creating subnet 1", + "error", err, + "subnet", subnet1Cidr.String(), + "vpcid", vpcid, + ) return err } _, err = svc.CreateSubnet(createSubnet2Input) if err != nil { + Config.Logger.Error("Error creating subnet 2", + "error", err, + "subnet", subnet2Cidr.String(), + "vpcid", vpcid, + ) return err } return nil @@ -174,7 +195,12 @@ func createSubnet(vpccidr string, vpcid string, svc *ec2.EC2) error { } func createInternetGW(name string, vpcid string, svc *ec2.EC2) (*string, error) { - Config.Logger.Printf("Setting up internet Gateway for VPC: %s", vpcid) + + Config.Logger.Info("Setting up internet Gateway for VPC", + "vpcid", vpcid, + "name", name, + ) + describeInternetGWInput := &ec2.DescribeInternetGatewaysInput{ Filters: []*ec2.Filter{ { @@ -188,7 +214,10 @@ func createInternetGW(name string, vpcid string, svc *ec2.EC2) (*string, error) return nil, err } if len(exIgw.InternetGateways) == 0 { - Config.Logger.Printf("No existing gateways found. Creating internet gateway for VPC: %s", vpcid) + + Config.Logger.Info("No existing internet gateways found. Creating internet gateway for VPC", + "vpcid", vpcid, + ) createInternetGWInput := &ec2.CreateInternetGatewayInput{ TagSpecifications: []*ec2.TagSpecification{ { @@ -236,11 +265,16 @@ func createInternetGW(name string, vpcid string, svc *ec2.EC2) (*string, error) if err != nil { return nil, err } - Config.Logger.Printf("Route: %s", route) + Config.Logger.Info("Route to internet created", + "route", route, + ) return igw.InternetGateway.InternetGatewayId, nil } else { if len(exIgw.InternetGateways[0].Attachments) == 0 { - Config.Logger.Printf("Existing gateway found but not attached to IGW. Attaching internet gateway for VPC: %s", vpcid) + + Config.Logger.Info("Existing gateway found but not attached to IGW. Attaching internet gateway for VPC", + "vpcid", vpcid, + ) _, err = svc.AttachInternetGateway(&ec2.AttachInternetGatewayInput{ InternetGatewayId: exIgw.InternetGateways[0].InternetGatewayId, VpcId: &vpcid, diff --git a/main.go b/main.go index 2d658d46..a479b145 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "errors" - "fmt" "log" "net/http" "os" @@ -10,6 +9,7 @@ import ( "strings" "github.com/uc-cdis/hatchery/hatchery" + "go.uber.org/zap" httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" "gopkg.in/DataDog/dd-trace-go.v1/profiler" @@ -36,21 +36,27 @@ func main() { `) return } - logger := log.New(os.Stdout, "", log.LstdFlags) + // logger := log.New(os.Stdout, "", log.LstdFlags) + zapLogger, _ := zap.NewProduction() + defer zapLogger.Sync() + logger := zapLogger.Sugar() + cleanPath, err := verifyPath(configPath) if err != nil { - logger.Printf(fmt.Sprintf("Failed to load config - got %v", err)) + // logger.Printf(fmt.Sprintf("Failed to load config - got %v", err)) + logger.Errorf("Failed to load config - got %v", err) return } config, err := hatchery.LoadConfig(cleanPath, logger) if err != nil { - config.Logger.Printf(fmt.Sprintf("Failed to load config - got %v", err)) + logger.Errorf("Failed to load config - got %v", err) return } hatchery.Config = config ddEnabled := os.Getenv("DD_ENABLED") if strings.ToLower(ddEnabled) == "true" { - config.Logger.Printf("Setting up datadog") + // config.Logger.Printf("Setting up datadog") + config.Logger.Info("Setting up datadog") tracer.Start() defer tracer.Stop() if err := profiler.Start( @@ -64,18 +70,24 @@ func main() { // profiler.GoroutineProfile, ), ); err != nil { - config.Logger.Printf("DD profiler setup failed with error: %s", err) + // config.Logger.Printf("DD profiler setup failed with error: %s", err) + logger.Error("Failed to setup DD profiler", + "error", err, + ) } defer profiler.Stop() } else { - config.Logger.Printf("Datadog not enabled in manifest, skipping...") + // config.Logger.Printf("Datadog not enabled in manifest, skipping...") + config.Logger.Info("Datadog not enabled in manifest, skipping...") } - config.Logger.Printf("Setting up routes") + // config.Logger.Printf("Setting up routes") + config.Logger.Info("Setting up routes for hatchery api") mux := httptrace.NewServeMux() hatchery.RegisterSystem(mux) hatchery.RegisterHatchery(mux) - config.Logger.Printf("Running main") + // config.Logger.Printf("Running main") + config.Logger.Info("Running main") log.Fatal(http.ListenAndServe("0.0.0.0:8000", mux)) } From 14ef42eac41d5bd15cd49ea2d86af0f9035869ea Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Thu, 4 May 2023 23:40:54 +0200 Subject: [PATCH 02/16] Add structured logging via zap --- hatchery/alb.go | 18 ++++--- hatchery/cloudwatch.go | 4 +- hatchery/config.go | 10 ++-- hatchery/ec2.go | 2 +- hatchery/ecs.go | 98 +++++++++++++++++++------------------- hatchery/efs.go | 12 ++--- hatchery/hatchery.go | 26 +++++----- hatchery/paymodels.go | 6 +-- hatchery/pods.go | 56 +++++++++++----------- hatchery/prisma.go | 10 ++-- hatchery/ram.go | 12 ++--- hatchery/transitgateway.go | 46 +++++++++--------- hatchery/vpc.go | 18 +++---- main.go | 10 ++-- 14 files changed, 167 insertions(+), 161 deletions(-) diff --git a/hatchery/alb.go b/hatchery/alb.go index d2885bc0..b80ca266 100644 --- a/hatchery/alb.go +++ b/hatchery/alb.go @@ -38,13 +38,19 @@ func (creds *CREDS) createTargetGroup(userName string, vpcId string, svc *elbv2. case elbv2.ErrCodeTooManyTagsException: fmt.Println(elbv2.ErrCodeTooManyTagsException, aerr.Error()) default: - fmt.Println(aerr.Error()) + Config.Logger.Errorw("Error creating target group", + "error", aerr.Error(), + "code", aerr.Code(), + "message", aerr.Message(), + "user", userName, + ) return nil, err } } else { - // Print the error, cast err to awserr.Error to get the Code and - // Message from an error. - fmt.Println(err.Error()) + Config.Logger.Errorw("Error creating target group", + "error", err.Error(), + "user", userName, + ) return nil, err } return nil, err @@ -238,7 +244,7 @@ func (creds *CREDS) terminateLoadBalancerTargetGroup(userName string) error { return nil } } else { - Config.Logger.Error("Error describing target group", + Config.Logger.Errorw("Error describing target group", "error", err.Error(), "username", userName, ) @@ -258,7 +264,7 @@ func (creds *CREDS) terminateLoadBalancerTargetGroup(userName string) error { return nil } } else { - Config.Logger.Error("Error deleting target group", + Config.Logger.Errorw("Error deleting target group", "error", err.Error(), "username", userName, ) diff --git a/hatchery/cloudwatch.go b/hatchery/cloudwatch.go index 246ec722..13c4408b 100644 --- a/hatchery/cloudwatch.go +++ b/hatchery/cloudwatch.go @@ -20,7 +20,7 @@ func (sess *CREDS) CreateLogGroup(LogGroupName string, creds *credentials.Creden logGroup, err := c.DescribeLogGroups(describeLogGroupIn) if err != nil { - Config.Logger.Error("Error in DescribeLogGroup", + Config.Logger.Errorw("Error in DescribeLogGroup", "error", err, "logGroupNamePrefix", LogGroupName, ) @@ -32,7 +32,7 @@ func (sess *CREDS) CreateLogGroup(LogGroupName string, creds *credentials.Creden } newLogGroup, err := c.CreateLogGroup(createLogGroupIn) if err != nil { - Config.Logger.Error("Error in CreateLogGroup", + Config.Logger.Errorw("Error in CreateLogGroup", "error", err, "newLogGroup", newLogGroup, ) diff --git a/hatchery/config.go b/hatchery/config.go index 7a1f7df7..232ee684 100644 --- a/hatchery/config.go +++ b/hatchery/config.go @@ -122,7 +122,7 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful if nil != err { cwd, _ := os.Getwd() // data.Logger.Printf("failed to load %v from cwd %v got - %v", configFilePath, cwd, err) - data.Logger.Error("Failed to load config file.", + data.Logger.Errorw("Failed to load config file.", "configFilePath", configFilePath, "cwd", cwd, "error", err, @@ -149,7 +149,7 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful composeModel, err := DockstoreComposeFromFile(info.Path) if nil != err { // data.Logger.Printf("failed to load config from %v, got: %v", info.Path, err) - data.Logger.Error("Failed to load config.", + data.Logger.Errorw("Failed to load config.", "path", info.Path, "error", err, ) @@ -163,7 +163,7 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful hatchApp.Name = info.Name if nil != err { // data.Logger.Printf("failed to translate app, got: %v", err) - data.Logger.Error("Failed to translate app.", + data.Logger.Errorw("Failed to translate app.", "error", err, ) return nil, err @@ -171,7 +171,7 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful data.Config.Containers = append(data.Config.Containers, *hatchApp) } else { // data.Logger.Printf("ignoring config of unsupported type: %v", info.AppType) - data.Logger.Warn("Ignoring config of unsupported type.", + data.Logger.Warnw("Ignoring config of unsupported type.", "appType", info.AppType, ) } @@ -185,7 +185,7 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful if data.Config.PayModelsDynamodbTable == "" { // data.Logger.Printf("Warning: no 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") - data.Logger.Warn("No 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") + data.Logger.Warnw("No 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") } for _, payModel := range data.Config.PayModels { diff --git a/hatchery/ec2.go b/hatchery/ec2.go index 909359ad..f6f52fb2 100644 --- a/hatchery/ec2.go +++ b/hatchery/ec2.go @@ -108,7 +108,7 @@ func (creds *CREDS) describeWorkspaceNetwork(userName string) (*NetworkInfo, err if err != nil { return nil, err } - Config.Logger.Info("Creating security group", + Config.Logger.Infow("Creating security group", "security_group_id", *newSecurityGroup.GroupId, "security_group_name", createSecurityGroupInput.GroupName, "vpc_id", createSecurityGroupInput.VpcId, diff --git a/hatchery/ecs.go b/hatchery/ecs.go index e8238787..163ee676 100644 --- a/hatchery/ecs.go +++ b/hatchery/ecs.go @@ -129,7 +129,7 @@ func (sess *CREDS) findEcsCluster() (*ecs.Cluster, error) { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. // Config.Logger.Println(err.Error()) - Config.Logger.Error("Error describing ECS cluster", + Config.Logger.Errorw("Error describing ECS cluster", "clusterName", clusterName, "error", err.Error(), ) @@ -172,7 +172,7 @@ func (sess *CREDS) findEcsCluster() (*ecs.Cluster, error) { return describeClusterResult.Clusters[0], nil } } - Config.Logger.Error("Failed to describe ECS cluster.", + Config.Logger.Errorw("Failed to describe ECS cluster.", "clusterName", clusterName, ) @@ -217,7 +217,7 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce if statusMessage == "ACTIVE" && (*service.Services[0].RunningCount == *service.Services[0].DesiredCount) { taskDefName = *service.Services[0].TaskDefinition if taskDefName == "" { - Config.Logger.Error("No task definition found for user.", + Config.Logger.Errorw("No task definition found for user.", "userName", userName, ) } else { @@ -254,19 +254,19 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce break } if i == len(args)-1 { - Config.Logger.Error("Unable to find kernel idle shutdown time in args.", + Config.Logger.Errorw("Unable to find kernel idle shutdown time in args.", "userName", userName, ) } } } else { - Config.Logger.Error("No env vars found for task definition.", + Config.Logger.Errorw("No env vars found for task definition.", "taskDefName", taskDefName, "userName", userName, ) } } else { - Config.Logger.Error("No container definition found for task definition.", + Config.Logger.Errorw("No container definition found for task definition.", "taskDefName", taskDefName, "userName", userName, ) @@ -288,7 +288,7 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce // Terminate workspace running in ECS // TODO: Make this terminate ALB as well. func terminateEcsWorkspace(ctx context.Context, userName string, accessToken string, awsAcctID string) (string, error) { - Config.Logger.Info("Terminating ECS workspace.", + Config.Logger.Infow("Terminating ECS workspace.", "username", userName, ) roleARN := "arn:aws:iam::" + awsAcctID + ":role/csoc_adminvm" @@ -318,7 +318,7 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str return "", errors.New("No service found for " + userName) } if taskDefName == "" { - Config.Logger.Info("No task definition found for user, skipping API key deletion.", + Config.Logger.Infow("No task definition found for user, skipping API key deletion.", "userName", userName, ) } else { @@ -339,7 +339,7 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str ) err := deleteAPIKeyWithContext(ctx, accessToken, *ev.Value) if err != nil { - Config.Logger.Error("Error occurred when deleting API Key.", + Config.Logger.Errorw("Error occurred when deleting API Key.", "username", userName, "error", err.Error(), ) @@ -347,24 +347,24 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str break } if i == len(envVars)-1 { - Config.Logger.Error("Unable to find API Key ID in env vars.", + Config.Logger.Errorw("Unable to find API Key ID in env vars.", "username", userName, ) } } } else { - Config.Logger.Info("Skipping API key deletion as no environment variables were found.", + Config.Logger.Infow("Skipping API key deletion as no environment variables were found.", "taskDefName", taskDefName, "username", userName) } } else { - Config.Logger.Info("Skipping API key deletion as no environment variables were found.", + Config.Logger.Infow("Skipping API key deletion as no environment variables were found.", "taskDefName", taskDefName, "username", userName) } } // Terminate ECS service - Config.Logger.Info("Terminating ECS service.", + Config.Logger.Infow("Terminating ECS service.", "svcName", svcName, "username", userName, ) @@ -378,12 +378,12 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str } // Terminate load balancer - Config.Logger.Info("Terminating load balancer.", + Config.Logger.Infow("Terminating load balancer.", "username", userName, ) err = svc.terminateLoadBalancer(userName) if err != nil { - Config.Logger.Error("Failed to terminate load balancer.", + Config.Logger.Errorw("Failed to terminate load balancer.", "username", userName, "error", err.Error(), ) @@ -392,7 +392,7 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str // Terminate target group err = svc.terminateLoadBalancerTargetGroup(userName) if err != nil { - Config.Logger.Error("Failed to terminate load balancer target group.", + Config.Logger.Errorw("Failed to terminate load balancer target group.", "username", userName, "error", err.Error(), ) @@ -401,7 +401,7 @@ func terminateEcsWorkspace(ctx context.Context, userName string, accessToken str // Terminate transit gateway err = teardownTransitGateway(userName) if err != nil { - Config.Logger.Error("Failed to terminate transit gateway resources.", + Config.Logger.Errorw("Failed to terminate transit gateway resources.", "username", userName, "error", err.Error(), ) @@ -423,7 +423,7 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod mem, err := mem(hatchApp.MemoryLimit) if err != nil { // Log error and return without launching workspace - Config.Logger.Error("Failed to launch ECS workspace.", + Config.Logger.Errorw("Failed to launch ECS workspace.", "username", userName, "error", err.Error(), ) @@ -432,7 +432,7 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod cpu, err := cpu(hatchApp.CPULimit) if err != nil { // Log error and return without launching workspace - Config.Logger.Error("Failed to launch ECS workspace.", + Config.Logger.Errorw("Failed to launch ECS workspace.", "username", userName, "error", err.Error(), ) @@ -441,7 +441,7 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod // Make sure ECS cluster exists _, err = svc.launchEcsCluster(userName) if err != nil { - Config.Logger.Error("Failed to launch ECS cluster.", + Config.Logger.Errorw("Failed to launch ECS cluster.", "username", userName, "error", err.Error(), ) @@ -449,22 +449,22 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod } // Get Gen3 API key to be used in workspace - Config.Logger.Info("Creating API key.", + Config.Logger.Infow("Creating API key.", "username", userName, ) apiKey, err := getAPIKeyWithContext(ctx, accessToken) if err != nil { - Config.Logger.Error("Failed to create API key.", + Config.Logger.Errorw("Failed to create API key.", "username", userName, "error", err.Error(), ) - Config.Logger.Info( + Config.Logger.Infow( "Moving on but workspace won't have API key.", "username", userName, ) apiKey = &APIKeyStruct{} } else { - Config.Logger.Info("Created API key.", + Config.Logger.Infow("Created API key.", "username", userName, "keyID", apiKey.KeyID, ) @@ -495,45 +495,45 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod Value: os.Getenv("GEN3_ENDPOINT"), }) - Config.Logger.Info("Setting up EFS.", + Config.Logger.Infow("Setting up EFS.", "username", userName, ) volumes, err := svc.EFSFileSystem(userName) if err != nil { - Config.Logger.Error("Failed to set up EFS.", + Config.Logger.Errorw("Failed to set up EFS.", "username", userName, "error", err.Error(), ) return err } - Config.Logger.Info("Setting up task role.", + Config.Logger.Infow("Setting up task role.", "username", userName, ) taskRole, err := svc.taskRole(userName) if err != nil { // Log the error - Config.Logger.Error("Failed to set up task role.", + Config.Logger.Errorw("Failed to set up task role.", "username", userName, "error", err.Error(), ) return err } - Config.Logger.Info("Setting up task execution role.", + Config.Logger.Infow("Setting up task execution role.", "username", userName, ) _, err = svc.CreateEcsTaskExecutionRole() if err != nil { // Log the error - Config.Logger.Error("Failed to set up task execution role.", + Config.Logger.Errorw("Failed to set up task execution role.", "username", userName, "error", err.Error(), ) return err } - Config.Logger.Info("Setting up ECS task definition.", + Config.Logger.Infow("Setting up ECS task definition.", "username", userName, ) taskDef := CreateTaskDefinitionInput{ @@ -607,13 +607,13 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod taskDefResult, err := svc.CreateTaskDefinition(&taskDef, userName, hash, payModel.AWSAccountId) if err != nil { // Log the error - Config.Logger.Error("Failed to set up task definition.", + Config.Logger.Errorw("Failed to set up task definition.", "username", userName, "error", err.Error(), ) aerr := deleteAPIKeyWithContext(ctx, accessToken, apiKey.KeyID) if aerr != nil { - Config.Logger.Error("Error occurred when deleting API Key.", + Config.Logger.Errorw("Error occurred when deleting API Key.", "username", userName, "error", err.Error(), ) @@ -621,19 +621,19 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod return err } - Config.Logger.Info("Launching ECS workspace service.", + Config.Logger.Infow("Launching ECS workspace service.", "username", userName, ) launchTask, err := svc.launchService(ctx, taskDefResult, userName, hash, payModel) if err != nil { // Log the error - Config.Logger.Error("Failed to launch ECS workspace service.", + Config.Logger.Errorw("Failed to launch ECS workspace service.", "username", userName, "error", err.Error(), ) aerr := deleteAPIKeyWithContext(ctx, accessToken, apiKey.KeyID) if aerr != nil { - Config.Logger.Error("Error occurred when deleting API Key.", + Config.Logger.Errorw("Error occurred when deleting API Key.", "username", userName, "error", err.Error(), ) @@ -641,20 +641,20 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod return err } - Config.Logger.Info("Setting up Transit Gateway.", + Config.Logger.Infow("Setting up Transit Gateway.", "username", userName, ) err = setupTransitGateway(userName) if err != nil { // Log the error - Config.Logger.Error("Failed to set up Transit Gateway.", + Config.Logger.Errorw("Failed to set up Transit Gateway.", "username", userName, "error", err.Error(), ) return err } - Config.Logger.Info("Launched ECS workspace service.", + Config.Logger.Infow("Launched ECS workspace service.", "username", userName, "launchTask", launchTask, ) @@ -707,7 +707,7 @@ func (sess *CREDS) launchService(ctx context.Context, taskDefArn string, userNam case ecs.ErrCodeInvalidParameterException: if aerr.Error() == "InvalidParameterException: Creation of service was not idempotent." { // Config.Logger.Print("Service already exists.. ") - Config.Logger.Info("ECS Service already exists. Moving on.", + Config.Logger.Infow("ECS Service already exists. Moving on.", "username", userName, ) return "", nil @@ -715,19 +715,19 @@ func (sess *CREDS) launchService(ctx context.Context, taskDefArn string, userNam } } // Config.Logger.Println(err.Error()) - Config.Logger.Error("Error occurred when creating ECS service.", + Config.Logger.Errorw("Error occurred when creating ECS service.", "username", userName, "error", err.Error(), ) return "", err } - Config.Logger.Info("Service launched.", + Config.Logger.Infow("Service launched.", "username", userName, "service", *result.Service.ServiceName, ) err = createLocalService(ctx, userName, hash, *loadBalancer.LoadBalancers[0].DNSName, payModel) if err != nil { - Config.Logger.Error("Error occurred when creating local kubernetes service (used by ambassador).", + Config.Logger.Errorw("Error occurred when creating local kubernetes service (used by ambassador).", "username", userName, "loadbalancer", *loadBalancer.LoadBalancers[0].DNSName, "paymodel", payModel.Name, @@ -743,7 +743,7 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa creds := sess.creds LogGroup, err := sess.CreateLogGroup(fmt.Sprintf("/hatchery/%s/", awsAcctID), creds) if err != nil { - Config.Logger.Error("Failed to create/get LogGroup.", + Config.Logger.Errorw("Failed to create/get LogGroup.", "username", userName, "loggroup", "/hatchery/"+awsAcctID+"/", "error", err.Error(), @@ -755,7 +755,7 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa Region: aws.String("us-east-1"), }))) - Config.Logger.Info("Creating ECS task definition.", + Config.Logger.Infow("Creating ECS task definition.", "username", userName, ) @@ -803,7 +803,7 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa installBundle, err := getInstallBundle() if err != nil { // Config.Logger.Print(err, " error getting prisma install bundle") - Config.Logger.Error("Error occurred when getting prisma install bundle.", + Config.Logger.Errorw("Error occurred when getting prisma install bundle.", "username", userName, "error", err.Error(), ) @@ -813,7 +813,7 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa image, err := getPrismaImage() if err != nil { // Config.Logger.Print(err, " error getting prisma image") - Config.Logger.Error("Error occurred when getting prisma image.", + Config.Logger.Errorw("Error occurred when getting prisma image.", "username", userName, "error", err.Error(), ) @@ -885,7 +885,7 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa if err != nil { // Config.Logger.Print(err, " Couldn't register ECS task definition") - Config.Logger.Error("Couldn't register ECS task definition.", + Config.Logger.Errorw("Couldn't register ECS task definition.", "username", userName, "error", err.Error(), ) @@ -894,7 +894,7 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa td := resp.TaskDefinition - Config.Logger.Info("Created ECS task definition.", + Config.Logger.Infow("Created ECS task definition.", "username", userName, "taskdefinition", fmt.Sprintf("%s:%d", aws.StringValue(td.Family), aws.Int64Value(td.Revision)), ) diff --git a/hatchery/efs.go b/hatchery/efs.go index 9aacf61a..0ebe69bf 100644 --- a/hatchery/efs.go +++ b/hatchery/efs.go @@ -134,7 +134,7 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { exisitingFS, _ = creds.getEFSFileSystem(userName, svc) for *exisitingFS.FileSystems[0].LifeCycleState != "available" { - Config.Logger.Info("Waiting for EFS filesystem to be available", + Config.Logger.Infow("Waiting for EFS filesystem to be available", "FilesystemID", *exisitingFS.FileSystems[0].FileSystemId, "FileSystemName", fsName, "CreationToken", *exisitingFS.FileSystems[0].CreationToken, @@ -149,7 +149,7 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { mountTarget, err := creds.createMountTarget(*result.FileSystemId, svc, userName) if err != nil { // Log error - Config.Logger.Error("Failed to create EFS MountTarget", + Config.Logger.Errorw("Failed to create EFS MountTarget", "username", userName, "FileSystemID", *result.FileSystemId, "MountTargetID", *mountTarget.MountTargetId, @@ -159,7 +159,7 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { ) return nil, fmt.Errorf("Failed to create EFS MountTarget: %s", err) } - Config.Logger.Info("MountTarget created", + Config.Logger.Infow("MountTarget created", "username", userName, "MountTargetID", *mountTarget.MountTargetId, "FileSystemID", *mountTarget.FileSystemId, @@ -169,7 +169,7 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { accessPoint, err := creds.createAccessPoint(*result.FileSystemId, userName, svc) if err != nil { // Log error - Config.Logger.Error("Failed to create EFS AccessPoint", + Config.Logger.Errorw("Failed to create EFS AccessPoint", "username", userName, "FileSystemID", *result.FileSystemId, "AccessPointID", *accessPoint, @@ -177,7 +177,7 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { ) return nil, fmt.Errorf("Failed to create EFS AccessPoint: %s", err) } - Config.Logger.Info("EFS AccessPoint created", + Config.Logger.Infow("EFS AccessPoint created", "username", userName, "FileSystemID", *result.FileSystemId, "AccessPointID", *accessPoint, @@ -218,7 +218,7 @@ func (creds *CREDS) EFSFileSystem(userName string) (*EFS, error) { if err != nil { return nil, fmt.Errorf("Failed to create EFS MountTarget: %s", err) } - Config.Logger.Info("EFS MountTarget created", + Config.Logger.Infow("EFS MountTarget created", "username", userName, "MountTargetID", *mountTarget.MountTargetId, "FileSystemID", *mountTarget.FileSystemId, diff --git a/hatchery/hatchery.go b/hatchery/hatchery.go index 18a7922e..6102de78 100644 --- a/hatchery/hatchery.go +++ b/hatchery/hatchery.go @@ -224,7 +224,7 @@ func launch(w http.ResponseWriter, r *http.Request) { userName := getCurrentUserName(r) payModel, err := getCurrentPayModel(userName) if err != nil { - Config.Logger.Error("Failed to get current paymodel", + Config.Logger.Errorw("Failed to get current paymodel", "error", err.Error(), "username", userName, ) @@ -236,7 +236,7 @@ func launch(w http.ResponseWriter, r *http.Request) { if payModel.Status != "active" { // send 500 response. // TODO: 403 is the correct code, but it triggers a 302 to the default 403 page in revproxy instead of showing error message. - Config.Logger.Error("Launch forbidden: pay model is not active", + Config.Logger.Errorw("Launch forbidden: pay model is not active", "username", userName, "paymodelstatus", payModel.Status, "paymodel", payModel, @@ -245,7 +245,7 @@ func launch(w http.ResponseWriter, r *http.Request) { return } - Config.Logger.Info("Launching ECS workspace", + Config.Logger.Infow("Launching ECS workspace", "username", userName, "paymodel", payModel.Name, ) @@ -259,7 +259,7 @@ func launch(w http.ResponseWriter, r *http.Request) { err = createExternalK8sPod(r.Context(), hash, userName, accessToken, *payModel) } if err != nil { - Config.Logger.Error("Error during launch", + Config.Logger.Errorw("Error during launch", "error", err.Error(), "username", userName, "paymodel", payModel, @@ -280,12 +280,12 @@ func terminate(w http.ResponseWriter, r *http.Request) { payModel, err := getCurrentPayModel(userName) if err != nil { - Config.Logger.Error("Failed to get current paymodel", + Config.Logger.Errorw("Failed to get current paymodel", "error", err.Error(), "username", userName, ) } - Config.Logger.Info("Terminating workspace", + Config.Logger.Infow("Terminating workspace", "username", userName, ) if payModel != nil && payModel.Ecs { @@ -294,7 +294,7 @@ func terminate(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } else { - Config.Logger.Info("Succesfully terminated all resources related to ECS workspace", + Config.Logger.Infow("Succesfully terminated all resources related to ECS workspace", "username", userName, ) fmt.Fprintf(w, "Terminated ECS workspace") @@ -305,7 +305,7 @@ func terminate(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - Config.Logger.Info("Terminated workspace", + Config.Logger.Infow("Terminated workspace", "username", userName, "paymodel", payModel, ) @@ -351,7 +351,7 @@ func createECSCluster(w http.ResponseWriter, r *http.Request) { var reader *strings.Reader if err != nil { reader = strings.NewReader(err.Error()) - Config.Logger.Error("Error creating ECS cluster", + Config.Logger.Errorw("Error creating ECS cluster", "error", err.Error(), "username", userName, "paymodel", payModel, @@ -362,7 +362,7 @@ func createECSCluster(w http.ResponseWriter, r *http.Request) { // TODO: What's happening here? _, err = io.Copy(w, reader) if err != nil { - Config.Logger.Error("Error copying response", + Config.Logger.Errorw("Error copying response", "error", err.Error(), "username", userName, "paymodel", payModel, @@ -380,7 +380,7 @@ func statusEcs(ctx context.Context, userName string, accessToken string, awsAcct svc := NewSVC(sess, roleARN) result, err := svc.statusEcsWorkspace(ctx, userName, accessToken) if err != nil { - Config.Logger.Error("Error getting status of ECS workspace", + Config.Logger.Errorw("Error getting status of ECS workspace", "error", err.Error(), "username", userName, "awsAcctID", awsAcctID, @@ -396,7 +396,7 @@ func launchEcsWorkspaceWrapper(userName string, hash string, accessToken string, err := launchEcsWorkspace(userName, hash, accessToken, payModel) if err != nil { - Config.Logger.Error("Error launching ECS workspace", + Config.Logger.Errorw("Error launching ECS workspace", "error", err.Error(), "username", userName, "paymodel", payModel, @@ -404,7 +404,7 @@ func launchEcsWorkspaceWrapper(userName string, hash string, accessToken string, // Terminate ECS workspace if launch fails. _, err = terminateEcsWorkspace(context.Background(), userName, accessToken, payModel.AWSAccountId) if err != nil { - Config.Logger.Error("Error terminating ECS workspace", + Config.Logger.Errorw("Error terminating ECS workspace", "error", err.Error(), "username", userName, "paymodel", payModel, diff --git a/hatchery/paymodels.go b/hatchery/paymodels.go index b9fd21bb..009514f3 100644 --- a/hatchery/paymodels.go +++ b/hatchery/paymodels.go @@ -31,7 +31,7 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel } expr, err := expression.NewBuilder().WithFilter(filt).Build() if err != nil { - Config.Logger.Error("Got error building dyamoDB expression", + Config.Logger.Errorw("Got error building dyamoDB expression", "error", err, ) return nil, err @@ -45,7 +45,7 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel } res, err := dynamodbSvc.Scan(params) if err != nil { - Config.Logger.Error("Failed to query dynamoDB", + Config.Logger.Errorw("Failed to query dynamoDB", "error", err, ) return nil, err @@ -55,7 +55,7 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel var payModelMap []PayModel err = dynamodbattribute.UnmarshalListOfMaps(res.Items, &payModelMap) if err != nil { - Config.Logger.Error("Failed to unmarshal dynamoDB paymodels", + Config.Logger.Errorw("Failed to unmarshal dynamoDB paymodels", "error", err, ) return nil, err diff --git a/hatchery/pods.go b/hatchery/pods.go index a0e2278e..672f9a78 100644 --- a/hatchery/pods.go +++ b/hatchery/pods.go @@ -72,7 +72,7 @@ func getPodClient(ctx context.Context, userName string, payModelPtr *PayModel) ( if payModelPtr != nil && !(*payModelPtr).Local { podClient, err := NewEKSClientset(ctx, userName, *payModelPtr) if err != nil { - Config.Logger.Error("Error fetching EKS kubeconfig", + Config.Logger.Errorw("Error fetching EKS kubeconfig", "error", err, "userName", userName, "payModel", *payModelPtr, @@ -92,7 +92,7 @@ func getLocalPodClient() corev1.CoreV1Interface { config.WrapTransport = kubernetestrace.WrapRoundTripper if err != nil { - Config.Logger.Error("Error creating in-cluster kubeconfig", + Config.Logger.Errorw("Error creating in-cluster kubeconfig", "error", err, ) return nil @@ -101,7 +101,7 @@ func getLocalPodClient() corev1.CoreV1Interface { clientset, err := kubernetes.NewForConfig(config) if err != nil { - Config.Logger.Error("Error creating in-cluster clientset", + Config.Logger.Errorw("Error creating in-cluster clientset", "error", err, ) return nil @@ -127,7 +127,7 @@ func NewEKSClientset(ctx context.Context, userName string, payModel PayModel) (c result, err := eksSvc.DescribeClusterWithContext(ctx, input) if err != nil { - Config.Logger.Error("Error calling DescribeCluster", + Config.Logger.Errorw("Error calling DescribeCluster", "error", err, "userName", userName, "payModel", payModel, @@ -204,7 +204,7 @@ func podStatus(ctx context.Context, userName string, accessToken string, payMode err := podClient.Services(Config.Config.UserNamespace).Delete(ctx, serviceName, deleteOptions) if err != nil { - Config.Logger.Error("Error deleting service", + Config.Logger.Errorw("Error deleting service", "error", err, "userName", userName, "serviceName", serviceName, @@ -285,7 +285,7 @@ func statusK8sPod(ctx context.Context, userName string, accessToken string, payM if err != nil { status.Status = fmt.Sprintf("%v", err) - Config.Logger.Error("Error getting status", + Config.Logger.Errorw("Error getting status", "error", err, "userName", userName, ) @@ -389,7 +389,7 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str // a null image indicates a dockstore app - always mount user volume mountUserVolume := hatchApp.UserVolumeLocation != "" - Config.Logger.Info("building pod", + Config.Logger.Infow("building pod", "podName", podName, "userName", userName, "appName", hatchApp.Name, @@ -645,7 +645,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.Info("Creating a Local kubernetes pod for workspace.", + Config.Logger.Infow("Creating a Local kubernetes pod for workspace.", "userName", userName, "image", hatchApp.Image, "cpulimit", hatchApp.CPULimit, @@ -655,14 +655,14 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access apiKey, err := getAPIKeyWithContext(ctx, accessToken) if err != nil { - Config.Logger.Error("Failed to get API key.", + Config.Logger.Errorw("Failed to get API key.", "userName", userName, "error", err, ) return err } - Config.Logger.Info("Created API key.", + Config.Logger.Infow("Created API key.", "userName", userName, "keyID", apiKey.KeyID, ) @@ -679,7 +679,7 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access pod, err := buildPod(Config, &hatchApp, userName, extraVars) if err != nil { - Config.Logger.Error("Failed to configure pod for launch.", + Config.Logger.Errorw("Failed to configure pod for launch.", "username", userName, "error", err, ) @@ -688,7 +688,7 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access podName := userToResourceName(userName, "pod") podClient, _, err := getPodClient(ctx, userName, nil) if err != nil { - Config.Logger.Error("Failed to get pod client.", + Config.Logger.Errorw("Failed to get pod client.", "error", err, "username", userName, ) @@ -723,7 +723,7 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Create(ctx, pvc, metav1.CreateOptions{}) if err != nil { - Config.Logger.Error("Failed to create PVC.", + Config.Logger.Errorw("Failed to create PVC.", "claimName", claimName, "error", err, ) @@ -735,7 +735,7 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access _, err = podClient.Pods(Config.Config.UserNamespace).Create(ctx, pod, metav1.CreateOptions{}) if err != nil { - Config.Logger.Error("Failed to launch pod.", + Config.Logger.Errorw("Failed to launch pod.", "podName", hatchApp.Name, "username", userName, "image", hatchApp.Image, @@ -746,7 +746,7 @@ func createLocalK8sPod(ctx context.Context, hash string, userName string, access return err } - Config.Logger.Info("Launched pod.", + Config.Logger.Infow("Launched pod.", "podName", hatchApp.Name, "username", userName, "image", hatchApp.Image, @@ -810,7 +810,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.Info("Creating a workspace pod in external kubernetes cluster.", + Config.Logger.Infow("Creating a workspace pod in external kubernetes cluster.", "username", userName, "image", hatchApp.Image, "cpulimit", hatchApp.CPULimit, @@ -819,7 +819,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc podClient, err := NewEKSClientset(ctx, userName, payModel) if err != nil { - Config.Logger.Error("Failed to create external pod client.", + Config.Logger.Errorw("Failed to create external pod client.", "username", userName, "error", err, ) @@ -829,14 +829,14 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc apiKey, err := getAPIKeyWithContext(ctx, accessToken) if err != nil { - Config.Logger.Error("Failed to get API key.", + Config.Logger.Errorw("Failed to get API key.", "username", userName, "error", err, ) return err } - Config.Logger.Info("Created API key.", + Config.Logger.Infow("Created API key.", "username", userName, "keyID", apiKey.KeyID, ) @@ -852,14 +852,14 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err = podClient.Namespaces().Create(ctx, nsName, metav1.CreateOptions{}) if err != nil { - Config.Logger.Error("Failed to create namespace.", + Config.Logger.Errorw("Failed to create namespace.", "namespace", Config.Config.UserNamespace, "username", userName, "error", err, ) } else { - Config.Logger.Info("Created namespace.", + Config.Logger.Infow("Created namespace.", "namespace", ns, "username", userName, ) @@ -889,7 +889,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc pod, err := buildPod(Config, &hatchApp, userName, extraVars) if err != nil { - Config.Logger.Error("Failed to configure pod for launch.", + Config.Logger.Errorw("Failed to configure pod for launch.", "username", userName, "error", err, ) @@ -904,7 +904,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Get(ctx, claimName, metav1.GetOptions{}) if err != nil { - Config.Logger.Info("Creating PersistentVolumeClaim.", + Config.Logger.Infow("Creating PersistentVolumeClaim.", "claimName", claimName, "username", userName, ) @@ -927,7 +927,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err := podClient.PersistentVolumeClaims(Config.Config.UserNamespace).Create(ctx, pvc, metav1.CreateOptions{}) if err != nil { - Config.Logger.Error("Failed to create PVC.", + Config.Logger.Errorw("Failed to create PVC.", "claimName", claimName, "username", userName, "error", err, @@ -940,7 +940,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc _, err = podClient.Pods(Config.Config.UserNamespace).Create(ctx, pod, metav1.CreateOptions{}) if err != nil { - Config.Logger.Error("Failed to launch pod.", + Config.Logger.Errorw("Failed to launch pod.", "podName", podName, "username", userName, "image", hatchApp.Image, @@ -951,7 +951,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc return err } - Config.Logger.Info("Launched pod.", + Config.Logger.Infow("Launched pod.", "podName", podName, "username", userName, "image", hatchApp.Image, @@ -1009,7 +1009,7 @@ func createExternalK8sPod(ctx context.Context, hash string, userName string, acc return err } - Config.Logger.Info("Launched service.", + Config.Logger.Infow("Launched service.", "serviceName", serviceName, "username", userName, "port", hatchApp.TargetPort, @@ -1111,7 +1111,7 @@ tls: %s return err } - Config.Logger.Info("Launched local service.", + Config.Logger.Infow("Launched local service.", "serviceName", serviceName, "username", userName, "port", hatchApp.TargetPort, diff --git a/hatchery/prisma.go b/hatchery/prisma.go index 51dc5f1a..7fd2fa46 100644 --- a/hatchery/prisma.go +++ b/hatchery/prisma.go @@ -33,7 +33,7 @@ func getPrismaToken(username string, password string) (*string, error) { defer resp.Body.Close() if resp.StatusCode != 200 { b, _ := ioutil.ReadAll(resp.Body) - Config.Logger.Error("Error authenticating with Prisma Cloud", + Config.Logger.Errorw("Error authenticating with Prisma Cloud", "error", string(b), ) return nil, errors.New("Error authenticating with Prisma Cloud: " + string(b)) @@ -47,7 +47,7 @@ func getPrismaToken(username string, password string) (*string, error) { var result Token if err := json.Unmarshal(body, &result); err != nil { // fmt.Println("Invalid response from prisma auth endpoint: " + authEndpoint) - Config.Logger.Error("Invalid response from prisma auth endpoint", + Config.Logger.Errorw("Invalid response from prisma auth endpoint", "error", err, "endpoint", authEndpoint, ) @@ -84,7 +84,7 @@ func getInstallBundle() (*InstallBundle, error) { if resp.StatusCode != 200 { b, _ := ioutil.ReadAll(resp.Body) // Config.Logger.Print(string(b)) - Config.Logger.Error("Error getting prismacloud install bundle", + Config.Logger.Errorw("Error getting prismacloud install bundle", "error", string(b), ) return nil, errors.New("Error getting install bundle: " + string(b)) @@ -97,7 +97,7 @@ func getInstallBundle() (*InstallBundle, error) { var result InstallBundle if err := json.Unmarshal(body, &result); err != nil { // fmt.Println("Invalid response from prisma install_bundle endpoint: " + installBundleEndpoint) - Config.Logger.Error("Invalid response from prisma install_bundle endpoint", + Config.Logger.Errorw("Invalid response from prisma install_bundle endpoint", "error", err, "endpoint", installBundleEndpoint, ) @@ -132,7 +132,7 @@ func getPrismaImage() (*string, error) { defer resp.Body.Close() if resp.StatusCode != 200 { b, _ := ioutil.ReadAll(resp.Body) - Config.Logger.Error("Error getting prismacloud image name", + Config.Logger.Errorw("Error getting prismacloud image name", "error", string(b), ) return nil, errors.New("Error getting prismacloud image name: " + string(b)) diff --git a/hatchery/ram.go b/hatchery/ram.go index 629f5542..e117cb5b 100644 --- a/hatchery/ram.go +++ b/hatchery/ram.go @@ -17,7 +17,7 @@ func acceptTransitGatewayShare(pm *PayModel, userName string, sess *session.Sess if err != nil { // Log error - Config.Logger.Error("Failed to accept transitgateway share", + Config.Logger.Errorw("Failed to accept transitgateway share", "error", err, "username", userName, "paymodel", pm, @@ -42,7 +42,7 @@ func (creds *CREDS) acceptTGWShare(ramArn *string) error { } resourceShareInvitation, err := svc.GetResourceShareInvitations(ramInvitationInput) if err != nil { - Config.Logger.Error("Failed to get resource share invitation", + Config.Logger.Errorw("Failed to get resource share invitation", "error", err, "ram-share-arn", ramArn, ) @@ -50,7 +50,7 @@ func (creds *CREDS) acceptTGWShare(ramArn *string) error { } if len(resourceShareInvitation.ResourceShareInvitations) == 0 { - Config.Logger.Warn("No invitations found for RAM share", + Config.Logger.Warnw("No invitations found for RAM share", "ram-share-arn", ramArn, ) return nil @@ -94,7 +94,7 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri return nil, err } if len(exRs.ResourceShares) == 0 { - Config.Logger.Info("Resource share not found, creating new resource share", + Config.Logger.Infow("Resource share not found, creating new resource share", "ramName", ramName, "tgwArn", tgwArn, "accountId", accountid) @@ -123,7 +123,7 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri } return resourceShare.ResourceShare.ResourceShareArn, nil } else { - Config.Logger.Info("Existing resource share found. Associating resource share with account.", + Config.Logger.Infow("Existing resource share found. Associating resource share with account.", "ramName", ramName, "tgwArn", tgwArn, "accountId", accountid) @@ -145,7 +145,7 @@ func shareTransitGateway(session *session.Session, tgwArn string, accountid stri listPrincipals, err := svc.ListPrincipals(listPrincipalsInput) if err != nil { - Config.Logger.Error("failed to ListPrincipals", + Config.Logger.Errorw("failed to ListPrincipals", "listPrincipalsInput", listPrincipalsInput, "error", err, ) diff --git a/hatchery/transitgateway.go b/hatchery/transitgateway.go index e01ce96b..553b9f15 100644 --- a/hatchery/transitgateway.go +++ b/hatchery/transitgateway.go @@ -25,7 +25,7 @@ func setupTransitGateway(userName string) error { return err } - Config.Logger.Info("Setting up transit gateway in main account", + Config.Logger.Infow("Setting up transit gateway in main account", "username", userName, ) tgwid, tgwarn, tgwRouteTableId, err := createTransitGateway(sess, userName) @@ -52,7 +52,7 @@ func setupTransitGateway(userName string) error { return err } - Config.Logger.Info("Setting up remote account", + Config.Logger.Infow("Setting up remote account", "username", userName, "paymodel", pm.Name, ) @@ -74,7 +74,7 @@ func createLocalTransitGatewayAttachment(userName string, tgwid string, tgwRoute // ec2 session to main AWS account. ec2Local := ec2.New(sess) // Create Transit Gateway Attachment in local VPC - Config.Logger.Info("Creating transitgateway attachment in local VPC", + Config.Logger.Infow("Creating transitgateway attachment in local VPC", "username", userName, "vpcid", vpcid, ) @@ -83,7 +83,7 @@ func createLocalTransitGatewayAttachment(userName string, tgwid string, tgwRoute return err } - Config.Logger.Info("Transit Gateway attachment created", + Config.Logger.Infow("Transit Gateway attachment created", "username", userName, "attachment", *tgwAttachment, ) @@ -91,7 +91,7 @@ func createLocalTransitGatewayAttachment(userName string, tgwid string, tgwRoute // Create Transit Gateway Route Table _, err = TGWRoutes(userName, tgwRouteTableId, tgwAttachment, ec2Local, true, false, nil) if err != nil { - Config.Logger.Error("Failed to set up transit gateway route table", + Config.Logger.Errorw("Failed to set up transit gateway route table", "username", userName, "error", err.Error(), ) @@ -103,7 +103,7 @@ func createLocalTransitGatewayAttachment(userName string, tgwid string, tgwRoute func teardownTransitGateway(userName string) error { - Config.Logger.Info("Terminating remote transit gateway attachment", + Config.Logger.Infow("Terminating remote transit gateway attachment", "username", userName, ) err := setupRemoteAccount(userName, true) @@ -201,7 +201,7 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string // Create Transit Gateway if it doesn't exist if len(exTg.TransitGateways) == 0 { - Config.Logger.Info("No existing transit gateway found. Creating a new transit gateway...") + Config.Logger.Infow("No existing transit gateway found. Creating a new transit gateway...") tgwName := strings.ReplaceAll(os.Getenv("GEN3_ENDPOINT"), ".", "-") + "-tgw" tg, err := ec2Local.CreateTransitGateway(&ec2.CreateTransitGatewayInput{ DryRun: aws.Bool(false), @@ -230,7 +230,7 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string return nil, nil, nil, err } - Config.Logger.Info("Transit gateway created", + Config.Logger.Infow("Transit gateway created", "transit_gateway_id", *tg.TransitGateway.TransitGatewayId, "username", userName, ) @@ -238,7 +238,7 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string return tg.TransitGateway.TransitGatewayId, tg.TransitGateway.TransitGatewayArn, tg.TransitGateway.Options.AssociationDefaultRouteTableId, nil } else { // Config.Logger.Print("Existing transit gateway found. Skipping creation...") - Config.Logger.Info("Existing transit gateway found. Skipping creation...", + Config.Logger.Infow("Existing transit gateway found. Skipping creation...", "transit_gateway_id", *exTg.TransitGateways[len(exTg.TransitGateways)-1].TransitGatewayId, "username", userName, ) @@ -248,7 +248,7 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, local bool, sess *CREDS, userName string) (*string, error) { - Config.Logger.Info("Creating transit gateway attachment for VPC", + Config.Logger.Infow("Creating transit gateway attachment for VPC", "vpc_id", vpcid, "username", userName, ) @@ -280,7 +280,7 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l return nil, err } for *exTg.TransitGateways[0].State != "available" { - Config.Logger.Info("TransitGateway is not ready yet. Waiting for 10 seconds...", + Config.Logger.Infow("TransitGateway is not ready yet. Waiting for 10 seconds...", "state", *exTg.TransitGateways[0].State, "username", userName, ) @@ -348,7 +348,7 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l }, }, } - Config.Logger.Info("Creating a new local transit gateway attachment as none was found.", + Config.Logger.Infow("Creating a new local transit gateway attachment as none was found.", "username", userName, ) for i := range networkInfo.subnets.Subnets { @@ -359,14 +359,14 @@ func createTransitGatewayAttachments(svc *ec2.EC2, vpcid string, tgwid string, l return nil, fmt.Errorf("cannot create transitgatewayattachment: %s", err.Error()) } - Config.Logger.Info("Created transitgatewayattachment", + Config.Logger.Infow("Created transitgatewayattachment", "username", userName, "transitGatewayAttachment", *tgwAttachment.TransitGatewayVpcAttachment.TransitGatewayAttachmentId, ) return tgwAttachment.TransitGatewayVpcAttachment.TransitGatewayAttachmentId, nil } else { - Config.Logger.Info("Using existing local transit gateway attachment", + Config.Logger.Infow("Using existing local transit gateway attachment", "username", userName, "transitGatewayAttachment", *exTgwAttachment.TransitGatewayAttachments[0].TransitGatewayAttachmentId, ) @@ -410,7 +410,7 @@ func deleteTransitGatewayAttachment(svc *ec2.EC2, tgwid string, userName string) } } else { - Config.Logger.Error("Error describing transit gateway attachment", + Config.Logger.Errorw("Error describing transit gateway attachment", "username", userName, "error", err.Error(), ) @@ -475,7 +475,7 @@ func setupRemoteAccount(userName string, teardown bool) error { } for len(exTg.TransitGateways) == 0 { - Config.Logger.Warn("Waiting to find ex_tgw", + Config.Logger.Warnw("Waiting to find ex_tgw", "username", userName, ) // err := svc.acceptTGWShare() @@ -507,7 +507,7 @@ func setupRemoteAccount(userName string, teardown bool) error { } else { tgw_attachment, err = createTransitGatewayAttachments(ec2Remote, *vpc.Vpcs[0].VpcId, *exTg.TransitGateways[0].TransitGatewayId, false, &svc, userName) if err != nil { - Config.Logger.Error("Cannot create remote TransitGatewayAttachment", + Config.Logger.Errorw("Cannot create remote TransitGatewayAttachment", "username", userName, "error", err.Error(), ) @@ -577,7 +577,7 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string } else { for *tgwAttachment.TransitGatewayAttachments[0].State != "available" { - Config.Logger.Warn("Transit Gateway Attachment is not ready", + Config.Logger.Warnw("Transit Gateway Attachment is not ready", "username", userName, "state", *tgwAttachment.TransitGatewayAttachments[0].State, ) @@ -601,7 +601,7 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string if err != nil { // log error - Config.Logger.Error("error SearchTransitGatewayRoutes", + Config.Logger.Errorw("error SearchTransitGatewayRoutes", "username", userName, "error", err.Error(), ) @@ -613,7 +613,7 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string // Delete route only if it's blackhole route if *exRoutes.Routes[0].State == "blackhole" { - Config.Logger.Info("Route is blackhole, deleting", + Config.Logger.Infow("Route is blackhole, deleting", "username", userName, "route", *exRoutes.Routes[0].DestinationCidrBlock, "state", *exRoutes.Routes[0].State, @@ -627,7 +627,7 @@ func TGWRoutes(userName string, tgwRoutetableId *string, tgwAttachmentId *string return nil, err } } else { - Config.Logger.Info("Route already exists", + Config.Logger.Infow("Route already exists", "username", userName, "route", *networkInfo.vpc.Vpcs[0].CidrBlock, ) @@ -696,7 +696,7 @@ func VPCRoutes(remote_network_info *NetworkInfo, main_network_info *NetworkInfo, return err } - Config.Logger.Info("Route added to remote VPC", + Config.Logger.Infow("Route added to remote VPC", "routeTableId", *remote_network_info.routeTable.RouteTables[0].RouteTableId, "destinationCidrBlock", *remote_network_info.vpc.Vpcs[0].CidrBlock, "transitGatewayId", *tgwId, @@ -713,7 +713,7 @@ func VPCRoutes(remote_network_info *NetworkInfo, main_network_info *NetworkInfo, return err } - Config.Logger.Info("Route added to local VPC", + Config.Logger.Infow("Route added to local VPC", "routeTableId", *main_network_info.routeTable.RouteTables[0].RouteTableId, "destinationCidrBlock", *remote_network_info.vpc.Vpcs[0].CidrBlock, "transitGatewayId", *tgwId, diff --git a/hatchery/vpc.go b/hatchery/vpc.go index d94c6ad4..9dd07172 100644 --- a/hatchery/vpc.go +++ b/hatchery/vpc.go @@ -15,13 +15,13 @@ func setupVPC(userName string) (*string, error) { pm, err := getCurrentPayModel(userName) if err != nil { - Config.Logger.Error("Failed to get current paymodel", + Config.Logger.Errorw("Failed to get current paymodel", "error", err, "username", userName, ) return nil, err } - Config.Logger.Info("Setting up remote VPC", + Config.Logger.Infow("Setting up remote VPC", "username", userName, "paymodel", pm.Name, "awsAccount", pm.AWSAccountId, @@ -80,7 +80,7 @@ func setupVPC(userName string) (*string, error) { return nil, err } - Config.Logger.Info("VPC created in remote account", + Config.Logger.Infow("VPC created in remote account", "username", userName, "vpcid", *vpc.Vpc.VpcId, ) @@ -174,7 +174,7 @@ func createSubnet(vpccidr string, vpcid string, svc *ec2.EC2) error { } _, err = svc.CreateSubnet(createSubnet1Input) if err != nil { - Config.Logger.Error("Error creating subnet 1", + Config.Logger.Errorw("Error creating subnet 1", "error", err, "subnet", subnet1Cidr.String(), "vpcid", vpcid, @@ -183,7 +183,7 @@ func createSubnet(vpccidr string, vpcid string, svc *ec2.EC2) error { } _, err = svc.CreateSubnet(createSubnet2Input) if err != nil { - Config.Logger.Error("Error creating subnet 2", + Config.Logger.Errorw("Error creating subnet 2", "error", err, "subnet", subnet2Cidr.String(), "vpcid", vpcid, @@ -196,7 +196,7 @@ func createSubnet(vpccidr string, vpcid string, svc *ec2.EC2) error { func createInternetGW(name string, vpcid string, svc *ec2.EC2) (*string, error) { - Config.Logger.Info("Setting up internet Gateway for VPC", + Config.Logger.Infow("Setting up internet Gateway for VPC", "vpcid", vpcid, "name", name, ) @@ -215,7 +215,7 @@ func createInternetGW(name string, vpcid string, svc *ec2.EC2) (*string, error) } if len(exIgw.InternetGateways) == 0 { - Config.Logger.Info("No existing internet gateways found. Creating internet gateway for VPC", + Config.Logger.Infow("No existing internet gateways found. Creating internet gateway for VPC", "vpcid", vpcid, ) createInternetGWInput := &ec2.CreateInternetGatewayInput{ @@ -265,14 +265,14 @@ func createInternetGW(name string, vpcid string, svc *ec2.EC2) (*string, error) if err != nil { return nil, err } - Config.Logger.Info("Route to internet created", + Config.Logger.Infow("Route to internet created", "route", route, ) return igw.InternetGateway.InternetGatewayId, nil } else { if len(exIgw.InternetGateways[0].Attachments) == 0 { - Config.Logger.Info("Existing gateway found but not attached to IGW. Attaching internet gateway for VPC", + Config.Logger.Infow("Existing gateway found but not attached to IGW. Attaching internet gateway for VPC", "vpcid", vpcid, ) _, err = svc.AttachInternetGateway(&ec2.AttachInternetGatewayInput{ diff --git a/main.go b/main.go index a479b145..88736700 100644 --- a/main.go +++ b/main.go @@ -56,7 +56,7 @@ func main() { ddEnabled := os.Getenv("DD_ENABLED") if strings.ToLower(ddEnabled) == "true" { // config.Logger.Printf("Setting up datadog") - config.Logger.Info("Setting up datadog") + config.Logger.Infow("Setting up datadog") tracer.Start() defer tracer.Stop() if err := profiler.Start( @@ -71,23 +71,23 @@ func main() { ), ); err != nil { // config.Logger.Printf("DD profiler setup failed with error: %s", err) - logger.Error("Failed to setup DD profiler", + logger.Errorw("Failed to setup DD profiler", "error", err, ) } defer profiler.Stop() } else { // config.Logger.Printf("Datadog not enabled in manifest, skipping...") - config.Logger.Info("Datadog not enabled in manifest, skipping...") + config.Logger.Infow("Datadog not enabled in manifest, skipping...") } // config.Logger.Printf("Setting up routes") - config.Logger.Info("Setting up routes for hatchery api") + config.Logger.Infow("Setting up routes for hatchery api") mux := httptrace.NewServeMux() hatchery.RegisterSystem(mux) hatchery.RegisterHatchery(mux) // config.Logger.Printf("Running main") - config.Logger.Info("Running main") + config.Logger.Infow("Running main") log.Fatal(http.ListenAndServe("0.0.0.0:8000", mux)) } From bac7a1eaa87eb3fed2a1158cf2b41ee2c05860ce Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Fri, 5 May 2023 12:57:57 +0200 Subject: [PATCH 03/16] Update go version --- Dockerfile | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 13c397d1..764645ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM quay.io/cdis/golang:1.17-bullseye as build-deps +FROM quay.io/cdis/golang:1.20-bullseye as build-deps ENV CGO_ENABLED=0 ENV GOOS=linux diff --git a/go.mod b/go.mod index c9b22e29..76d4f808 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/uc-cdis/hatchery -go 1.17 +go 1.20 require ( github.com/apparentlymart/go-cidr v1.1.0 From 5ee4f4053d6959c3df21b11b9dee8baf3c8f1e81 Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Fri, 5 May 2023 13:07:06 +0200 Subject: [PATCH 04/16] Update go version --- .github/workflows/golang-ci-workflow.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-ci-workflow.yaml b/.github/workflows/golang-ci-workflow.yaml index 95e2b152..0f094afc 100644 --- a/.github/workflows/golang-ci-workflow.yaml +++ b/.github/workflows/golang-ci-workflow.yaml @@ -7,7 +7,7 @@ jobs: name: golang-ci runs-on: ubuntu-latest env: - GO_VERSION: "1.17" + GO_VERSION: "1.20" COVERAGE_PROFILE_OUTPUT_LOCATION: "./profile.cov" steps: - name: Checkout code / lint code / install dependencies for goveralls / run tests From ea311cbd5ba9e17bf49c820371a838b7600d3aff Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Fri, 5 May 2023 13:11:28 +0200 Subject: [PATCH 05/16] Update go version --- .github/workflows/golang-ci-workflow.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-ci-workflow.yaml b/.github/workflows/golang-ci-workflow.yaml index 0f094afc..1a9eb0cd 100644 --- a/.github/workflows/golang-ci-workflow.yaml +++ b/.github/workflows/golang-ci-workflow.yaml @@ -7,12 +7,12 @@ jobs: name: golang-ci runs-on: ubuntu-latest env: - GO_VERSION: "1.20" COVERAGE_PROFILE_OUTPUT_LOCATION: "./profile.cov" steps: - name: Checkout code / lint code / install dependencies for goveralls / run tests uses: uc-cdis/.github/.github/actions/golang-ci@master with: + GO_VERSION: "1.20" COVERAGE_PROFILE_OUTPUT_LOCATION: ${{ env.COVERAGE_PROFILE_OUTPUT_LOCATION }} - name: Send coverage to coveralls using goveralls env: From 07c1005d54445ddcf744f66094e757816a5bdc24 Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Fri, 5 May 2023 15:06:38 +0200 Subject: [PATCH 06/16] Fix tests --- hatchery/config_test.go | 9 +++++++-- hatchery/pods.go | 3 +-- hatchery/pods_test.go | 21 ++++++++++++++++----- main.go | 1 - 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/hatchery/config_test.go b/hatchery/config_test.go index c803b12c..ab7c7e7a 100644 --- a/hatchery/config_test.go +++ b/hatchery/config_test.go @@ -3,10 +3,15 @@ package hatchery import ( "encoding/json" "testing" + + "go.uber.org/zap" ) func TestLoadConfig(t *testing.T) { - config, err := LoadConfig("../testData/testConfig.json", nil) + zapLogger, _ := zap.NewProduction() + defer zapLogger.Sync() + logger := zapLogger.Sugar() + config, err := LoadConfig("../testData/testConfig.json", logger) if nil != err { t.Errorf("failed to load config, got: %v", err) return @@ -29,5 +34,5 @@ func TestLoadConfig(t *testing.T) { if config.Config.Containers[numContainers-1].Name != "DockstoreTest" { t.Errorf("unexpected more-info app name - expected DockstoreTest, got: %v", config.Config.Containers[numContainers-1].Name) } - config.Logger.Printf("config_test marshalled config: %v", string(jsBytes)) + config.Logger.Infow("config_test marshalled config", "config", string(jsBytes)) } diff --git a/hatchery/pods.go b/hatchery/pods.go index 672f9a78..3908da23 100644 --- a/hatchery/pods.go +++ b/hatchery/pods.go @@ -388,8 +388,7 @@ func buildPod(hatchConfig *FullHatcheryConfig, hatchApp *Container, userName str var envVars []k8sv1.EnvVar // a null image indicates a dockstore app - always mount user volume mountUserVolume := hatchApp.UserVolumeLocation != "" - - Config.Logger.Infow("building pod", + hatchConfig.Logger.Infow("building pod", "podName", podName, "userName", userName, "appName", hatchApp.Name, diff --git a/hatchery/pods_test.go b/hatchery/pods_test.go index 3a9e00f3..3e3b35e0 100644 --- a/hatchery/pods_test.go +++ b/hatchery/pods_test.go @@ -3,10 +3,15 @@ package hatchery import ( "encoding/json" "testing" + + "go.uber.org/zap" ) func TestBuildPodFromJSON(t *testing.T) { - config, err := LoadConfig("../testData/testConfig.json", nil) + zapLogger, _ := zap.NewProduction() + defer zapLogger.Sync() + logger := zapLogger.Sugar() + config, err := LoadConfig("../testData/testConfig.json", logger) if nil != err { t.Errorf("failed to load config, got: %v", err) return @@ -32,11 +37,14 @@ func TestBuildPodFromJSON(t *testing.T) { t.Errorf("failed to marshal JSON - %v", err) } - config.Logger.Printf("pod_test marshalled pod: %v", string(jsBytes)) + config.Logger.Debugw("pod_test marshalled pod", "pod", string(jsBytes)) } func TestBuildPodFromDockstore(t *testing.T) { - config, err := LoadConfig("../testData/testConfig.json", nil) + zapLogger, _ := zap.NewProduction() + defer zapLogger.Sync() + logger := zapLogger.Sugar() + config, err := LoadConfig("../testData/testConfig.json", logger) if nil != err { t.Errorf("failed to load config, got: %v", err) return @@ -48,8 +56,11 @@ func TestBuildPodFromDockstore(t *testing.T) { } app := &config.Config.Containers[numApps-2] pod, err := buildPod(config, app, "frickjack", nil) - if nil != err { + // Log error using suggared loggared from config + config.Logger.Errorw("failed to build a pod", + "error", err, + ) t.Errorf("failed to build a pod - %v", err) } @@ -62,5 +73,5 @@ func TestBuildPodFromDockstore(t *testing.T) { t.Errorf("failed to marshal JSON - %v", err) } - config.Logger.Printf("pod_test marshalled pod: %v", string(jsBytes)) + config.Logger.Debugw("pod_test marshalled pod", "pod", string(jsBytes)) } diff --git a/main.go b/main.go index 88736700..f6fe431c 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,6 @@ func main() { `) return } - // logger := log.New(os.Stdout, "", log.LstdFlags) zapLogger, _ := zap.NewProduction() defer zapLogger.Sync() logger := zapLogger.Sugar() From 0c095904bbcebbe7db791b00f8977a253f3c174c Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Tue, 16 May 2023 19:40:59 +0200 Subject: [PATCH 07/16] test upgraded golang-ci-lint --- .github/workflows/golang-ci-workflow.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-ci-workflow.yaml b/.github/workflows/golang-ci-workflow.yaml index 1a9eb0cd..aaa21fd6 100644 --- a/.github/workflows/golang-ci-workflow.yaml +++ b/.github/workflows/golang-ci-workflow.yaml @@ -10,7 +10,7 @@ jobs: COVERAGE_PROFILE_OUTPUT_LOCATION: "./profile.cov" steps: - name: Checkout code / lint code / install dependencies for goveralls / run tests - uses: uc-cdis/.github/.github/actions/golang-ci@master + uses: uc-cdis/.github/.github/actions/golang-ci@feat/upgrade-golang-ci-lint with: GO_VERSION: "1.20" COVERAGE_PROFILE_OUTPUT_LOCATION: ${{ env.COVERAGE_PROFILE_OUTPUT_LOCATION }} From abddeba3046974cd1ab8a3609eb7a2581768eacc Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Tue, 16 May 2023 20:13:52 +0200 Subject: [PATCH 08/16] Fix linting issues --- hatchery/config.go | 4 +--- hatchery/config_test.go | 1 - hatchery/dockstore.go | 3 +-- hatchery/pods_test.go | 2 -- hatchery/prisma.go | 14 +++++++------- main.go | 7 ++++++- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/hatchery/config.go b/hatchery/config.go index 232ee684..e31abd80 100644 --- a/hatchery/config.go +++ b/hatchery/config.go @@ -7,7 +7,6 @@ import ( "crypto/md5" "encoding/json" "fmt" - "io/ioutil" "os" ) @@ -110,10 +109,9 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful logger := loggerIn if nil == loggerIn { zapLogger, _ := zap.NewProduction() - defer zapLogger.Sync() logger = zapLogger.Sugar() } - plan, err := ioutil.ReadFile(configFilePath) + plan, err := os.ReadFile(configFilePath) data := &FullHatcheryConfig{ Logger: logger, diff --git a/hatchery/config_test.go b/hatchery/config_test.go index ab7c7e7a..bf644db4 100644 --- a/hatchery/config_test.go +++ b/hatchery/config_test.go @@ -9,7 +9,6 @@ import ( func TestLoadConfig(t *testing.T) { zapLogger, _ := zap.NewProduction() - defer zapLogger.Sync() logger := zapLogger.Sugar() config, err := LoadConfig("../testData/testConfig.json", logger) if nil != err { diff --git a/hatchery/dockstore.go b/hatchery/dockstore.go index 2193cf93..913485bb 100644 --- a/hatchery/dockstore.go +++ b/hatchery/dockstore.go @@ -2,7 +2,6 @@ package hatchery import ( "fmt" - "io/ioutil" "log" "os" "strconv" @@ -78,7 +77,7 @@ const magicPort = "${SERVICE_PORT}" // make it easy to test locally // DockstoreComposeFromFile loads a hatchery application (container) // config from a compose.yaml file func DockstoreComposeFromFile(filePath string) (model *ComposeFull, err error) { - fileBytes, err := ioutil.ReadFile(filePath) + fileBytes, err := os.ReadFile(filePath) if nil != err { return nil, err diff --git a/hatchery/pods_test.go b/hatchery/pods_test.go index 3e3b35e0..8fbea833 100644 --- a/hatchery/pods_test.go +++ b/hatchery/pods_test.go @@ -9,7 +9,6 @@ import ( func TestBuildPodFromJSON(t *testing.T) { zapLogger, _ := zap.NewProduction() - defer zapLogger.Sync() logger := zapLogger.Sugar() config, err := LoadConfig("../testData/testConfig.json", logger) if nil != err { @@ -42,7 +41,6 @@ func TestBuildPodFromJSON(t *testing.T) { func TestBuildPodFromDockstore(t *testing.T) { zapLogger, _ := zap.NewProduction() - defer zapLogger.Sync() logger := zapLogger.Sugar() config, err := LoadConfig("../testData/testConfig.json", logger) if nil != err { diff --git a/hatchery/prisma.go b/hatchery/prisma.go index 7fd2fa46..fa6008ad 100644 --- a/hatchery/prisma.go +++ b/hatchery/prisma.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "os" "strconv" @@ -32,14 +32,14 @@ func getPrismaToken(username string, password string) (*string, error) { } defer resp.Body.Close() if resp.StatusCode != 200 { - b, _ := ioutil.ReadAll(resp.Body) + b, _ := io.ReadAll(resp.Body) Config.Logger.Errorw("Error authenticating with Prisma Cloud", "error", string(b), ) return nil, errors.New("Error authenticating with Prisma Cloud: " + string(b)) } //We Read the response body on the line below. - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -82,7 +82,7 @@ func getInstallBundle() (*InstallBundle, error) { } defer resp.Body.Close() if resp.StatusCode != 200 { - b, _ := ioutil.ReadAll(resp.Body) + b, _ := io.ReadAll(resp.Body) // Config.Logger.Print(string(b)) Config.Logger.Errorw("Error getting prismacloud install bundle", "error", string(b), @@ -90,7 +90,7 @@ func getInstallBundle() (*InstallBundle, error) { return nil, errors.New("Error getting install bundle: " + string(b)) } //We Read the response body on the line below. - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -131,14 +131,14 @@ func getPrismaImage() (*string, error) { } defer resp.Body.Close() if resp.StatusCode != 200 { - b, _ := ioutil.ReadAll(resp.Body) + b, _ := io.ReadAll(resp.Body) Config.Logger.Errorw("Error getting prismacloud image name", "error", string(b), ) return nil, errors.New("Error getting prismacloud image name: " + string(b)) } //We Read the response body on the line below. - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/main.go b/main.go index f6fe431c..5d0b1f59 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,12 @@ func main() { return } zapLogger, _ := zap.NewProduction() - defer zapLogger.Sync() + defer func() { + if err := zapLogger.Sync(); err != nil { + // Handle the error appropriately + log.Println("Error syncing logger:", err) + } + }() logger := zapLogger.Sugar() cleanPath, err := verifyPath(configPath) From 5f3710b8b34a2f8f30dbfe3d9bb8105b521832ab Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Tue, 16 May 2023 20:20:05 +0200 Subject: [PATCH 09/16] fix ineffectual assignment to err (ineffassign) --- hatchery/paymodels.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hatchery/paymodels.go b/hatchery/paymodels.go index 009514f3..d516e5e3 100644 --- a/hatchery/paymodels.go +++ b/hatchery/paymodels.go @@ -84,6 +84,12 @@ func getCurrentPayModel(userName string) (result *PayModel, err error) { if Config.Config.PayModelsDynamodbTable != "" { // Fetch pay models from DynamoDB with current_pay_model as `true` pm, err = payModelsFromDatabase(userName, true) + if err != nil { + Config.Logger.Errorw("Failed to get current pay model from dynamoDB", + "error", err, + ) + return nil, err + } } payModel := PayModel{} From 0fd83c8bb0ca89e2d9717d11e8b0e9659b4ddf17 Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Thu, 18 May 2023 18:40:13 +0200 Subject: [PATCH 10/16] use workflow from master --- .github/workflows/golang-ci-workflow.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golang-ci-workflow.yaml b/.github/workflows/golang-ci-workflow.yaml index aaa21fd6..1a9eb0cd 100644 --- a/.github/workflows/golang-ci-workflow.yaml +++ b/.github/workflows/golang-ci-workflow.yaml @@ -10,7 +10,7 @@ jobs: COVERAGE_PROFILE_OUTPUT_LOCATION: "./profile.cov" steps: - name: Checkout code / lint code / install dependencies for goveralls / run tests - uses: uc-cdis/.github/.github/actions/golang-ci@feat/upgrade-golang-ci-lint + uses: uc-cdis/.github/.github/actions/golang-ci@master with: GO_VERSION: "1.20" COVERAGE_PROFILE_OUTPUT_LOCATION: ${{ env.COVERAGE_PROFILE_OUTPUT_LOCATION }} From ee26e98eac7763ab75d945ff0799fc3c9e9f7e2e Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Tue, 23 May 2023 18:10:02 +0200 Subject: [PATCH 11/16] tidy go.mod --- go.mod | 2 +- go.sum | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 76d4f808..2e784274 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.20 require ( github.com/apparentlymart/go-cidr v1.1.0 github.com/aws/aws-sdk-go v1.43.28 + go.uber.org/zap v1.24.0 gopkg.in/DataDog/dd-trace-go.v1 v1.33.0 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.22.3 @@ -45,7 +46,6 @@ require ( github.com/tinylib/msgp v1.1.6 // indirect go.uber.org/atomic v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect - go.uber.org/zap v1.24.0 // indirect golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect diff --git a/go.sum b/go.sum index 0ce90799..1e8071ca 100644 --- a/go.sum +++ b/go.sum @@ -72,6 +72,7 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l github.com/aws/aws-sdk-go v1.43.28 h1:HrBUf2pYEMRB3GDkSa/bZ2lkZIe8gSUOz/IEupG1Te0= github.com/aws/aws-sdk-go v1.43.28/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -80,7 +81,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -334,6 +334,7 @@ github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ= github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -398,8 +399,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tinylib/msgp v1.1.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw= @@ -432,6 +433,7 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= @@ -778,7 +780,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 25eaee6f6e9f9447d24969af0879f5a71f24821d Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Tue, 23 May 2023 20:58:50 +0200 Subject: [PATCH 12/16] Remove old logging --- .secrets.baseline | 4 ++-- hatchery/alb.go | 4 ++-- hatchery/config.go | 8 -------- hatchery/ecs.go | 19 ------------------- hatchery/pods.go | 1 - hatchery/prisma.go | 3 --- hatchery/transitgateway.go | 1 - main.go | 7 ------- 8 files changed, 4 insertions(+), 43 deletions(-) diff --git a/.secrets.baseline b/.secrets.baseline index e2e4a088..d77b10ce 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": "go.sum|^.secrets.baseline$", "lines": null }, - "generated_at": "2023-05-04T21:28:41Z", + "generated_at": "2023-05-23T18:16:26Z", "plugins_used": [ { "name": "AWSKeyDetector" @@ -99,7 +99,7 @@ "hashed_secret": "96e77458817d8486fab4c56c8d4c51b83f85c79d", "is_secret": false, "is_verified": false, - "line_number": 465, + "line_number": 460, "type": "Secret Keyword" } ], diff --git a/hatchery/alb.go b/hatchery/alb.go index b80ca266..c4f510e5 100644 --- a/hatchery/alb.go +++ b/hatchery/alb.go @@ -42,14 +42,14 @@ func (creds *CREDS) createTargetGroup(userName string, vpcId string, svc *elbv2. "error", aerr.Error(), "code", aerr.Code(), "message", aerr.Message(), - "user", userName, + "username", userName, ) return nil, err } } else { Config.Logger.Errorw("Error creating target group", "error", err.Error(), - "user", userName, + "username", userName, ) return nil, err } diff --git a/hatchery/config.go b/hatchery/config.go index e31abd80..4655b894 100644 --- a/hatchery/config.go +++ b/hatchery/config.go @@ -119,7 +119,6 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful if nil != err { cwd, _ := os.Getwd() - // data.Logger.Printf("failed to load %v from cwd %v got - %v", configFilePath, cwd, err) data.Logger.Errorw("Failed to load config file.", "configFilePath", configFilePath, "cwd", cwd, @@ -127,7 +126,6 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful ) return data, err } - // data.Logger.Printf("loaded config: %v", string(plan)) data.Logger.Debug("Loaded config.", "config", string(plan), ) @@ -140,27 +138,23 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful if info.Name == "" { return nil, fmt.Errorf("Empty name for more-configs app at: %v", info.Path) } - // data.Logger.Printf("loading config from %v", info.Path) data.Logger.Debug("Loading config.", "path", info.Path, ) composeModel, err := DockstoreComposeFromFile(info.Path) if nil != err { - // data.Logger.Printf("failed to load config from %v, got: %v", info.Path, err) data.Logger.Errorw("Failed to load config.", "path", info.Path, "error", err, ) return nil, err } - // data.Logger.Printf("%v", composeModel) data.Logger.Debug("Loaded config.", "composeModel", composeModel, ) hatchApp, err := composeModel.BuildHatchApp() hatchApp.Name = info.Name if nil != err { - // data.Logger.Printf("failed to translate app, got: %v", err) data.Logger.Errorw("Failed to translate app.", "error", err, ) @@ -168,7 +162,6 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful } data.Config.Containers = append(data.Config.Containers, *hatchApp) } else { - // data.Logger.Printf("ignoring config of unsupported type: %v", info.AppType) data.Logger.Warnw("Ignoring config of unsupported type.", "appType", info.AppType, ) @@ -182,7 +175,6 @@ func LoadConfig(configFilePath string, loggerIn *zap.SugaredLogger) (config *Ful } if data.Config.PayModelsDynamodbTable == "" { - // data.Logger.Printf("Warning: no 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") data.Logger.Warnw("No 'pay-models-dynamodb-table' in configuration: will be unable to query pay model data in DynamoDB") } diff --git a/hatchery/ecs.go b/hatchery/ecs.go index 163ee676..d24acf8f 100644 --- a/hatchery/ecs.go +++ b/hatchery/ecs.go @@ -126,9 +126,6 @@ func (sess *CREDS) findEcsCluster() (*ecs.Cluster, error) { return nil, aerr } } else { - // Print the error, cast err to awserr.Error to get the Code and - // Message from an error. - // Config.Logger.Println(err.Error()) Config.Logger.Errorw("Error describing ECS cluster", "clusterName", clusterName, "error", err.Error(), @@ -238,14 +235,12 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce lastActivityTime, err := getKernelIdleTimeWithContext(ctx, accessToken) status.LastActivityTime = lastActivityTime if err != nil { - // Config.Logger.Println(err.Error()) Config.Logger.Debug("Cannot get kernel idle time.", "error", err.Error(), "userName", userName, ) } } else { - // Config.Logger.Println(err.Error()) Config.Logger.Debug("Cannot convert idle time limit to int.", "error", err.Error(), "userName", userName, @@ -494,7 +489,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod Key: "GEN3_ENDPOINT", Value: os.Getenv("GEN3_ENDPOINT"), }) - Config.Logger.Infow("Setting up EFS.", "username", userName, ) @@ -506,7 +500,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod ) return err } - Config.Logger.Infow("Setting up task role.", "username", userName, ) @@ -519,7 +512,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod ) return err } - Config.Logger.Infow("Setting up task execution role.", "username", userName, ) @@ -532,7 +524,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod ) return err } - Config.Logger.Infow("Setting up ECS task definition.", "username", userName, ) @@ -620,7 +611,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod } return err } - Config.Logger.Infow("Launching ECS workspace service.", "username", userName, ) @@ -640,7 +630,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod } return err } - Config.Logger.Infow("Setting up Transit Gateway.", "username", userName, ) @@ -653,7 +642,6 @@ func launchEcsWorkspace(userName string, hash string, accessToken string, payMod ) return err } - Config.Logger.Infow("Launched ECS workspace service.", "username", userName, "launchTask", launchTask, @@ -706,7 +694,6 @@ func (sess *CREDS) launchService(ctx context.Context, taskDefArn string, userNam switch aerr.Code() { case ecs.ErrCodeInvalidParameterException: if aerr.Error() == "InvalidParameterException: Creation of service was not idempotent." { - // Config.Logger.Print("Service already exists.. ") Config.Logger.Infow("ECS Service already exists. Moving on.", "username", userName, ) @@ -714,7 +701,6 @@ func (sess *CREDS) launchService(ctx context.Context, taskDefArn string, userNam } } } - // Config.Logger.Println(err.Error()) Config.Logger.Errorw("Error occurred when creating ECS service.", "username", userName, "error", err.Error(), @@ -754,7 +740,6 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa Credentials: creds, Region: aws.String("us-east-1"), }))) - Config.Logger.Infow("Creating ECS task definition.", "username", userName, ) @@ -802,7 +787,6 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa if Config.Config.PrismaConfig.Enable { installBundle, err := getInstallBundle() if err != nil { - // Config.Logger.Print(err, " error getting prisma install bundle") Config.Logger.Errorw("Error occurred when getting prisma install bundle.", "username", userName, "error", err.Error(), @@ -812,7 +796,6 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa image, err := getPrismaImage() if err != nil { - // Config.Logger.Print(err, " error getting prisma image") Config.Logger.Errorw("Error occurred when getting prisma image.", "username", userName, "error", err.Error(), @@ -884,7 +867,6 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa ) if err != nil { - // Config.Logger.Print(err, " Couldn't register ECS task definition") Config.Logger.Errorw("Couldn't register ECS task definition.", "username", userName, "error", err.Error(), @@ -893,7 +875,6 @@ func (sess *CREDS) CreateTaskDefinition(input *CreateTaskDefinitionInput, userNa } td := resp.TaskDefinition - Config.Logger.Infow("Created ECS task definition.", "username", userName, "taskdefinition", fmt.Sprintf("%s:%d", aws.StringValue(td.Family), aws.Int64Value(td.Revision)), diff --git a/hatchery/pods.go b/hatchery/pods.go index 3908da23..4cc3344a 100644 --- a/hatchery/pods.go +++ b/hatchery/pods.go @@ -183,7 +183,6 @@ func podStatus(ctx context.Context, userName string, accessToken string, payMode status := WorkspaceStatus{} podClient, isExternalClient, err := getPodClient(ctx, userName, payModelPtr) if err != nil { - // Config.Logger.Panic("Error trying to fetch kubeConfig: %v", err) status.Status = fmt.Sprintf("%v", err) return &status, err } diff --git a/hatchery/prisma.go b/hatchery/prisma.go index fa6008ad..9ce75e81 100644 --- a/hatchery/prisma.go +++ b/hatchery/prisma.go @@ -46,7 +46,6 @@ func getPrismaToken(username string, password string) (*string, error) { var result Token if err := json.Unmarshal(body, &result); err != nil { - // fmt.Println("Invalid response from prisma auth endpoint: " + authEndpoint) Config.Logger.Errorw("Invalid response from prisma auth endpoint", "error", err, "endpoint", authEndpoint, @@ -83,7 +82,6 @@ func getInstallBundle() (*InstallBundle, error) { defer resp.Body.Close() if resp.StatusCode != 200 { b, _ := io.ReadAll(resp.Body) - // Config.Logger.Print(string(b)) Config.Logger.Errorw("Error getting prismacloud install bundle", "error", string(b), ) @@ -96,7 +94,6 @@ func getInstallBundle() (*InstallBundle, error) { } var result InstallBundle if err := json.Unmarshal(body, &result); err != nil { - // fmt.Println("Invalid response from prisma install_bundle endpoint: " + installBundleEndpoint) Config.Logger.Errorw("Invalid response from prisma install_bundle endpoint", "error", err, "endpoint", installBundleEndpoint, diff --git a/hatchery/transitgateway.go b/hatchery/transitgateway.go index 553b9f15..3d61e085 100644 --- a/hatchery/transitgateway.go +++ b/hatchery/transitgateway.go @@ -237,7 +237,6 @@ func createTransitGateway(sess *session.Session, userName string) (tgwid *string return tg.TransitGateway.TransitGatewayId, tg.TransitGateway.TransitGatewayArn, tg.TransitGateway.Options.AssociationDefaultRouteTableId, nil } else { - // Config.Logger.Print("Existing transit gateway found. Skipping creation...") Config.Logger.Infow("Existing transit gateway found. Skipping creation...", "transit_gateway_id", *exTg.TransitGateways[len(exTg.TransitGateways)-1].TransitGatewayId, "username", userName, diff --git a/main.go b/main.go index 5d0b1f59..65506356 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,6 @@ func main() { hatchery.Config = config ddEnabled := os.Getenv("DD_ENABLED") if strings.ToLower(ddEnabled) == "true" { - // config.Logger.Printf("Setting up datadog") config.Logger.Infow("Setting up datadog") tracer.Start() defer tracer.Stop() @@ -74,24 +73,18 @@ func main() { // profiler.GoroutineProfile, ), ); err != nil { - // config.Logger.Printf("DD profiler setup failed with error: %s", err) logger.Errorw("Failed to setup DD profiler", "error", err, ) } defer profiler.Stop() } else { - // config.Logger.Printf("Datadog not enabled in manifest, skipping...") config.Logger.Infow("Datadog not enabled in manifest, skipping...") } - - // config.Logger.Printf("Setting up routes") config.Logger.Infow("Setting up routes for hatchery api") mux := httptrace.NewServeMux() hatchery.RegisterSystem(mux) hatchery.RegisterHatchery(mux) - - // config.Logger.Printf("Running main") config.Logger.Infow("Running main") log.Fatal(http.ListenAndServe("0.0.0.0:8000", mux)) } From 2671d4e4948755c1d56f600ee1358dd10fa654c7 Mon Sep 17 00:00:00 2001 From: "J. Q" <55899496+jawadqur@users.noreply.github.com> Date: Thu, 8 Jun 2023 11:49:04 -0500 Subject: [PATCH 13/16] Update hatchery/paymodels.go Co-authored-by: Pauline Ribeyre <4224001+paulineribeyre@users.noreply.github.com> --- hatchery/paymodels.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hatchery/paymodels.go b/hatchery/paymodels.go index d516e5e3..6c337b01 100644 --- a/hatchery/paymodels.go +++ b/hatchery/paymodels.go @@ -31,7 +31,7 @@ func payModelsFromDatabase(userName string, current bool) (payModels *[]PayModel } expr, err := expression.NewBuilder().WithFilter(filt).Build() if err != nil { - Config.Logger.Errorw("Got error building dyamoDB expression", + Config.Logger.Errorw("Got error building dynamoDB expression", "error", err, ) return nil, err From 9b9ceadc25f761d4075a8f1ec2c403bf9b179d32 Mon Sep 17 00:00:00 2001 From: "J. Q" <55899496+jawadqur@users.noreply.github.com> Date: Thu, 8 Jun 2023 11:49:44 -0500 Subject: [PATCH 14/16] Update main.go Co-authored-by: Pauline Ribeyre <4224001+paulineribeyre@users.noreply.github.com> --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 65506356..78c1e0ce 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,7 @@ func main() { } config, err := hatchery.LoadConfig(cleanPath, logger) if err != nil { - logger.Errorf("Failed to load config - got %v", err) + config.Logger.Errorf("Failed to load config - got %v", err) return } hatchery.Config = config From 0b784863b3f9bbf4ae2cc03bdfc6ce78d8d19748 Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Thu, 8 Jun 2023 19:52:10 +0300 Subject: [PATCH 15/16] Remove old logging --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 78c1e0ce..5ffeb6cc 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,6 @@ func main() { cleanPath, err := verifyPath(configPath) if err != nil { - // logger.Printf(fmt.Sprintf("Failed to load config - got %v", err)) logger.Errorf("Failed to load config - got %v", err) return } From cfb98d95d940ab26a54e768bd2bd994c106e4635 Mon Sep 17 00:00:00 2001 From: Jawad Qureshi Date: Thu, 15 Jun 2023 17:01:26 +0300 Subject: [PATCH 16/16] remove comments --- .secrets.baseline | 4 ++-- hatchery/ec2.go | 2 +- hatchery/ecs.go | 1 - hatchery/hatchery.go | 4 +--- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.secrets.baseline b/.secrets.baseline index d77b10ce..f2c1a631 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": "go.sum|^.secrets.baseline$", "lines": null }, - "generated_at": "2023-05-23T18:16:26Z", + "generated_at": "2023-06-15T14:01:20Z", "plugins_used": [ { "name": "AWSKeyDetector" @@ -99,7 +99,7 @@ "hashed_secret": "96e77458817d8486fab4c56c8d4c51b83f85c79d", "is_secret": false, "is_verified": false, - "line_number": 460, + "line_number": 459, "type": "Secret Keyword" } ], diff --git a/hatchery/ec2.go b/hatchery/ec2.go index f6f52fb2..7d2706cc 100644 --- a/hatchery/ec2.go +++ b/hatchery/ec2.go @@ -42,7 +42,7 @@ func (creds *CREDS) describeWorkspaceNetwork(userName string) (*NetworkInfo, err if err != nil { return nil, err } - // TODO: BETTER ERROR HANDLING HERE!! + if len(vpcs.Vpcs) == 0 { return nil, fmt.Errorf("No existing vpcs found.") } diff --git a/hatchery/ecs.go b/hatchery/ecs.go index d24acf8f..725210e5 100644 --- a/hatchery/ecs.go +++ b/hatchery/ecs.go @@ -281,7 +281,6 @@ func (sess *CREDS) statusEcsWorkspace(ctx context.Context, userName string, acce } // Terminate workspace running in ECS -// TODO: Make this terminate ALB as well. func terminateEcsWorkspace(ctx context.Context, userName string, accessToken string, awsAcctID string) (string, error) { Config.Logger.Infow("Terminating ECS workspace.", "username", userName, diff --git a/hatchery/hatchery.go b/hatchery/hatchery.go index 6102de78..a5005e81 100644 --- a/hatchery/hatchery.go +++ b/hatchery/hatchery.go @@ -235,7 +235,7 @@ func launch(w http.ResponseWriter, r *http.Request) { if payModel.Status != "active" { // send 500 response. - // TODO: 403 is the correct code, but it triggers a 302 to the default 403 page in revproxy instead of showing error message. + // 403 is the correct code, but it triggers a 302 to the default 403 page in revproxy instead of showing error message. Config.Logger.Errorw("Launch forbidden: pay model is not active", "username", userName, "paymodelstatus", payModel.Status, @@ -328,7 +328,6 @@ func getBearerToken(r *http.Request) string { // ECS functions // Function to create ECS cluster. -// TODO: NEED TO CALL THIS FUNCTION IF IT DOESN'T EXIST!!! func createECSCluster(w http.ResponseWriter, r *http.Request) { userName := getCurrentUserName(r) payModel, err := getCurrentPayModel(userName) @@ -359,7 +358,6 @@ func createECSCluster(w http.ResponseWriter, r *http.Request) { } else { reader = strings.NewReader(result.String()) } - // TODO: What's happening here? _, err = io.Copy(w, reader) if err != nil { Config.Logger.Errorw("Error copying response",