From 079e3ce92f377888184a5e9ad51488d3337399f4 Mon Sep 17 00:00:00 2001 From: Nick Tran <10810510+njtran@users.noreply.github.com> Date: Thu, 2 Nov 2023 10:15:39 -0700 Subject: [PATCH] chore: remove v1alpha5 utils (#658) --- pkg/controllers/disruption/events/events.go | 98 +--- .../nodeclaim/consistency/events.go | 11 - pkg/controllers/nodeclaim/lifecycle/events.go | 21 - pkg/test/expectations/expectations.go | 116 +---- pkg/utils/machine/machine.go | 301 ------------- pkg/utils/machine/suite_test.go | 425 ------------------ pkg/utils/nodeclaim/nodeclaim.go | 155 +------ pkg/utils/nodeclaim/suite_test.go | 293 ------------ 8 files changed, 20 insertions(+), 1400 deletions(-) delete mode 100644 pkg/utils/machine/machine.go delete mode 100644 pkg/utils/machine/suite_test.go diff --git a/pkg/controllers/disruption/events/events.go b/pkg/controllers/disruption/events/events.go index bc5e01ccdc..b4b1001899 100644 --- a/pkg/controllers/disruption/events/events.go +++ b/pkg/controllers/disruption/events/events.go @@ -24,20 +24,9 @@ import ( "github.com/aws/karpenter-core/pkg/apis/v1beta1" "github.com/aws/karpenter-core/pkg/events" - machineutil "github.com/aws/karpenter-core/pkg/utils/machine" ) func Launching(nodeClaim *v1beta1.NodeClaim, reason string) events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return events.Event{ - InvolvedObject: machine, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningLaunching", - Message: fmt.Sprintf("Launching Machine: %s", cases.Title(language.Und, cases.NoLower).String(reason)), - DedupeValues: []string{string(machine.UID), reason}, - } - } return events.Event{ InvolvedObject: nodeClaim, Type: v1.EventTypeNormal, @@ -48,16 +37,6 @@ func Launching(nodeClaim *v1beta1.NodeClaim, reason string) events.Event { } func WaitingOnReadiness(nodeClaim *v1beta1.NodeClaim) events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return events.Event{ - InvolvedObject: machine, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningWaitingReadiness", - Message: "Waiting on readiness to continue deprovisioning", - DedupeValues: []string{string(machine.UID)}, - } - } return events.Event{ InvolvedObject: nodeClaim, Type: v1.EventTypeNormal, @@ -68,16 +47,6 @@ func WaitingOnReadiness(nodeClaim *v1beta1.NodeClaim) events.Event { } func WaitingOnDeletion(nodeClaim *v1beta1.NodeClaim) events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return events.Event{ - InvolvedObject: machine, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningWaitingDeletion", - Message: "Waiting on deletion to continue deprovisioning", - DedupeValues: []string{string(machine.UID)}, - } - } return events.Event{ InvolvedObject: nodeClaim, Type: v1.EventTypeNormal, @@ -88,25 +57,6 @@ func WaitingOnDeletion(nodeClaim *v1beta1.NodeClaim) events.Event { } func Terminating(node *v1.Node, nodeClaim *v1beta1.NodeClaim, reason string) []events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return []events.Event{ - { - InvolvedObject: node, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningTerminating", - Message: fmt.Sprintf("Deprovisioning Node: %s", cases.Title(language.Und, cases.NoLower).String(reason)), - DedupeValues: []string{string(node.UID), reason}, - }, - { - InvolvedObject: machine, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningTerminating", - Message: fmt.Sprintf("Deprovisioning Machine: %s", cases.Title(language.Und, cases.NoLower).String(reason)), - DedupeValues: []string{string(machine.UID), reason}, - }, - } - } return []events.Event{ { InvolvedObject: node, @@ -125,30 +75,9 @@ func Terminating(node *v1.Node, nodeClaim *v1beta1.NodeClaim, reason string) []e } } -// Unconsolidatable is an event that informs the user that a Machine/Node combination cannot be consolidated -// due to the state of the Machine/Node or due to some state of the pods that are scheduled to the Machine/Node +// Unconsolidatable is an event that informs the user that a NodeClaim/Node combination cannot be consolidated +// due to the state of the NodeClaim/Node or due to some state of the pods that are scheduled to the NodeClaim/Node func Unconsolidatable(node *v1.Node, nodeClaim *v1beta1.NodeClaim, reason string) []events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return []events.Event{ - { - InvolvedObject: node, - Type: v1.EventTypeNormal, - Reason: "Unconsolidatable", - Message: reason, - DedupeValues: []string{string(node.UID)}, - DedupeTimeout: time.Minute * 15, - }, - { - InvolvedObject: machine, - Type: v1.EventTypeNormal, - Reason: "Unconsolidatable", - Message: reason, - DedupeValues: []string{string(machine.UID)}, - DedupeTimeout: time.Minute * 15, - }, - } - } return []events.Event{ { InvolvedObject: node, @@ -169,28 +98,9 @@ func Unconsolidatable(node *v1.Node, nodeClaim *v1beta1.NodeClaim, reason string } } -// Blocked is an event that informs the user that a Machine/Node combination is blocked on deprovisioning -// due to the state of the Machine/Node or due to some state of the pods that are scheduled to the Machine/Node +// Blocked is an event that informs the user that a NodeClaim/Node combination is blocked on deprovisioning +// due to the state of the NodeClaim/Node or due to some state of the pods that are scheduled to the NodeClaim/Node func Blocked(node *v1.Node, nodeClaim *v1beta1.NodeClaim, reason string) []events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return []events.Event{ - { - InvolvedObject: node, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningBlocked", - Message: fmt.Sprintf("Cannot deprovision Node: %s", reason), - DedupeValues: []string{string(node.UID)}, - }, - { - InvolvedObject: machine, - Type: v1.EventTypeNormal, - Reason: "DeprovisioningBlocked", - Message: fmt.Sprintf("Cannot deprovision Machine: %s", reason), - DedupeValues: []string{string(machine.UID)}, - }, - } - } return []events.Event{ { InvolvedObject: node, diff --git a/pkg/controllers/nodeclaim/consistency/events.go b/pkg/controllers/nodeclaim/consistency/events.go index 026ed7e95a..9b44296f05 100644 --- a/pkg/controllers/nodeclaim/consistency/events.go +++ b/pkg/controllers/nodeclaim/consistency/events.go @@ -19,20 +19,9 @@ import ( "github.com/aws/karpenter-core/pkg/apis/v1beta1" "github.com/aws/karpenter-core/pkg/events" - machineutil "github.com/aws/karpenter-core/pkg/utils/machine" ) func FailedConsistencyCheckEvent(nodeClaim *v1beta1.NodeClaim, message string) events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return events.Event{ - InvolvedObject: machine, - Type: v1.EventTypeWarning, - Reason: "FailedConsistencyCheck", - Message: message, - DedupeValues: []string{string(machine.UID), message}, - } - } return events.Event{ InvolvedObject: nodeClaim, Type: v1.EventTypeWarning, diff --git a/pkg/controllers/nodeclaim/lifecycle/events.go b/pkg/controllers/nodeclaim/lifecycle/events.go index c69a8fbaed..e94e6fecb8 100644 --- a/pkg/controllers/nodeclaim/lifecycle/events.go +++ b/pkg/controllers/nodeclaim/lifecycle/events.go @@ -21,20 +21,9 @@ import ( "github.com/aws/karpenter-core/pkg/apis/v1beta1" "github.com/aws/karpenter-core/pkg/events" - machineutil "github.com/aws/karpenter-core/pkg/utils/machine" ) func InsufficientCapacityErrorEvent(nodeClaim *v1beta1.NodeClaim, err error) events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return events.Event{ - InvolvedObject: machine, - Type: v1.EventTypeWarning, - Reason: "InsufficientCapacityError", - Message: fmt.Sprintf("Machine %s event: %s", machine.Name, truncateMessage(err.Error())), - DedupeValues: []string{string(machine.UID)}, - } - } return events.Event{ InvolvedObject: nodeClaim, Type: v1.EventTypeWarning, @@ -45,16 +34,6 @@ func InsufficientCapacityErrorEvent(nodeClaim *v1beta1.NodeClaim, err error) eve } func NodeClassNotReadyEvent(nodeClaim *v1beta1.NodeClaim, err error) events.Event { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return events.Event{ - InvolvedObject: machine, - Type: v1.EventTypeWarning, - Reason: "ProviderNotReady", - Message: fmt.Sprintf("Machine %s event: %s", machine.Name, truncateMessage(err.Error())), - DedupeValues: []string{string(machine.UID)}, - } - } return events.Event{ InvolvedObject: nodeClaim, Type: v1.EventTypeWarning, diff --git a/pkg/test/expectations/expectations.go b/pkg/test/expectations/expectations.go index e529b1ff64..c65eb40025 100644 --- a/pkg/test/expectations/expectations.go +++ b/pkg/test/expectations/expectations.go @@ -58,8 +58,6 @@ import ( "github.com/aws/karpenter-core/pkg/operator/scheme" pscheduling "github.com/aws/karpenter-core/pkg/scheduling" "github.com/aws/karpenter-core/pkg/test" - machineutil "github.com/aws/karpenter-core/pkg/utils/machine" - nodeclaimutil "github.com/aws/karpenter-core/pkg/utils/nodeclaim" ) const ( @@ -277,28 +275,14 @@ func ExpectProvisionedNoBinding(ctx context.Context, c client.Client, cluster *s if err != nil { return bindings } - if key.IsMachine { - machine := &v1alpha5.Machine{} - Expect(c.Get(ctx, types.NamespacedName{Name: key.Name}, machine)).To(Succeed()) - machine, node := ExpectMachineDeployed(ctx, c, cluster, cloudProvider, machine) - if machine != nil && node != nil { - for _, pod := range m.Pods { - bindings[pod] = &Binding{ - Machine: machine, - Node: node, - } - } - } - } else { - nodeClaim := &v1beta1.NodeClaim{} - Expect(c.Get(ctx, types.NamespacedName{Name: key.Name}, nodeClaim)).To(Succeed()) - nodeClaim, node := ExpectNodeClaimDeployed(ctx, c, cluster, cloudProvider, nodeClaim) - if nodeClaim != nil && node != nil { - for _, pod := range m.Pods { - bindings[pod] = &Binding{ - NodeClaim: nodeClaim, - Node: node, - } + nodeClaim := &v1beta1.NodeClaim{} + Expect(c.Get(ctx, types.NamespacedName{Name: key.Name}, nodeClaim)).To(Succeed()) + nodeClaim, node := ExpectNodeClaimDeployed(ctx, c, cluster, cloudProvider, nodeClaim) + if nodeClaim != nil && node != nil { + for _, pod := range m.Pods { + bindings[pod] = &Binding{ + NodeClaim: nodeClaim, + Node: node, } } } @@ -309,51 +293,13 @@ func ExpectProvisionedNoBinding(ctx context.Context, c client.Client, cluster *s Node: node.Node, } if node.NodeClaim != nil { - if node.NodeClaim.IsMachine { - bindings[pod].Machine = machineutil.NewFromNodeClaim(node.NodeClaim) - } else { - bindings[pod].NodeClaim = node.NodeClaim - } + bindings[pod].NodeClaim = node.NodeClaim } } } return bindings } -func ExpectMachineDeployedNoNode(ctx context.Context, c client.Client, cluster *state.Cluster, cloudProvider cloudprovider.CloudProvider, m *v1alpha5.Machine) (*v1alpha5.Machine, error) { - GinkgoHelper() - resolved, err := cloudProvider.Create(ctx, nodeclaimutil.New(m)) - // TODO @joinnis: Check this error rather than swallowing it. This is swallowed right now due to how we are doing some testing in the cloudprovider - if err != nil { - return m, err - } - Expect(err).To(Succeed()) - - // Make the machine ready in the status conditions - m = machineutil.NewFromNodeClaim(lifecycle.PopulateNodeClaimDetails(nodeclaimutil.New(m), resolved)) - m.StatusConditions().MarkTrue(v1alpha5.MachineLaunched) - ExpectApplied(ctx, c, m) - cluster.UpdateNodeClaim(nodeclaimutil.New(m)) - return m, nil -} - -func ExpectMachineDeployed(ctx context.Context, c client.Client, cluster *state.Cluster, cloudProvider cloudprovider.CloudProvider, m *v1alpha5.Machine) (*v1alpha5.Machine, *v1.Node) { - GinkgoHelper() - m, err := ExpectMachineDeployedNoNode(ctx, c, cluster, cloudProvider, m) - if err != nil { - return m, nil - } - m.StatusConditions().MarkTrue(v1alpha5.MachineRegistered) - - // Mock the machine launch and node joining at the apiserver - node := test.MachineLinkedNode(m) - node.Labels = lo.Assign(node.Labels, map[string]string{v1alpha5.LabelNodeRegistered: "true"}) - ExpectApplied(ctx, c, m, node) - Expect(cluster.UpdateNode(ctx, node)).To(Succeed()) - cluster.UpdateNodeClaim(nodeclaimutil.New(m)) - return m, node -} - func ExpectNodeClaimDeployedNoNode(ctx context.Context, c client.Client, cluster *state.Cluster, cloudProvider cloudprovider.CloudProvider, nc *v1beta1.NodeClaim) (*v1beta1.NodeClaim, error) { GinkgoHelper() resolved, err := cloudProvider.Create(ctx, nc) @@ -388,24 +334,6 @@ func ExpectNodeClaimDeployed(ctx context.Context, c client.Client, cluster *stat return nc, node } -func ExpectMachinesCascadeDeletion(ctx context.Context, c client.Client, machines ...*v1alpha5.Machine) { - GinkgoHelper() - nodes := ExpectNodes(ctx, c) - for _, machine := range machines { - err := c.Get(ctx, client.ObjectKeyFromObject(machine), &v1alpha5.Machine{}) - if !errors.IsNotFound(err) { - continue - } - for _, node := range nodes { - if node.Spec.ProviderID == machine.Status.ProviderID { - Expect(c.Delete(ctx, node)) - ExpectFinalizersRemoved(ctx, c, node) - ExpectNotFound(ctx, c, node) - } - } - } -} - func ExpectNodeClaimsCascadeDeletion(ctx context.Context, c client.Client, nodeClaims ...*v1beta1.NodeClaim) { GinkgoHelper() nodes := ExpectNodes(ctx, c) @@ -435,17 +363,6 @@ func ExpectMakeNodeClaimsInitialized(ctx context.Context, c client.Client, nodeC } } -func ExpectMakeMachinesInitialized(ctx context.Context, c client.Client, machines ...*v1alpha5.Machine) { - GinkgoHelper() - for i := range machines { - machines[i] = ExpectExists(ctx, c, machines[i]) - machines[i].StatusConditions().MarkTrue(v1alpha5.MachineLaunched) - machines[i].StatusConditions().MarkTrue(v1alpha5.MachineRegistered) - machines[i].StatusConditions().MarkTrue(v1alpha5.MachineInitialized) - ExpectApplied(ctx, c, machines[i]) - } -} - func ExpectMakeNodesInitialized(ctx context.Context, c client.Client, nodes ...*v1.Node) { GinkgoHelper() ExpectMakeNodesReady(ctx, c, nodes...) @@ -618,21 +535,6 @@ func ExpectNodeClaims(ctx context.Context, c client.Client) []*v1beta1.NodeClaim return lo.ToSlicePtr(nodeClaims.Items) } -func ExpectMakeNodesAndMachinesInitializedAndStateUpdated(ctx context.Context, c client.Client, nodeStateController, machineStateController controller.Controller, nodes []*v1.Node, machines []*v1alpha5.Machine) { - GinkgoHelper() - - ExpectMakeNodesInitialized(ctx, c, nodes...) - ExpectMakeMachinesInitialized(ctx, c, machines...) - - // Inform cluster state about node and machine readiness - for _, n := range nodes { - ExpectReconcileSucceeded(ctx, nodeStateController, client.ObjectKeyFromObject(n)) - } - for _, m := range machines { - ExpectReconcileSucceeded(ctx, machineStateController, client.ObjectKeyFromObject(m)) - } -} - func ExpectMakeNodesAndNodeClaimsInitializedAndStateUpdated(ctx context.Context, c client.Client, nodeStateController, nodeClaimStateController controller.Controller, nodes []*v1.Node, nodeClaims []*v1beta1.NodeClaim) { GinkgoHelper() diff --git a/pkg/utils/machine/machine.go b/pkg/utils/machine/machine.go deleted file mode 100644 index f6e0bcd505..0000000000 --- a/pkg/utils/machine/machine.go +++ /dev/null @@ -1,301 +0,0 @@ -/* -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package machine - -import ( - "context" - "errors" - "fmt" - - "github.com/samber/lo" - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - "knative.dev/pkg/apis" - "knative.dev/pkg/ptr" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - - "github.com/aws/karpenter-core/pkg/apis/v1alpha5" - "github.com/aws/karpenter-core/pkg/apis/v1beta1" - "github.com/aws/karpenter-core/pkg/scheduling" -) - -// PodEventHandler is a watcher on v1.Pods that maps Pods to NodeClaim based on the node names -// and enqueues reconcile.Requests for the Machines -func PodEventHandler(c client.Client) handler.EventHandler { - return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) (requests []reconcile.Request) { - if name := o.(*v1.Pod).Spec.NodeName; name != "" { - node := &v1.Node{} - if err := c.Get(ctx, types.NamespacedName{Name: name}, node); err != nil { - return []reconcile.Request{} - } - machineList := &v1alpha5.MachineList{} - if err := c.List(ctx, machineList, client.MatchingFields{"status.providerID": node.Spec.ProviderID}); err != nil { - return []reconcile.Request{} - } - return lo.Map(machineList.Items, func(m v1alpha5.Machine, _ int) reconcile.Request { - return reconcile.Request{ - NamespacedName: client.ObjectKeyFromObject(&m), - } - }) - } - return requests - }) -} - -// NodeEventHandler is a watcher on v1.Node that maps Nodes to Machines based on provider ids -// and enqueues reconcile.Requests for the Machines -func NodeEventHandler(c client.Client) handler.EventHandler { - return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request { - node := o.(*v1.Node) - machineList := &v1alpha5.MachineList{} - if err := c.List(ctx, machineList, client.MatchingFields{"status.providerID": node.Spec.ProviderID}); err != nil { - return []reconcile.Request{} - } - return lo.Map(machineList.Items, func(m v1alpha5.Machine, _ int) reconcile.Request { - return reconcile.Request{ - NamespacedName: client.ObjectKeyFromObject(&m), - } - }) - }) -} - -// ProvisionerEventHandler is a watcher on v1alpha5.Machine that maps Provisioner to Machines based -// on the v1alpha5.ProvsionerNameLabelKey and enqueues reconcile.Requests for the NodeClaim -func ProvisionerEventHandler(c client.Client) handler.EventHandler { - return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) (requests []reconcile.Request) { - machineList := &v1alpha5.MachineList{} - if err := c.List(ctx, machineList, client.MatchingLabels(map[string]string{v1alpha5.ProvisionerNameLabelKey: o.GetName()})); err != nil { - return requests - } - return lo.Map(machineList.Items, func(machine v1alpha5.Machine, _ int) reconcile.Request { - return reconcile.Request{NamespacedName: types.NamespacedName{Name: machine.Name}} - }) - }) -} - -// NodeNotFoundError is an error returned when no v1.Nodes are found matching the passed providerID -type NodeNotFoundError struct { - ProviderID string -} - -func (e *NodeNotFoundError) Error() string { - return fmt.Sprintf("no nodes found for provider id '%s'", e.ProviderID) -} - -func IsNodeNotFoundError(err error) bool { - if err == nil { - return false - } - nnfErr := &NodeNotFoundError{} - return errors.As(err, &nnfErr) -} - -func IgnoreNodeNotFoundError(err error) error { - if !IsNodeNotFoundError(err) { - return err - } - return nil -} - -// DuplicateNodeError is an error returned when multiple v1.Nodes are found matching the passed providerID -type DuplicateNodeError struct { - ProviderID string -} - -func (e *DuplicateNodeError) Error() string { - return fmt.Sprintf("multiple found for provider id '%s'", e.ProviderID) -} - -func IsDuplicateNodeError(err error) bool { - if err == nil { - return false - } - dnErr := &DuplicateNodeError{} - return errors.As(err, &dnErr) -} - -func IgnoreDuplicateNodeError(err error) error { - if !IsDuplicateNodeError(err) { - return err - } - return nil -} - -// NodeForMachine is a helper function that takes a v1alpha5.Machine and attempts to find the matching v1.Node by its providerID -// This function will return errors if: -// 1. No v1.Nodes match the v1alpha5.Machine providerID -// 2. Multiple v1.Nodes match the v1alpha5.Machine providerID -func NodeForMachine(ctx context.Context, c client.Client, machine *v1alpha5.Machine) (*v1.Node, error) { - nodes, err := AllNodesForMachine(ctx, c, machine) - if err != nil { - return nil, err - } - if len(nodes) > 1 { - return nil, &DuplicateNodeError{ProviderID: machine.Status.ProviderID} - } - if len(nodes) == 0 { - return nil, &NodeNotFoundError{ProviderID: machine.Status.ProviderID} - } - return nodes[0], nil -} - -// AllNodesForMachine is a helper function that takes a v1alpha5.Machine and finds ALL matching v1.Nodes by their providerID -// If the providerID is not resolved for a Machine, then no Nodes will map to it -func AllNodesForMachine(ctx context.Context, c client.Client, machine *v1alpha5.Machine) ([]*v1.Node, error) { - // Machines that have no resolved providerID have no nodes mapped to them - if machine.Status.ProviderID == "" { - return nil, nil - } - nodeList := v1.NodeList{} - if err := c.List(ctx, &nodeList, client.MatchingFields{"spec.providerID": machine.Status.ProviderID}); err != nil { - return nil, fmt.Errorf("listing nodes, %w", err) - } - return lo.ToSlicePtr(nodeList.Items), nil -} - -// New converts a node into a Machine using known values from the node and provisioner spec values -// Deprecated: This Machine generator function can be removed when v1beta1 migration has completed. -func New(node *v1.Node, provisioner *v1alpha5.Provisioner) *v1alpha5.Machine { - machine := NewFromNode(node) - machine.Annotations = lo.Assign(provisioner.Spec.Annotations, v1alpha5.ProviderAnnotation(provisioner.Spec.Provider)) - machine.Labels = lo.Assign(provisioner.Spec.Labels, map[string]string{v1alpha5.ProvisionerNameLabelKey: provisioner.Name}) - machine.OwnerReferences = []metav1.OwnerReference{ - { - APIVersion: v1alpha5.SchemeGroupVersion.String(), - Kind: "Provisioner", - Name: provisioner.Name, - UID: provisioner.UID, - BlockOwnerDeletion: ptr.Bool(true), - }, - } - machine.Spec.Kubelet = provisioner.Spec.KubeletConfiguration - machine.Spec.Taints = provisioner.Spec.Taints - machine.Spec.StartupTaints = provisioner.Spec.StartupTaints - machine.Spec.Requirements = provisioner.Spec.Requirements - machine.Spec.MachineTemplateRef = provisioner.Spec.ProviderRef - return machine -} - -// NewFromNode converts a node into a pseudo-Machine using known values from the node -// Deprecated: This Machine generator function can be removed when v1beta1 migration has completed. -func NewFromNode(node *v1.Node) *v1alpha5.Machine { - m := &v1alpha5.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Name: node.Name, - Annotations: node.Annotations, - Labels: node.Labels, - Finalizers: []string{v1alpha5.TerminationFinalizer}, - }, - Spec: v1alpha5.MachineSpec{ - Taints: node.Spec.Taints, - Requirements: scheduling.NewLabelRequirements(node.Labels).NodeSelectorRequirements(), - Resources: v1alpha5.ResourceRequirements{ - Requests: node.Status.Allocatable, - }, - }, - Status: v1alpha5.MachineStatus{ - NodeName: node.Name, - ProviderID: node.Spec.ProviderID, - Capacity: node.Status.Capacity, - Allocatable: node.Status.Allocatable, - }, - } - if _, ok := node.Labels[v1alpha5.LabelNodeInitialized]; ok { - m.StatusConditions().MarkTrue(v1alpha5.MachineInitialized) - } - m.StatusConditions().MarkTrue(v1alpha5.MachineLaunched) - m.StatusConditions().MarkTrue(v1alpha5.MachineRegistered) - return m -} - -func NewFromNodeClaim(nodeClaim *v1beta1.NodeClaim) *v1alpha5.Machine { - return &v1alpha5.Machine{ - TypeMeta: nodeClaim.TypeMeta, - ObjectMeta: nodeClaim.ObjectMeta, - Spec: v1alpha5.MachineSpec{ - Taints: nodeClaim.Spec.Taints, - StartupTaints: nodeClaim.Spec.StartupTaints, - Requirements: nodeClaim.Spec.Requirements, - Resources: v1alpha5.ResourceRequirements{ - Requests: nodeClaim.Spec.Resources.Requests, - }, - Kubelet: NewKubeletConfiguration(nodeClaim.Spec.Kubelet), - MachineTemplateRef: NewMachineTemplateRef(nodeClaim.Spec.NodeClassRef), - }, - Status: v1alpha5.MachineStatus{ - NodeName: nodeClaim.Status.NodeName, - ProviderID: nodeClaim.Status.ProviderID, - Capacity: nodeClaim.Status.Capacity, - Allocatable: nodeClaim.Status.Allocatable, - Conditions: NewConditions(nodeClaim.Status.Conditions), - }, - } -} - -func NewConditions(conds apis.Conditions) apis.Conditions { - out := conds.DeepCopy() - for i := range out { - switch out[i].Type { - case v1beta1.Launched: - out[i].Type = v1alpha5.MachineLaunched - case v1beta1.Registered: - out[i].Type = v1alpha5.MachineRegistered - case v1beta1.Initialized: - out[i].Type = v1alpha5.MachineInitialized - case v1beta1.Empty: - out[i].Type = v1alpha5.MachineEmpty - case v1beta1.Expired: - out[i].Type = v1alpha5.MachineExpired - case v1beta1.Drifted: - out[i].Type = v1alpha5.MachineDrifted - } - } - return out -} - -func NewKubeletConfiguration(kc *v1beta1.KubeletConfiguration) *v1alpha5.KubeletConfiguration { - if kc == nil { - return nil - } - return &v1alpha5.KubeletConfiguration{ - ClusterDNS: kc.ClusterDNS, - ContainerRuntime: kc.ContainerRuntime, - MaxPods: kc.MaxPods, - PodsPerCore: kc.PodsPerCore, - SystemReserved: kc.SystemReserved, - KubeReserved: kc.KubeReserved, - EvictionHard: kc.EvictionHard, - EvictionSoft: kc.EvictionSoft, - EvictionSoftGracePeriod: kc.EvictionSoftGracePeriod, - EvictionMaxPodGracePeriod: kc.EvictionMaxPodGracePeriod, - ImageGCHighThresholdPercent: kc.ImageGCHighThresholdPercent, - ImageGCLowThresholdPercent: kc.ImageGCLowThresholdPercent, - CPUCFSQuota: kc.CPUCFSQuota, - } -} - -func NewMachineTemplateRef(ncr *v1beta1.NodeClassReference) *v1alpha5.MachineTemplateRef { - if ncr == nil { - return nil - } - return &v1alpha5.MachineTemplateRef{ - Kind: ncr.Kind, - Name: ncr.Name, - APIVersion: ncr.APIVersion, - } -} diff --git a/pkg/utils/machine/suite_test.go b/pkg/utils/machine/suite_test.go deleted file mode 100644 index 3401cea0ad..0000000000 --- a/pkg/utils/machine/suite_test.go +++ /dev/null @@ -1,425 +0,0 @@ -/* -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package machine_test - -import ( - "context" - "encoding/json" - "testing" - "time" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/samber/lo" - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "knative.dev/pkg/apis" - . "knative.dev/pkg/logging/testing" - "knative.dev/pkg/ptr" - - "github.com/aws/karpenter-core/pkg/apis/v1alpha5" - "github.com/aws/karpenter-core/pkg/apis/v1beta1" - "github.com/aws/karpenter-core/pkg/scheduling" - "github.com/aws/karpenter-core/pkg/test" - machineutil "github.com/aws/karpenter-core/pkg/utils/machine" -) - -var ctx context.Context - -func TestAPIs(t *testing.T) { - ctx = TestContextWithLogger(t) - RegisterFailHandler(Fail) - RunSpecs(t, "MachineUtils") -} - -var _ = Describe("MachineUtils", func() { - var provisioner *v1alpha5.Provisioner - var node *v1.Node - var nodeClaim *v1beta1.NodeClaim - BeforeEach(func() { - provisioner = test.Provisioner(test.ProvisionerOptions{ - Limits: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("10"), - v1.ResourceMemory: resource.MustParse("10Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1000Gi"), - }, - ProviderRef: &v1alpha5.MachineTemplateRef{ - Kind: "MachineTemplate", - APIVersion: "test.cloudprovider/v1", - Name: "default", - }, - Kubelet: &v1alpha5.KubeletConfiguration{ - ContainerRuntime: ptr.String("containerd"), - MaxPods: ptr.Int32(110), - PodsPerCore: ptr.Int32(10), - SystemReserved: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("200m"), - v1.ResourceMemory: resource.MustParse("200Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1Gi"), - }, - KubeReserved: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("200m"), - v1.ResourceMemory: resource.MustParse("200Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1Gi"), - }, - EvictionHard: map[string]string{ - "memory.available": "5%", - "nodefs.available": "5%", - "nodefs.inodesFree": "5%", - "imagefs.available": "5%", - "imagefs.inodesFree": "5%", - "pid.available": "3%", - }, - EvictionSoft: map[string]string{ - "memory.available": "10%", - "nodefs.available": "10%", - "nodefs.inodesFree": "10%", - "imagefs.available": "10%", - "imagefs.inodesFree": "10%", - "pid.available": "6%", - }, - EvictionSoftGracePeriod: map[string]metav1.Duration{ - "memory.available": {Duration: time.Minute * 2}, - "nodefs.available": {Duration: time.Minute * 2}, - "nodefs.inodesFree": {Duration: time.Minute * 2}, - "imagefs.available": {Duration: time.Minute * 2}, - "imagefs.inodesFree": {Duration: time.Minute * 2}, - "pid.available": {Duration: time.Minute * 2}, - }, - EvictionMaxPodGracePeriod: ptr.Int32(120), - ImageGCHighThresholdPercent: ptr.Int32(50), - ImageGCLowThresholdPercent: ptr.Int32(10), - CPUCFSQuota: ptr.Bool(false), - }, - Annotations: map[string]string{ - "test-annotation-key": "test-annotation-value", - "test-annotation-key2": "test-annotation-value2", - }, - Labels: map[string]string{ - "test-label-key": "test-label-value", - "test-label-key2": "test-label-value2", - }, - Taints: []v1.Taint{ - { - Key: "test-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-taint-value", - }, - { - Key: "test-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-taint-value2", - }, - }, - StartupTaints: []v1.Taint{ - { - Key: "test-startup-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-startup-taint-value", - }, - { - Key: "test-startup-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-startup-taint-value2", - }, - }, - Requirements: []v1.NodeSelectorRequirement{ - { - Key: v1.LabelTopologyZone, - Operator: v1.NodeSelectorOpIn, - Values: []string{"test-zone-1", "test-zone-2"}, - }, - { - Key: v1alpha5.LabelCapacityType, - Operator: v1.NodeSelectorOpIn, - Values: []string{v1alpha5.CapacityTypeOnDemand}, - }, - { - Key: v1.LabelHostname, - Operator: v1.NodeSelectorOpExists, - }, - }, - TTLSecondsUntilExpired: lo.ToPtr[int64](2592000), - TTLSecondsAfterEmpty: lo.ToPtr[int64](30), - Consolidation: &v1alpha5.Consolidation{ - Enabled: lo.ToPtr(true), - }, - Weight: lo.ToPtr[int32](100), - Status: v1alpha5.ProvisionerStatus{ - Resources: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("5"), - v1.ResourceMemory: resource.MustParse("5Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("500Gi"), - }, - LastScaleTime: &apis.VolatileTime{Inner: metav1.Time{Time: time.Now()}}, - }, - }) - provisioner.Spec.Provider = nil - node = test.Node(test.NodeOptions{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - v1.LabelTopologyZone: "test-zone-1", - v1.LabelTopologyRegion: "test-region", - "test-label-key": "test-label-value", - "test-label-key2": "test-label-value2", - v1alpha5.LabelNodeRegistered: "true", - v1alpha5.LabelNodeInitialized: "true", - v1alpha5.ProvisionerNameLabelKey: provisioner.Name, - v1alpha5.LabelCapacityType: v1alpha5.CapacityTypeOnDemand, - v1.LabelOSStable: "linux", - v1.LabelInstanceTypeStable: "test-instance-type", - }, - Annotations: map[string]string{ - "test-annotation-key": "test-annotation-value", - "test-annotation-key2": "test-annotation-value2", - "node-custom-annotation-key": "node-custom-annotation-value", - }, - }, - ReadyStatus: v1.ConditionTrue, - Taints: []v1.Taint{ - { - Key: "test-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-taint-value", - }, - { - Key: "test-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-taint-value2", - }, - }, - ProviderID: test.RandomProviderID(), - Capacity: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("10"), - v1.ResourceMemory: resource.MustParse("10Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("100Gi"), - }, - Allocatable: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("8"), - v1.ResourceMemory: resource.MustParse("8Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("95Gi"), - }, - }) - nodeClaim = test.NodeClaim(v1beta1.NodeClaim{ - Spec: v1beta1.NodeClaimSpec{ - Taints: []v1.Taint{ - { - Key: "test-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-taint-value", - }, - { - Key: "test-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-taint-value2", - }, - }, - StartupTaints: []v1.Taint{ - { - Key: "test-startup-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-startup-taint-value", - }, - { - Key: "test-startup-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-startup-taint-value2", - }, - }, - Requirements: []v1.NodeSelectorRequirement{ - { - Key: v1.LabelTopologyZone, - Operator: v1.NodeSelectorOpIn, - Values: []string{"test-zone-1", "test-zone-2"}, - }, - { - Key: v1alpha5.LabelCapacityType, - Operator: v1.NodeSelectorOpIn, - Values: []string{v1alpha5.CapacityTypeOnDemand}, - }, - { - Key: v1.LabelHostname, - Operator: v1.NodeSelectorOpExists, - }, - }, - Resources: v1beta1.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("5"), - v1.ResourceMemory: resource.MustParse("5Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("100Gi"), - }, - }, - Kubelet: &v1beta1.KubeletConfiguration{ - ContainerRuntime: ptr.String("containerd"), - MaxPods: ptr.Int32(110), - PodsPerCore: ptr.Int32(10), - SystemReserved: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("200m"), - v1.ResourceMemory: resource.MustParse("200Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1Gi"), - }, - KubeReserved: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("200m"), - v1.ResourceMemory: resource.MustParse("200Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1Gi"), - }, - EvictionHard: map[string]string{ - "memory.available": "5%", - "nodefs.available": "5%", - "nodefs.inodesFree": "5%", - "imagefs.available": "5%", - "imagefs.inodesFree": "5%", - "pid.available": "3%", - }, - EvictionSoft: map[string]string{ - "memory.available": "10%", - "nodefs.available": "10%", - "nodefs.inodesFree": "10%", - "imagefs.available": "10%", - "imagefs.inodesFree": "10%", - "pid.available": "6%", - }, - EvictionSoftGracePeriod: map[string]metav1.Duration{ - "memory.available": {Duration: time.Minute * 2}, - "nodefs.available": {Duration: time.Minute * 2}, - "nodefs.inodesFree": {Duration: time.Minute * 2}, - "imagefs.available": {Duration: time.Minute * 2}, - "imagefs.inodesFree": {Duration: time.Minute * 2}, - "pid.available": {Duration: time.Minute * 2}, - }, - EvictionMaxPodGracePeriod: ptr.Int32(120), - ImageGCHighThresholdPercent: ptr.Int32(50), - ImageGCLowThresholdPercent: ptr.Int32(10), - CPUCFSQuota: ptr.Bool(false), - }, - NodeClassRef: &v1beta1.NodeClassReference{ - Kind: "NodeClassRef", - APIVersion: "test.cloudprovider/v1", - Name: "default", - }, - }, - Status: v1beta1.NodeClaimStatus{ - NodeName: node.Name, - ProviderID: node.Spec.ProviderID, - Capacity: node.Status.Capacity, - Allocatable: node.Status.Allocatable, - }, - }) - nodeClaim.StatusConditions().MarkTrue(v1beta1.Launched) - nodeClaim.StatusConditions().MarkTrue(v1beta1.Registered) - nodeClaim.StatusConditions().MarkTrue(v1beta1.Initialized) - }) - It("should convert a Node and a Provisioner to a Machine", func() { - machine := machineutil.New(node, provisioner) - for k, v := range provisioner.Spec.Annotations { - Expect(machine.Annotations).To(HaveKeyWithValue(k, v)) - } - for k, v := range provisioner.Spec.Labels { - Expect(machine.Labels).To(HaveKeyWithValue(k, v)) - } - Expect(machine.OwnerReferences).To(ContainElement(metav1.OwnerReference{ - APIVersion: v1alpha5.SchemeGroupVersion.String(), - Kind: "Provisioner", - Name: provisioner.Name, - UID: provisioner.UID, - BlockOwnerDeletion: ptr.Bool(true), - })) - Expect(lo.Contains(machine.Finalizers, v1alpha5.TerminationFinalizer)).To(BeTrue()) - Expect(machine.Spec.Kubelet).To(Equal(provisioner.Spec.KubeletConfiguration)) - Expect(machine.Spec.Taints).To(Equal(provisioner.Spec.Taints)) - Expect(machine.Spec.StartupTaints).To(Equal(provisioner.Spec.StartupTaints)) - Expect(machine.Spec.Requirements).To(Equal(provisioner.Spec.Requirements)) - Expect(machine.Spec.MachineTemplateRef).To(Equal(provisioner.Spec.ProviderRef)) - Expect(machine.Status.NodeName).To(Equal(node.Name)) - Expect(machine.Status.ProviderID).To(Equal(node.Spec.ProviderID)) - Expect(machine.Status.Capacity).To(Equal(node.Status.Capacity)) - Expect(machine.Status.Allocatable).To(Equal(node.Status.Allocatable)) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineLaunched).IsTrue()).To(BeTrue()) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineRegistered).IsTrue()).To(BeTrue()) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineInitialized).IsTrue()).To(BeTrue()) - }) - It("should convert a Node and a Provisioner to a Machine (with Provider)", func() { - provisioner.Spec.Provider = &runtime.RawExtension{Raw: lo.Must(json.Marshal(map[string]string{ - "test-key": "test-value", - "test-key2": "test-value2", - }))} - provisioner.Spec.ProviderRef = nil - - machine := machineutil.New(node, provisioner) - Expect(machine.Annotations).To(HaveKeyWithValue(v1alpha5.ProviderCompatabilityAnnotationKey, string(lo.Must(json.Marshal(provisioner.Spec.Provider))))) - }) - It("should convert a Node to a Machine", func() { - machine := machineutil.NewFromNode(node) - for k, v := range node.Annotations { - Expect(machine.Annotations).To(HaveKeyWithValue(k, v)) - } - for k, v := range node.Labels { - Expect(machine.Labels).To(HaveKeyWithValue(k, v)) - } - Expect(lo.Contains(machine.Finalizers, v1alpha5.TerminationFinalizer)).To(BeTrue()) - Expect(machine.Spec.Taints).To(Equal(node.Spec.Taints)) - Expect(machine.Spec.Requirements).To(ContainElements(scheduling.NewLabelRequirements(node.Labels).NodeSelectorRequirements())) - Expect(machine.Spec.Resources.Requests).To(Equal(node.Status.Allocatable)) - Expect(machine.Status.NodeName).To(Equal(node.Name)) - Expect(machine.Status.ProviderID).To(Equal(node.Spec.ProviderID)) - Expect(machine.Status.Capacity).To(Equal(node.Status.Capacity)) - Expect(machine.Status.Allocatable).To(Equal(node.Status.Allocatable)) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineLaunched).IsTrue()).To(BeTrue()) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineRegistered).IsTrue()).To(BeTrue()) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineInitialized).IsTrue()).To(BeTrue()) - }) - It("should convert a NodeClaim to a Machine", func() { - machine := machineutil.NewFromNodeClaim(nodeClaim) - - for k, v := range nodeClaim.Annotations { - Expect(machine.Annotations).To(HaveKeyWithValue(k, v)) - } - for k, v := range nodeClaim.Labels { - Expect(machine.Labels).To(HaveKeyWithValue(k, v)) - } - Expect(machine.Spec.Taints).To(Equal(nodeClaim.Spec.Taints)) - Expect(machine.Spec.StartupTaints).To(Equal(nodeClaim.Spec.StartupTaints)) - Expect(machine.Spec.Requirements).To(ContainElements(nodeClaim.Spec.Requirements)) - Expect(machine.Spec.Resources.Requests).To(Equal(nodeClaim.Spec.Resources.Requests)) - - Expect(machine.Spec.Kubelet.ClusterDNS).To(Equal(nodeClaim.Spec.Kubelet.ClusterDNS)) - Expect(machine.Spec.Kubelet.ContainerRuntime).To(Equal(nodeClaim.Spec.Kubelet.ContainerRuntime)) - Expect(machine.Spec.Kubelet.MaxPods).To(Equal(nodeClaim.Spec.Kubelet.MaxPods)) - Expect(machine.Spec.Kubelet.PodsPerCore).To(Equal(nodeClaim.Spec.Kubelet.PodsPerCore)) - Expect(machine.Spec.Kubelet.SystemReserved).To(Equal(nodeClaim.Spec.Kubelet.SystemReserved)) - Expect(machine.Spec.Kubelet.KubeReserved).To(Equal(nodeClaim.Spec.Kubelet.KubeReserved)) - Expect(machine.Spec.Kubelet.EvictionHard).To(Equal(nodeClaim.Spec.Kubelet.EvictionHard)) - Expect(machine.Spec.Kubelet.EvictionSoft).To(Equal(nodeClaim.Spec.Kubelet.EvictionSoft)) - Expect(machine.Spec.Kubelet.EvictionSoftGracePeriod).To(Equal(nodeClaim.Spec.Kubelet.EvictionSoftGracePeriod)) - Expect(machine.Spec.Kubelet.EvictionMaxPodGracePeriod).To(Equal(nodeClaim.Spec.Kubelet.EvictionMaxPodGracePeriod)) - Expect(machine.Spec.Kubelet.ImageGCHighThresholdPercent).To(Equal(nodeClaim.Spec.Kubelet.ImageGCHighThresholdPercent)) - Expect(machine.Spec.Kubelet.ImageGCLowThresholdPercent).To(Equal(nodeClaim.Spec.Kubelet.ImageGCLowThresholdPercent)) - Expect(machine.Spec.Kubelet.CPUCFSQuota).To(Equal(nodeClaim.Spec.Kubelet.CPUCFSQuota)) - - Expect(machine.Spec.MachineTemplateRef.Kind).To(Equal(nodeClaim.Spec.NodeClassRef.Kind)) - Expect(machine.Spec.MachineTemplateRef.APIVersion).To(Equal(nodeClaim.Spec.NodeClassRef.APIVersion)) - Expect(machine.Spec.MachineTemplateRef.Name).To(Equal(nodeClaim.Spec.NodeClassRef.Name)) - - Expect(machine.Status.NodeName).To(Equal(nodeClaim.Status.NodeName)) - Expect(machine.Status.ProviderID).To(Equal(nodeClaim.Status.ProviderID)) - Expect(machine.Status.Capacity).To(Equal(nodeClaim.Status.Capacity)) - Expect(machine.Status.Allocatable).To(Equal(nodeClaim.Status.Allocatable)) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineLaunched).IsTrue()).To(BeTrue()) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineRegistered).IsTrue()).To(BeTrue()) - Expect(machine.StatusConditions().GetCondition(v1alpha5.MachineInitialized).IsTrue()).To(BeTrue()) - }) -}) diff --git a/pkg/utils/nodeclaim/nodeclaim.go b/pkg/utils/nodeclaim/nodeclaim.go index d1bee5f4d8..4210298d76 100644 --- a/pkg/utils/nodeclaim/nodeclaim.go +++ b/pkg/utils/nodeclaim/nodeclaim.go @@ -26,7 +26,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" - "knative.dev/pkg/apis" "knative.dev/pkg/ptr" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" @@ -36,7 +35,6 @@ import ( "github.com/aws/karpenter-core/pkg/apis/v1beta1" "github.com/aws/karpenter-core/pkg/metrics" "github.com/aws/karpenter-core/pkg/scheduling" - machineutil "github.com/aws/karpenter-core/pkg/utils/machine" nodepoolutil "github.com/aws/karpenter-core/pkg/utils/nodepool" ) @@ -181,52 +179,6 @@ func AllNodesForNodeClaim(ctx context.Context, c client.Client, nodeClaim *v1bet return lo.ToSlicePtr(nodeList.Items), nil } -func New(machine *v1alpha5.Machine) *v1beta1.NodeClaim { - return &v1beta1.NodeClaim{ - TypeMeta: machine.TypeMeta, - ObjectMeta: machine.ObjectMeta, - Spec: v1beta1.NodeClaimSpec{ - Taints: machine.Spec.Taints, - StartupTaints: machine.Spec.StartupTaints, - Requirements: machine.Spec.Requirements, - Resources: v1beta1.ResourceRequirements{ - Requests: machine.Spec.Resources.Requests, - }, - Kubelet: NewKubeletConfiguration(machine.Spec.Kubelet), - NodeClassRef: NewNodeClassReference(machine.Spec.MachineTemplateRef), - }, - Status: v1beta1.NodeClaimStatus{ - NodeName: machine.Status.NodeName, - ProviderID: machine.Status.ProviderID, - Capacity: machine.Status.Capacity, - Allocatable: machine.Status.Allocatable, - Conditions: NewConditions(machine.Status.Conditions), - }, - IsMachine: true, - } -} - -func NewConditions(conds apis.Conditions) apis.Conditions { - out := conds.DeepCopy() - for i := range out { - switch out[i].Type { - case v1alpha5.MachineLaunched: - out[i].Type = v1beta1.Launched - case v1alpha5.MachineRegistered: - out[i].Type = v1beta1.Registered - case v1alpha5.MachineInitialized: - out[i].Type = v1beta1.Initialized - case v1alpha5.MachineEmpty: - out[i].Type = v1beta1.Empty - case v1alpha5.MachineExpired: - out[i].Type = v1beta1.Expired - case v1alpha5.MachineDrifted: - out[i].Type = v1beta1.Drifted - } - } - return out -} - func NewKubeletConfiguration(kc *v1alpha5.KubeletConfiguration) *v1beta1.KubeletConfiguration { if kc == nil { return nil @@ -248,17 +200,6 @@ func NewKubeletConfiguration(kc *v1alpha5.KubeletConfiguration) *v1beta1.Kubelet } } -func NewNodeClassReference(mtr *v1alpha5.MachineTemplateRef) *v1beta1.NodeClassReference { - if mtr == nil { - return nil - } - return &v1beta1.NodeClassReference{ - Kind: mtr.Kind, - Name: mtr.Name, - APIVersion: mtr.APIVersion, - } -} - // NewFromNode converts a node into a pseudo-NodeClaim using known values from the node // Deprecated: This NodeClaim generator function can be removed when v1beta1 migration has completed. func NewFromNode(node *v1.Node) *v1beta1.NodeClaim { @@ -292,13 +233,6 @@ func NewFromNode(node *v1.Node) *v1beta1.NodeClaim { } func Get(ctx context.Context, c client.Client, key Key) (*v1beta1.NodeClaim, error) { - if key.IsMachine { - machine := &v1alpha5.Machine{} - if err := c.Get(ctx, types.NamespacedName{Name: key.Name}, machine); err != nil { - return nil, err - } - return New(machine), nil - } nodeClaim := &v1beta1.NodeClaim{} if err := c.Get(ctx, types.NamespacedName{Name: key.Name}, nodeClaim); err != nil { return nil, err @@ -307,62 +241,30 @@ func Get(ctx context.Context, c client.Client, key Key) (*v1beta1.NodeClaim, err } func List(ctx context.Context, c client.Client, opts ...client.ListOption) (*v1beta1.NodeClaimList, error) { - machineList := &v1alpha5.MachineList{} - if err := c.List(ctx, machineList, opts...); err != nil { - return nil, err - } - convertedNodeClaims := lo.Map(machineList.Items, func(m v1alpha5.Machine, _ int) v1beta1.NodeClaim { - return *New(&m) - }) nodeClaimList := &v1beta1.NodeClaimList{} if err := c.List(ctx, nodeClaimList, opts...); err != nil { return nil, err } - nodeClaimList.Items = append(nodeClaimList.Items, convertedNodeClaims...) return nodeClaimList, nil } func UpdateStatus(ctx context.Context, c client.Client, nodeClaim *v1beta1.NodeClaim) error { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return c.Status().Update(ctx, machine) - } return c.Status().Update(ctx, nodeClaim) } func Patch(ctx context.Context, c client.Client, stored, nodeClaim *v1beta1.NodeClaim) error { - if nodeClaim.IsMachine { - storedMachine := machineutil.NewFromNodeClaim(stored) - machine := machineutil.NewFromNodeClaim(nodeClaim) - return c.Patch(ctx, machine, client.MergeFrom(storedMachine)) - } return c.Patch(ctx, nodeClaim, client.MergeFrom(stored)) } func PatchStatus(ctx context.Context, c client.Client, stored, nodeClaim *v1beta1.NodeClaim) error { - if nodeClaim.IsMachine { - storedMachine := machineutil.NewFromNodeClaim(stored) - machine := machineutil.NewFromNodeClaim(nodeClaim) - return c.Status().Patch(ctx, machine, client.MergeFrom(storedMachine)) - } return c.Status().Patch(ctx, nodeClaim, client.MergeFrom(stored)) } func Delete(ctx context.Context, c client.Client, nodeClaim *v1beta1.NodeClaim) error { - if nodeClaim.IsMachine { - machine := machineutil.NewFromNodeClaim(nodeClaim) - return c.Delete(ctx, machine) - } return c.Delete(ctx, nodeClaim) } func CreatedCounter(nodeClaim *v1beta1.NodeClaim, reason string) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesCreatedCounter.With(prometheus.Labels{ - metrics.ReasonLabel: reason, - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsCreatedCounter.With(prometheus.Labels{ metrics.ReasonLabel: reason, metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], @@ -370,45 +272,24 @@ func CreatedCounter(nodeClaim *v1beta1.NodeClaim, reason string) prometheus.Coun } func LaunchedCounter(nodeClaim *v1beta1.NodeClaim) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesLaunchedCounter.With(prometheus.Labels{ - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsLaunchedCounter.With(prometheus.Labels{ metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], }) } func RegisteredCounter(nodeClaim *v1beta1.NodeClaim) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesRegisteredCounter.With(prometheus.Labels{ - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsRegisteredCounter.With(prometheus.Labels{ metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], }) } func InitializedCounter(nodeClaim *v1beta1.NodeClaim) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesInitializedCounter.With(prometheus.Labels{ - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsInitializedCounter.With(prometheus.Labels{ metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], }) } func TerminatedCounter(nodeClaim *v1beta1.NodeClaim, reason string) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesTerminatedCounter.With(prometheus.Labels{ - metrics.ReasonLabel: reason, - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsTerminatedCounter.With(prometheus.Labels{ metrics.ReasonLabel: reason, metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], @@ -416,12 +297,6 @@ func TerminatedCounter(nodeClaim *v1beta1.NodeClaim, reason string) prometheus.C } func DisruptedCounter(nodeClaim *v1beta1.NodeClaim, disruptionType string) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesDisruptedCounter.With(prometheus.Labels{ - metrics.TypeLabel: disruptionType, - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsDisruptedCounter.With(prometheus.Labels{ metrics.TypeLabel: disruptionType, metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], @@ -429,12 +304,6 @@ func DisruptedCounter(nodeClaim *v1beta1.NodeClaim, disruptionType string) prome } func DriftedCounter(nodeClaim *v1beta1.NodeClaim, driftType string) prometheus.Counter { - if nodeClaim.IsMachine { - return metrics.MachinesDriftedCounter.With(prometheus.Labels{ - metrics.TypeLabel: driftType, - metrics.ProvisionerLabel: nodeClaim.Labels[v1alpha5.ProvisionerNameLabelKey], - }) - } return metrics.NodeClaimsDisruptedCounter.With(prometheus.Labels{ metrics.TypeLabel: driftType, metrics.NodePoolLabel: nodeClaim.Labels[v1beta1.NodePoolLabelKey], @@ -446,23 +315,13 @@ func UpdateNodeOwnerReferences(nodeClaim *v1beta1.NodeClaim, node *v1.Node) *v1. node.OwnerReferences = lo.Reject(node.OwnerReferences, func(o metav1.OwnerReference, _ int) bool { return o.Kind == "Provisioner" }) - if nodeClaim.IsMachine { - node.OwnerReferences = append(node.OwnerReferences, metav1.OwnerReference{ - APIVersion: v1alpha5.SchemeGroupVersion.String(), - Kind: "Machine", - Name: nodeClaim.Name, - UID: nodeClaim.UID, - BlockOwnerDeletion: ptr.Bool(true), - }) - } else { - node.OwnerReferences = append(node.OwnerReferences, metav1.OwnerReference{ - APIVersion: v1beta1.SchemeGroupVersion.String(), - Kind: "NodeClaim", - Name: nodeClaim.Name, - UID: nodeClaim.UID, - BlockOwnerDeletion: ptr.Bool(true), - }) - } + node.OwnerReferences = append(node.OwnerReferences, metav1.OwnerReference{ + APIVersion: v1beta1.SchemeGroupVersion.String(), + Kind: "NodeClaim", + Name: nodeClaim.Name, + UID: nodeClaim.UID, + BlockOwnerDeletion: ptr.Bool(true), + }) return node } diff --git a/pkg/utils/nodeclaim/suite_test.go b/pkg/utils/nodeclaim/suite_test.go index e4311abb63..25ef70eb52 100644 --- a/pkg/utils/nodeclaim/suite_test.go +++ b/pkg/utils/nodeclaim/suite_test.go @@ -17,7 +17,6 @@ package nodeclaim_test import ( "context" "testing" - "time" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -27,7 +26,6 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" . "knative.dev/pkg/logging/testing" - "knative.dev/pkg/ptr" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" @@ -64,7 +62,6 @@ var _ = AfterEach(func() { }) var _ = Describe("NodeClaimUtils", func() { - var machine *v1alpha5.Machine var node *v1.Node BeforeEach(func() { node = test.Node(test.NodeOptions{ @@ -112,174 +109,6 @@ var _ = Describe("NodeClaimUtils", func() { v1.ResourceEphemeralStorage: resource.MustParse("95Gi"), }, }) - machine = test.Machine(v1alpha5.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - v1.LabelTopologyZone: "test-zone-1", - v1.LabelTopologyRegion: "test-region", - "test-label-key": "test-label-value", - "test-label-key2": "test-label-value2", - v1alpha5.LabelNodeRegistered: "true", - v1alpha5.LabelNodeInitialized: "true", - v1alpha5.ProvisionerNameLabelKey: "default", - v1alpha5.LabelCapacityType: v1alpha5.CapacityTypeOnDemand, - v1.LabelOSStable: "linux", - v1.LabelInstanceTypeStable: "test-instance-type", - }, - Annotations: map[string]string{ - "test-annotation-key": "test-annotation-value", - "test-annotation-key2": "test-annotation-value2", - "node-custom-annotation-key": "node-custom-annotation-value", - }, - }, - Spec: v1alpha5.MachineSpec{ - Taints: []v1.Taint{ - { - Key: "test-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-taint-value", - }, - { - Key: "test-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-taint-value2", - }, - }, - StartupTaints: []v1.Taint{ - { - Key: "test-startup-taint-key", - Effect: v1.TaintEffectNoSchedule, - Value: "test-startup-taint-value", - }, - { - Key: "test-startup-taint-key2", - Effect: v1.TaintEffectNoExecute, - Value: "test-startup-taint-value2", - }, - }, - Requirements: []v1.NodeSelectorRequirement{ - { - Key: v1.LabelTopologyZone, - Operator: v1.NodeSelectorOpIn, - Values: []string{"test-zone-1", "test-zone-2"}, - }, - { - Key: v1alpha5.LabelCapacityType, - Operator: v1.NodeSelectorOpIn, - Values: []string{v1alpha5.CapacityTypeOnDemand}, - }, - { - Key: v1.LabelHostname, - Operator: v1.NodeSelectorOpExists, - }, - }, - Resources: v1alpha5.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("5"), - v1.ResourceMemory: resource.MustParse("5Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("100Gi"), - }, - }, - Kubelet: &v1alpha5.KubeletConfiguration{ - ContainerRuntime: ptr.String("containerd"), - MaxPods: ptr.Int32(110), - PodsPerCore: ptr.Int32(10), - SystemReserved: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("200m"), - v1.ResourceMemory: resource.MustParse("200Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1Gi"), - }, - KubeReserved: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("200m"), - v1.ResourceMemory: resource.MustParse("200Mi"), - v1.ResourceEphemeralStorage: resource.MustParse("1Gi"), - }, - EvictionHard: map[string]string{ - "memory.available": "5%", - "nodefs.available": "5%", - "nodefs.inodesFree": "5%", - "imagefs.available": "5%", - "imagefs.inodesFree": "5%", - "pid.available": "3%", - }, - EvictionSoft: map[string]string{ - "memory.available": "10%", - "nodefs.available": "10%", - "nodefs.inodesFree": "10%", - "imagefs.available": "10%", - "imagefs.inodesFree": "10%", - "pid.available": "6%", - }, - EvictionSoftGracePeriod: map[string]metav1.Duration{ - "memory.available": {Duration: time.Minute * 2}, - "nodefs.available": {Duration: time.Minute * 2}, - "nodefs.inodesFree": {Duration: time.Minute * 2}, - "imagefs.available": {Duration: time.Minute * 2}, - "imagefs.inodesFree": {Duration: time.Minute * 2}, - "pid.available": {Duration: time.Minute * 2}, - }, - EvictionMaxPodGracePeriod: ptr.Int32(120), - ImageGCHighThresholdPercent: ptr.Int32(50), - ImageGCLowThresholdPercent: ptr.Int32(10), - CPUCFSQuota: ptr.Bool(false), - }, - MachineTemplateRef: &v1alpha5.MachineTemplateRef{ - Kind: "MachineTemplate", - APIVersion: "test.cloudprovider/v1", - Name: "default", - }, - }, - Status: v1alpha5.MachineStatus{ - NodeName: node.Name, - ProviderID: node.Spec.ProviderID, - Capacity: node.Status.Capacity, - Allocatable: node.Status.Allocatable, - }, - }) - machine.StatusConditions().MarkTrue(v1alpha5.MachineLaunched) - machine.StatusConditions().MarkTrue(v1alpha5.MachineRegistered) - machine.StatusConditions().MarkTrue(v1alpha5.MachineInitialized) - }) - It("should convert a Machine to a NodeClaim", func() { - nodeClaim := nodeclaimutil.New(machine) - - for k, v := range machine.Annotations { - Expect(nodeClaim.Annotations).To(HaveKeyWithValue(k, v)) - } - for k, v := range machine.Labels { - Expect(nodeClaim.Labels).To(HaveKeyWithValue(k, v)) - } - Expect(nodeClaim.Spec.Taints).To(Equal(machine.Spec.Taints)) - Expect(nodeClaim.Spec.StartupTaints).To(Equal(machine.Spec.StartupTaints)) - Expect(nodeClaim.Spec.Requirements).To(ContainElements(machine.Spec.Requirements)) - Expect(nodeClaim.Spec.Resources.Requests).To(Equal(machine.Spec.Resources.Requests)) - - Expect(nodeClaim.Spec.Kubelet.ClusterDNS).To(Equal(machine.Spec.Kubelet.ClusterDNS)) - Expect(nodeClaim.Spec.Kubelet.ContainerRuntime).To(Equal(machine.Spec.Kubelet.ContainerRuntime)) - Expect(nodeClaim.Spec.Kubelet.MaxPods).To(Equal(machine.Spec.Kubelet.MaxPods)) - Expect(nodeClaim.Spec.Kubelet.PodsPerCore).To(Equal(machine.Spec.Kubelet.PodsPerCore)) - Expect(nodeClaim.Spec.Kubelet.SystemReserved).To(Equal(machine.Spec.Kubelet.SystemReserved)) - Expect(nodeClaim.Spec.Kubelet.KubeReserved).To(Equal(machine.Spec.Kubelet.KubeReserved)) - Expect(nodeClaim.Spec.Kubelet.EvictionHard).To(Equal(machine.Spec.Kubelet.EvictionHard)) - Expect(nodeClaim.Spec.Kubelet.EvictionSoft).To(Equal(machine.Spec.Kubelet.EvictionSoft)) - Expect(nodeClaim.Spec.Kubelet.EvictionSoftGracePeriod).To(Equal(machine.Spec.Kubelet.EvictionSoftGracePeriod)) - Expect(nodeClaim.Spec.Kubelet.EvictionMaxPodGracePeriod).To(Equal(machine.Spec.Kubelet.EvictionMaxPodGracePeriod)) - Expect(nodeClaim.Spec.Kubelet.ImageGCHighThresholdPercent).To(Equal(machine.Spec.Kubelet.ImageGCHighThresholdPercent)) - Expect(nodeClaim.Spec.Kubelet.ImageGCLowThresholdPercent).To(Equal(machine.Spec.Kubelet.ImageGCLowThresholdPercent)) - Expect(nodeClaim.Spec.Kubelet.CPUCFSQuota).To(Equal(machine.Spec.Kubelet.CPUCFSQuota)) - - Expect(nodeClaim.Spec.NodeClassRef.Kind).To(Equal(machine.Spec.MachineTemplateRef.Kind)) - Expect(nodeClaim.Spec.NodeClassRef.APIVersion).To(Equal(machine.Spec.MachineTemplateRef.APIVersion)) - Expect(nodeClaim.Spec.NodeClassRef.Name).To(Equal(machine.Spec.MachineTemplateRef.Name)) - - Expect(nodeClaim.Status.NodeName).To(Equal(machine.Status.NodeName)) - Expect(nodeClaim.Status.ProviderID).To(Equal(machine.Status.ProviderID)) - Expect(nodeClaim.Status.Capacity).To(Equal(machine.Status.Capacity)) - Expect(nodeClaim.Status.Allocatable).To(Equal(machine.Status.Allocatable)) - - Expect(nodeClaim.StatusConditions().GetCondition(v1beta1.Launched).IsTrue()).To(BeTrue()) - Expect(nodeClaim.StatusConditions().GetCondition(v1beta1.Registered).IsTrue()).To(BeTrue()) - Expect(nodeClaim.StatusConditions().GetCondition(v1beta1.Initialized).IsTrue()).To(BeTrue()) }) It("should convert a Node to a NodeClaim", func() { nodeClaim := nodeclaimutil.NewFromNode(node) @@ -317,39 +146,6 @@ var _ = Describe("NodeClaimUtils", func() { Expect(err).ToNot(HaveOccurred()) Expect(retrieved.Name).To(Equal(nodeClaim.Name)) }) - It("should retrieve a Machine with a get call", func() { - machine := test.Machine() - ExpectApplied(ctx, env.Client, machine) - - retrieved, err := nodeclaimutil.Get(ctx, env.Client, nodeclaimutil.Key{Name: machine.Name, IsMachine: true}) - Expect(err).ToNot(HaveOccurred()) - Expect(retrieved.Name).To(Equal(machine.Name)) - }) - It("should retrieve both NodeClaims and Machines on a list call", func() { - Skip("Re-enable this test when NodeClaims are enabled and v1beta1 is released") - - numNodeClaims := 3 - numMachines := 5 - - for i := 0; i < numNodeClaims; i++ { - ExpectApplied(ctx, env.Client, test.NodeClaim(v1beta1.NodeClaim{ - Spec: v1beta1.NodeClaimSpec{ - NodeClassRef: &v1beta1.NodeClassReference{ - Kind: "NodeClassRef", - APIVersion: "test.cloudprovider/v1", - Name: "default", - }, - }, - })) - } - for i := 0; i < numMachines; i++ { - ExpectApplied(ctx, env.Client, test.Machine()) - } - - retrieved, err := nodeclaimutil.List(ctx, env.Client) - Expect(err).ToNot(HaveOccurred()) - Expect(retrieved.Items).To(HaveLen(8)) - }) It("should update the status on a NodeClaim", func() { nodeClaim := test.NodeClaim(v1beta1.NodeClaim{ Spec: v1beta1.NodeClaimSpec{ @@ -370,19 +166,6 @@ var _ = Describe("NodeClaimUtils", func() { Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(nodeClaim), retrieved)).To(Succeed()) Expect(retrieved.Status.ProviderID).To(Equal(providerID)) }) - It("should update the status on a Machine", func() { - machine := test.Machine() - ExpectApplied(ctx, env.Client, machine) - - nodeClaim := nodeclaimutil.New(machine) - providerID := test.RandomProviderID() - nodeClaim.Status.ProviderID = providerID - Expect(nodeclaimutil.UpdateStatus(ctx, env.Client, nodeClaim)).To(Succeed()) - - retrieved := &v1alpha5.Machine{} - Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(machine), retrieved)).To(Succeed()) - Expect(retrieved.Status.ProviderID).To(Equal(providerID)) - }) It("should patch a NodeClaim", func() { nodeClaim := test.NodeClaim(v1beta1.NodeClaim{ Spec: v1beta1.NodeClaimSpec{ @@ -405,21 +188,6 @@ var _ = Describe("NodeClaimUtils", func() { Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(nodeClaim), retrieved)).To(Succeed()) Expect(retrieved.Labels).To(HaveKeyWithValue("custom-key", "custom-value")) }) - It("should patch a Machine", func() { - machine := test.Machine() - ExpectApplied(ctx, env.Client, machine) - - nodeClaim := nodeclaimutil.New(machine) - stored := nodeClaim.DeepCopy() - nodeClaim.Labels = lo.Assign(nodeClaim.Labels, map[string]string{ - "custom-key": "custom-value", - }) - Expect(nodeclaimutil.Patch(ctx, env.Client, stored, nodeClaim)).To(Succeed()) - - retrieved := &v1alpha5.Machine{} - Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(machine), retrieved)).To(Succeed()) - Expect(retrieved.Labels).To(HaveKeyWithValue("custom-key", "custom-value")) - }) It("should patch the status on a NodeClaim", func() { nodeClaim := test.NodeClaim(v1beta1.NodeClaim{ Spec: v1beta1.NodeClaimSpec{ @@ -441,20 +209,6 @@ var _ = Describe("NodeClaimUtils", func() { Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(nodeClaim), retrieved)).To(Succeed()) Expect(retrieved.Status.ProviderID).To(Equal(providerID)) }) - It("should patch the status on a Machine", func() { - machine := test.Machine() - ExpectApplied(ctx, env.Client, machine) - - nodeClaim := nodeclaimutil.New(machine) - stored := nodeClaim.DeepCopy() - providerID := test.RandomProviderID() - nodeClaim.Status.ProviderID = providerID - Expect(nodeclaimutil.PatchStatus(ctx, env.Client, stored, nodeClaim)).To(Succeed()) - - retrieved := &v1alpha5.Machine{} - Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(machine), retrieved)).To(Succeed()) - Expect(retrieved.Status.ProviderID).To(Equal(providerID)) - }) It("should delete a NodeClaim with a delete call", func() { nodeClaim := test.NodeClaim(v1beta1.NodeClaim{ Spec: v1beta1.NodeClaimSpec{ @@ -475,20 +229,6 @@ var _ = Describe("NodeClaimUtils", func() { Expect(nodeClaimList.Items).To(HaveLen(0)) Expect(errors.IsNotFound(env.Client.Get(ctx, client.ObjectKeyFromObject(nodeClaim), &v1beta1.NodeClaim{}))).To(BeTrue()) }) - It("should delete a Machine with a delete call", func() { - machine := test.Machine() - ExpectApplied(ctx, env.Client, machine) - - nodeClaim := nodeclaimutil.New(machine) - - err := nodeclaimutil.Delete(ctx, env.Client, nodeClaim) - Expect(err).ToNot(HaveOccurred()) - - machineList := &v1alpha5.MachineList{} - Expect(env.Client.List(ctx, machineList)).To(Succeed()) - Expect(machineList.Items).To(HaveLen(0)) - Expect(errors.IsNotFound(env.Client.Get(ctx, client.ObjectKeyFromObject(machine), &v1alpha5.Machine{}))).To(BeTrue()) - }) It("should update the owner for a Node to a NodeClaim", func() { nodeClaim := test.NodeClaim(v1beta1.NodeClaim{ Spec: v1beta1.NodeClaimSpec{ @@ -510,20 +250,6 @@ var _ = Describe("NodeClaimUtils", func() { BlockOwnerDeletion: lo.ToPtr(true), })) }) - It("should update the owner for a Node to a Machine", func() { - machine := test.Machine() - node = test.Node(test.NodeOptions{ProviderID: machine.Status.ProviderID}) - nodeClaim := nodeclaimutil.New(machine) - node = nodeclaimutil.UpdateNodeOwnerReferences(nodeClaim, node) - - Expect(lo.Contains(node.OwnerReferences, metav1.OwnerReference{ - APIVersion: lo.Must(apiutil.GVKForObject(machine, scheme.Scheme)).GroupVersion().String(), - Kind: lo.Must(apiutil.GVKForObject(machine, scheme.Scheme)).String(), - Name: machine.Name, - UID: machine.UID, - BlockOwnerDeletion: lo.ToPtr(true), - })) - }) It("should retrieve the owner for a NodeClaim", func() { nodePool := test.NodePool(v1beta1.NodePool{ Spec: v1beta1.NodePoolSpec{ @@ -562,23 +288,4 @@ var _ = Describe("NodeClaimUtils", func() { Expect(err).ToNot(HaveOccurred()) Expect(owner.Name).To(Equal(nodePool.Name)) }) - It("should retrieve the owner for a Machine", func() { - provisioner := test.Provisioner() - machine := test.Machine(v1alpha5.Machine{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - v1alpha5.ProvisionerNameLabelKey: provisioner.Name, - }, - }, - }) - ExpectApplied(ctx, env.Client, provisioner, machine) - - ownerKey := nodeclaimutil.OwnerKey(machine) - Expect(ownerKey.Name).To(Equal(provisioner.Name)) - Expect(ownerKey.IsProvisioner).To(BeTrue()) - - owner, err := nodeclaimutil.Owner(ctx, env.Client, machine) - Expect(err).ToNot(HaveOccurred()) - Expect(owner.Name).To(Equal(provisioner.Name)) - }) })