Skip to content

Commit

Permalink
feat: working poc of ARG Queries and nic garbage collection, need to …
Browse files Browse the repository at this point in the history
…fix tests
  • Loading branch information
Bryce-Soghigian committed Jan 8, 2025
1 parent 7fbc86a commit d229ecb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
10 changes: 5 additions & 5 deletions pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (c *CloudProvider) List(ctx context.Context) ([]*karpv1.NodeClaim, error) {
if err != nil {
return nil, fmt.Errorf("listing instances, %w", err)
}

var nodeClaims []*karpv1.NodeClaim
for _, instance := range instances {
instanceType, err := c.resolveInstanceTypeFromInstance(ctx, instance)
Expand Down Expand Up @@ -339,9 +340,8 @@ func (c *CloudProvider) instanceToNodeClaim(ctx context.Context, vm *armcompute.

labels[karpv1.CapacityTypeLabelKey] = instance.GetCapacityType(vm)

// TODO: v1beta1 new kes/labels
if tag, ok := vm.Tags[instance.NodePoolTagKey]; ok {
labels[karpv1.NodePoolLabelKey] = *tag
labels[karpv1.NodePoolLabelKey] = lo.FromPtr(tag)
}

inPlaceUpdateHash, err := inplaceupdate.HashFromVM(vm)
Expand All @@ -350,15 +350,15 @@ func (c *CloudProvider) instanceToNodeClaim(ctx context.Context, vm *armcompute.
}
annotations[v1alpha2.AnnotationInPlaceUpdateHash] = inPlaceUpdateHash

nodeClaim.Name = GenerateNodeClaimName(*vm.Name)
nodeClaim.Name = GenerateNodeClaimName(lo.FromPtr(vm.Name))
nodeClaim.Labels = labels
nodeClaim.Annotations = annotations
nodeClaim.CreationTimestamp = metav1.Time{Time: *vm.Properties.TimeCreated}
nodeClaim.CreationTimestamp = metav1.Time{Time: lo.FromPtr(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()}
}
nodeClaim.Status.ProviderID = utils.ResourceIDToProviderID(ctx, *vm.ID)
nodeClaim.Status.ProviderID = utils.ResourceIDToProviderID(ctx, lo.FromPtr(vm.ID))
if vm.Properties != nil && vm.Properties.StorageProfile != nil && vm.Properties.StorageProfile.ImageReference != nil {
nodeClaim.Status.ImageID = utils.ImageReferenceToString(vm.Properties.StorageProfile.ImageReference)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewControllers(ctx context.Context, mgr manager.Manager, kubeClient client.
nodeclasshash.NewController(kubeClient),
nodeclassstatus.NewController(kubeClient),
nodeclasstermination.NewController(kubeClient, recorder),
nodeclaimgarbagecollection.NewController(kubeClient, cloudProvider),
nodeclaimgarbagecollection.NewController(kubeClient, cloudProvider, instanceProvider),
// TODO: nodeclaim tagging
inplaceupdate.NewController(kubeClient, instanceProvider),
status.NewController[*v1alpha2.AKSNodeClass](kubeClient, mgr.GetEventRecorderFor("karpenter")),
Expand Down
20 changes: 12 additions & 8 deletions pkg/controllers/nodeclaim/garbagecollection/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,34 @@ type Controller struct {
successfulCount uint64 // keeps track of successful reconciles for more aggressive requeueing near the start of the controller
}

func NewController(kubeClient client.Client, cloudProvider corecloudprovider.CloudProvider) *Controller {
func NewController(kubeClient client.Client, cloudProvider corecloudprovider.CloudProvider, instanceProvider instance.Provider) *Controller {
return &Controller{
kubeClient: kubeClient,
cloudProvider: cloudProvider,
successfulCount: 0,
kubeClient: kubeClient,
cloudProvider: cloudProvider,
instanceProvider: instanceProvider,
successfulCount: 0,
}
}

func (c *Controller) Reconcile(ctx context.Context) (reconcile.Result, error) {
ctx = injection.WithControllerName(ctx, "instance.garbagecollection")
var aggregatedError error

// Perform VM garbage collection
if err := c.gcVMs(ctx); err != nil {
return reconcile.Result{}, fmt.Errorf("VM garbage collection failed: %w", err)
aggregatedError = multierr.Append(aggregatedError, fmt.Errorf("VM garbage collection failed: %w", err))
}

// Perform NIC garbage collection
if err := c.gcNics(ctx); err != nil {
return reconcile.Result{}, fmt.Errorf("NIC garbage collection failed: %w", err)
aggregatedError = multierr.Append(aggregatedError, fmt.Errorf("NIC garbage collection failed: %w", err))
}

c.successfulCount++

return reconcile.Result{
RequeueAfter: lo.Ternary(c.successfulCount <= 20, time.Second*10, time.Minute*2),
}, nil
RequeueAfter: lo.Ternary(c.successfulCount <= 20, 10*time.Second, 2*time.Minute),
}, aggregatedError
}

// gcVMs handles the garbage collection of virtual machines.
Expand Down Expand Up @@ -156,6 +159,7 @@ func (c *Controller) gcNics(ctx context.Context) error {
gcErrors = append(gcErrors, fmt.Errorf("deleting NIC %s: %w", nicName, err))
mu.Unlock()
}
logging.FromContext(ctx).With("nic", nicName).Infof("garbage collected NIC")
}
})

Expand Down
6 changes: 1 addition & 5 deletions pkg/providers/instance/arglist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const (
nicResourceType = "microsoft.network/networkinterfaces"
)

var (
vmListQuery string
nicListQuery string
)

// getResourceListQueryBuilder returns a KQL query builder for listing resources with nodepool tags
func getResourceListQueryBuilder(rg string, resourceType string) *kql.Builder {
return kql.New(`Resources`).
Expand Down Expand Up @@ -73,6 +68,7 @@ func createNICFromQueryResponseData(data map[string]interface{}) (*armnetwork.In
if err != nil {
return nil, err
}

nic := armnetwork.Interface{}
err = json.Unmarshal(jsonString, &nic)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/providers/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ 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 @@ -112,6 +117,8 @@ func NewDefaultProvider(
subscriptionID string,
provisionMode string,
) *DefaultProvider {
vmListQuery = GetVMListQueryBuilder(resourceGroup).String()
nicListQuery = GetNICListQueryBuilder(resourceGroup).String()
return &DefaultProvider{
azClient: azClient,
instanceTypeProvider: instanceTypeProvider,
Expand Down

0 comments on commit d229ecb

Please sign in to comment.