Skip to content

Commit

Permalink
refactor: network interface garbage collection followup (#670)
Browse files Browse the repository at this point in the history
* refactor: nicListQuery and vmListQuery vars should be scoped to the provider
* style: breaking test.Interface calls into multiple lines
  • Loading branch information
Bryce-Soghigian authored Jan 31, 2025
1 parent 7563bca commit 1336858
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
23 changes: 17 additions & 6 deletions pkg/controllers/nodeclaim/garbagecollection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ var _ = Describe("NetworkInterface Garbage Collection", func() {
ExpectApplied(ctx, env.Client, nodeClaim)

// Create a managed NIC
nic := test.Interface(test.InterfaceOptions{Name: instance.GenerateResourceName(nodeClaim.Name), NodepoolName: nodePool.Name})
nic := test.Interface(test.InterfaceOptions{
Name: instance.GenerateResourceName(nodeClaim.Name),
NodepoolName: nodePool.Name,
})
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(nic.ID), *nic)

nicsBeforeGC, err := azureEnv.InstanceProvider.ListNics(ctx)
Expand All @@ -340,8 +343,12 @@ var _ = Describe("NetworkInterface Garbage Collection", func() {
Expect(len(nicsAfterGC)).To(Equal(1))
})
It("should delete a NIC if there is no associated VM", func() {
nic := test.Interface(test.InterfaceOptions{NodepoolName: nodePool.Name})
nic2 := test.Interface(test.InterfaceOptions{NodepoolName: nodePool.Name})
nic := test.Interface(test.InterfaceOptions{
NodepoolName: nodePool.Name,
})
nic2 := test.Interface(test.InterfaceOptions{
NodepoolName: nodePool.Name,
})
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(nic.ID), *nic)
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(nic2.ID), *nic2)
nicsBeforeGC, err := azureEnv.InstanceProvider.ListNics(ctx)
Expand All @@ -355,7 +362,9 @@ var _ = Describe("NetworkInterface Garbage Collection", func() {
Expect(len(nicsAfterGC)).To(Equal(0))
})
It("should not delete a NIC if there is an associated VM", func() {
managedNic := test.Interface(test.InterfaceOptions{NodepoolName: nodePool.Name})
managedNic := test.Interface(test.InterfaceOptions{
NodepoolName: nodePool.Name,
})
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(managedNic.ID), *managedNic)
managedVM := test.VirtualMachine(test.VirtualMachineOptions{Name: lo.FromPtr(managedNic.Name), NodepoolName: nodePool.Name})
azureEnv.VirtualMachinesAPI.VirtualMachinesBehavior.Instances.Store(lo.FromPtr(managedVM.ID), *managedVM)
Expand All @@ -366,8 +375,10 @@ var _ = Describe("NetworkInterface Garbage Collection", func() {
Expect(len(nicsAfterGC)).To(Equal(1))

})
It("the vm gc controller should remove the nic if there is an associated vm", func() {
managedNic := test.Interface(test.InterfaceOptions{NodepoolName: nodePool.Name})
It("the vm gc controller should handle deletion of network interfaces if a nic is associated with a vm", func() {
managedNic := test.Interface(test.InterfaceOptions{
NodepoolName: nodePool.Name,
})
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(managedNic.ID), *managedNic)
managedVM := test.VirtualMachine(test.VirtualMachineOptions{
Name: lo.FromPtr(managedNic.Name),
Expand Down
16 changes: 7 additions & 9 deletions pkg/providers/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
)

var (
vmListQuery string
nicListQuery string
)

var (
NodePoolTagKey = strings.ReplaceAll(karpv1.NodePoolLabelKey, "/", "_")

Expand Down Expand Up @@ -106,6 +101,8 @@ type DefaultProvider struct {
subscriptionID string
unavailableOfferings *cache.UnavailableOfferings
provisionMode string

vmListQuery, nicListQuery string
}

func NewDefaultProvider(
Expand All @@ -119,8 +116,6 @@ func NewDefaultProvider(
subscriptionID string,
provisionMode string,
) *DefaultProvider {
vmListQuery = GetVMListQueryBuilder(resourceGroup).String()
nicListQuery = GetNICListQueryBuilder(resourceGroup).String()
return &DefaultProvider{
azClient: azClient,
instanceTypeProvider: instanceTypeProvider,
Expand All @@ -131,6 +126,9 @@ func NewDefaultProvider(
subscriptionID: subscriptionID,
unavailableOfferings: offeringsCache,
provisionMode: provisionMode,

vmListQuery: GetVMListQueryBuilder(resourceGroup).String(),
nicListQuery: GetNICListQueryBuilder(resourceGroup).String(),
}
}

Expand Down Expand Up @@ -180,7 +178,7 @@ func (p *DefaultProvider) Get(ctx context.Context, vmName string) (*armcompute.V
}

func (p *DefaultProvider) List(ctx context.Context) ([]*armcompute.VirtualMachine, error) {
req := NewQueryRequest(&(p.subscriptionID), vmListQuery)
req := NewQueryRequest(&(p.subscriptionID), p.vmListQuery)
client := p.azClient.azureResourceGraphClient
data, err := GetResourceData(ctx, client, *req)
if err != nil {
Expand Down Expand Up @@ -212,7 +210,7 @@ func (p *DefaultProvider) GetNic(ctx context.Context, rg, nicName string) (*armn

// ListNics returns all network interfaces in the resource group that have the nodepool tag
func (p *DefaultProvider) ListNics(ctx context.Context) ([]*armnetwork.Interface, error) {
req := NewQueryRequest(&(p.subscriptionID), nicListQuery)
req := NewQueryRequest(&(p.subscriptionID), p.nicListQuery)
client := p.azClient.azureResourceGraphClient
data, err := GetResourceData(ctx, client, *req)
if err != nil {
Expand Down

0 comments on commit 1336858

Please sign in to comment.