feat: do not redeploy global service if spec hasn't changed

This commit is contained in:
Pavel Sviderski
2025-02-14 17:49:46 +10:00
parent 5e2b2ac836
commit a53fa2c1d8
6 changed files with 107 additions and 34 deletions
+18 -4
View File
@@ -58,10 +58,24 @@ func (c *Container) ServicePorts() ([]PortSpec, error) {
return ports, nil
}
func (c *Container) ServiceSpec() ServiceSpec {
// TODO: migrate api.Container type to use ContainerJSON to make it possible to construct
// a ServiceSpec from a Container.
return ServiceSpec{}
// ServiceSpec constructs a service spec from the container's configuration.
func (c *Container) ServiceSpec() (ServiceSpec, error) {
ports, err := c.ServicePorts()
if err != nil {
return ServiceSpec{}, fmt.Errorf("get service ports: %w", err)
}
return ServiceSpec{
Container: ContainerSpec{
Command: c.Config.Cmd,
Image: c.Config.Image,
Init: c.HostConfig.Init,
Volumes: c.HostConfig.Binds,
},
Mode: c.ServiceMode(),
Name: c.ServiceName(),
Ports: ports,
}, nil
}
// Healthy determines if the container is running and healthy.
+46
View File
@@ -6,9 +6,55 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/netip"
"reflect"
"testing"
)
func TestContainer_ServiceSpec(t *testing.T) {
t.Parallel()
init := true
ctr := &Container{ContainerJSON: types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
HostConfig: &container.HostConfig{
Binds: []string{"/host/path:/container/path"},
Init: &init,
},
},
Config: &container.Config{
Cmd: []string{"/app/server"},
Image: "app:latest",
Labels: map[string]string{
LabelServiceID: "test-service-id",
LabelServiceName: "test-service-name",
LabelServicePorts: "app.example.com:8000/https",
},
},
}}
expectedSpec := ServiceSpec{
Container: ContainerSpec{
Command: []string{"/app/server"},
Image: "app:latest",
Init: &init,
Volumes: []string{"/host/path:/container/path"},
},
Name: "test-service-name",
Ports: []PortSpec{
{
Hostname: "app.example.com",
ContainerPort: 8000,
Protocol: ProtocolHTTPS,
Mode: PortModeIngress,
},
},
}
spec, err := ctr.ServiceSpec()
require.NoError(t, err)
assert.True(t, reflect.DeepEqual(spec, expectedSpec))
}
func TestContainer_Healthy(t *testing.T) {
t.Parallel()
+1
View File
@@ -39,6 +39,7 @@ func (s *ServiceSpec) Validate() error {
}
func (s *ServiceSpec) Equals(spec ServiceSpec) bool {
// TODO: ignore order of ports.
return reflect.DeepEqual(*s, spec)
}
+2 -2
View File
@@ -47,8 +47,8 @@ func (cli *Client) CreateContainer(
api.LabelManaged: "",
},
}
if spec.Mode == api.ServiceModeGlobal {
config.Labels[api.LabelServiceMode] = api.ServiceModeGlobal
if spec.Mode != "" {
config.Labels[api.LabelServiceMode] = spec.Mode
}
if len(spec.Ports) > 0 {
+4 -1
View File
@@ -120,7 +120,10 @@ func reconcileGlobalContainer(
continue
}
svcSpec := c.Container.ServiceSpec()
svcSpec, err := c.Container.ServiceSpec()
if err != nil {
return nil, fmt.Errorf("get service spec: %w", err)
}
if svcSpec.Equals(spec) {
// The container is already running with the same spec.
upToDate = true