mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: compare container ports to spec in assertContainerMatchesSpec
This commit is contained in:
@@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -261,3 +262,31 @@ func validateHostname(hostname string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PortsEqual returns true if the two port sets are equal. The order of the ports is not important.
|
||||||
|
func PortsEqual(a, b []PortSpec) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
aSerialised := make([]string, len(a))
|
||||||
|
bSerialised := make([]string, len(b))
|
||||||
|
|
||||||
|
for i := range a {
|
||||||
|
aSerialised[i], err = a[i].String()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
bSerialised[i], err = b[i].String()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slices.Sort(aSerialised)
|
||||||
|
slices.Sort(bSerialised)
|
||||||
|
|
||||||
|
return slices.Equal(aSerialised, bSerialised)
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,8 +24,16 @@ func CompareContainerToSpec(ctr api.Container, spec api.ServiceSpec) (ContainerS
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: compare mutable properties such as memory or CPU limits when they are implemented.
|
// TODO: compare mutable properties such as memory or CPU limits when they are implemented.
|
||||||
|
|
||||||
// TODO: compare ports
|
// TODO: remove ports check when ports are stored in the local machine store instead of as labels.
|
||||||
|
ports, err := ctr.ServicePorts()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("get service ports: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !api.PortsEqual(ports, spec.Ports) {
|
||||||
|
return ContainerNeedsRecreate, nil
|
||||||
|
}
|
||||||
|
|
||||||
return ContainerUpToDate, nil
|
return ContainerUpToDate, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user