|
8 | 8 | "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
9 | 9 | "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4" |
10 | 10 | "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2" |
| 11 | + "k8s.io/utils/ptr" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | type lbInput struct { |
@@ -37,6 +38,14 @@ type vmInput struct { |
37 | 38 | nicClient *armnetwork.InterfacesClient |
38 | 39 | } |
39 | 40 |
|
| 41 | +type securityGroupInput struct { |
| 42 | + resourceGroupName string |
| 43 | + securityGroupName string |
| 44 | + securityRuleName string |
| 45 | + securityRulePort string |
| 46 | + securityGroupsClient *armnetwork.SecurityGroupsClient |
| 47 | +} |
| 48 | + |
40 | 49 | func createPublicIP(ctx context.Context, in *pipInput) (*armnetwork.PublicIPAddress, error) { |
41 | 50 | pollerResp, err := in.pipClient.BeginCreateOrUpdate( |
42 | 51 | ctx, |
@@ -266,3 +275,65 @@ func associateVMToBackendPool(ctx context.Context, in vmInput) error { |
266 | 275 | } |
267 | 276 | return nil |
268 | 277 | } |
| 278 | + |
| 279 | +func addSecurityGroupRule(ctx context.Context, in *securityGroupInput) error { |
| 280 | + securityGroupResp, err := in.securityGroupsClient.Get(ctx, in.resourceGroupName, in.securityGroupName, nil) |
| 281 | + if err != nil { |
| 282 | + return fmt.Errorf("failed to get security group: %w", err) |
| 283 | + } |
| 284 | + securityGroup := securityGroupResp.SecurityGroup |
| 285 | + |
| 286 | + priority := int32(100) |
| 287 | + for _, securityRule := range securityGroup.Properties.SecurityRules { |
| 288 | + if *securityRule.Properties.Priority >= priority { |
| 289 | + priority = *securityRule.Properties.Priority + 1 |
| 290 | + } |
| 291 | + } |
| 292 | + // Assume inbound tcp connections from any port to destination port for now |
| 293 | + securityGroup.Properties.SecurityRules = append(securityGroup.Properties.SecurityRules, |
| 294 | + &armnetwork.SecurityRule{ |
| 295 | + Name: ptr.To(in.securityRuleName), |
| 296 | + Properties: &armnetwork.SecurityRulePropertiesFormat{ |
| 297 | + Access: ptr.To(armnetwork.SecurityRuleAccessAllow), |
| 298 | + Direction: ptr.To(armnetwork.SecurityRuleDirectionInbound), |
| 299 | + Protocol: ptr.To(armnetwork.SecurityRuleProtocolTCP), |
| 300 | + DestinationAddressPrefix: ptr.To("*"), |
| 301 | + DestinationPortRange: ptr.To(in.securityRulePort), |
| 302 | + Priority: ptr.To[int32](priority), |
| 303 | + SourceAddressPrefix: ptr.To("*"), |
| 304 | + SourcePortRange: ptr.To("*"), |
| 305 | + }, |
| 306 | + }, |
| 307 | + ) |
| 308 | + |
| 309 | + _, err = in.securityGroupsClient.BeginCreateOrUpdate(ctx, in.resourceGroupName, in.securityGroupName, securityGroup, nil) |
| 310 | + if err != nil { |
| 311 | + return fmt.Errorf("failed to add security rule: %w", err) |
| 312 | + } |
| 313 | + |
| 314 | + return nil |
| 315 | +} |
| 316 | + |
| 317 | +func deleteSecurityGroupRule(ctx context.Context, in *securityGroupInput) error { |
| 318 | + securityGroupResp, err := in.securityGroupsClient.Get(ctx, in.resourceGroupName, in.securityGroupName, nil) |
| 319 | + if err != nil { |
| 320 | + return fmt.Errorf("failed to get security group: %w", err) |
| 321 | + } |
| 322 | + securityGroup := securityGroupResp.SecurityGroup |
| 323 | + |
| 324 | + var securityRules []*armnetwork.SecurityRule |
| 325 | + for _, securityRule := range securityGroup.Properties.SecurityRules { |
| 326 | + if *securityRule.Name == in.securityRuleName { |
| 327 | + continue |
| 328 | + } |
| 329 | + securityRules = append(securityRules, securityRule) |
| 330 | + } |
| 331 | + securityGroup.Properties.SecurityRules = securityRules |
| 332 | + |
| 333 | + _, err = in.securityGroupsClient.BeginCreateOrUpdate(ctx, in.resourceGroupName, in.securityGroupName, securityGroup, nil) |
| 334 | + if err != nil { |
| 335 | + return fmt.Errorf("failed to update security group: %w", err) |
| 336 | + } |
| 337 | + |
| 338 | + return nil |
| 339 | +} |
0 commit comments