-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda.go
63 lines (50 loc) · 1.54 KB
/
lambda.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package transport
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/aws/aws-xray-sdk-go/header"
"github.com/aws/aws-xray-sdk-go/xray"
"github.com/martinbaillie/ocistow/pkg/config"
"github.com/martinbaillie/ocistow/pkg/service"
)
type StowRequest struct {
SrcImgRef string `json:"SrcImageRef"`
DstImgRef string `json:"DstImageRef"`
Annotations map[string]string `json:"Annotations"`
}
type StowLambdaHandler func(context.Context, StowRequest) error
func NewStowLambdaHandler(cfg *config.Config, svc service.Service) StowLambdaHandler {
return func(ctx context.Context, req StowRequest) error {
logger := cfg.Logger()
if *cfg.AWSXray {
// Extract key Lambda invoke information.
var traceID string
{
if traceHeaderVal := ctx.Value(xray.LambdaTraceHeaderKey); traceHeaderVal != nil {
traceHeader := traceHeaderVal.(string)
traceID = header.FromString(traceHeader).TraceID
}
}
lc, _ := lambdacontext.FromContext(ctx)
// And create a contextual Lambda invoke-local logger.
ll := logger.With().
Str("aws_request_id", lc.AwsRequestID).
Str("trace_id", traceID).
Logger()
logger = &ll
}
ctx = logger.WithContext(ctx)
if *cfg.Copy {
if err := svc.Copy(ctx, req.SrcImgRef, req.DstImgRef, req.Annotations); err != nil {
return fmt.Errorf("failed copy: %w", err)
}
}
if *cfg.Sign {
if err := svc.Sign(ctx, req.DstImgRef, req.Annotations); err != nil {
return fmt.Errorf("failed sign: %w", err)
}
}
return nil
}
}