Skip to content

Commit

Permalink
controller: fix resource update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CapacitorSet committed Jan 14, 2022
1 parent ad40c17 commit eca414e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ func subResources(currentResources, toSub corev1.ResourceList) {
// updateResources is a utility function to update resources.
func updateResources(currentResources, oldResources, newResources corev1.ResourceList) {
for resourceName, quantity := range newResources {
value := currentResources[resourceName]
if oldQuantity, exists := oldResources[resourceName]; exists {
value := currentResources[resourceName]
quantityToUpdate := resource.NewQuantity(quantity.Value()-oldQuantity.Value(),
difference := resource.NewQuantity(quantity.Value()-oldQuantity.Value(),
quantity.Format)
value.Add(*quantityToUpdate)
currentResources[resourceName] = value
value.Add(*difference)
} else {
currentResources[resourceName] = quantity
value.Add(quantity)
}
currentResources[resourceName] = value
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ var _ = Describe("ResourceRequest Operator", func() {
return true
}, timeout, interval).Should(BeTrue())

By("Checking update node resources")
By("Decreasing node resources")
toUpdate := node1.Status.Allocatable.DeepCopy()
for _, quantity := range toUpdate {
quantity.Sub(*resource.NewQuantity(1, quantity.Format))
Expand All @@ -388,6 +388,48 @@ var _ = Describe("ResourceRequest Operator", func() {
}
return true
}, timeout, interval).Should(BeTrue())

By("Creating new resources on node1")
toUpdate = node1.Status.Allocatable.DeepCopy()
toUpdate["liqo.io/fake-resource"] = *resource.NewQuantity(10, resource.DecimalSI)
node1.Status.Allocatable = toUpdate.DeepCopy()
node1, err = clientset.CoreV1().Nodes().UpdateStatus(ctx, node1, metav1.UpdateOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
resourcesRead := scaledMonitor.ReadResources(ClusterID1)
for resourceName, quantity := range resourcesRead {
toCheck := node2.Status.Allocatable[resourceName].DeepCopy()
toCheck.Add(node1.Status.Allocatable[resourceName])
toCheck.Sub(podReq[resourceName])
ScaleResources(resourceName, &toCheck, DefaultScaleFactor)
if quantity.Cmp(toCheck) != 0 {
return false
}
}
return true
}, timeout, interval).Should(BeTrue())

// Reproduces issue #1052.
By("Creating new resources on node2")
toUpdate = node2.Status.Allocatable.DeepCopy()
toUpdate["liqo.io/fake-resource"] = *resource.NewQuantity(10, resource.DecimalSI)
node2.Status.Allocatable = toUpdate.DeepCopy()
node2, err = clientset.CoreV1().Nodes().UpdateStatus(ctx, node2, metav1.UpdateOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
resourcesRead := scaledMonitor.ReadResources(ClusterID1)
for resourceName, quantity := range resourcesRead {
toCheck := node2.Status.Allocatable[resourceName].DeepCopy()
toCheck.Add(node1.Status.Allocatable[resourceName])
toCheck.Sub(podReq[resourceName])
ScaleResources(resourceName, &toCheck, DefaultScaleFactor)
if quantity.Cmp(toCheck) != 0 {
return false
}
}
return true
}, timeout, interval).Should(BeTrue())

By("Checking if ResourceOffer has been updated correctly")
Eventually(func() bool {
nodeList := []corev1.ResourceList{
Expand Down

0 comments on commit eca414e

Please sign in to comment.