mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
refactor: cpu and memory under Resources struct
This commit is contained in:
@@ -29,12 +29,10 @@ func EvalContainerSpecChange(current api.ServiceSpec, new api.ServiceSpec) Conta
|
||||
// Pull policy doesn't affect the container configuration.
|
||||
new.Container.PullPolicy = current.Container.PullPolicy
|
||||
|
||||
// Save mutable container properties that can be updated without recreation.
|
||||
newCPU := new.Container.CPU
|
||||
newMemory := new.Container.Memory
|
||||
// Temporarily set mutable container properties to current values to check if other properties changed.
|
||||
new.Container.CPU = current.Container.CPU
|
||||
new.Container.Memory = current.Container.Memory
|
||||
// Save mutable container resources that can be updated without recreation.
|
||||
newResources := new.Container.Resources
|
||||
// Temporarily set mutable container resources to current values to check if other properties changed.
|
||||
new.Container.Resources = current.Container.Resources
|
||||
|
||||
// Check if immutable container properties changed.
|
||||
if !current.Container.Equals(new.Container) {
|
||||
@@ -69,7 +67,7 @@ func EvalContainerSpecChange(current api.ServiceSpec, new api.ServiceSpec) Conta
|
||||
}
|
||||
|
||||
// Check if any mutable properties changed.
|
||||
if !reflect.DeepEqual(current.Container.CPU, newCPU) || !reflect.DeepEqual(current.Container.Memory, newMemory) {
|
||||
if !reflect.DeepEqual(current.Container.Resources, newResources) {
|
||||
return ContainerNeedsUpdate
|
||||
}
|
||||
|
||||
|
||||
+134
-140
@@ -8,39 +8,145 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEvalContainerSpecChange_ContainerCPU(t *testing.T) {
|
||||
func TestEvalContainerSpecChange_ContainerResources(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
current api.CPUResources
|
||||
new api.CPUResources
|
||||
current api.ContainerResources
|
||||
new api.ContainerResources
|
||||
want ContainerSpecStatus
|
||||
}{
|
||||
{
|
||||
name: "set limit",
|
||||
current: api.CPUResources{},
|
||||
new: api.CPUResources{
|
||||
Limit: 1000000000,
|
||||
name: "empty",
|
||||
current: api.ContainerResources{},
|
||||
new: api.ContainerResources{},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
|
||||
// CPU
|
||||
{
|
||||
name: "set CPU",
|
||||
current: api.ContainerResources{},
|
||||
new: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change limit",
|
||||
current: api.CPUResources{
|
||||
Limit: 1000000000,
|
||||
name: "change CPU",
|
||||
current: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
},
|
||||
new: api.CPUResources{
|
||||
Limit: 2000000000,
|
||||
new: api.ContainerResources{
|
||||
CPU: 2000000000,
|
||||
},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset limit",
|
||||
current: api.CPUResources{
|
||||
Limit: 1000000000,
|
||||
name: "unset CPU",
|
||||
current: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
},
|
||||
new: api.ContainerResources{},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
|
||||
// Memory
|
||||
{
|
||||
name: "equal memory",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024},
|
||||
new: api.ContainerResources{Memory: 100 * 1024 * 1024},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "equal reservation",
|
||||
current: api.ContainerResources{MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{MemoryReservation: 50 * 1024 * 1024},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "equal memory and reservation",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "set memory",
|
||||
current: api.ContainerResources{},
|
||||
new: api.ContainerResources{Memory: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "set reservation",
|
||||
current: api.ContainerResources{},
|
||||
new: api.ContainerResources{MemoryReservation: 50 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "set memory and reservation",
|
||||
current: api.ContainerResources{},
|
||||
new: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change memory",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024},
|
||||
new: api.ContainerResources{Memory: 200 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change reservation",
|
||||
current: api.ContainerResources{MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{MemoryReservation: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change memory and reservation",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{Memory: 200 * 1024 * 1024, MemoryReservation: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset memory",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{MemoryReservation: 50 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset reservation",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{Memory: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset memory and reservation",
|
||||
current: api.ContainerResources{Memory: 100 * 1024 * 1024, MemoryReservation: 50 * 1024 * 1024},
|
||||
new: api.ContainerResources{},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
|
||||
// Memory and CPU
|
||||
{
|
||||
name: "set CPU and memory",
|
||||
current: api.ContainerResources{},
|
||||
new: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
Memory: 100 * 1024 * 1024,
|
||||
},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "update CPU and memory",
|
||||
current: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
Memory: 100 * 1024 * 1024,
|
||||
},
|
||||
new: api.ContainerResources{
|
||||
CPU: 2000000000,
|
||||
Memory: 200 * 1024 * 1024,
|
||||
MemoryReservation: 100 * 1024 * 1024,
|
||||
},
|
||||
new: api.CPUResources{},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
}
|
||||
@@ -49,14 +155,14 @@ func TestEvalContainerSpecChange_ContainerCPU(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
currentSpec := api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
CPU: tt.current,
|
||||
Image: "nginx:latest",
|
||||
Resources: tt.current,
|
||||
},
|
||||
}
|
||||
newSpec := api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
CPU: tt.new,
|
||||
Image: "nginx:latest",
|
||||
Resources: tt.new,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -199,116 +305,6 @@ func TestEvalContainerSpecChange_ContainerLogDriver(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvalContainerSpecChange_ContainerMemory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
current api.MemoryResources
|
||||
new api.MemoryResources
|
||||
want ContainerSpecStatus
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
current: api.MemoryResources{},
|
||||
new: api.MemoryResources{},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "equal limit",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024},
|
||||
new: api.MemoryResources{Limit: 100 * 1024 * 1024},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "equal reservation",
|
||||
current: api.MemoryResources{Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{Reservation: 50 * 1024 * 1024},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "equal limit and reservation",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
want: ContainerUpToDate,
|
||||
},
|
||||
{
|
||||
name: "set limit",
|
||||
current: api.MemoryResources{},
|
||||
new: api.MemoryResources{Limit: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "set reservation",
|
||||
current: api.MemoryResources{},
|
||||
new: api.MemoryResources{Reservation: 50 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "set limit and reservation",
|
||||
current: api.MemoryResources{},
|
||||
new: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change limit",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024},
|
||||
new: api.MemoryResources{Limit: 200 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change reservation",
|
||||
current: api.MemoryResources{Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{Reservation: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "change limit and reservation",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{Limit: 200 * 1024 * 1024, Reservation: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset limit",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{Reservation: 50 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset reservation",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{Limit: 100 * 1024 * 1024},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
{
|
||||
name: "unset limit and reservation",
|
||||
current: api.MemoryResources{Limit: 100 * 1024 * 1024, Reservation: 50 * 1024 * 1024},
|
||||
new: api.MemoryResources{},
|
||||
want: ContainerNeedsUpdate,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
currentSpec := api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
Memory: tt.current,
|
||||
},
|
||||
}
|
||||
newSpec := api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
Memory: tt.new,
|
||||
},
|
||||
}
|
||||
|
||||
result := EvalContainerSpecChange(currentSpec, newSpec)
|
||||
assert.Equal(t, tt.want, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvalContainerSpecChange_ContainerPrivileged(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -1321,20 +1317,18 @@ func TestEvalContainerSpecChange_Mixed(t *testing.T) {
|
||||
current: api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
Memory: api.MemoryResources{
|
||||
Limit: 100 * 1024 * 1024,
|
||||
Resources: api.ContainerResources{
|
||||
Memory: 100 * 1024 * 1024,
|
||||
},
|
||||
},
|
||||
},
|
||||
new: api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
CPU: api.CPUResources{
|
||||
Limit: 1000000000,
|
||||
},
|
||||
Memory: api.MemoryResources{
|
||||
Limit: 200 * 1024 * 1024,
|
||||
Reservation: 100 * 1024 * 1024,
|
||||
Resources: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
Memory: 200 * 1024 * 1024,
|
||||
MemoryReservation: 100 * 1024 * 1024,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1350,8 +1344,8 @@ func TestEvalContainerSpecChange_Mixed(t *testing.T) {
|
||||
new: api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Image: "nginx:latest",
|
||||
CPU: api.CPUResources{
|
||||
Limit: 1000000000,
|
||||
Resources: api.ContainerResources{
|
||||
CPU: 1000000000,
|
||||
},
|
||||
User: "root",
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user