Skip to content

Commit b431f06

Browse files
authored
Merge pull request #830 from CecileRobertMichon/refactor-nsg
πŸ’Ž Refactor security groups service spec
2 parents 4b92107 + c1dccd6 commit b431f06

File tree

11 files changed

+881
-392
lines changed

11 files changed

+881
-392
lines changed

β€Žcloud/converters/rules.goβ€Ž

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package converters
18+
19+
import (
20+
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
21+
"github.com/Azure/go-autorest/autorest/to"
22+
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
23+
)
24+
25+
func IngresstoSecurityRule(ingress infrav1.IngressRule) network.SecurityRule {
26+
secRule := network.SecurityRule{
27+
Name: to.StringPtr(ingress.Name),
28+
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
29+
Description: to.StringPtr(ingress.Description),
30+
SourceAddressPrefix: ingress.Source,
31+
SourcePortRange: ingress.SourcePorts,
32+
DestinationAddressPrefix: ingress.Destination,
33+
DestinationPortRange: ingress.DestinationPorts,
34+
Access: network.SecurityRuleAccessAllow,
35+
Direction: network.SecurityRuleDirectionInbound,
36+
Priority: to.Int32Ptr(ingress.Priority),
37+
},
38+
}
39+
40+
switch ingress.Protocol {
41+
case infrav1.SecurityGroupProtocolAll:
42+
secRule.Protocol = network.SecurityRuleProtocolAsterisk
43+
case infrav1.SecurityGroupProtocolTCP:
44+
secRule.Protocol = network.SecurityRuleProtocolTCP
45+
case infrav1.SecurityGroupProtocolUDP:
46+
secRule.Protocol = network.SecurityRuleProtocolUDP
47+
}
48+
49+
return secRule
50+
}
51+
52+
func SecuritytoIngressRule(rule network.SecurityRule) infrav1.IngressRule {
53+
ingRule := infrav1.IngressRule{
54+
Name: to.String(rule.Name),
55+
Description: to.String(rule.Description),
56+
Priority: to.Int32(rule.Priority),
57+
SourcePorts: rule.SourcePortRange,
58+
DestinationPorts: rule.DestinationPortRange,
59+
Source: rule.SourceAddressPrefix,
60+
Destination: rule.DestinationAddressPrefix,
61+
}
62+
63+
switch rule.Protocol {
64+
case network.SecurityRuleProtocolAsterisk:
65+
ingRule.Protocol = infrav1.SecurityGroupProtocolAll
66+
case network.SecurityRuleProtocolTCP:
67+
ingRule.Protocol = infrav1.SecurityGroupProtocolTCP
68+
case network.SecurityRuleProtocolUDP:
69+
ingRule.Protocol = infrav1.SecurityGroupProtocolUDP
70+
}
71+
72+
return ingRule
73+
}

β€Žcloud/scope/cluster.goβ€Ž

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package scope
1919
import (
2020
"context"
2121
"fmt"
22+
"github.com/Azure/go-autorest/autorest/to"
23+
"strconv"
2224

2325
"github.com/Azure/go-autorest/autorest"
2426
"github.com/go-logr/logr"
@@ -153,6 +155,20 @@ func (s *ClusterScope) RouteTableSpecs() []azure.RouteTableSpec {
153155
}}
154156
}
155157

158+
// NSGSpecs returns the security group specs.
159+
func (s *ClusterScope) NSGSpecs() []azure.NSGSpec {
160+
return []azure.NSGSpec{
161+
{
162+
Name: s.ControlPlaneSubnet().SecurityGroup.Name,
163+
IngressRules: s.ControlPlaneSubnet().SecurityGroup.IngressRules,
164+
},
165+
{
166+
Name: s.NodeSubnet().SecurityGroup.Name,
167+
IngressRules: s.NodeSubnet().SecurityGroup.IngressRules,
168+
},
169+
}
170+
}
171+
156172
// SubnetSpecs returns the subnets specs.
157173
func (s *ClusterScope) SubnetSpecs() []azure.SubnetSpec {
158174
return []azure.SubnetSpec{
@@ -283,3 +299,30 @@ func (s *ClusterScope) SetFailureDomain(id string, spec clusterv1.FailureDomainS
283299
}
284300
s.AzureCluster.Status.FailureDomains[id] = spec
285301
}
302+
303+
func (s *ClusterScope) SetControlPlaneIngressRules() {
304+
if s.ControlPlaneSubnet().SecurityGroup.IngressRules == nil {
305+
s.ControlPlaneSubnet().SecurityGroup.IngressRules = infrav1.IngressRules{
306+
&infrav1.IngressRule{
307+
Name: "allow_ssh",
308+
Description: "Allow SSH",
309+
Priority: 100,
310+
Protocol: infrav1.SecurityGroupProtocolTCP,
311+
Source: to.StringPtr("*"),
312+
SourcePorts: to.StringPtr("*"),
313+
Destination: to.StringPtr("*"),
314+
DestinationPorts: to.StringPtr("22"),
315+
},
316+
&infrav1.IngressRule{
317+
Name: "allow_apiserver",
318+
Description: "Allow K8s API Server",
319+
Priority: 101,
320+
Protocol: infrav1.SecurityGroupProtocolTCP,
321+
Source: to.StringPtr("*"),
322+
SourcePorts: to.StringPtr("*"),
323+
Destination: to.StringPtr("*"),
324+
DestinationPorts: to.StringPtr(strconv.Itoa(int(s.APIServerPort()))),
325+
},
326+
}
327+
}
328+
}

β€Žcloud/services/loadbalancers/loadbalancers.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (s *Service) Delete(ctx context.Context) error {
191191
err := s.Client.Delete(ctx, s.Scope.ResourceGroup(), lbSpec.Name)
192192
if err != nil && azure.ResourceNotFound(err) {
193193
// already deleted
194-
return nil
194+
continue
195195
}
196196
if err != nil {
197197
return errors.Wrapf(err, "failed to delete load balancer %s in resource group %s", lbSpec.Name, s.Scope.ResourceGroup())

β€Žcloud/services/securitygroups/mock_securitygroups/client_mock.goβ€Ž

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žcloud/services/securitygroups/mock_securitygroups/doc.goβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ limitations under the License.
1515
*/
1616

1717
// Run go generate to regenerate this mock.
18-
//go:generate ../../../../hack/tools/bin/mockgen -destination securitygroups_mock.go -package mock_securitygroups -source ../client.go Client
18+
//go:generate ../../../../hack/tools/bin/mockgen -destination client_mock.go -package mock_securitygroups -source ../client.go Client
19+
//go:generate ../../../../hack/tools/bin/mockgen -destination securitygroups_mock.go -package mock_securitygroups -source ../service.go NSGScope
20+
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt client_mock.go > _client_mock.go && mv _client_mock.go client_mock.go"
1921
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt securitygroups_mock.go > _securitygroups_mock.go && mv _securitygroups_mock.go securitygroups_mock.go"
2022
package mock_securitygroups //nolint

0 commit comments

Comments
Β (0)