From 2561f103dc1be59a9d54d2a8706ae5387d425a6d Mon Sep 17 00:00:00 2001 From: taylorhere Date: Tue, 1 Nov 2022 21:19:11 +0800 Subject: [PATCH] feat(context): add podman support feat(CI): add setup podman --- .github/workflows/CI.yml | 2 ++ e2e/cli/context_test.go | 54 ++++++++++++++++++++++++++++++++++++++ pkg/app/context_create.go | 4 +-- pkg/buildkitd/buildkitd.go | 12 ++++++++- pkg/home/context.go | 5 ++-- pkg/types/envd.go | 2 ++ 6 files changed, 74 insertions(+), 5 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index bb73388f3..b3da46191 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -112,6 +112,8 @@ jobs: ${{ runner.OS }}- - uses: actions-ecosystem/action-get-latest-tag@v1 id: get-latest-tag + - name: setup podman + run: sudo apt install podman && systemctl --user start podman.socket && systemctl --user status podman.socket - name: e2e test run: make e2e-cli-test env: diff --git a/e2e/cli/context_test.go b/e2e/cli/context_test.go index aa4733255..35f31e075 100644 --- a/e2e/cli/context_test.go +++ b/e2e/cli/context_test.go @@ -15,6 +15,7 @@ package cli import ( + "os" "os/exec" . "github.com/onsi/ginkgo/v2" @@ -108,4 +109,57 @@ var _ = Describe("home context", func() { Expect(contexts.Current).To(Equal(defaultContext)) }) }) + + Describe("add a podman context", Ordered, func() { + testContext := "envd_home_test" + testBuilder := types.BuilderTypePodman + testBuilderAddress := "envd_buildkitd" + testRunner := types.RunnerTypePodman + c := types.Context{ + Name: testContext, + Builder: testBuilder, + BuilderAddress: testBuilderAddress, + Runner: testRunner, + } + originalEnv := os.Getenv("DOCKER_HOST") + BeforeAll(func() { + os.Setenv("DOCKER_HOST", "unix://"+os.Getenv("XDG_RUNTIME_DIR")+"/podman/podman.sock") + err := home.GetManager().ContextCreate(c, true) + Expect(err).NotTo(HaveOccurred()) + }) + + It("should find a new context", func() { + contexts, err := home.GetManager().ContextList() + Expect(err).NotTo(HaveOccurred()) + Expect(contexts.Current).To(Equal(testContext)) + }) + + Describe("connect buildkit through podman-container", Ordered, func() { + buildContext := "testdata/build-test" + + It("should be able to build image with podman context", func() { + args := []string{"envd.test", "--debug", "build", "--path", buildContext} + envdApp := app.New() + e2e.ResetEnvdApp() + err := envdApp.Run(args) + Expect(err).NotTo(HaveOccurred()) + }) + }) + + It("fail to delete the current context", func() { + err := home.GetManager().ContextRemove(testContext) + Expect(err).To(HaveOccurred()) + }) + + AfterAll(func() { + os.Setenv("DOCKER_HOST", originalEnv) + err := home.GetManager().ContextUse(defaultContext) + Expect(err).NotTo(HaveOccurred()) + err = home.GetManager().ContextRemove(testContext) + Expect(err).NotTo(HaveOccurred()) + contexts, err := home.GetManager().ContextList() + Expect(err).NotTo(HaveOccurred()) + Expect(contexts.Current).To(Equal(defaultContext)) + }) + }) }) diff --git a/pkg/app/context_create.go b/pkg/app/context_create.go index bbcf6ef9e..7feb076d3 100644 --- a/pkg/app/context_create.go +++ b/pkg/app/context_create.go @@ -35,7 +35,7 @@ var CommandContextCreate = &cli.Command{ }, &cli.StringFlag{ Name: "builder", - Usage: "Builder to use (docker-container, kube-pod, tcp, unix)", + Usage: "Builder to use (docker-container, podman-container, kube-pod, tcp, unix)", Value: string(types.BuilderTypeDocker), }, &cli.StringFlag{ @@ -45,7 +45,7 @@ var CommandContextCreate = &cli.Command{ }, &cli.StringFlag{ Name: "runner", - Usage: "Runner to use(docker, envd-server)", + Usage: "Runner to use(docker, podman, envd-server)", Value: string(types.RunnerTypeDocker), }, &cli.StringFlag{ diff --git a/pkg/buildkitd/buildkitd.go b/pkg/buildkitd/buildkitd.go index da28703cb..049b4e0cc 100644 --- a/pkg/buildkitd/buildkitd.go +++ b/pkg/buildkitd/buildkitd.go @@ -113,16 +113,26 @@ func (c generalClient) Close() error { // that can be used to connect to it. func (c *generalClient) maybeStart(ctx context.Context, runningTimeout, connectingTimeout time.Duration) (string, error) { - if c.driver == types.BuilderTypeDocker { + if c.driver == types.BuilderTypeDocker || c.driver == types.BuilderTypePodman { dockerClient, err := docker.NewClient(ctx) if err != nil { return "", err } + opt := envd.Options{ Context: &types.Context{ Runner: types.RunnerTypeDocker, }, } + + if c.driver == types.BuilderTypePodman { + opt = envd.Options{ + Context: &types.Context{ + Runner: types.RunnerTypePodman, + }, + } + } + engine, err := envd.New(ctx, opt) if err != nil { return "", err diff --git a/pkg/home/context.go b/pkg/home/context.go index 6b6f250a5..b927b8bd0 100644 --- a/pkg/home/context.go +++ b/pkg/home/context.go @@ -96,13 +96,14 @@ func (m *generalManager) ContextCreate(ctx types.Context, use bool) error { } switch ctx.Builder { case types.BuilderTypeDocker, types.BuilderTypeKubernetes, - types.BuilderTypeUNIXDomainSocket, types.BuilderTypeTCP: + types.BuilderTypeUNIXDomainSocket, types.BuilderTypeTCP, + types.BuilderTypePodman: break default: return errors.New("unknown builder type") } switch ctx.Runner { - case types.RunnerTypeDocker, types.RunnerTypeEnvdServer: + case types.RunnerTypeDocker, types.RunnerTypeEnvdServer, types.RunnerTypePodman: break default: return errors.New("unknown runner type") diff --git a/pkg/types/envd.go b/pkg/types/envd.go index c9422b097..3ed3c0680 100644 --- a/pkg/types/envd.go +++ b/pkg/types/envd.go @@ -129,6 +129,7 @@ type BuilderType string const ( BuilderTypeDocker BuilderType = "docker-container" + BuilderTypePodman BuilderType = "podman-container" BuilderTypeKubernetes BuilderType = "kube-pod" BuilderTypeTCP BuilderType = "tcp" BuilderTypeUNIXDomainSocket BuilderType = "unix" @@ -138,6 +139,7 @@ type RunnerType string const ( RunnerTypeDocker RunnerType = "docker" + RunnerTypePodman RunnerType = "podman" RunnerTypeEnvdServer RunnerType = "envd-server" )