-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource.go
137 lines (106 loc) · 3.09 KB
/
resource.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
136
137
package cpvpccidr
import (
"context"
"fmt"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/aws-operator/service/controller/controllercontext"
)
const (
Name = "cpvpccidr"
)
type Config struct {
Logger micrologger.Logger
VPCPeerID string
}
type Resource struct {
logger micrologger.Logger
// cidrs is a mapping of vpcs IDs and CIDRs, where the key is the ID
// and the value is the CIDR.
cidrs map[string]string
mutex sync.Mutex
vpcPeerID string
}
func New(config Config) (*Resource, error) {
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.VPCPeerID == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.VPCPeerID must not be empty", config)
}
r := &Resource{
logger: config.Logger,
cidrs: map[string]string{},
mutex: sync.Mutex{},
vpcPeerID: config.VPCPeerID,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}
func (r *Resource) addVPCCIDRToContext(ctx context.Context) error {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
{
cidr, err := r.lookup(ctx, cc.Client.ControlPlane.AWS.EC2, r.vpcPeerID)
if err != nil {
return microerror.Mask(err)
}
cc.Status.ControlPlane.VPC.CIDR = cidr
}
return nil
}
func (r *Resource) lookup(ctx context.Context, client EC2, vpc string) (string, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
// We check if we have a VPC CIDR cached for the requested VPC ID. If we find
// one, we return the result.
{
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("finding cached vpc cidr for %#q", vpc))
cidr, ok := r.cidrs[vpc]
if ok {
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("found cached vpc cidr %#q for %#q", cidr, vpc))
return cidr, nil
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("did not find cached vpc cidr for %#q", vpc))
}
// We do not have a cached VPC CIDR for the requested VPC ID. So we look it
// up.
var cidr string
{
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("finding vpc cidr for %#q", vpc))
i := &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpc),
},
},
},
}
o, err := client.DescribeVpcs(i)
if err != nil {
return "", microerror.Mask(err)
}
if len(o.Vpcs) != 1 {
return "", microerror.Maskf(executionFailedError, "expected one vpc, got %d", len(o.Vpcs))
}
cidr = *o.Vpcs[0].CidrBlock
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("found vpc cidr %#q for %#q", cidr, vpc))
}
// At this point we found a VPC CIDR and can cache it using the requested VPC
// ID.
{
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("caching vpc cidr %#q for %#q", cidr, vpc))
r.cidrs[vpc] = cidr
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("cached vpc cidr %#q for %#q", cidr, vpc))
}
return cidr, nil
}