Skip to content

Commit

Permalink
refactor: renaming controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce-Soghigian committed Jan 16, 2025
1 parent 20c6625 commit 7e7e3be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ import (
corecloudprovider "sigs.k8s.io/karpenter/pkg/cloudprovider"
)

type VirtualMachineController struct {
type VirtualMachine struct {
kubeClient client.Client
cloudProvider corecloudprovider.CloudProvider
successfulCount uint64 // keeps track of successful reconciles for more aggressive requeuing near the start of the controller
}

func NewVirtualMachineController(kubeClient client.Client, cloudProvider corecloudprovider.CloudProvider) *VirtualMachineController {
return &VirtualMachineController{
func NewVirtualMachine(kubeClient client.Client, cloudProvider corecloudprovider.CloudProvider) *VirtualMachine {
return &VirtualMachine{
kubeClient: kubeClient,
cloudProvider: cloudProvider,
successfulCount: 0,
}
}

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

// We LIST VMs on the CloudProvider BEFORE we grab NodeClaims/Nodes on the cluster so that we make sure that, if
Expand Down Expand Up @@ -93,7 +93,7 @@ func (c *VirtualMachineController) Reconcile(ctx context.Context) (reconcile.Res
return reconcile.Result{RequeueAfter: lo.Ternary(c.successfulCount <= 20, time.Second*10, time.Minute*2)}, nil
}

func (c *VirtualMachineController) garbageCollect(ctx context.Context, nodeClaim *karpv1.NodeClaim, nodeList *v1.NodeList) error {
func (c *VirtualMachine) garbageCollect(ctx context.Context, nodeClaim *karpv1.NodeClaim, nodeList *v1.NodeList) error {
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).With("provider-id", nodeClaim.Status.ProviderID))
if err := c.cloudProvider.Delete(ctx, nodeClaim); err != nil {
return corecloudprovider.IgnoreNodeClaimNotFoundError(err)
Expand All @@ -112,7 +112,7 @@ func (c *VirtualMachineController) garbageCollect(ctx context.Context, nodeClaim
return nil
}

func (c *VirtualMachineController) Register(_ context.Context, m manager.Manager) error {
func (c *VirtualMachine) Register(_ context.Context, m manager.Manager) error {
return controllerruntime.NewControllerManagedBy(m).
Named("instance.garbagecollection").
WatchesRawSource(singleton.Source()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
NodeclaimReason = "nc"
)

type NetworkInterfaceController struct {
type NetworkInterface struct {
kubeClient client.Client
instanceProvider instance.Provider
// A network interface is considered unremovable if it meets the following 3 criteria
Expand All @@ -59,9 +59,9 @@ type NetworkInterfaceController struct {
unremovableNics *cache.Cache
}

func NewNetworkInterfaceController(kubeClient client.Client, instanceProvider instance.Provider) *NetworkInterfaceController {
func NewNetworkInterface(kubeClient client.Client, instanceProvider instance.Provider) *NetworkInterface {
unremovableNics := cache.New(NicReservationDuration, time.Second*30)
return &NetworkInterfaceController{
return &NetworkInterface{
kubeClient: kubeClient,
instanceProvider: instanceProvider,
unremovableNics: unremovableNics,
Expand All @@ -73,7 +73,7 @@ func NewNetworkInterfaceController(kubeClient client.Client, instanceProvider in
// 1: Reserved by NRP: When creating a nic and attempting to assign it to a vm, the nic will be reserved for that vm arm_resource_id for 180 seconds
// 2: Belongs to a Nodeclaim: If a nodeclaim exists in the cluster we shouldn't attempt removing it
// 3: Belongs to VM: If the VM Garbage Collection controller is removing a vm, we should not attempt removing it in this controller, and delegate that responsibility to the vm gc controller since deleting a successfully provisioned vm has delete options to also clean up the associated nic
func (c *NetworkInterfaceController) populateUnremovableNics(ctx context.Context) error {
func (c *NetworkInterface) populateUnremovableNics(ctx context.Context) error {
vms, err := c.instanceProvider.List(ctx)
if err != nil {
return fmt.Errorf("listing VMs: %w", err)
Expand All @@ -93,15 +93,15 @@ func (c *NetworkInterfaceController) populateUnremovableNics(ctx context.Context
}

// we want to removeNodeclaimsFromUnremovableNics as we want fresh data on nodeclaim state whenever possible
func (c *NetworkInterfaceController) removeNodeclaimsFromUnremovableNics() {
func (c *NetworkInterface) removeNodeclaimsFromUnremovableNics() {
for key, reason := range c.unremovableNics.Items() {
if reason.Object.(string) == NodeclaimReason {
c.unremovableNics.Delete(key)
}
}
}

func (c *NetworkInterfaceController) Reconcile(ctx context.Context) (reconcile.Result, error) {
func (c *NetworkInterface) Reconcile(ctx context.Context) (reconcile.Result, error) {
ctx = injection.WithControllerName(ctx, NICGCControllerName)
nics, err := c.instanceProvider.ListNics(ctx)
if err != nil {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (c *NetworkInterfaceController) Reconcile(ctx context.Context) (reconcile.R
}, nil
}

func (c *NetworkInterfaceController) Register(_ context.Context, m manager.Manager) error {
func (c *NetworkInterface) Register(_ context.Context, m manager.Manager) error {
return controllerruntime.NewControllerManagedBy(m).
Named(NICGCControllerName).
WatchesRawSource(singleton.Source()).
Expand Down
9 changes: 4 additions & 5 deletions pkg/controllers/nodeclaim/garbagecollection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ var nodePool *karpv1.NodePool
var nodeClass *v1alpha2.AKSNodeClass
var cluster *state.Cluster
var cloudProvider *cloudprovider.CloudProvider
var virtualMachineGCController *garbagecollection.VirtualMachineController
var networkInterfaceGCController *garbagecollection.NetworkInterfaceController
var virtualMachineGCController *garbagecollection.VirtualMachine
var networkInterfaceGCController *garbagecollection.NetworkInterface
var prov *provisioning.Provisioner

func TestAPIs(t *testing.T) {
Expand All @@ -83,8 +83,8 @@ var _ = BeforeSuite(func() {
// ctx, stop = context.WithCancel(ctx)
azureEnv = test.NewEnvironment(ctx, env)
cloudProvider = cloudprovider.New(azureEnv.InstanceTypesProvider, azureEnv.InstanceProvider, events.NewRecorder(&record.FakeRecorder{}), env.Client, azureEnv.ImageProvider)
virtualMachineGCController = garbagecollection.NewVirtualMachineController(env.Client, cloudProvider)
networkInterfaceGCController = garbagecollection.NewNetworkInterfaceController(env.Client, azureEnv.InstanceProvider)
virtualMachineGCController = garbagecollection.NewVirtualMachine(env.Client, cloudProvider)
networkInterfaceGCController = garbagecollection.NewNetworkInterface(env.Client, azureEnv.InstanceProvider)
fakeClock = &clock.FakeClock{}
cluster = state.NewCluster(fakeClock, env.Client)
prov = provisioning.NewProvisioner(env.Client, events.NewRecorder(&record.FakeRecorder{}), cloudProvider, cluster, fakeClock)
Expand Down Expand Up @@ -435,4 +435,3 @@ var _ = Describe("NetworkInterface Garbage Collection", func() {

})

Check failure on line 437 in pkg/controllers/nodeclaim/garbagecollection/suite_test.go

View workflow job for this annotation

GitHub Actions / ci

expected '}', found 'EOF'

Check failure on line 437 in pkg/controllers/nodeclaim/garbagecollection/suite_test.go

View workflow job for this annotation

GitHub Actions / ci

expected '}', found 'EOF'

Check failure on line 437 in pkg/controllers/nodeclaim/garbagecollection/suite_test.go

View workflow job for this annotation

GitHub Actions / ci

expected '}', found 'EOF'

Check failure on line 437 in pkg/controllers/nodeclaim/garbagecollection/suite_test.go

View workflow job for this annotation

GitHub Actions / ci

expected '}', found 'EOF' (typecheck)
})

0 comments on commit 7e7e3be

Please sign in to comment.