diff --git a/cluster-autoscaler/simulator/dynamicresources/snapshot/snapshot.go b/cluster-autoscaler/simulator/dynamicresources/snapshot/snapshot.go index bf54c246798cda915191d5a55655527a56b3397a..35752b3d7fb3798a864d6f31a51c32994fa7ebc9 100644 --- a/cluster-autoscaler/simulator/dynamicresources/snapshot/snapshot.go +++ b/cluster-autoscaler/simulator/dynamicresources/snapshot/snapshot.go @@ -24,6 +24,7 @@ import ( "k8s.io/apimachinery/pkg/types" drautils "k8s.io/autoscaler/cluster-autoscaler/simulator/dynamicresources/utils" "k8s.io/autoscaler/cluster-autoscaler/simulator/framework" + resourceclaim "k8s.io/dynamic-resource-allocation/resourceclaim" schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework" ) @@ -194,7 +195,7 @@ func (s Snapshot) ReservePodClaims(pod *apiv1.Pod) error { return err } for _, claim := range claims { - if drautils.ClaimFullyReserved(claim) && !drautils.ClaimReservedForPod(claim, pod) { + if drautils.ClaimFullyReserved(claim) && !resourceclaim.IsReservedForPod(pod, claim) { return fmt.Errorf("claim %s/%s already has max number of reservations set, can't add more", claim.Namespace, claim.Name) } } diff --git a/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims.go b/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims.go index 94b1cfc086ca780a4365fc3e2264b9dd6c124ed9..89d5547e25192d7b88fdb67ac273424cba6987c0 100644 --- a/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims.go +++ b/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims.go @@ -24,6 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/component-helpers/scheduling/corev1" + resourceclaim "k8s.io/dynamic-resource-allocation/resourceclaim" "k8s.io/utils/ptr" ) @@ -48,16 +49,6 @@ func ClaimInUse(claim *resourceapi.ResourceClaim) bool { return len(claim.Status.ReservedFor) > 0 } -// ClaimReservedForPod returns whether the provided claim is currently reserved for the provided pod. -func ClaimReservedForPod(claim *resourceapi.ResourceClaim, pod *apiv1.Pod) bool { - for _, consumerRef := range claim.Status.ReservedFor { - if claimConsumerReferenceMatchesPod(pod, consumerRef) { - return true - } - } - return false -} - // ClaimFullyReserved returns whether the provided claim already has the maximum possible reservations // set, and no more can be added. func ClaimFullyReserved(claim *resourceapi.ResourceClaim) bool { @@ -105,7 +96,7 @@ func ClearPodReservationInPlace(claim *resourceapi.ResourceClaim, pod *apiv1.Pod // AddPodReservationInPlace adds a reservation for the provided pod to the provided Claim. It is a no-op // if the claim is already reserved for the Pod. func AddPodReservationInPlace(claim *resourceapi.ResourceClaim, pod *apiv1.Pod) { - if !ClaimReservedForPod(claim, pod) { + if !resourceclaim.IsReservedForPod(pod, claim) { claim.Status.ReservedFor = append(claim.Status.ReservedFor, PodClaimConsumerReference(pod)) } } diff --git a/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims_test.go b/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims_test.go index edd8dbc909d6511d06afa8bf91a7345a0775daea..6130d74d9ad000c316657e640b43aacbabf51616 100644 --- a/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims_test.go +++ b/cluster-autoscaler/simulator/dynamicresources/utils/resource_claims_test.go @@ -241,67 +241,6 @@ func TestClaimInUse(t *testing.T) { } } -func TestClaimReservedForPod(t *testing.T) { - pod := &apiv1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "chosenPod", UID: "chosenPodUid"}} - - for _, tc := range []struct { - testName string - claim *resourceapi.ResourceClaim - wantReserved bool - }{ - { - testName: "claim with no reservations", - claim: &resourceapi.ResourceClaim{ - ObjectMeta: metav1.ObjectMeta{Name: "claim", UID: "claimUid", Namespace: "default"}, - Status: resourceapi.ResourceClaimStatus{}, - }, - wantReserved: false, - }, - { - testName: "claim with some reservations, but none match the pod", - claim: &resourceapi.ResourceClaim{ - ObjectMeta: metav1.ObjectMeta{Name: "claim", UID: "claimUid", Namespace: "default"}, - Status: resourceapi.ResourceClaimStatus{ - ReservedFor: []resourceapi.ResourceClaimConsumerReference{ - {Resource: "pods", Name: "pod1", UID: "pod1Uid"}, - {Resource: "pods", Name: "pod2", UID: "pod2Uid"}, - {Resource: "somethingelses", Name: "somethingelse", UID: "somethingelseUid"}, - {Resource: "pods", Name: "chosenPod", UID: "badUid"}, - {Resource: "pods", Name: "badName", UID: "chosenPodUid"}, - {Resource: "badResource", Name: "chosenPod", UID: "chosenPodUid"}, - }, - }, - }, - wantReserved: false, - }, - { - testName: "claim with some reservations, one matches the pod", - claim: &resourceapi.ResourceClaim{ - ObjectMeta: metav1.ObjectMeta{Name: "claim", UID: "claimUid", Namespace: "default"}, - Status: resourceapi.ResourceClaimStatus{ - ReservedFor: []resourceapi.ResourceClaimConsumerReference{ - {Resource: "pods", Name: "pod1", UID: "pod1Uid"}, - {Resource: "pods", Name: "pod2", UID: "pod2Uid"}, - {Resource: "somethingelses", Name: "somethingelse", UID: "somethingelseUid"}, - {Resource: "pods", Name: "chosenPod", UID: "badUid"}, - {Resource: "pods", Name: "badName", UID: "chosenPodUid"}, - {Resource: "badResource", Name: "chosenPod", UID: "chosenPodUid"}, - {Resource: "pods", Name: "chosenPod", UID: "chosenPodUid"}, - }, - }, - }, - wantReserved: true, - }, - } { - t.Run(tc.testName, func(t *testing.T) { - reserved := ClaimReservedForPod(tc.claim, pod) - if tc.wantReserved != reserved { - t.Errorf("ClaimReservedForPod(): unexpected result: want %v, got %v", tc.wantReserved, reserved) - } - }) - } -} - func TestClaimFullyReserved(t *testing.T) { for _, tc := range []struct { testName string