-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
vpccidrblock.go
186 lines (149 loc) · 4.81 KB
/
vpccidrblock.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
178
179
180
181
182
183
184
185
186
/*
Copyright 2019 The Kubernetes Authors.
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 awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)
// +kops:fitask
type VPCCIDRBlock struct {
Name *string
Lifecycle fi.Lifecycle
VPC *VPC
CIDRBlock *string
// Shared is set if this is a shared VPC
Shared *bool
}
func (e *VPCCIDRBlock) Find(c *fi.Context) (*VPCCIDRBlock, error) {
cloud := c.Cloud.(awsup.AWSCloud)
vpcID := aws.StringValue(e.VPC.ID)
// If the VPC doesn't (yet) exist, there is no association
if vpcID == "" {
return nil, nil
}
vpc, err := cloud.DescribeVPC(vpcID)
if err != nil {
return nil, err
}
found := false
if e.CIDRBlock != nil {
for _, cba := range vpc.CidrBlockAssociationSet {
if cba == nil || cba.CidrBlockState == nil {
continue
}
state := aws.StringValue(cba.CidrBlockState.State)
if state != ec2.VpcCidrBlockStateCodeAssociated && state != ec2.VpcCidrBlockStateCodeAssociating {
continue
}
if aws.StringValue(cba.CidrBlock) == aws.StringValue(e.CIDRBlock) {
found = true
break
}
}
}
if !found {
return nil, nil
}
actual := &VPCCIDRBlock{
VPC: &VPC{ID: vpc.VpcId},
CIDRBlock: e.CIDRBlock,
}
// Prevent spurious changes
actual.Shared = e.Shared
actual.Name = e.Name
actual.Lifecycle = e.Lifecycle
return actual, nil
}
func (e *VPCCIDRBlock) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *VPCCIDRBlock) CheckChanges(a, e, changes *VPCCIDRBlock) error {
if e.VPC == nil {
return fi.RequiredField("VPC")
}
if e.CIDRBlock == nil {
return fi.RequiredField("CIDRBlock")
}
if a != nil && changes != nil {
if changes.VPC != nil {
return fi.CannotChangeField("VPC")
}
if changes.CIDRBlock != nil {
return fi.CannotChangeField("CIDRBlock")
}
}
return nil
}
func (_ *VPCCIDRBlock) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *VPCCIDRBlock) error {
shared := aws.BoolValue(e.Shared)
if shared && a == nil {
// VPC not owned by kOps, no changes will be applied
// Verify that the CIDR block was found.
return fmt.Errorf("CIDR block %q not found", aws.StringValue(e.CIDRBlock))
}
if changes.CIDRBlock != nil {
request := &ec2.AssociateVpcCidrBlockInput{
VpcId: e.VPC.ID,
CidrBlock: e.CIDRBlock,
}
_, err := t.Cloud.EC2().AssociateVpcCidrBlock(request)
if err != nil {
return fmt.Errorf("error associating AdditionalCIDR to VPC: %v", err)
}
}
return nil // no tags
}
type terraformVPCCIDRBlock struct {
VPCID *terraformWriter.Literal `json:"vpc_id" cty:"vpc_id"`
CIDRBlock *string `json:"cidr_block" cty:"cidr_block"`
}
func (_ *VPCCIDRBlock) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *VPCCIDRBlock) error {
shared := aws.BoolValue(e.Shared)
if shared && a == nil {
// VPC not owned by kOps, no changes will be applied
// Verify that the CIDR block was found.
return fmt.Errorf("CIDR block %q not found", aws.StringValue(e.CIDRBlock))
}
// When this has been enabled please fix test TestAdditionalCIDR in integration_test.go to run runTestAWS.
tf := &terraformVPCCIDRBlock{
VPCID: e.VPC.TerraformLink(),
CIDRBlock: e.CIDRBlock,
}
// Terraform 0.12 doesn't support resource names that start with digits. See #7052
// and https://www.terraform.io/upgrade-guides/0-12.html#pre-upgrade-checklist
name := fmt.Sprintf("cidr-%v", *e.Name)
return t.RenderResource("aws_vpc_ipv4_cidr_block_association", name, tf)
}
type cloudformationVPCCIDRBlock struct {
VPCID *cloudformation.Literal `json:"VpcId"`
CIDRBlock *string `json:"CidrBlock"`
}
func (_ *VPCCIDRBlock) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *VPCCIDRBlock) error {
shared := aws.BoolValue(e.Shared)
if shared && a == nil {
// VPC not owned by kOps, no changes will be applied
// Verify that the CIDR block was found.
return fmt.Errorf("CIDR block %q not found", aws.StringValue(e.CIDRBlock))
}
cf := &cloudformationVPCCIDRBlock{
VPCID: e.VPC.CloudformationLink(),
CIDRBlock: e.CIDRBlock,
}
return t.RenderResource("AWS::EC2::VPCCidrBlock", *e.Name, cf)
}