Skip to content

Commit

Permalink
Merge pull request #10413 from glours/dry-run-create-support
Browse files Browse the repository at this point in the history
add dry-run support to create command
  • Loading branch information
glours committed Apr 3, 2023
2 parents 449a46a + b83edbd commit 6a37428
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/api/dryrunclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"net"
"net/http"
"runtime"
"strings"
"sync"

Expand Down Expand Up @@ -84,6 +85,12 @@ func NewDryRunClient(apiClient client.APIClient, cli *command.DockerCli) (*DryRu
}, nil
}

func getCallingFunction() string {
pc, _, _, _ := runtime.Caller(2)
fullName := runtime.FuncForPC(pc).Name()
return fullName[strings.LastIndex(fullName, ".")+1:]
}

// All methods and functions which need to be overridden for dry run.

func (d *DryRunClient) ContainerAttach(ctx context.Context, container string, options moby.ContainerAttachOptions) (moby.HijackedResponse, error) {
Expand Down Expand Up @@ -162,7 +169,14 @@ func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options
}

func (d *DryRunClient) ImageInspectWithRaw(ctx context.Context, imageName string) (moby.ImageInspect, []byte, error) {
return moby.ImageInspect{ID: "dryRunId"}, nil, nil
caller := getCallingFunction()
switch caller {
case "pullServiceImage", "buildContainerVolumes":
return moby.ImageInspect{ID: "dryRunId"}, nil, nil
default:
return d.apiClient.ImageInspectWithRaw(ctx, imageName)
}

}

func (d *DryRunClient) ImagePull(ctx context.Context, ref string, options moby.ImagePullOptions) (io.ReadCloser, error) {
Expand Down Expand Up @@ -204,7 +218,10 @@ func (d *DryRunClient) NetworkConnect(ctx context.Context, networkName, containe
}

func (d *DryRunClient) NetworkCreate(ctx context.Context, name string, options moby.NetworkCreate) (moby.NetworkCreateResponse, error) {
return moby.NetworkCreateResponse{}, ErrNotImplemented
return moby.NetworkCreateResponse{
ID: name,
Warning: "",
}, nil
}

func (d *DryRunClient) NetworkDisconnect(ctx context.Context, networkName, container string, force bool) error {
Expand Down
10 changes: 10 additions & 0 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
return nil
}

//TODO:glours - condition to be removed when dry-run support of build will be implemented.
if s.dryRun {
builder := "buildkit"
if !buildkitEnabled {
builder = "legacy builder"
}
fmt.Printf("%sBuilding image %s with %s\n", api.DRYRUN_PREFIX, service.Image, builder)
return nil
}

if !buildkitEnabled {
if service.Build.Args == nil {
service.Build.Args = args
Expand Down

0 comments on commit 6a37428

Please sign in to comment.