/
model_build_access_log_subscription.go
113 lines (95 loc) · 3.42 KB
/
model_build_access_log_subscription.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package gateway
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"sigs.k8s.io/controller-runtime/pkg/client"
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
"github.com/aws/aws-application-networking-k8s/pkg/k8s"
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
model "github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
"github.com/aws/aws-application-networking-k8s/pkg/utils"
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
)
type AccessLogSubscriptionModelBuilder interface {
Build(ctx context.Context, alp *anv1alpha1.AccessLogPolicy) (core.Stack, *model.AccessLogSubscription, error)
}
type accessLogSubscriptionModelBuilder struct {
log gwlog.Logger
client client.Client
}
func NewAccessLogSubscriptionModelBuilder(log gwlog.Logger, client client.Client) *accessLogSubscriptionModelBuilder {
return &accessLogSubscriptionModelBuilder{
client: client,
log: log,
}
}
func (b *accessLogSubscriptionModelBuilder) Build(
ctx context.Context,
accessLogPolicy *anv1alpha1.AccessLogPolicy,
) (core.Stack, *model.AccessLogSubscription, error) {
stack := core.NewDefaultStack(core.StackID(k8s.NamespacedName(accessLogPolicy)))
task := accessLogSubscriptionModelBuildTask{
log: b.log,
stack: stack,
accessLogPolicy: accessLogPolicy,
}
if err := task.run(ctx); err != nil {
return nil, nil, err
}
return task.stack, task.accessLogSubscription, nil
}
type accessLogSubscriptionModelBuildTask struct {
log gwlog.Logger
stack core.Stack
accessLogPolicy *anv1alpha1.AccessLogPolicy
accessLogSubscription *model.AccessLogSubscription
}
func (t *accessLogSubscriptionModelBuildTask) run(ctx context.Context) error {
var eventType = core.CreateEvent
if t.accessLogPolicy.DeletionTimestamp != nil {
eventType = core.DeleteEvent
} else if _, ok := t.accessLogPolicy.Annotations[anv1alpha1.AccessLogSubscriptionAnnotationKey]; ok {
eventType = core.UpdateEvent
}
sourceType := model.ServiceSourceType
if t.accessLogPolicy.Spec.TargetRef.Kind == "Gateway" {
sourceType = model.ServiceNetworkSourceType
}
sourceName, err := utils.TargetRefToLatticeResourceName(t.accessLogPolicy.Spec.TargetRef, t.accessLogPolicy.Namespace)
if err != nil && eventType != core.DeleteEvent {
return err
}
destinationArn := t.accessLogPolicy.Spec.DestinationArn
if destinationArn == nil {
if eventType != core.DeleteEvent {
return fmt.Errorf("access log policy's destinationArn cannot be nil")
}
destinationArn = aws.String("")
}
var status *model.AccessLogSubscriptionStatus
if eventType != core.CreateEvent {
value, exists := t.accessLogPolicy.Annotations[anv1alpha1.AccessLogSubscriptionAnnotationKey]
if exists {
status = &model.AccessLogSubscriptionStatus{
Arn: value,
}
} else {
t.log.Debugf("access log policy is missing %s annotation during %s event",
anv1alpha1.AccessLogSubscriptionAnnotationKey, eventType)
}
}
alsSpec := model.AccessLogSubscriptionSpec{
SourceType: sourceType,
SourceName: sourceName,
DestinationArn: *destinationArn,
ALPNamespacedName: t.accessLogPolicy.GetNamespacedName(),
EventType: eventType,
}
t.accessLogSubscription = model.NewAccessLogSubscription(t.stack, alsSpec, status)
err = t.stack.AddResource(t.accessLogSubscription)
if err != nil {
return err
}
return nil
}