sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/rules.go (about) 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/sdk/resourcemanager/network/armnetwork/v4" 21 "k8s.io/utils/ptr" 22 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 23 ) 24 25 // SecurityRuleToSDK converts a CAPZ security rule to an Azure network security rule. 26 func SecurityRuleToSDK(rule infrav1.SecurityRule) *armnetwork.SecurityRule { 27 secRule := &armnetwork.SecurityRule{ 28 Name: ptr.To(rule.Name), 29 Properties: &armnetwork.SecurityRulePropertiesFormat{ 30 Description: ptr.To(rule.Description), 31 SourceAddressPrefix: rule.Source, 32 SourceAddressPrefixes: rule.Sources, 33 SourcePortRange: rule.SourcePorts, 34 DestinationAddressPrefix: rule.Destination, 35 DestinationPortRange: rule.DestinationPorts, 36 Access: ptr.To(armnetwork.SecurityRuleAccess(rule.Action)), 37 Priority: ptr.To[int32](rule.Priority), 38 }, 39 } 40 41 switch rule.Protocol { 42 case infrav1.SecurityGroupProtocolAll: 43 secRule.Properties.Protocol = ptr.To(armnetwork.SecurityRuleProtocolAsterisk) 44 case infrav1.SecurityGroupProtocolTCP: 45 secRule.Properties.Protocol = ptr.To(armnetwork.SecurityRuleProtocolTCP) 46 case infrav1.SecurityGroupProtocolUDP: 47 secRule.Properties.Protocol = ptr.To(armnetwork.SecurityRuleProtocolUDP) 48 case infrav1.SecurityGroupProtocolICMP: 49 secRule.Properties.Protocol = ptr.To(armnetwork.SecurityRuleProtocolIcmp) 50 } 51 52 switch rule.Direction { 53 case infrav1.SecurityRuleDirectionOutbound: 54 secRule.Properties.Direction = ptr.To(armnetwork.SecurityRuleDirectionOutbound) 55 case infrav1.SecurityRuleDirectionInbound: 56 secRule.Properties.Direction = ptr.To(armnetwork.SecurityRuleDirectionInbound) 57 } 58 59 return secRule 60 }