-
Notifications
You must be signed in to change notification settings - Fork 22
/
create.go
177 lines (140 loc) · 5.58 KB
/
create.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
171
172
173
174
175
176
177
package tcnpf
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
infrastructurev1alpha3 "github.com/giantswarm/apiextensions/v6/pkg/apis/infrastructure/v1alpha3"
"github.com/giantswarm/microerror"
"github.com/giantswarm/aws-operator/v13/pkg/awstags"
"github.com/giantswarm/aws-operator/v13/service/controller/controllercontext"
"github.com/giantswarm/aws-operator/v13/service/controller/key"
"github.com/giantswarm/aws-operator/v13/service/controller/resource/tcnpf/template"
)
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
cr, err := key.ToMachineDeployment(obj)
if err != nil {
return microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
{
if len(cc.Spec.TenantCluster.TCNP.AvailabilityZones) == 0 {
r.logger.Debugf(ctx, "availability zone information not available yet")
r.logger.Debugf(ctx, "canceling resource")
return nil
}
if cc.Status.TenantCluster.TCCP.VPC.PeeringConnectionID == "" {
r.logger.Debugf(ctx, "did not find the VPC Peering Connection ID in the controller context")
r.logger.Debugf(ctx, "canceling resource")
return nil
}
}
{
r.logger.Debugf(ctx, "finding the tenant cluster's node pool finalizer cloud formation stack")
i := &cloudformation.DescribeStacksInput{
StackName: aws.String(key.StackNameTCNPF(&cr)),
}
o, err := cc.Client.ControlPlane.AWS.CloudFormation.DescribeStacks(i)
if IsNotExists(err) {
// fall through
} else if err != nil {
return microerror.Mask(err)
} else if len(o.Stacks) != 1 {
return microerror.Maskf(executionFailedError, "expected one stack, got %d", len(o.Stacks))
} else if *o.Stacks[0].StackStatus == cloudformation.StackStatusCreateFailed {
return microerror.Maskf(eventCFCreateError, "expected successful status, got %#q", *o.Stacks[0].StackStatus)
} else if *o.Stacks[0].StackStatus == cloudformation.StackStatusRollbackFailed {
return microerror.Maskf(eventCFRollbackError, "expected successful status, got %#q", *o.Stacks[0].StackStatus)
} else if *o.Stacks[0].StackStatus == cloudformation.StackStatusUpdateRollbackFailed {
return microerror.Maskf(eventCFUpdateRollbackError, "expected successful status, got %#q", *o.Stacks[0].StackStatus)
} else {
r.logger.Debugf(ctx, "found the tenant cluster's node pool finalizer cloud formation stack already exists")
r.logger.Debugf(ctx, "canceling resource")
return nil
}
r.logger.Debugf(ctx, "did not find the tenant cluster's node pool finalizer cloud formation stack")
}
var templateBody string
{
r.logger.Debugf(ctx, "computing the template of the tenant cluster's node pool finalizer cloud formation stack")
params, err := newTemplateParams(ctx, cr)
if err != nil {
return microerror.Mask(err)
}
templateBody, err = template.Render(params)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "computed the template of the tenant cluster's node pool finalizer cloud formation stack")
}
{
r.logger.Debugf(ctx, "requesting the creation of the tenant cluster's node pool finalizer cloud formation stack")
i := &cloudformation.CreateStackInput{
EnableTerminationProtection: aws.Bool(true),
StackName: aws.String(key.StackNameTCNPF(&cr)),
Tags: r.getCloudFormationTags(cr),
TemplateBody: aws.String(templateBody),
}
_, err = cc.Client.ControlPlane.AWS.CloudFormation.CreateStack(i)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "requested the creation of the tenant cluster's node pool finalizer cloud formation stack")
}
{
r.logger.Debugf(ctx, "waiting for the creation of the tenant cluster's node pool finalizer cloud formation stack")
i := &cloudformation.DescribeStacksInput{
StackName: aws.String(key.StackNameTCNPF(&cr)),
}
err = cc.Client.ControlPlane.AWS.CloudFormation.WaitUntilStackCreateComplete(i)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "waited for the creation of the tenant cluster's node pool finalizer cloud formation stack")
}
return nil
}
func newRouteTablesParams(ctx context.Context, cr infrastructurev1alpha3.AWSMachineDeployment) (*template.ParamsMainRouteTables, error) {
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return nil, microerror.Mask(err)
}
var peeringConnections []template.ParamsMainVPCPeeringConnection
for _, rt := range cc.Status.ControlPlane.RouteTables {
for _, az := range cc.Spec.TenantCluster.TCNP.AvailabilityZones {
pc := template.ParamsMainVPCPeeringConnection{
ID: cc.Status.TenantCluster.TCCP.VPC.PeeringConnectionID,
Name: key.SanitizeCFResourceName(key.VPCPeeringRouteName(az.Name), awstags.ValueForKey(rt.Tags, "Name")),
RouteTable: template.ParamsMainVPCPeeringConnectionRouteTable{
ID: *rt.RouteTableId,
},
Subnet: template.ParamsMainVPCPeeringConnectionSubnet{
CIDR: az.Subnet.Private.CIDR.String(),
},
}
peeringConnections = append(peeringConnections, pc)
}
}
var routeTables *template.ParamsMainRouteTables
{
routeTables = &template.ParamsMainRouteTables{
PeeringConnections: peeringConnections,
}
}
return routeTables, nil
}
func newTemplateParams(ctx context.Context, cr infrastructurev1alpha3.AWSMachineDeployment) (*template.ParamsMain, error) {
var params *template.ParamsMain
{
routeTables, err := newRouteTablesParams(ctx, cr)
if err != nil {
return nil, microerror.Mask(err)
}
params = &template.ParamsMain{
RouteTables: routeTables,
}
}
return params, nil
}