Skip to content

Commit

Permalink
Merge pull request #48 from kerthcet/cleanup/allocation
Browse files Browse the repository at this point in the history
Remove item when values are empty in map
  • Loading branch information
elezar authored Jan 23, 2024
2 parents d1ddc1c + 53f9c25 commit deba5a7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/nvidia-dra-controller/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func (p *PerNodeAllocatedClaims) RemoveNode(claimUID, node string) {
}

delete(p.allocations[claimUID], node)
if len(p.allocations[claimUID]) == 0 {
delete(p.allocations, claimUID)
}
}

func (p *PerNodeAllocatedClaims) Remove(claimUID string) {
Expand Down
59 changes: 59 additions & 0 deletions cmd/nvidia-dra-controller/allocations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
*
* 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 main

import (
"testing"

"github.com/stretchr/testify/assert"

nascrd "github.com/NVIDIA/k8s-dra-driver/api/nvidia.com/resource/gpu/nas/v1alpha1"
)

func Test_PerNodeAllocatedClaims(t *testing.T) {
allocationClaims := &PerNodeAllocatedClaims{
allocations: make(map[string]map[string]nascrd.AllocatedDevices),
}

// Test Exists()
exists := allocationClaims.Exists("claim-not-exist", "fake-node")
assert.Equal(t, false, exists)

// Test Set()
device1 := nascrd.AllocatedDevices{ClaimInfo: &nascrd.ClaimInfo{Namespace: "default", Name: "device1"}}
device2 := nascrd.AllocatedDevices{ClaimInfo: &nascrd.ClaimInfo{Namespace: "default", Name: "device2"}}
allocationClaims.Set("fake-claim", "fake-node", device1)
allocationClaims.Set("fake-claim", "fake-node", device2)

// Test Get()
exists = allocationClaims.Exists("fake-claim", "fake-node")
assert.Equal(t, true, exists)
wantDevice := allocationClaims.Get("fake-claim", "fake-node")
assert.Equal(t, device2, wantDevice)

// Test Remove()
allocationClaims.Remove("fake-claim")
assert.Equal(t, allocationClaims.allocations, map[string]map[string]nascrd.AllocatedDevices{})

// Test RemoveNode()
allocationClaims.Set("fake-claim", "fake-node-1", device1)
allocationClaims.Set("fake-claim", "fake-node-2", device2)
allocationClaims.RemoveNode("fake-claim", "fake-node-1")
assert.Equal(t, allocationClaims.allocations, map[string]map[string]nascrd.AllocatedDevices{"fake-claim": {"fake-node-2": device2}})
allocationClaims.RemoveNode("fake-claim", "fake-node-2")
assert.Equal(t, allocationClaims.allocations, map[string]map[string]nascrd.AllocatedDevices{})
}

0 comments on commit deba5a7

Please sign in to comment.