Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: refresh image labels during self-upgrade (#3479)
Kyle Mendell7304f1df09c8
diff --git a/backend/cli/upgrade/upgrade.go b/backend/cli/upgrade/upgrade.goindex f6dd5a453..b77b23100 100644--- a/backend/cli/upgrade/upgrade.go+++ b/backend/cli/upgrade/upgrade.go@@ -209,6 +209,89 @@ func normalizeRecreatedArcaneLabelsInternal(containerLabels map[string]string) mreturn normalized}+func refreshRecreatedImageLabelsInternal(containerLabels, previousImageLabels, imageLabels map[string]string, imageID string) map[string]string {+ refreshed := maps.Clone(containerLabels)+ for key, value := range containerLabels {+ if !strings.HasPrefix(strings.ToLower(key), "org.opencontainers.image.") {+ continue+ }+ for previousKey, previousValue := range previousImageLabels {+ if strings.EqualFold(previousKey, key) && previousValue == value {+ delete(refreshed, key)+ break+ }+ }+ }++ for key, value := range imageLabels {+ if !strings.HasPrefix(strings.ToLower(key), "org.opencontainers.image.") {+ continue+ }+ preserved := false+ for existingKey := range refreshed {+ if strings.EqualFold(existingKey, key) {+ preserved = true+ break+ }+ }+ if preserved {+ continue+ }+ if refreshed == nil {+ refreshed = make(map[string]string)+ }+ refreshed[key] = value+ }++ if refreshed != nil && imageID != "" {+ for key := range refreshed {+ if strings.EqualFold(key, "com.docker.compose.image") {+ refreshed[key] = imageID+ }+ }+ }++ return refreshed+}++func refreshRecreatedContainerLabelsInternal(ctx context.Context, dockerClient *client.Client, containerLabels map[string]string, previousImage, targetImage string) map[string]string {+ imageInspect, imageInspectErr := dockerClient.ImageInspect(ctx, targetImage)+ if imageInspectErr != nil {+ slog.Warn("Could not inspect target image labels; preserving container OCI overrides",+ "image", targetImage,+ "error", imageInspectErr,+ )+ return maps.Clone(containerLabels)+ }++ imageLabels := map[string]string(nil)+ previousImageLabels := map[string]string(nil)+ if imageInspect.Config != nil {+ imageLabels = imageInspect.Config.Labels+ if hasOCIImageLabelInternal(containerLabels) && strings.TrimSpace(previousImage) != "" {+ previousImageInspect, previousImageInspectErr := dockerClient.ImageInspect(ctx, previousImage)+ if previousImageInspectErr != nil {+ slog.Warn("Could not inspect previous image labels; preserving existing OCI labels",+ "image", previousImage,+ "error", previousImageInspectErr,+ )+ } else if previousImageInspect.Config != nil {+ previousImageLabels = previousImageInspect.Config.Labels+ }+ }+ }+ return refreshRecreatedImageLabelsInternal(containerLabels, previousImageLabels, imageLabels, imageInspect.ID)+}++func hasOCIImageLabelInternal(containerLabels map[string]string) bool {+ for key := range containerLabels {+ if strings.HasPrefix(strings.ToLower(key), "org.opencontainers.image.") {+ return true+ }+ }+ return false+}+func isAgentContainer(inspect container.InspectResponse) bool {if inspect.Config == nil {return false@@ -381,6 +464,8 @@ func upgradeContainer(ctx context.Context, dockerClient *client.Client, oldContaconfig.Image = newImageconfig.Labels = normalizeRecreatedArcaneLabelsInternal(config.Labels)+ config.Labels = refreshRecreatedContainerLabelsInternal(ctx, dockerClient, config.Labels, oldContainer.Image, newImage)+hostConfig, sanitizedMemorySwappiness, engineInfo, err := libarcane.PrepareRecreateHostConfigForEngine(ctx, dockerClient, oldContainer.HostConfig)if err != nil {return errors.WrapIf(err, "prepare host config")diff --git a/backend/cli/upgrade/upgrade_test.go b/backend/cli/upgrade/upgrade_test.goindex d47c630a5..229fef940 100644--- a/backend/cli/upgrade/upgrade_test.go+++ b/backend/cli/upgrade/upgrade_test.go@@ -1,8 +1,13 @@package upgradeimport (+ "context"+ "net/http"+ "net/http/httptest"+ "strings""testing"+ "github.com/moby/moby/client""github.com/stretchr/testify/require""go.getarcane.app/updater/labels")@@ -69,3 +74,136 @@ func TestNormalizeRecreatedArcaneLabelsInternal(t *testing.T) {})}}++func TestRefreshRecreatedImageLabelsInternal(t *testing.T) {+ containerLabels := map[string]string{+ "ORG.OPENCONTAINERS.IMAGE.VERSION": "v2.6.0-next.30",+ "org.opencontainers.image.revision": "old-revision",+ "org.opencontainers.image.authors": "https://container.example/override",+ "com.docker.compose.image": "sha256:old-image",+ "com.docker.compose.project": "arcane",+ labels.LabelArcane: "true",+ labels.LabelUpdater: "false",+ "com.example.custom": "keep",+ }+ originalContainerLabels := map[string]string{+ "ORG.OPENCONTAINERS.IMAGE.VERSION": "v2.6.0-next.30",+ "org.opencontainers.image.revision": "old-revision",+ "org.opencontainers.image.authors": "https://container.example/override",+ "com.docker.compose.image": "sha256:old-image",+ "com.docker.compose.project": "arcane",+ labels.LabelArcane: "true",+ labels.LabelUpdater: "false",+ "com.example.custom": "keep",+ }+ targetImageLabels := map[string]string{+ "org.opencontainers.image.version": "v2.7.0-next.17",+ "org.opencontainers.image.revision": "new-revision",+ "org.opencontainers.image.authors": "new-author",+ "org.opencontainers.image.title": "Arcane",+ "com.example.image": "must-not-be-copied",+ }+ previousImageLabels := map[string]string{+ "org.opencontainers.image.version": "v2.6.0-next.30",+ "org.opencontainers.image.revision": "old-revision",+ "org.opencontainers.image.authors": "old-author",+ }++ got := refreshRecreatedImageLabelsInternal(containerLabels, previousImageLabels, targetImageLabels, "sha256:new-image")++ require.Equal(t, map[string]string{+ "org.opencontainers.image.version": "v2.7.0-next.17",+ "org.opencontainers.image.revision": "new-revision",+ "org.opencontainers.image.authors": "https://container.example/override",+ "org.opencontainers.image.title": "Arcane",+ "com.docker.compose.image": "sha256:new-image",+ "com.docker.compose.project": "arcane",+ labels.LabelArcane: "true",+ labels.LabelUpdater: "false",+ "com.example.custom": "keep",+ }, got)+ require.Equal(t, originalContainerLabels, containerLabels)+}++func TestRefreshRecreatedImageLabelsInternalKeepsNilWhenNoLabelsExist(t *testing.T) {+ require.Nil(t, refreshRecreatedImageLabelsInternal(nil, nil, nil, ""))+}++func TestRefreshRecreatedContainerLabelsInternalPreservesLabelsWhenTargetInspectFails(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if !strings.Contains(r.URL.Path, "/images/target:latest/json") {+ t.Errorf("unexpected Docker API request: %s %s", r.Method, r.URL.Path)+ }+ http.Error(w, "target inspect failed", http.StatusInternalServerError)+ }))+ t.Cleanup(server.Close)++ dockerClient, err := client.New(+ client.WithHost("tcp://"+strings.TrimPrefix(server.URL, "http://")),+ client.WithAPIVersion("1.41"),+ )+ require.NoError(t, err)+ t.Cleanup(func() { _ = dockerClient.Close() })++ containerLabels := map[string]string{+ "org.opencontainers.image.version": "v2.6.0-next.30",+ "org.opencontainers.image.authors": "operator",+ "com.docker.compose.image": "sha256:old-image",+ }++ got := refreshRecreatedContainerLabelsInternal(+ context.Background(),+ dockerClient,+ containerLabels,+ "sha256:old-image",+ "target:latest",+ )++ require.Equal(t, containerLabels, got)+ got["org.opencontainers.image.version"] = "changed"+ require.Equal(t, "v2.6.0-next.30", containerLabels["org.opencontainers.image.version"])+}++func TestRefreshRecreatedContainerLabelsInternalPreservesOverridesWhenPreviousInspectFails(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch {+ case strings.Contains(r.URL.Path, "/images/target:latest/json"):+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"Id":"sha256:new-image","Config":{"Labels":{"org.opencontainers.image.version":"v2.7.0-next.17","org.opencontainers.image.title":"Arcane"}}}`))+ case strings.Contains(r.URL.Path, "/images/sha256:old-image/json"):+ http.Error(w, "previous inspect failed", http.StatusInternalServerError)+ default:+ t.Errorf("unexpected Docker API request: %s %s", r.Method, r.URL.Path)+ http.NotFound(w, r)+ }+ }))+ t.Cleanup(server.Close)++ dockerClient, err := client.New(+ client.WithHost("tcp://"+strings.TrimPrefix(server.URL, "http://")),+ client.WithAPIVersion("1.41"),+ )+ require.NoError(t, err)+ t.Cleanup(func() { _ = dockerClient.Close() })++ containerLabels := map[string]string{+ "org.opencontainers.image.version": "v2.6.0-next.30",+ "org.opencontainers.image.authors": "operator",+ "com.docker.compose.image": "sha256:old-image",+ }++ got := refreshRecreatedContainerLabelsInternal(+ context.Background(),+ dockerClient,+ containerLabels,+ "sha256:old-image",+ "target:latest",+ )++ require.Equal(t, map[string]string{+ "org.opencontainers.image.version": "v2.6.0-next.30",+ "org.opencontainers.image.authors": "operator",+ "org.opencontainers.image.title": "Arcane",+ "com.docker.compose.image": "sha256:new-image",+ }, got)+}diff --git a/backend/go.mod b/backend/go.modindex 6b6b659cc..f722ed79b 100644--- a/backend/go.mod+++ b/backend/go.mod@@ -61,7 +61,7 @@ require (go.getarcane.app/sys/atomic v0.1.0go.getarcane.app/sys/cgroup v0.1.1go.getarcane.app/sys/crypto v0.1.0- go.getarcane.app/updater v0.7.0+ go.getarcane.app/updater v0.7.1go.uber.org/fx v1.24.0go.yaml.in/yaml/v4 v4.0.0-rc.6golang.org/x/crypto v0.54.0diff --git a/backend/go.sum b/backend/go.sumindex e27ff428a..3545bebca 100644--- a/backend/go.sum+++ b/backend/go.sum@@ -569,8 +569,8 @@ go.getarcane.app/sys/cgroup v0.1.1 h1:c39W8FDqMXlKOZrpH+itaY+t8oC0tva0+6ztTrKvVIgo.getarcane.app/sys/cgroup v0.1.1/go.mod h1:2dUDhWV8qe9LRnLIqa5VQDhdWbTQ04EepttdIAe9dcM=go.getarcane.app/sys/crypto v0.1.0 h1:HDQ2CKHkm+wZe5xb/qR7MzW5fKi0Wq8Jcyr7qDVgRWg=go.getarcane.app/sys/crypto v0.1.0/go.mod h1:YtoRB0TENwfdNl0ZUTWkeas/8rT7NhCbI8ENsZzCCdU=-go.getarcane.app/updater v0.7.0 h1:Rd6qJGtdJrMJ8SxfGpuP1+ZpJ604BNnhYb9UsB6O0Mg=-go.getarcane.app/updater v0.7.0/go.mod h1:6b8zVFJEUMhLzDOi4jwKBA/GmbVgguv0FeGOoO67JW8=+go.getarcane.app/updater v0.7.1 h1:MuS+mOEwlYpCx44Fvmj1CkOdju4d4/OaAYzZUpJll+U=+go.getarcane.app/updater v0.7.1/go.mod h1:6b8zVFJEUMhLzDOi4jwKBA/GmbVgguv0FeGOoO67JW8=go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=diff --git a/go.work.sum b/go.work.sumindex a03dc7d4b..10cfbdb90 100644--- a/go.work.sum+++ b/go.work.sum@@ -2171,6 +2171,8 @@ go.getarcane.app/updater v0.3.6 h1:+BqoycaRsHj+cLVSzcYQuqbHxUKbtmaozMNblz5bVSw=go.getarcane.app/updater v0.3.6/go.mod h1:2ENitZR/lA5MuBn60OZYR5wvU3ZCE+pAgfrebfOlRDA=go.getarcane.app/updater v0.3.7 h1:WqHsxudvt8gvCR7iSHdxdzwH0+hM0FAh64bSypBX9Mo=go.getarcane.app/updater v0.3.7/go.mod h1:9RZOKaBbpUJfVA4Q9/jvqtjvWBwCnVw0OkPeQCnqAAo=+go.getarcane.app/updater v0.7.0 h1:Rd6qJGtdJrMJ8SxfGpuP1+ZpJ604BNnhYb9UsB6O0Mg=+go.getarcane.app/updater v0.7.0/go.mod h1:6b8zVFJEUMhLzDOi4jwKBA/GmbVgguv0FeGOoO67JW8=go.mongodb.org/mongo-driver v1.7.5 h1:ny3p0reEpgsR2cfA5cjgwFZg3Cv/ofFh/8jbhGtz9VI=go.mongodb.org/mongo-driver v1.7.5/go.mod h1:VXEWRZ6URJIkUq2SCAyapmhH0ZLRBP+FT4xhp5Zvxng=go.mongodb.org/mongo-driver v1.17.6 h1:87JUG1wZfWsr6rIz3ZmpH90rL5tea7O3IHuSwHUpsss=