-
Notifications
You must be signed in to change notification settings - Fork 907
/
subnets.go
114 lines (102 loc) · 3.24 KB
/
subnets.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aws
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/mitchellh/hashstructure/v2"
"github.com/patrickmn/go-cache"
"knative.dev/pkg/logging"
"github.com/aws/karpenter/pkg/cloudprovider/aws/apis/v1alpha1"
"github.com/aws/karpenter/pkg/utils/functional"
"github.com/aws/karpenter/pkg/utils/pretty"
)
type SubnetProvider struct {
sync.Mutex
ec2api ec2iface.EC2API
cache *cache.Cache
cm *pretty.ChangeMonitor
}
func NewSubnetProvider(ec2api ec2iface.EC2API) *SubnetProvider {
return &SubnetProvider{
ec2api: ec2api,
cm: pretty.NewChangeMonitor(),
cache: cache.New(CacheTTL, CacheCleanupInterval),
}
}
func (p *SubnetProvider) Get(ctx context.Context, provider *v1alpha1.AWS) ([]*ec2.Subnet, error) {
p.Lock()
defer p.Unlock()
filters := getFilters(provider)
hash, err := hashstructure.Hash(filters, hashstructure.FormatV2, nil)
if err != nil {
return nil, err
}
if subnets, ok := p.cache.Get(fmt.Sprint(hash)); ok {
return subnets.([]*ec2.Subnet), nil
}
output, err := p.ec2api.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{Filters: filters})
if err != nil {
return nil, fmt.Errorf("describing subnets %s, %w", pretty.Concise(filters), err)
}
if len(output.Subnets) == 0 {
return nil, fmt.Errorf("no subnets matched selector %v", provider.SubnetSelector)
}
p.cache.SetDefault(fmt.Sprint(hash), output.Subnets)
subnetLog := prettySubnets(output.Subnets)
if p.cm.HasChanged("subnets", subnetLog) {
logging.FromContext(ctx).Debugf("Discovered subnets: %s", subnetLog)
}
return output.Subnets, nil
}
func (p *SubnetProvider) LivenessProbe(req *http.Request) error {
p.Lock()
//nolint: staticcheck
p.Unlock()
return nil
}
func getFilters(provider *v1alpha1.AWS) []*ec2.Filter {
filters := []*ec2.Filter{}
// Filter by subnet
for key, value := range provider.SubnetSelector {
if key == "aws-ids" {
filterValues := functional.SplitCommaSeparatedString(value)
filters = append(filters, &ec2.Filter{
Name: aws.String("subnet-id"),
Values: aws.StringSlice(filterValues),
})
} else if value == "*" {
filters = append(filters, &ec2.Filter{
Name: aws.String("tag-key"),
Values: []*string{aws.String(key)},
})
} else {
filters = append(filters, &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", key)),
Values: []*string{aws.String(value)},
})
}
}
return filters
}
func prettySubnets(subnets []*ec2.Subnet) []string {
names := []string{}
for _, subnet := range subnets {
names = append(names, fmt.Sprintf("%s (%s)", aws.StringValue(subnet.SubnetId), aws.StringValue(subnet.AvailabilityZone)))
}
return names
}