mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: replace immutable hash with container spec comparison
This commit is contained in:
@@ -11,12 +11,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
LabelManaged = "uncloud.managed"
|
||||
LabelServiceID = "uncloud.service.id"
|
||||
LabelServiceName = "uncloud.service.name"
|
||||
LabelServiceMode = "uncloud.service.mode"
|
||||
LabelServicePorts = "uncloud.service.ports"
|
||||
LabelServiceSpecHash = "uncloud.service.spec-hash"
|
||||
LabelManaged = "uncloud.managed"
|
||||
LabelServiceID = "uncloud.service.id"
|
||||
LabelServiceName = "uncloud.service.name"
|
||||
LabelServiceMode = "uncloud.service.mode"
|
||||
LabelServicePorts = "uncloud.service.ports"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
|
||||
+33
-72
@@ -1,8 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
@@ -37,6 +35,8 @@ func ValidateServiceID(id string) bool {
|
||||
return serviceIDRegexp.MatchString(id)
|
||||
}
|
||||
|
||||
// ServiceSpec defines the desired state of a service.
|
||||
// ATTENTION: after changing this struct, verify if deploy.EvalContainerSpecChange needs to be updated.
|
||||
type ServiceSpec struct {
|
||||
Container ContainerSpec
|
||||
// Mode is the replication mode of the service. Default is ServiceModeReplicated if empty.
|
||||
@@ -48,18 +48,19 @@ type ServiceSpec struct {
|
||||
Replicas uint `json:",omitempty"`
|
||||
}
|
||||
|
||||
func (s *ServiceSpec) ApplyDefaults() {
|
||||
if s.Mode == "" {
|
||||
s.Mode = ServiceModeReplicated
|
||||
func (s *ServiceSpec) SetDefaults() ServiceSpec {
|
||||
spec := s.Clone()
|
||||
|
||||
if spec.Mode == "" {
|
||||
spec.Mode = ServiceModeReplicated
|
||||
}
|
||||
// Ensure the replicated service has at least one replica.
|
||||
if s.Mode == ServiceModeReplicated && s.Replicas == 0 {
|
||||
s.Replicas = 1
|
||||
if spec.Mode == ServiceModeReplicated && spec.Replicas == 0 {
|
||||
spec.Replicas = 1
|
||||
}
|
||||
spec.Container = spec.Container.SetDefaults()
|
||||
|
||||
if s.Container.PullPolicy == "" {
|
||||
s.Container.PullPolicy = PullPolicyMissing
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
func (s *ServiceSpec) Validate() error {
|
||||
@@ -87,68 +88,6 @@ func (s *ServiceSpec) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ImmutableHash returns a hash of the immutable parts of the ServiceSpec that require container recreation if changed.
|
||||
func (s *ServiceSpec) ImmutableHash() (string, error) {
|
||||
var err error
|
||||
// Serialise and sort the ports to ensure the hash is consistent.
|
||||
ports := make([]string, len(s.Ports))
|
||||
for i, p := range s.Ports {
|
||||
ports[i], err = p.String()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("encode service port spec: %w", err)
|
||||
}
|
||||
}
|
||||
slices.Sort(ports)
|
||||
|
||||
volumes := make([]string, 0, len(s.Container.Volumes))
|
||||
volumes = append(volumes, s.Container.Volumes...)
|
||||
slices.Sort(volumes)
|
||||
|
||||
hashSpec := immutableHashSpec{
|
||||
Command: s.Container.Command,
|
||||
Entrypoint: s.Container.Entrypoint,
|
||||
Image: s.Container.Image,
|
||||
Init: s.Container.Init,
|
||||
Ports: ports,
|
||||
Volumes: volumes,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(hashSpec)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("marshal immutable hash spec: %w", err)
|
||||
}
|
||||
|
||||
hasher := sha256.New()
|
||||
if _, err = hasher.Write(data); err != nil {
|
||||
return "", fmt.Errorf("write to SHA256 hasher: %w", err)
|
||||
}
|
||||
|
||||
return hex.EncodeToString(hasher.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// immutableHashSpec contains only the immutable fields from ServiceSpec that require container recreation if changed.
|
||||
type immutableHashSpec struct {
|
||||
Command []string `json:",omitempty"`
|
||||
Entrypoint []string `json:",omitempty"`
|
||||
Image string
|
||||
Init *bool `json:",omitempty"`
|
||||
// Ports are set as labels on the container which are immutable.
|
||||
// TODO: store ingress ports in the cluster store instead of as labels which will allow changing them without
|
||||
// recreating the container.
|
||||
Ports []string `json:",omitempty"`
|
||||
Volumes []string `json:",omitempty"`
|
||||
}
|
||||
|
||||
// Equals returns true if the service spec is equal to the given spec ignoring the number of replicas.
|
||||
func (s *ServiceSpec) Equals(spec ServiceSpec) bool {
|
||||
// TODO: ignore order of ports.
|
||||
sCopy := *s
|
||||
// Ignore the number of replicas when comparing.
|
||||
sCopy.Replicas = 0
|
||||
spec.Replicas = 0
|
||||
return reflect.DeepEqual(*s, spec)
|
||||
}
|
||||
|
||||
func (s *ServiceSpec) Clone() ServiceSpec {
|
||||
spec := *s
|
||||
|
||||
@@ -161,6 +100,8 @@ func (s *ServiceSpec) Clone() ServiceSpec {
|
||||
return spec
|
||||
}
|
||||
|
||||
// ContainerSpec defines the desired state of a container in a service.
|
||||
// ATTENTION: after changing this struct, verify if deploy.EvalContainerSpecChange needs to be updated.
|
||||
type ContainerSpec struct {
|
||||
// Command overrides the default CMD of the image to be executed when running a container.
|
||||
Command []string
|
||||
@@ -176,6 +117,16 @@ type ContainerSpec struct {
|
||||
Volumes []string
|
||||
}
|
||||
|
||||
// SetDefaults returns a copy of the container spec with default values set.
|
||||
func (s *ContainerSpec) SetDefaults() ContainerSpec {
|
||||
spec := s.Clone()
|
||||
if spec.PullPolicy == "" {
|
||||
spec.PullPolicy = PullPolicyMissing
|
||||
}
|
||||
|
||||
return spec
|
||||
}
|
||||
|
||||
func (s *ContainerSpec) Validate() error {
|
||||
if _, err := reference.ParseDockerRef(s.Image); err != nil {
|
||||
return fmt.Errorf("invalid image: %w", err)
|
||||
@@ -184,6 +135,16 @@ func (s *ContainerSpec) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ContainerSpec) Equals(spec ContainerSpec) bool {
|
||||
orig := s.SetDefaults()
|
||||
spec = spec.SetDefaults()
|
||||
|
||||
slices.Sort(orig.Volumes)
|
||||
slices.Sort(spec.Volumes)
|
||||
|
||||
return reflect.DeepEqual(orig, spec)
|
||||
}
|
||||
|
||||
func (s *ContainerSpec) Clone() ContainerSpec {
|
||||
spec := *s
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ func (cli *Client) CreateContainer(
|
||||
) (container.CreateResponse, error) {
|
||||
var resp container.CreateResponse
|
||||
|
||||
spec.ApplyDefaults()
|
||||
spec = spec.SetDefaults()
|
||||
if err := spec.Validate(); err != nil {
|
||||
return resp, fmt.Errorf("invalid service spec: %w", err)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package deploy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
)
|
||||
|
||||
@@ -12,30 +10,30 @@ const ContainerUpToDate ContainerSpecStatus = "up-to-date"
|
||||
const ContainerNeedsUpdate ContainerSpecStatus = "needs-update"
|
||||
const ContainerNeedsRecreate ContainerSpecStatus = "needs-recreate"
|
||||
|
||||
func CompareContainerToSpec(ctr api.ServiceContainer, spec api.ServiceSpec) (ContainerSpecStatus, error) {
|
||||
// TODO: replace the hash comparison with a more detailed comparison of ctr.ServiceSpec and spec.
|
||||
specHash, err := spec.ImmutableHash()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("calculate immutable hash for service spec: %w", err)
|
||||
func EvalContainerSpecChange(current api.ServiceSpec, new api.ServiceSpec) ContainerSpecStatus {
|
||||
current = current.SetDefaults()
|
||||
new = new.SetDefaults()
|
||||
|
||||
// Pull policy doesn't affect the container configuration.
|
||||
new.Container.PullPolicy = current.Container.PullPolicy
|
||||
if !current.Container.Equals(new.Container) {
|
||||
return ContainerNeedsRecreate
|
||||
}
|
||||
|
||||
// Is the hash label is unset, there is no easy way to compare its configuration with the spec,
|
||||
// so let's recreate as well.
|
||||
if ctr.Config.Labels[api.LabelServiceSpecHash] != specHash {
|
||||
return ContainerNeedsRecreate, nil
|
||||
if current.Mode != new.Mode {
|
||||
return ContainerNeedsRecreate
|
||||
}
|
||||
if current.Name != new.Name {
|
||||
return ContainerNeedsRecreate
|
||||
}
|
||||
|
||||
// TODO: compare mutable properties such as memory or CPU limits when they are implemented.
|
||||
|
||||
// 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)
|
||||
// TODO: change ports check to ContainerNeedsUpdate when ingress ports are stored only the machine DB instead
|
||||
// of as labels ans synced to the cluster store. Host ports changes should be handled as ContainerNeedsRecreate.
|
||||
if !api.PortsEqual(current.Ports, new.Ports) {
|
||||
return ContainerNeedsRecreate
|
||||
}
|
||||
|
||||
if !api.PortsEqual(ports, spec.Ports) {
|
||||
return ContainerNeedsRecreate, nil
|
||||
}
|
||||
|
||||
return ContainerUpToDate, nil
|
||||
return ContainerUpToDate
|
||||
}
|
||||
|
||||
@@ -100,10 +100,7 @@ func (s *RollingStrategy) planReplicated(
|
||||
continue
|
||||
}
|
||||
|
||||
status, err := CompareContainerToSpec(c.Container, spec)
|
||||
if err != nil {
|
||||
return plan, fmt.Errorf("compare container to spec: %w", err)
|
||||
}
|
||||
status := EvalContainerSpecChange(c.Container.ServiceSpec, spec)
|
||||
containerSpecStatuses[c.Container.ID] = status
|
||||
|
||||
if status == ContainerUpToDate {
|
||||
@@ -294,10 +291,7 @@ func reconcileGlobalContainer(
|
||||
continue
|
||||
}
|
||||
|
||||
status, err := CompareContainerToSpec(c.Container, spec)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("compare container to spec: %w", err)
|
||||
}
|
||||
status := EvalContainerSpecChange(c.Container.ServiceSpec, spec)
|
||||
if status == ContainerUpToDate {
|
||||
// The container is already running with the same spec.
|
||||
upToDate = true
|
||||
|
||||
Reference in New Issue
Block a user