-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NSG extension to merge SecurityRules #3121
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3ff17d
Add NSG extention to merge SecurityRules
super-harsh 4f91ab5
Merge branch 'main' into fix/nsg
super-harsh 8d3e160
Add index
super-harsh 8a47210
Merge branch 'main' into fix/nsg
super-harsh df083f1
Add for SecurityRules
super-harsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
v2/api/network/customizations/network_security_group_extension.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
package customizations | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/conversion" | ||
|
||
network "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101storage" | ||
. "github.com/Azure/azure-service-operator/v2/internal/logging" | ||
"github.com/Azure/azure-service-operator/v2/internal/resolver" | ||
"github.com/Azure/azure-service-operator/v2/internal/util/kubeclient" | ||
"github.com/Azure/azure-service-operator/v2/pkg/genruntime" | ||
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/extensions" | ||
) | ||
|
||
// Attention: A lot of code in this file is very similar to the logic in load_balancer_extension.go, route_table_extensions.go and virtual_network_extensions.go. | ||
// The two should be kept in sync as much as possible. | ||
// NOTE: This wouldn't work without adding indexes in 'getGeneratedStorageTypes' method in controller_resources.go | ||
|
||
var _ extensions.ARMResourceModifier = &NetworkSecurityGroupExtension{} | ||
|
||
func (extension *NetworkSecurityGroupExtension) ModifyARMResource( | ||
ctx context.Context, | ||
armObj genruntime.ARMResource, | ||
obj genruntime.ARMMetaObject, | ||
kubeClient kubeclient.Client, | ||
resolver *resolver.Resolver, | ||
log logr.Logger, | ||
) (genruntime.ARMResource, error) { | ||
typedObj, ok := obj.(*network.NetworkSecurityGroup) | ||
if !ok { | ||
return nil, errors.Errorf("cannot run on unknown resource type %T, expected *network.NetworkSecurityGroup", obj) | ||
} | ||
|
||
// Type assert that we are the hub type. This will fail to compile if | ||
// the hub type has been changed but this extension has not been updated to match | ||
var _ conversion.Hub = typedObj | ||
|
||
networkSecurityGroupsSecurityRuleGVK := getNetworkSecurityGroupsSecurityRuleGVK(obj) | ||
|
||
networkSecurityGroupsSecurityRules := &network.NetworkSecurityGroupsSecurityRuleList{} | ||
matchingFields := client.MatchingFields{".metadata.ownerReferences[0]": string(obj.GetUID())} | ||
err := kubeClient.List(ctx, networkSecurityGroupsSecurityRules, matchingFields) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed listing NetworkSecurityGroupsSecurityRule owned by NetworkSecurityGroup %s/%s", obj.GetNamespace(), obj.GetName()) | ||
} | ||
|
||
armNetworkSecurityGroupsSecurityRule := make([]genruntime.ARMResourceSpec, 0, len(networkSecurityGroupsSecurityRules.Items)) | ||
for _, networkSecurityGroupsSecurityRule := range networkSecurityGroupsSecurityRules.Items { | ||
networkSecurityGroupsSecurityRule := networkSecurityGroupsSecurityRule | ||
|
||
var transformed genruntime.ARMResourceSpec | ||
transformed, err = transformToARM(ctx, &networkSecurityGroupsSecurityRule, networkSecurityGroupsSecurityRuleGVK, kubeClient, resolver) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to transform NetworkSecurityGroupsSecurityRules %s/%s", networkSecurityGroupsSecurityRule.GetNamespace(), networkSecurityGroupsSecurityRule.GetName()) | ||
} | ||
armNetworkSecurityGroupsSecurityRule = append(armNetworkSecurityGroupsSecurityRule, transformed) | ||
} | ||
|
||
log.V(Info).Info("Found NetworkSecurityGroupsSecurityRule to include on NetworkSecurityGroup", "count", len(armNetworkSecurityGroupsSecurityRule), "names", genruntime.ARMSpecNames(armNetworkSecurityGroupsSecurityRule)) | ||
|
||
err = fuzzySetResources(armObj.Spec(), armNetworkSecurityGroupsSecurityRule, "SecurityRules") | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to set networkSecurityGroupsSecurityRules") | ||
} | ||
|
||
return armObj, nil | ||
} | ||
|
||
func getNetworkSecurityGroupsSecurityRuleGVK(nsg genruntime.ARMMetaObject) schema.GroupVersionKind { | ||
gvk := genruntime.GetOriginalGVK(nsg) | ||
gvk.Kind = reflect.TypeOf(network.NetworkSecurityGroupsSecurityRule{}).Name() // "NetworkSecurityGroupsSecurityRule" | ||
|
||
return gvk | ||
} |
50 changes: 50 additions & 0 deletions
50
v2/api/network/customizations/network_security_group_extension_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
|
||
package customizations | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
network "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101" | ||
"github.com/Azure/azure-service-operator/v2/internal/util/to" | ||
"github.com/Azure/azure-service-operator/v2/pkg/genruntime" | ||
) | ||
|
||
func Test_FuzzySetNetworkSecurityGroupsSecurityRules(t *testing.T) { | ||
t.Parallel() | ||
g := NewGomegaWithT(t) | ||
|
||
nsg := &network.NetworkSecurityGroup_Spec_ARM{ | ||
Location: to.Ptr("westus"), | ||
Name: "my-nsg", | ||
} | ||
|
||
rule := &network.NetworkSecurityGroups_SecurityRule_Spec_ARM{ | ||
Name: "myrule", | ||
Properties: &network.SecurityRulePropertiesFormat_NetworkSecurityGroups_SecurityRule_SubResourceEmbedded_ARM{ | ||
Access: to.Ptr(network.SecurityRuleAccess_Allow), | ||
DestinationAddressPrefix: to.Ptr("*"), | ||
DestinationPortRange: to.Ptr("46-56"), | ||
Direction: to.Ptr(network.SecurityRuleDirection_Inbound), | ||
Priority: to.Ptr(123), | ||
Protocol: to.Ptr(network.SecurityRulePropertiesFormat_Protocol_Tcp), | ||
SourceAddressPrefix: to.Ptr("*"), | ||
SourcePortRange: to.Ptr("23-45"), | ||
}, | ||
} | ||
|
||
err := fuzzySetResources(nsg, []genruntime.ARMResourceSpec{rule}, "SecurityRules") | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(nsg.Location).To(Equal(to.Ptr("westus"))) | ||
g.Expect(nsg.Properties).ToNot(BeNil()) | ||
g.Expect(nsg.Properties.SecurityRules).To(HaveLen(1)) | ||
g.Expect(nsg.Properties.SecurityRules[0].Properties).ToNot(BeNil()) | ||
g.Expect(nsg.Properties.SecurityRules[0].Name).To(Equal(to.Ptr("myrule"))) | ||
g.Expect(nsg.Properties.SecurityRules[0].Properties.DestinationAddressPrefix).ToNot(BeNil()) | ||
g.Expect(nsg.Properties.SecurityRules[0].Properties.DestinationPortRange).ToNot(BeNil()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this preserved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is here