Skip to content

Commit

Permalink
Fix buildpack builder script
Browse files Browse the repository at this point in the history
Bypass Moby bug where 500 is returned instead of 404.

Signed-off-by: Matej Vašek <[email protected]>
  • Loading branch information
matejvasek committed Dec 19, 2024
1 parent 11c1a0f commit e427b79
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions hack/update-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
pack "github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/dist"
bpimage "github.com/buildpacks/pack/pkg/image"
"github.com/containerd/errdefs"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
docker "github.com/docker/docker/client"
Expand Down Expand Up @@ -131,7 +132,14 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
}
addGoAndRustBuildpacks(&builderConfig)

packClient, err := pack.NewClient(pack.WithKeychain(DefaultKeychain))
var dockerClient docker.CommonAPIClient
dockerClient, err = docker.NewClientWithOpts(docker.FromEnv, docker.WithAPIVersionNegotiation())
if err != nil {
return "", fmt.Errorf("cannot create docker client")
}
dockerClient = &hackDockerClient{dockerClient}

packClient, err := pack.NewClient(pack.WithKeychain(DefaultKeychain), pack.WithDockerClient(dockerClient))
if err != nil {
return "", fmt.Errorf("cannot create pack client: %w", err)
}
Expand Down Expand Up @@ -162,11 +170,6 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
return "", fmt.Errorf("canont create builder: %w", err)
}

dockerClient, err := docker.NewClientWithOpts(docker.FromEnv, docker.WithAPIVersionNegotiation())
if err != nil {
return "", fmt.Errorf("cannot create docker client")
}

pushImage := func(img string) (string, error) {
regAuth, err := dockerDaemonAuthStr(img)
if err != nil {
Expand Down Expand Up @@ -769,3 +772,17 @@ func dockerDaemonAuthStr(img string) (string, error) {

return base64.StdEncoding.EncodeToString(bs), nil
}

// Hack implementation of docker client returns NotFound for images ghcr.io/knative/buildpacks/*
// For some reason moby/docker erroneously returns 500 HTTP code for these missing images.
// Interestingly podman correctly returns 404 for same request.
type hackDockerClient struct {
docker.CommonAPIClient
}

func (c hackDockerClient) ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error) {
if strings.HasPrefix(ref, "ghcr.io/knative/buildpacks/") {
return nil, fmt.Errorf("this image is supposed to exist only in daemon: %w", errdefs.ErrNotFound)
}
return c.CommonAPIClient.ImagePull(ctx, ref, options)
}

0 comments on commit e427b79

Please sign in to comment.