Skip to content

Commit

Permalink
refactor: added nodepoolName to test.VirtualMachine and test.Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce-Soghigian committed Jan 28, 2025
1 parent c0e4043 commit 50d9455
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
30 changes: 8 additions & 22 deletions pkg/controllers/nodeclaim/garbagecollection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,7 @@ 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),
Tags: test.ManagedTags(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 @@ -362,12 +359,8 @@ 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{
Tags: test.ManagedTags(nodePool.Name),
})
nic2 := test.Interface(test.InterfaceOptions{
Tags: test.ManagedTags(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 @@ -381,14 +374,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{
Tags: test.ManagedTags(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),
Tags: test.ManagedTags(nodePool.Name),
})
managedVM := test.VirtualMachine(test.VirtualMachineOptions{Name: lo.FromPtr(managedNic.Name), NodepoolName: nodePool.Name})
azureEnv.VirtualMachinesAPI.VirtualMachinesBehavior.Instances.Store(lo.FromPtr(managedVM.ID), *managedVM)
ExpectSingletonReconciled(ctx, networkInterfaceGCController)
// We should still have a network interface here
Expand All @@ -398,13 +386,11 @@ var _ = Describe("NetworkInterface Garbage Collection", func() {

})
It("the vm gc controller should remove the nic if there is an associated vm", func() {
managedNic := test.Interface(test.InterfaceOptions{
Tags: test.ManagedTags(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),
Tags: test.ManagedTags(nodePool.Name),
Name: lo.FromPtr(managedNic.Name),
NodepoolName: nodePool.Name,
Properties: &armcompute.VirtualMachineProperties{
TimeCreated: lo.ToPtr(time.Now().Add(-time.Minute * 16)), // Needs to be older than the nodeclaim registration ttl
},
Expand Down
8 changes: 2 additions & 6 deletions pkg/providers/instance/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,8 @@ var _ = Describe("InstanceProvider", func() {
Expect(len(interfaces)).To(Equal(1))
})
It("should only list nics that belong to karpenter", func() {
managedNic := test.Interface(
test.InterfaceOptions{
Tags: test.ManagedTags(nodePool.Name),
},
)
unmanagedNic := test.Interface()
managedNic := test.Interface(test.InterfaceOptions{NodepoolName: nodePool.Name})
unmanagedNic := test.Interface(test.InterfaceOptions{Tags: map[string]*string{"kubernetes.io/cluster/test-cluster": lo.ToPtr("random-aks-vm")}})

azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(managedNic.ID), *managedNic)
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(unmanagedNic.ID), *unmanagedNic)
Expand Down
15 changes: 11 additions & 4 deletions pkg/test/networkinterfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import (

// InterfaceOptions customizes an Azure Network Interface for testing.
type InterfaceOptions struct {
Name string
Location string
Properties *armnetwork.InterfacePropertiesFormat
Tags map[string]*string
Name string
NodepoolName string
Location string
Properties *armnetwork.InterfacePropertiesFormat
Tags map[string]*string
}

// Interface creates a test Azure Network Interface with defaults that can be overridden by InterfaceOptions.
Expand All @@ -46,9 +47,15 @@ func Interface(overrides ...InterfaceOptions) *armnetwork.Interface {
if options.Name == "" {
options.Name = RandomName("aks")
}
if options.NodepoolName == "" {
options.NodepoolName = "default"
}
if options.Location == "" {
options.Location = "eastus"
}
if options.Tags == nil {
options.Tags = ManagedTags(options.NodepoolName)
}
if options.Properties == nil {
options.Properties = &armnetwork.InterfacePropertiesFormat{}
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/test/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (

// VirtualMachineOptions customizes an Azure Virtual Machine for testing.
type VirtualMachineOptions struct {
Name string
Location string
Properties *armcompute.VirtualMachineProperties
Tags map[string]*string
Name string
NodepoolName string
Location string
Properties *armcompute.VirtualMachineProperties
Tags map[string]*string
}

// VirtualMachine creates a test Azure Virtual Machine with defaults that can be overridden by VirtualMachineOptions.
Expand All @@ -47,12 +48,18 @@ func VirtualMachine(overrides ...VirtualMachineOptions) *armcompute.VirtualMachi
if options.Name == "" {
options.Name = RandomName("aks")
}
if options.NodepoolName == "" {
options.NodepoolName = "default"
}
if options.Location == "" {
options.Location = "eastus"
}
if options.Properties == nil {
options.Properties = &armcompute.VirtualMachineProperties{}
}
if options.Tags == nil {
options.Tags = ManagedTags(options.NodepoolName)
}
if options.Properties.TimeCreated == nil {
options.Properties.TimeCreated = lo.ToPtr(time.Now())
}
Expand Down

0 comments on commit 50d9455

Please sign in to comment.