-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: garbage collection controller removes orphaned nics (#686)
* test: garbage collection controller removes orphaned nics * refactor: breaking logic into modular steps to reduce cyclomatic complexity * ci: golang-ci lint * fix: going back to utilization as the default * refactor: moving env vars to azureEnv struct * refactor: using azure clients defined inside of env * refactor: using more generic list approach that can be generalized and used elsewhere * refactor: using AZURE_RESOURCE_GROUP_MC as a name rather than AZURE_RESOURCE_GROUP * ci: lint * test: checkin azure garbage collection into our e2e matrix * fix: propagating values to makefile fix: propagating values to makefile * fix: constructing mc rg * fix: use CLUSTER_NAME instead of AZURE_CLUSTER_NAME * test: refactoring to use environment * fix: propagating location * refactor: have acr e2e consume from environment vars stored in azureEnv * fix: removing import * refactor: renaming CLUSTER_NAME to match all other variables * refactor: moving env vars outside of az-e2etest since e2etest now holds that state * ci: make presubmit * refactor: removing readme * refactor: using lo.Must() + os.LookupEnv * revert: go.mod go version change * refactor: moving azuregc suite to the nodeclaim suite * Update Makefile-az.mk * refactor: using env block instead * fix: comment --------- Co-authored-by: Alex Leites <[email protected]>
- Loading branch information
1 parent
b199e51
commit 11f23ea
Showing
6 changed files
with
175 additions
and
13 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Portions Copyright (c) Microsoft Corporation. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/samber/lo" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" | ||
) | ||
|
||
func (env *Environment) EventuallyExpectKarpenterNicsToBeDeleted() { | ||
GinkgoHelper() | ||
Eventually(func() bool { | ||
pager := env.InterfacesClient.NewListPager(env.NodeResourceGroup, nil) | ||
for pager.More() { | ||
resp, err := pager.NextPage(env.Context) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
for _, nic := range resp.Value { | ||
if nic.Tags != nil { | ||
if _, exists := nic.Tags[strings.ReplaceAll(karpv1.NodePoolLabelKey, "/", "_")]; exists { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}).WithTimeout(10*time.Minute).WithPolling(10*time.Second).Should(BeTrue(), "Expected all orphan NICs to be deleted") | ||
} | ||
|
||
func (env *Environment) ExpectCreatedInterface(networkInterface armnetwork.Interface) { | ||
GinkgoHelper() | ||
poller, err := env.InterfacesClient.BeginCreateOrUpdate(env.Context, env.NodeResourceGroup, lo.FromPtr(networkInterface.Name), networkInterface, nil) | ||
Expect(err).ToNot(HaveOccurred()) | ||
_, err = poller.PollUntilDone(env.Context, nil) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
|
||
func (env *Environment) GetClusterSubnet() *armnetwork.Subnet { | ||
GinkgoHelper() | ||
vnet, err := firstVNETInRG(env.Context, env.VNETClient, env.VNETResourceGroup) | ||
Expect(err).ToNot(HaveOccurred()) | ||
return vnet.Properties.Subnets[0] | ||
} | ||
|
||
// This returns the first vnet we find in the resource group, works for managed vnet, it hasn't been tested on custom vnet. | ||
func firstVNETInRG(ctx context.Context, client *armnetwork.VirtualNetworksClient, vnetRG string) (*armnetwork.VirtualNetwork, error) { | ||
pager := client.NewListPager(vnetRG, nil) | ||
for pager.More() { | ||
resp, err := pager.NextPage(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list virtual networks: %w", err) | ||
} | ||
if len(resp.VirtualNetworkListResult.Value) > 0 { | ||
return resp.VirtualNetworkListResult.Value[0], nil | ||
} | ||
} | ||
return nil, fmt.Errorf("no virtual networks found in resource group: %s", vnetRG) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Portions Copyright (c) Microsoft Corporation. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nodeclaim_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
"github.com/samber/lo" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" | ||
azkarptest "github.com/Azure/karpenter-provider-azure/pkg/test" | ||
) | ||
|
||
var _ = Describe("gc", func() { | ||
It("should garbage collect network interfaces created by karpenter", func() { | ||
env.ExpectCreatedInterface(armnetwork.Interface{ | ||
Name: lo.ToPtr("orphan-nic"), | ||
Location: lo.ToPtr(env.Region), | ||
Tags: azkarptest.ManagedTags("default"), | ||
Properties: &armnetwork.InterfacePropertiesFormat{ | ||
IPConfigurations: []*armnetwork.InterfaceIPConfiguration{ | ||
{ | ||
Name: lo.ToPtr("ip-config"), | ||
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{ | ||
Primary: lo.ToPtr(true), | ||
Subnet: env.GetClusterSubnet(), | ||
PrivateIPAllocationMethod: lo.ToPtr(armnetwork.IPAllocationMethodDynamic), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
env.EventuallyExpectKarpenterNicsToBeDeleted() | ||
}) | ||
}) |