-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource.go
170 lines (134 loc) · 3.9 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cproutetables
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/v14/service/controller/controllercontext"
"github.com/giantswarm/aws-operator/v14/service/controller/key"
)
const (
Name = "cproutetables"
)
type Config struct {
Logger micrologger.Logger
Installation string
Names []string
}
type Resource struct {
logger micrologger.Logger
installation string
mutex sync.Mutex
routeTables []*ec2.RouteTable
names []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.Installation == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.Installation must not be empty", config)
}
r := &Resource{
logger: config.Logger,
installation: config.Installation,
routeTables: []*ec2.RouteTable{},
mutex: sync.Mutex{},
names: config.Names,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}
func (r *Resource) addRouteTablesToContext(ctx context.Context) error {
r.mutex.Lock()
defer r.mutex.Unlock()
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "finding cached route tables")
if len(r.routeTables) > 0 {
r.logger.Debugf(ctx, "found cached route tables")
cc.Status.ControlPlane.RouteTables = r.routeTables
return nil
}
r.logger.Debugf(ctx, "did not find cached route tables")
r.logger.Debugf(ctx, "caching route tables")
if len(r.names) == 0 {
// We do not have the cached route tables, so we look them up using tags.
tables, err := r.lookupByTag(ctx, cc.Client.ControlPlane.AWS.EC2, r.installation)
if err != nil {
return microerror.Mask(err)
}
r.routeTables = tables
} else {
// We do not have the cached route tables, so we look them up using names
// supplied via RouteTables flag.
for _, name := range r.names {
rt, err := r.lookupByName(ctx, cc.Client.ControlPlane.AWS.EC2, name)
if err != nil {
return microerror.Mask(err)
}
r.routeTables = append(r.routeTables, rt)
}
}
r.logger.Debugf(ctx, "cached route tables")
cc.Status.ControlPlane.RouteTables = r.routeTables
return nil
}
func (r *Resource) lookupByName(ctx context.Context, client EC2, name string) (*ec2.RouteTable, error) {
r.logger.Debugf(ctx, "finding route table ID for %#q", name)
i := &ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{
aws.String(name),
},
},
},
}
o, err := client.DescribeRouteTables(i)
if err != nil {
return nil, microerror.Mask(err)
}
if len(o.RouteTables) != 1 {
return nil, microerror.Maskf(executionFailedError, "expected one route table, got %d", len(o.RouteTables))
}
rt := o.RouteTables[0]
r.logger.Debugf(ctx, "found route table for %#q", name)
return rt, nil
}
func (r *Resource) lookupByTag(ctx context.Context, client EC2, installation string) ([]*ec2.RouteTable, error) {
r.logger.Debugf(ctx, "finding route tables for installation %#q", installation)
i := &ec2.DescribeRouteTablesInput{
Filters: []*ec2.Filter{
{
Name: aws.String(fmt.Sprintf("tag:%s", key.TagCluster)),
Values: []*string{
aws.String(installation),
},
},
{
Name: aws.String(fmt.Sprintf("tag:%s", key.TagRouteTableType)),
Values: []*string{
aws.String("private"),
},
},
},
}
o, err := client.DescribeRouteTables(i)
if err != nil {
return nil, microerror.Mask(err)
}
if len(o.RouteTables) == 0 {
return nil, microerror.Maskf(executionFailedError, "expected at least one route table, got 0")
}
r.logger.Debugf(ctx, "found %d route tables for installation %#q", len(o.RouteTables), installation)
return o.RouteTables, nil
}