-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathprovider.go
135 lines (116 loc) · 5.54 KB
/
provider.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package tracking
import (
"fmt"
"sigs.k8s.io/aws-load-balancer-controller/pkg/algorithm"
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
)
//we use AWS tags and K8s labels to track resources we have created.
//
//For AWS resources created by this controller, the tagging strategy is as follows:
// * `elbv2.k8s.aws/cluster: cluster-name` will be applied on all AWS resources.
// * `ingress.k8s.aws/stack: stack-id` will be applied on all AWS resources provisioned for Ingress resources:
// * For explicit IngressGroup, `stack-id` will be `groupName`
// * For implicit IngressGroup, `stack-id` will be `namespace/ingressName`
// * `ingress.k8s.aws/resource: resource-id` will be applied on all AWS resources provisioned for Ingress resources:
// * For LoadBalancer, `resource-id` will be `LoadBalancer`
// * For Managed LB SecurityGroup, `resource-id` will be `ManagedLBSecurityGroup`
// * For TargetGroup, `resource-id` will be `namespace/ingressName-serviceName:servicePort`
// * `service.k8s.aws/stack: stack-id` will be applied on all AWS resources provisioned for Service resources:
// * `stack-id` will be `namespace/serviceName`
// * `service.k8s.aws/resource: resource-id` will be applied on all AWS resources provisioned for Service resources:
// * For LoadBalancer, `resource-id` will be `LoadBalancer`
// * For TargetGroup, `resource-id` will be `namespace/serviceName:servicePort`
//For K8s resources created by this controller, the labelling strategy is as follows:
// * For explicit IngressGroup, the following tags will be applied on all K8s resources:
// * `ingress.k8s.aws/stack: groupName`
// * For implicit IngressGroup, the following tags will be applied on all K8s resources:
// * `ingress.k8s.aws/stack-namespace: namespace`
// * `ingress.k8s.aws/stack-name: ingressName`
// * For Service, the following tags will be applied on all K8s resources:
// * `service.k8s.aws/stack-namespace: namespace`
// * `service.k8s.aws/stack-name: serviceName`
// Legacy AWS TagKey for cluster resources, which is used by AWSALBIngressController(v1.1.3+)
const clusterNameTagKeyLegacy = "ingress.k8s.aws/cluster"
// an abstraction that generates metadata to track actual resources provisioned for stack.
type Provider interface {
// ResourceIDTagKey provide the tagKey for resourceID.
ResourceIDTagKey() string
// StackTags provide the tags for stack.
StackTags(stack core.Stack) map[string]string
// ResourceTags provide the tags for stack resources
ResourceTags(stack core.Stack, res core.Resource, additionalTags map[string]string) map[string]string
// StackLabels provide the suitable k8s labels for stack.
StackLabels(stack core.Stack) map[string]string
// StackTagsLegacy provides the tags for stack with legacy clusterName.
// this is for backwards compatibility with AWSALBIngressController(v1.1.3+)
StackTagsLegacy(stack core.Stack) map[string]string
// LegacyTagKeys returns AWS tag keys added to AWS resources provisioned by AWSALBIngressController(v1.1.3+).
// These tag keys is required for AWSALBIngressController(v1.1.3+) to identify resources.
// To be able to downgrade AWSLoadBalancerController to AWSALBIngressController(v1.1.3+), we shouldn't remove these tag keys.
LegacyTagKeys() []string
}
// NewDefaultProvider constructs defaultProvider
func NewDefaultProvider(clusterTagPrefix string, resourceTagPrefix string, clusterName string) *defaultProvider {
return &defaultProvider{
clusterTagPrefix: clusterTagPrefix,
resourceTagPrefix: resourceTagPrefix,
clusterName: clusterName,
}
}
var _ Provider = &defaultProvider{}
// defaultImplementation for Provider
type defaultProvider struct {
clusterTagPrefix string
resourceTagPrefix string
clusterName string
}
func (p *defaultProvider) ResourceIDTagKey() string {
return p.prefixedTrackingKey(p.resourceTagPrefix, "resource")
}
func (p *defaultProvider) StackTags(stack core.Stack) map[string]string {
stackID := stack.StackID()
return map[string]string{
p.prefixedTrackingKey(p.clusterTagPrefix, "cluster"): p.clusterName,
p.prefixedTrackingKey(p.resourceTagPrefix, "stack"): stackID.String(),
}
}
func (p *defaultProvider) ResourceTags(stack core.Stack, res core.Resource, additionalTags map[string]string) map[string]string {
stackTags := p.StackTags(stack)
resourceIDTags := map[string]string{
p.ResourceIDTagKey(): res.ID(),
}
return algorithm.MergeStringMap(stackTags, resourceIDTags, additionalTags)
}
func (p *defaultProvider) StackLabels(stack core.Stack) map[string]string {
stackID := stack.StackID()
if stackID.Namespace == "" {
return map[string]string{
p.prefixedTrackingKey(p.resourceTagPrefix, "stack"): stackID.Name,
}
}
return map[string]string{
p.prefixedTrackingKey(p.resourceTagPrefix, "stack-namespace"): stackID.Namespace,
p.prefixedTrackingKey(p.resourceTagPrefix, "stack-name"): stackID.Name,
}
}
func (p *defaultProvider) StackTagsLegacy(stack core.Stack) map[string]string {
stackID := stack.StackID()
return map[string]string{
clusterNameTagKeyLegacy: p.clusterName,
p.prefixedTrackingKey(p.resourceTagPrefix, "stack"): stackID.String(),
}
}
func (p *defaultProvider) LegacyTagKeys() []string {
return []string{
fmt.Sprintf("kubernetes.io/cluster/%s", p.clusterName),
"kubernetes.io/cluster-name",
"kubernetes.io/namespace",
"kubernetes.io/ingress-name",
"kubernetes.io/service-name",
"kubernetes.io/service-port",
clusterNameTagKeyLegacy,
}
}
func (p *defaultProvider) prefixedTrackingKey(prefix string, tag string) string {
return fmt.Sprintf("%v/%v", prefix, tag)
}