Browse Source

fix OCI compose override support

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 months ago
parent
commit
e59150baa8

+ 6 - 0
internal/oci/resolver.go

@@ -20,6 +20,7 @@ import (
 	"context"
 	"io"
 	"net/url"
+	"os"
 	"strings"
 
 	"github.com/containerd/containerd/v2/core/remotes"
@@ -50,6 +51,11 @@ func NewResolver(config *configfile.ConfigFile) remotes.Resolver {
 					return auth.Username, auth.Password, nil
 				}),
 			)),
+			docker.WithPlainHTTP(func(s string) (bool, error) {
+				// Used for testing **only**
+				_, b := os.LookupEnv("__TEST__INSECURE__REGISTRY__")
+				return b, nil
+			}),
 		),
 	})
 }

+ 3 - 0
pkg/e2e/fixtures/publish/oci/compose-override.yaml

@@ -0,0 +1,3 @@
+services:
+  app:
+    env_file: test.env

+ 5 - 0
pkg/e2e/fixtures/publish/oci/compose.yaml

@@ -0,0 +1,5 @@
+services:
+  app:
+    extends:
+      file: extends.yaml
+      service: test

+ 3 - 0
pkg/e2e/fixtures/publish/oci/extends.yaml

@@ -0,0 +1,3 @@
+services:
+  test:
+    image: alpine

+ 1 - 0
pkg/e2e/fixtures/publish/oci/test.env

@@ -0,0 +1 @@
+HELLO=WORLD

+ 41 - 0
pkg/e2e/publish_test.go

@@ -17,6 +17,7 @@
 package e2e
 
 import (
+	"fmt"
 	"strings"
 	"testing"
 
@@ -173,3 +174,43 @@ FOO=bar`), out)
 		assert.Assert(t, strings.Contains(output, "Private Key\n\"\": -----BEGIN DSA PRIVATE KEY-----\nwxyz+ABC=\n-----END DSA PRIVATE KEY-----"), output)
 	})
 }
+
+func TestPublish(t *testing.T) {
+	c := NewParallelCLI(t)
+	const projectName = "compose-e2e-publish"
+	const registryName = projectName + "-registry"
+	c.RunDockerCmd(t, "run", "--name", registryName, "-P", "-d", "registry:3")
+	port := c.RunDockerCmd(t, "inspect", "--format", `{{ (index (index .NetworkSettings.Ports "5000/tcp") 0).HostPort }}`, registryName).Stdout()
+	registry := "localhost:" + strings.TrimSpace(port)
+	t.Cleanup(func() {
+		c.RunDockerCmd(t, "rm", "--force", registryName)
+	})
+
+	cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/publish/oci/compose.yaml", "-f", "./fixtures/publish/oci/compose-override.yaml",
+		"-p", projectName, "publish", "--with-env", "--yes", registry+"/test:test")
+	icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
+		cmd.Env = append(cmd.Env, "__TEST__INSECURE__REGISTRY__=true")
+	}).Assert(t, icmd.Expected{ExitCode: 0})
+
+	// docker exec -it compose-e2e-publish-registry tree /var/lib/registry/docker/registry/v2/
+
+	cmd = c.NewDockerComposeCmd(t, "--verbose", "--project-name=oci", "-f", fmt.Sprintf("oci://%s/test:test", registry), "config")
+	res := icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
+		cmd.Env = append(cmd.Env,
+			"XDG_CACHE_HOME="+t.TempDir(),
+			"__TEST__INSECURE__REGISTRY__=true")
+	})
+	res.Assert(t, icmd.Expected{ExitCode: 0})
+	assert.Equal(t, res.Stdout(), `name: oci
+services:
+  app:
+    environment:
+      HELLO: WORLD
+    image: alpine
+    networks:
+      default: null
+networks:
+  default:
+    name: oci_default
+`)
+}

+ 1 - 1
pkg/remote/oci.go

@@ -223,7 +223,7 @@ func writeComposeFile(layer spec.Descriptor, i int, local string, content []byte
 			return err
 		}
 	}
-	f, err := os.Create(filepath.Join(local, file))
+	f, err := os.OpenFile(filepath.Join(local, file), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
 	if err != nil {
 		return err
 	}