@@ -21,6 +21,7 @@ import (
21
21
"net"
22
22
"strings"
23
23
24
+ "k8s.io/apimachinery/pkg/util/sets"
24
25
"k8s.io/klog/v2"
25
26
"k8s.io/kops/pkg/apis/kops"
26
27
"k8s.io/kops/pkg/apis/kops/model"
@@ -57,16 +58,16 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
57
58
Lifecycle : b .Lifecycle ,
58
59
Network : network ,
59
60
Family : gcetasks .AddressFamilyIPv4 ,
60
- SourceRanges : [] string {
61
+ SourceRanges : sets . New (
61
62
// IP ranges for load balancer health checks
62
63
// https://cloud.google.com/load-balancing/docs/health-checks
63
64
"35.191.0.0/16" ,
64
65
"130.211.0.0/22" ,
65
66
"209.85.204.0/22" ,
66
67
"209.85.152.0/22" ,
67
- } ,
68
+ ) ,
68
69
TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane )},
69
- Allowed : [] string { "tcp" } ,
70
+ Allowed : sets . New ( "tcp" ) ,
70
71
})
71
72
}
72
73
@@ -82,7 +83,7 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
82
83
Network : network ,
83
84
SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
84
85
TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
85
- Allowed : allProtocols ,
86
+ Allowed : sets . New ( allProtocols ... ) ,
86
87
}
87
88
c .AddTask (t )
88
89
}
@@ -99,7 +100,7 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
99
100
Network : network ,
100
101
SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
101
102
TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
102
- Allowed : allProtocols ,
103
+ Allowed : sets . New ( allProtocols ... ) ,
103
104
}
104
105
c .AddTask (t )
105
106
}
@@ -116,7 +117,7 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
116
117
Network : network ,
117
118
SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
118
119
TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
119
- Allowed : allProtocols ,
120
+ Allowed : sets . New ( allProtocols ... ) ,
120
121
}
121
122
c .AddTask (t )
122
123
}
@@ -133,25 +134,25 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
133
134
Network : network ,
134
135
SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
135
136
TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
136
- Allowed : [] string {
137
+ Allowed : sets . New (
137
138
fmt .Sprintf ("tcp:%d" , wellknownports .KubeAPIServer ),
138
139
fmt .Sprintf ("tcp:%d" , wellknownports .KubeletAPI ),
139
140
fmt .Sprintf ("tcp:%d" , wellknownports .KopsControllerPort ),
140
- } ,
141
+ ) ,
141
142
}
142
143
if b .Cluster .UsesLegacyGossip () {
143
- t .Allowed = append ( t . Allowed , fmt .Sprintf ("udp:%d" , wellknownports .DNSControllerGossipMemberlist ))
144
- t .Allowed = append ( t . Allowed , fmt .Sprintf ("tcp:%d" , wellknownports .DNSControllerGossipMemberlist ))
145
- t .Allowed = append ( t . Allowed , fmt .Sprintf ("udp:%d" , wellknownports .ProtokubeGossipMemberlist ))
146
- t .Allowed = append ( t . Allowed , fmt .Sprintf ("tcp:%d" , wellknownports .ProtokubeGossipMemberlist ))
144
+ t .Allowed . Insert ( fmt .Sprintf ("udp:%d" , wellknownports .DNSControllerGossipMemberlist ))
145
+ t .Allowed . Insert ( fmt .Sprintf ("tcp:%d" , wellknownports .DNSControllerGossipMemberlist ))
146
+ t .Allowed . Insert ( fmt .Sprintf ("udp:%d" , wellknownports .ProtokubeGossipMemberlist ))
147
+ t .Allowed . Insert ( fmt .Sprintf ("tcp:%d" , wellknownports .ProtokubeGossipMemberlist ))
147
148
}
148
149
if b .NetworkingIsCalico () {
149
- t .Allowed = append ( t . Allowed , "ipip" )
150
+ t .Allowed . Insert ( "ipip" )
150
151
}
151
152
if b .NetworkingIsCilium () {
152
- t .Allowed = append ( t . Allowed , fmt .Sprintf ("udp:%d" , wellknownports .VxlanUDP ))
153
+ t .Allowed . Insert ( fmt .Sprintf ("udp:%d" , wellknownports .VxlanUDP ))
153
154
if model .UseCiliumEtcd (b .Cluster ) {
154
- t .Allowed = append ( t . Allowed , fmt .Sprintf ("tcp:%d" , wellknownports .EtcdCiliumClientPort ))
155
+ t .Allowed . Insert ( fmt .Sprintf ("tcp:%d" , wellknownports .EtcdCiliumClientPort ))
155
156
}
156
157
}
157
158
c .AddTask (t )
@@ -174,9 +175,9 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
174
175
b .AddFirewallRulesTasks (c , "pod-cidrs-to-node" , & gcetasks.FirewallRule {
175
176
Lifecycle : b .Lifecycle ,
176
177
Network : network ,
177
- SourceRanges : [] string { b .Cluster .Spec .Networking .PodCIDR } ,
178
+ SourceRanges : sets . New ( b .Cluster .Spec .Networking .PodCIDR ) ,
178
179
TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
179
- Allowed : allProtocols ,
180
+ Allowed : sets . New ( allProtocols ... ) ,
180
181
})
181
182
}
182
183
}
@@ -189,19 +190,19 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
189
190
// Furthermore, an empty SourceRange with empty SourceTags is interpreted as allow-everything,
190
191
// but we intend for it to block everything; so we can Disabled to achieve the desired blocking.
191
192
func (b * GCEModelContext ) AddFirewallRulesTasks (c * fi.CloudupModelBuilderContext , name string , rule * gcetasks.FirewallRule ) {
192
- var ipv4SourceRanges [] string
193
- var ipv6SourceRanges [] string
194
- for _ , sourceRange := range rule .SourceRanges {
193
+ ipv4SourceRanges := sets . New [ string ]()
194
+ ipv6SourceRanges := sets . New [ string ]()
195
+ for sourceRange := range rule .SourceRanges {
195
196
_ , cidr , err := net .ParseCIDR (sourceRange )
196
197
if err != nil {
197
198
klog .Fatalf ("failed to parse invalid sourceRange %q" , sourceRange )
198
199
}
199
200
200
201
// Split into ipv4s and ipv6s, but treat IPv4-mapped IPv6 addresses as IPv6
201
202
if cidr .IP .To4 () != nil && ! strings .Contains (sourceRange , ":" ) {
202
- ipv4SourceRanges = append ( ipv4SourceRanges , sourceRange )
203
+ ipv4SourceRanges . Insert ( sourceRange )
203
204
} else {
204
- ipv6SourceRanges = append ( ipv6SourceRanges , sourceRange )
205
+ ipv6SourceRanges . Insert ( sourceRange )
205
206
}
206
207
}
207
208
@@ -214,7 +215,7 @@ func (b *GCEModelContext) AddFirewallRulesTasks(c *fi.CloudupModelBuilderContext
214
215
// This is helpful because empty SourceRanges and SourceTags are interpreted as allow everything,
215
216
// but the intent is usually to block everything, which can be achieved with Disabled=true.
216
217
ipv4 .Disabled = true
217
- ipv4 .SourceRanges = [] string { "0.0.0.0/0" }
218
+ ipv4 .SourceRanges = sets . New ( "0.0.0.0/0" )
218
219
}
219
220
}
220
221
c .AddTask (& ipv4 )
@@ -227,16 +228,16 @@ func (b *GCEModelContext) AddFirewallRulesTasks(c *fi.CloudupModelBuilderContext
227
228
if len (ipv6 .SourceRanges ) == 0 {
228
229
// We specify explicitly so the rule is in IPv6 mode
229
230
ipv6 .Disabled = true
230
- ipv6 .SourceRanges = [] string { "::/0" }
231
+ ipv6 .SourceRanges = sets . New ( "::/0" )
231
232
}
232
233
}
233
- var ipv6Allowed [] string
234
- for _ , allowed := range ipv6 .Allowed {
234
+ ipv6Allowed := sets . New [ string ]()
235
+ for allowed := range ipv6 .Allowed {
235
236
// Map icmp to icmpv6; easier than maintaining separate lists
236
237
if allowed == "icmp" {
237
238
allowed = "58" // 58 == the IANA protocol number for ICMPv6
238
239
}
239
- ipv6Allowed = append ( ipv6Allowed , allowed )
240
+ ipv6Allowed . Insert ( allowed )
240
241
}
241
242
ipv6 .Allowed = ipv6Allowed
242
243
c .AddTask (& ipv6 )
0 commit comments