-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathsubnet_mapping.go
92 lines (80 loc) · 2.49 KB
/
subnet_mapping.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
/*
Copyright 2021 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 (
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
)
type SubnetMapping struct {
Subnet *Subnet
// PrivateIPv4Address only valid for NLBs
PrivateIPv4Address *string
// AllocationID only valid for NLBs
AllocationID *string
}
// OrderSubnetsById implements sort.Interface for []Subnet, based on ID
type OrderSubnetMappingsByID []*SubnetMapping
func (a OrderSubnetMappingsByID) Len() int { return len(a) }
func (a OrderSubnetMappingsByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a OrderSubnetMappingsByID) Less(i, j int) bool {
v1 := fi.StringValue(a[i].Subnet.ID)
v2 := fi.StringValue(a[j].Subnet.ID)
if v1 == v2 {
if a[i].PrivateIPv4Address != nil && a[j].PrivateIPv4Address != nil {
return fi.StringValue(a[i].PrivateIPv4Address) < fi.StringValue(a[j].PrivateIPv4Address)
}
if a[i].AllocationID != nil && a[j].AllocationID != nil {
return fi.StringValue(a[i].AllocationID) < fi.StringValue(a[j].AllocationID)
}
}
return v1 < v2
}
func subnetMappingSlicesEqualIgnoreOrder(l, r []*SubnetMapping) bool {
lBySubnet := make(map[string]*SubnetMapping)
for _, s := range l {
lBySubnet[*s.Subnet.ID] = s
}
rBySubnet := make(map[string]*SubnetMapping)
for _, s := range r {
if s.Subnet == nil || s.Subnet.ID == nil {
klog.V(4).Infof("Subnet ID not set; returning not-equal: %v", s)
return false
}
rBySubnet[*s.Subnet.ID] = s
}
if len(lBySubnet) != len(rBySubnet) {
return false
}
for n, s := range lBySubnet {
s2, ok := rBySubnet[n]
if !ok {
return false
}
if fi.StringValue(s.PrivateIPv4Address) != fi.StringValue(s2.PrivateIPv4Address) {
return false
}
if fi.StringValue(s.AllocationID) != fi.StringValue(s2.AllocationID) {
return false
}
}
return true
}
func (e *SubnetMapping) GetDependencies(tasks map[string]fi.Task) []fi.Task {
var deps []fi.Task
for _, task := range tasks {
if _, ok := task.(*Subnet); ok {
deps = append(deps, task)
}
}
return deps
}