Skip to content

Commit d54e72f

Browse files
committed
Fix Lambda handler, and move to runner package
Tool: gitpod/catfood.gitpod.cloud
1 parent b003ddb commit d54e72f

File tree

3 files changed

+77
-54
lines changed

3 files changed

+77
-54
lines changed

gitpod-network-check/cmd/lambda_handler.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,16 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
6-
"net/http"
75
"os"
8-
"time"
96

107
"github.com/aws/aws-lambda-go/lambda"
118
log "github.com/sirupsen/logrus"
129
"github.com/spf13/cobra"
1310

14-
"github.com/gitpod-io/enterprise-deployment-toolkit/gitpod-network-check/pkg/lambda_types"
11+
"github.com/gitpod-io/enterprise-deployment-toolkit/gitpod-network-check/pkg/runner"
1512
)
1613

17-
// Core logic for handling the Lambda event
18-
// This function is called by the aws-lambda-go library.
19-
func handleLambdaEvent(ctx context.Context, request lambda_types.CheckRequest) (lambda_types.CheckResponse, error) {
20-
log.Infof("Lambda Handler: Received check request for %d endpoints.", len(request.Endpoints))
21-
22-
response := lambda_types.CheckResponse{
23-
Results: make(map[string]lambda_types.CheckResult),
24-
}
25-
26-
client := &http.Client{
27-
Timeout: 10 * time.Second, // Consider making this configurable if needed
28-
}
29-
30-
// Perform checks
31-
for name, url := range request.Endpoints {
32-
log.Debugf("Lambda Handler: Checking endpoint: %s (%s)", name, url)
33-
34-
// Use the context provided by the Lambda runtime
35-
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
36-
if err != nil {
37-
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("failed to create request: %v", err)}
38-
log.Warnf(" -> Failed (request creation): %v", err)
39-
continue
40-
}
41-
42-
resp, err := client.Do(req)
43-
if err != nil {
44-
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("HTTP request failed: %v", err)}
45-
log.Warnf(" -> Failed (HTTP request): %v", err)
46-
} else {
47-
resp.Body.Close() // Ensure body is closed
48-
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
49-
response.Results[name] = lambda_types.CheckResult{Success: true}
50-
log.Debugf(" -> Success (Status: %d)", resp.StatusCode)
51-
} else {
52-
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("unexpected status code: %d", resp.StatusCode)}
53-
log.Warnf(" -> Failed (Status: %d)", resp.StatusCode)
54-
}
55-
}
56-
}
57-
58-
log.Info("Lambda Handler: Check processing complete.")
59-
// The lambda library handles marshalling the response and deals with errors.
60-
// We return the response struct and nil error if processing logic itself didn't fail critically.
61-
return response, nil
62-
}
63-
6414
// lambdaHandlerCmd is the Cobra command invoked when the binary is run with the "lambda-handler" argument.
6515
// This happens inside the AWS Lambda environment via the bootstrap script.
6616
var lambdaHandlerCmd = &cobra.Command{
@@ -79,7 +29,7 @@ var lambdaHandlerCmd = &cobra.Command{
7929
// The aws-lambda-go library takes over execution when lambda.Start is called.
8030
// It handles reading events, invoking the handler, and writing responses.
8131
log.Info("Lambda Handler: Starting AWS Lambda handler loop.")
82-
lambda.Start(handleLambdaEvent)
32+
lambda.Start(runner.HandleLambdaEvent)
8333
// lambda.Start blocks and never returns unless there's a critical error during initialization
8434
log.Error("Lambda Handler: lambda.Start returned unexpectedly (should not happen)")
8535
return fmt.Errorf("lambda.Start returned unexpectedly")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package runner
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"time"
9+
10+
"github.com/gitpod-io/enterprise-deployment-toolkit/gitpod-network-check/pkg/lambda_types"
11+
log "github.com/sirupsen/logrus"
12+
)
13+
14+
// HandleLambdaEvent is handling the Lambda event.
15+
// This function is called by the aws-lambda-go library.
16+
func HandleLambdaEvent(ctx context.Context, request lambda_types.CheckRequest) (lambda_types.CheckResponse, error) {
17+
log.Infof("Lambda Handler: Received check request for %d endpoints.", len(request.Endpoints))
18+
19+
response := lambda_types.CheckResponse{
20+
Results: make(map[string]lambda_types.CheckResult),
21+
}
22+
23+
client := &http.Client{
24+
Timeout: 10 * time.Second, // Consider making this configurable if needed
25+
}
26+
27+
// Perform checks
28+
for name, targetUrlStr := range request.Endpoints {
29+
30+
targetUrl, err := url.Parse(targetUrlStr)
31+
if err != nil {
32+
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("invalid URL: %v", err)}
33+
log.Warnf(" -> Failed URL parsing for '%s': %v", targetUrlStr, err)
34+
continue
35+
}
36+
if targetUrl.Scheme == "" {
37+
// Default to HTTPS if no scheme is provided
38+
targetUrl.Scheme = "https"
39+
}
40+
41+
log.Debugf("Lambda Handler: Checking endpoint: %s (%s)", name, targetUrl.String())
42+
43+
// Use the context provided by the Lambda runtime
44+
log := log.WithField("endpoint", targetUrl.String())
45+
46+
req, err := http.NewRequestWithContext(ctx, "GET", targetUrl.String(), nil)
47+
if err != nil {
48+
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("failed to create request: %v", err)}
49+
log.Warnf(" -> Failed (request creation): %v", err)
50+
continue
51+
}
52+
53+
resp, err := client.Do(req)
54+
if err != nil {
55+
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("HTTP request failed: %v", err)}
56+
log.Warnf(" -> Failed (HTTP request): %v", err)
57+
} else {
58+
resp.Body.Close() // Ensure body is closed
59+
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
60+
response.Results[name] = lambda_types.CheckResult{Success: true}
61+
log.Debugf(" -> Success (Status: %d)", resp.StatusCode)
62+
} else {
63+
response.Results[name] = lambda_types.CheckResult{Success: false, Error: fmt.Sprintf("unexpected status code: %d", resp.StatusCode)}
64+
log.Warnf(" -> Failed (Status: %d)", resp.StatusCode)
65+
}
66+
}
67+
}
68+
69+
log.Info("Lambda Handler: Check processing complete.")
70+
// The lambda library handles marshalling the response and deals with errors.
71+
// We return the response struct and nil error if processing logic itself didn't fail critically.
72+
return response, nil
73+
}

gitpod-network-check/cmd/lambda_handler_test.go renamed to gitpod-network-check/pkg/runner/lambda_handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package runner
22

33
import (
44
"context"
@@ -133,7 +133,7 @@ func TestHandleLambdaEvent(t *testing.T) {
133133
for _, tt := range tests {
134134
tt := tt // Capture range variable
135135
t.Run(tt.name, func(t *testing.T) {
136-
actualResp, err := handleLambdaEvent(context.Background(), tt.request)
136+
actualResp, err := HandleLambdaEvent(context.Background(), tt.request)
137137
if err != nil {
138138
t.Errorf("handleLambdaEvent returned an unexpected error: %v", err)
139139
}

0 commit comments

Comments
 (0)