/
awsmachine_security_groups.go
144 lines (117 loc) · 4.38 KB
/
awsmachine_security_groups.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
/*
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 controllers
import (
"sort"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha3"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
service "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services"
)
const (
// SecurityGroupsLastAppliedAnnotation is the key for the machine object
// annotation which tracks the SecurityGroups that the machine actuator is
// responsible for. These are the SecurityGroups that have been handled by
// the AdditionalSecurityGroups in the Machine Provider Config.
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
// for annotation formatting rules.
SecurityGroupsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-aws-last-applied-security-groups"
)
// Ensures that the security groups of the machine are correct
// Returns bool, error
// Bool indicates if changes were made or not, allowing the caller to decide
// if the machine should be updated.
func (r *AWSMachineReconciler) ensureSecurityGroups(ec2svc service.EC2MachineInterface, scope *scope.MachineScope, additional []infrav1.AWSResourceReference, existing map[string][]string) (bool, error) {
annotation, err := r.machineAnnotationJSON(scope.AWSMachine, SecurityGroupsLastAppliedAnnotation)
if err != nil {
return false, err
}
core, err := ec2svc.GetCoreSecurityGroups(scope)
if err != nil {
return false, err
}
additionalSecurityGroupsIDs, err := r.getAdditionalSecurityGroupsIDs(ec2svc, additional)
if err != nil {
return false, nil
}
changed, ids := r.securityGroupsChanged(annotation, core, additionalSecurityGroupsIDs, existing)
if !changed {
return false, nil
}
if err := ec2svc.UpdateInstanceSecurityGroups(*scope.GetInstanceID(), ids); err != nil {
return false, err
}
// Build and store annotation.
newAnnotation := make(map[string]interface{}, len(additionalSecurityGroupsIDs))
for _, id := range additionalSecurityGroupsIDs {
newAnnotation[id] = struct{}{}
}
if err := r.updateMachineAnnotationJSON(scope.AWSMachine, SecurityGroupsLastAppliedAnnotation, newAnnotation); err != nil {
return false, err
}
return true, nil
}
// securityGroupsChanged determines which security groups to delete and which to add.
func (r *AWSMachineReconciler) securityGroupsChanged(annotation map[string]interface{}, core []string, additional []string, existing map[string][]string) (bool, []string) {
state := map[string]bool{}
for _, s := range additional {
state[s] = true
}
// Loop over `annotation`, checking the state for things that were deleted since last time.
// If we find something in the `annotation`, but not in the state, we flag it as `false` (not found, deleted).
for groupID := range annotation {
if _, ok := state[groupID]; !ok {
state[groupID] = false
}
}
// add (or add back) the core security groups
for _, s := range core {
state[s] = true
}
// Build the security group list.
res := []string{}
for id, keep := range state {
if keep {
res = append(res, id)
}
}
for _, actual := range existing {
if len(actual) != len(res) {
return true, res
}
// Length is the same, check if the ids are the same too.
sort.Strings(actual)
sort.Strings(res)
for i, id := range res {
if actual[i] != id {
return true, res
}
}
}
return false, res
}
func (r *AWSMachineReconciler) getAdditionalSecurityGroupsIDs(ec2svc service.EC2MachineInterface, securitygroups []infrav1.AWSResourceReference) ([]string, error) {
additionalSecurityGroupsIDs := []string{}
for _, sg := range securitygroups {
if sg.ID != nil {
additionalSecurityGroupsIDs = append(additionalSecurityGroupsIDs, *sg.ID)
}
if sg.Filters != nil {
id, err := ec2svc.GetFilteredSecurityGroupID(sg)
if err != nil {
return nil, err
}
additionalSecurityGroupsIDs = append(additionalSecurityGroupsIDs, id)
}
}
return additionalSecurityGroupsIDs, nil
}