@@ -21,6 +21,7 @@ import (
2121 "net"
2222 "strings"
2323
24+ "k8s.io/apimachinery/pkg/util/sets"
2425 "k8s.io/klog/v2"
2526 "k8s.io/kops/pkg/apis/kops"
2627 "k8s.io/kops/pkg/apis/kops/model"
@@ -57,16 +58,16 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
5758 Lifecycle : b .Lifecycle ,
5859 Network : network ,
5960 Family : gcetasks .AddressFamilyIPv4 ,
60- SourceRanges : [] string {
61+ SourceRanges : sets . New (
6162 // IP ranges for load balancer health checks
6263 // https://cloud.google.com/load-balancing/docs/health-checks
6364 "35.191.0.0/16" ,
6465 "130.211.0.0/22" ,
6566 "209.85.204.0/22" ,
6667 "209.85.152.0/22" ,
67- } ,
68+ ) ,
6869 TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane )},
69- Allowed : [] string { "tcp" } ,
70+ Allowed : sets . New ( "tcp" ) ,
7071 })
7172 }
7273
@@ -82,7 +83,7 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
8283 Network : network ,
8384 SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
8485 TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
85- Allowed : allProtocols ,
86+ Allowed : sets . New ( allProtocols ... ) ,
8687 }
8788 c .AddTask (t )
8889 }
@@ -99,7 +100,7 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
99100 Network : network ,
100101 SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
101102 TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
102- Allowed : allProtocols ,
103+ Allowed : sets . New ( allProtocols ... ) ,
103104 }
104105 c .AddTask (t )
105106 }
@@ -116,7 +117,7 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
116117 Network : network ,
117118 SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
118119 TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
119- Allowed : allProtocols ,
120+ Allowed : sets . New ( allProtocols ... ) ,
120121 }
121122 c .AddTask (t )
122123 }
@@ -133,25 +134,25 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
133134 Network : network ,
134135 SourceTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
135136 TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleControlPlane ), b .GCETagForRole ("Master" )},
136- Allowed : [] string {
137+ Allowed : sets . New (
137138 fmt .Sprintf ("tcp:%d" , wellknownports .KubeAPIServer ),
138139 fmt .Sprintf ("tcp:%d" , wellknownports .KubeletAPI ),
139140 fmt .Sprintf ("tcp:%d" , wellknownports .KopsControllerPort ),
140- } ,
141+ ) ,
141142 }
142143 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 ))
147148 }
148149 if b .NetworkingIsCalico () {
149- t .Allowed = append ( t . Allowed , "ipip" )
150+ t .Allowed . Insert ( "ipip" )
150151 }
151152 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 ))
153154 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 ))
155156 }
156157 }
157158 c .AddTask (t )
@@ -174,9 +175,9 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
174175 b .AddFirewallRulesTasks (c , "pod-cidrs-to-node" , & gcetasks.FirewallRule {
175176 Lifecycle : b .Lifecycle ,
176177 Network : network ,
177- SourceRanges : [] string { b .Cluster .Spec .Networking .PodCIDR } ,
178+ SourceRanges : sets . New ( b .Cluster .Spec .Networking .PodCIDR ) ,
178179 TargetTags : []string {b .GCETagForRole (kops .InstanceGroupRoleNode )},
179- Allowed : allProtocols ,
180+ Allowed : sets . New ( allProtocols ... ) ,
180181 })
181182 }
182183 }
@@ -189,19 +190,19 @@ func (b *FirewallModelBuilder) Build(c *fi.CloudupModelBuilderContext) error {
189190// Furthermore, an empty SourceRange with empty SourceTags is interpreted as allow-everything,
190191// but we intend for it to block everything; so we can Disabled to achieve the desired blocking.
191192func (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 {
195196 _ , cidr , err := net .ParseCIDR (sourceRange )
196197 if err != nil {
197198 klog .Fatalf ("failed to parse invalid sourceRange %q" , sourceRange )
198199 }
199200
200201 // Split into ipv4s and ipv6s, but treat IPv4-mapped IPv6 addresses as IPv6
201202 if cidr .IP .To4 () != nil && ! strings .Contains (sourceRange , ":" ) {
202- ipv4SourceRanges = append ( ipv4SourceRanges , sourceRange )
203+ ipv4SourceRanges . Insert ( sourceRange )
203204 } else {
204- ipv6SourceRanges = append ( ipv6SourceRanges , sourceRange )
205+ ipv6SourceRanges . Insert ( sourceRange )
205206 }
206207 }
207208
@@ -214,7 +215,7 @@ func (b *GCEModelContext) AddFirewallRulesTasks(c *fi.CloudupModelBuilderContext
214215 // This is helpful because empty SourceRanges and SourceTags are interpreted as allow everything,
215216 // but the intent is usually to block everything, which can be achieved with Disabled=true.
216217 ipv4 .Disabled = true
217- ipv4 .SourceRanges = [] string { "0.0.0.0/0" }
218+ ipv4 .SourceRanges = sets . New ( "0.0.0.0/0" )
218219 }
219220 }
220221 c .AddTask (& ipv4 )
@@ -227,16 +228,16 @@ func (b *GCEModelContext) AddFirewallRulesTasks(c *fi.CloudupModelBuilderContext
227228 if len (ipv6 .SourceRanges ) == 0 {
228229 // We specify explicitly so the rule is in IPv6 mode
229230 ipv6 .Disabled = true
230- ipv6 .SourceRanges = [] string { "::/0" }
231+ ipv6 .SourceRanges = sets . New ( "::/0" )
231232 }
232233 }
233- var ipv6Allowed [] string
234- for _ , allowed := range ipv6 .Allowed {
234+ ipv6Allowed := sets . New [ string ]()
235+ for allowed := range ipv6 .Allowed {
235236 // Map icmp to icmpv6; easier than maintaining separate lists
236237 if allowed == "icmp" {
237238 allowed = "58" // 58 == the IANA protocol number for ICMPv6
238239 }
239- ipv6Allowed = append ( ipv6Allowed , allowed )
240+ ipv6Allowed . Insert ( allowed )
240241 }
241242 ipv6 .Allowed = ipv6Allowed
242243 c .AddTask (& ipv6 )
0 commit comments