diff --git a/.dagger/build_publish.go b/.dagger/build_publish.go index f263e49..31a7f23 100644 --- a/.dagger/build_publish.go +++ b/.dagger/build_publish.go @@ -13,7 +13,7 @@ func (m *Cuestomize) Build( platform string, // +default="" ldflags string, -) (*dagger.Container, error) { +) *dagger.Container { containerOpts := dagger.ContainerOpts{} if platform != "" { containerOpts.Platform = dagger.Platform(platform) @@ -29,7 +29,7 @@ func (m *Cuestomize) Build( WithFile("/usr/local/bin/cuestomize", builder.File("/workspace/cuestomize")). WithEntrypoint([]string{"/usr/local/bin/cuestomize"}) - return container, nil + return container } func (m *Cuestomize) BuildAndPublish( @@ -73,10 +73,7 @@ func (m *Cuestomize) BuildAndPublish( platformVariants := make([]*dagger.Container, 0, len(platforms)) for _, platform := range platforms { - container, err := m.Build(ctx, buildContext, string(platform), ldflags) - if err != nil { - return err - } + container := m.Build(ctx, buildContext, string(platform), ldflags) platformVariants = append(platformVariants, container) } diff --git a/.dagger/constants.go b/.dagger/constants.go index 17cefde..b1fbe17 100644 --- a/.dagger/constants.go +++ b/.dagger/constants.go @@ -29,6 +29,8 @@ var ( Exclude: []string{ ".go-version", "README.md", ".vscode", "examples", + ".dagger", "docs", }, + Gitignore: true, } ) diff --git a/.dagger/main.go b/.dagger/main.go index 30bcb6c..2ebdfdb 100644 --- a/.dagger/main.go +++ b/.dagger/main.go @@ -10,10 +10,9 @@ type Cuestomize struct{} // repoBaseContainer creates a container with the repository files in it and go dependencies installed. // The working directory is set to `/workspace` and contains the root of the repository. -func repoBaseContainer(buildContext *dagger.Directory, excludedOpts *dagger.ContainerWithDirectoryOpts, containerOpts ...dagger.ContainerOpts) *dagger.Container { - var exOpts dagger.ContainerWithDirectoryOpts - if excludedOpts == nil { - exOpts = DefaultExcludedOpts +func repoBaseContainer(buildContext *dagger.Directory, dirOpts *dagger.ContainerWithDirectoryOpts, containerOpts ...dagger.ContainerOpts) *dagger.Container { + if dirOpts == nil { + dirOpts = &DefaultExcludedOpts } // Create a container to run the tests @@ -22,10 +21,8 @@ func repoBaseContainer(buildContext *dagger.Directory, excludedOpts *dagger.Cont WithWorkdir("/workspace"). WithFile("/workspace/go.mod", buildContext.File("go.mod")). WithFile("/workspace/go.sum", buildContext.File("go.sum")). - WithFile("/workspace/.dagger/go.mod", buildContext.File(".dagger/go.mod")). - WithFile("/workspace/.dagger/go.sum", buildContext.File(".dagger/go.sum")). WithExec([]string{"go", "mod", "download"}). - WithDirectory("/workspace", buildContext, exOpts) + WithDirectory("/workspace", buildContext, *dirOpts) } // cuestomizeBuilderContainer returns a container that can be used to build the cuestomize binary. diff --git a/.dagger/testing_pipelines.go b/.dagger/testing_pipelines.go index f1a16cf..6bf86e9 100644 --- a/.dagger/testing_pipelines.go +++ b/.dagger/testing_pipelines.go @@ -38,13 +38,14 @@ func (m *Cuestomize) E2E_Test( ctx context.Context, // +defaultPath=./ buildContext *dagger.Directory, + // sock *dagger.Socket, ) error { // build cuestomize - cuestomize, err := cuestomizeBuilderContainer(buildContext, "").Sync(ctx) - if err != nil { - return fmt.Errorf("failed to build cuestomize: %w", err) - } - cuestomizeBinary := cuestomize.File("/workspace/cuestomize") + cuestomize := m.Build(ctx, buildContext, "", "") + + cuestomizeBinary := cuestomize.File("/usr/local/bin/cuestomize") + + cuestomizeTar := cuestomize.AsTarball() testdataDir := buildContext.Directory("e2e/testdata") @@ -71,13 +72,49 @@ func (m *Cuestomize) E2E_Test( return fmt.Errorf("failed to run e2e tests: %w", err) } + dind := dag.Container(). + From("docker:dind"). + WithEnvVariable("TINI_SUBREAPER", "true"). + WithServiceBinding("registry_auth", registryWithAuthService). + WithMountedCache("/var/lib/docker", dag.CacheVolume("dind-data")). + WithExposedPort(2375).AsService(dagger.ContainerAsServiceOpts{ + Args: []string{ + "dockerd", "--tls=false", "--host=tcp://0.0.0.0:2375", + }, + InsecureRootCapabilities: true, + UseEntrypoint: true, + }) + + dindService, err := dind.Start(ctx) + if err != nil { + return fmt.Errorf("failed to start dind: %w", err) + } + defer dindService.Stop(ctx) + + dockerCli := dag.Container().From("docker:cli") + // Load the image into DIND and tag it + _, err = dockerCli. + WithServiceBinding("docker-host", dindService). + WithEnvVariable("DOCKER_HOST", "tcp://docker-host:2375"). + WithFile("/tmp/image.tar", cuestomizeTar). + WithExec([]string{"sh", "-c", ` + SOURCE=$(docker load -i /tmp/image.tar -q | cut -d' ' -f 4) + docker tag $SOURCE cuestomize:latest + `}).Sync(ctx) + if err != nil { + return fmt.Errorf("failed to load cuestomize image into dind: %w", err) + } + // run e2e tests // TODO: save output to file and extract it for comparison kustomize := dag.Container().From(KustomizeImage). WithServiceBinding("registry", registryService). WithServiceBinding("registry_auth", registryWithAuthService). + WithServiceBinding("docker-host", dindService). + WithEnvVariable("DOCKER_HOST", "tcp://docker-host:2375"). WithDirectory("/testdata", testdataDir). WithFile("/bin/cuestomize", cuestomizeBinary). + WithFile("/usr/local/bin/docker", dockerCli.File("/usr/local/bin/docker")). WithDirectory("/cue-resources", dag.Directory()). WithNewFile( "/testdata/kustomize-auth/.env.secret", @@ -87,8 +124,6 @@ func (m *Cuestomize) E2E_Test( return fmt.Errorf("kustomize with no auth e2e failed: %w", err) } - kustomize = kustomize.WithoutDirectory("/cue-resources").WithDirectory("/cue-resources", dag.Directory()) - if _, err := kustomize.WithExec([]string{"kustomize", "build", "--enable-alpha-plugins", "--network", "/testdata/kustomize-auth"}).Sync(ctx); err != nil { return fmt.Errorf("kustomize with auth e2e failed: %w", err) } diff --git a/.dagger/toolchain.go b/.dagger/toolchain.go index 45f5976..9b8e290 100644 --- a/.dagger/toolchain.go +++ b/.dagger/toolchain.go @@ -12,11 +12,7 @@ func (m *Cuestomize) GoGenerate( // +defaultPath=./ buildContext *dagger.Directory, ) *dagger.Container { - container := repoBaseContainer(buildContext, &dagger.ContainerWithDirectoryOpts{ - Exclude: []string{ - ".go-version", "README.md", ".vscode", - }, - }). + container := repoBaseContainer(buildContext, nil). WithExec([]string{"go", "install", fmt.Sprintf("cuelang.org/go/cmd/cue@%s", CuelangVersion)}). WithExec([]string{"go", "generate", "./..."}) return container diff --git a/e2e/testdata/kustomize-auth/krm-func.yaml b/e2e/testdata/kustomize-auth/krm-func.yaml index 6e322c2..498aa4d 100644 --- a/e2e/testdata/kustomize-auth/krm-func.yaml +++ b/e2e/testdata/kustomize-auth/krm-func.yaml @@ -5,8 +5,9 @@ metadata: annotations: config.kubernetes.io/local-config: "true" config.kubernetes.io/function: | - exec: - path: /bin/cuestomize + container: + image: cuestomize:latest + network: true input: configMapName: example-configmap includes: @@ -20,9 +21,7 @@ includes: name: example-service namespace: example-namespace remoteModule: - registry: registry_auth:5000 - repo: sample-module - tag: latest + ref: registry_auth:5000/sample-module:latest auth: kind: Secret name: regcred