Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,9 @@ func (c *CloudProvider) vmInstanceToNodeClaim(ctx context.Context, vm *armcomput
nodeClaim.Name = GetNodeClaimNameFromVMName(*vm.Name)
nodeClaim.Labels = labels
nodeClaim.Annotations = annotations
nodeClaim.CreationTimestamp = metav1.Time{Time: *vm.Properties.TimeCreated}
if vm.Properties != nil && vm.Properties.TimeCreated != nil {
nodeClaim.CreationTimestamp = metav1.Time{Time: *vm.Properties.TimeCreated}
}
// Set the deletionTimestamp to be the current time if the instance is currently terminating
if utils.IsVMDeleting(*vm) {
nodeClaim.DeletionTimestamp = &metav1.Time{Time: time.Now()}
Expand Down
67 changes: 67 additions & 0 deletions pkg/cloudprovider/cloudprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ limitations under the License.
package cloudprovider

import (
"context"
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v7"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGenerateNodeClaimName(t *testing.T) {
Expand Down Expand Up @@ -54,3 +59,65 @@ func TestGenerateNodeClaimName(t *testing.T) {
})
}
}

func TestVmInstanceToNodeClaim_NilProperties(t *testing.T) {
tests := []struct {
name string
vm *armcompute.VirtualMachine
expectNoTime bool
expectTimeSet bool
}{
{
name: "nil Properties",
vm: &armcompute.VirtualMachine{
Name: to.Ptr("aks-test-vm"),
ID: to.Ptr("/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/aks-test-vm"),
},
expectNoTime: true,
},
{
name: "nil TimeCreated",
vm: &armcompute.VirtualMachine{
Name: to.Ptr("aks-test-vm"),
ID: to.Ptr("/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/aks-test-vm"),
Properties: &armcompute.VirtualMachineProperties{},
},
expectNoTime: true,
},
{
name: "valid TimeCreated",
vm: &armcompute.VirtualMachine{
Name: to.Ptr("aks-test-vm"),
ID: to.Ptr("/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/aks-test-vm"),
Properties: &armcompute.VirtualMachineProperties{
TimeCreated: to.Ptr(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)),
},
},
expectTimeSet: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
ctx := context.Background()

cp := &CloudProvider{}
nodeClaim, err := cp.vmInstanceToNodeClaim(ctx, tt.vm, nil)

g.Expect(err).ToNot(HaveOccurred())
g.Expect(nodeClaim).ToNot(BeNil())

if tt.expectNoTime {
// CreationTimestamp should be zero value when Properties or TimeCreated is nil
g.Expect(nodeClaim.CreationTimestamp).To(Equal(metav1.Time{}))
}

if tt.expectTimeSet {
// CreationTimestamp should be set when TimeCreated is valid
g.Expect(nodeClaim.CreationTimestamp).ToNot(Equal(metav1.Time{}))
g.Expect(nodeClaim.CreationTimestamp.Time).To(Equal(time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)))
}
})
}
}