Skip to content

Commit

Permalink
Fix buildpack builder script (#2629)
Browse files Browse the repository at this point in the history
* Pass Go context.Context where appropriate

Signed-off-by: Matej Vašek <mvasek@redhat.com>

* Fix buildpack builder script

Bypass Moby bug where 500 is returned instead of 404.

Signed-off-by: Matej Vašek <mvasek@redhat.com>

* fixup: update go.mod

Signed-off-by: Matej Vašek <mvasek@redhat.com>

---------

Signed-off-by: Matej Vašek <mvasek@redhat.com>
  • Loading branch information
matejvasek authored Dec 20, 2024
1 parent e3a24d3 commit 1122bf7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/buildpacks/pack v0.36.0
github.com/chainguard-dev/git-urls v1.0.2
github.com/cloudevents/sdk-go/v2 v2.15.2
github.com/containerd/errdefs v0.3.0
github.com/containerd/platforms v0.2.1
github.com/containers/image/v5 v5.31.1
github.com/coreos/go-semver v0.3.1
Expand Down Expand Up @@ -119,7 +120,6 @@ require (
github.com/cloudevents/sdk-go/sql/v2 v2.15.2 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/errdefs v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containerd/typeurl/v2 v2.2.0 // indirect
Expand Down
32 changes: 25 additions & 7 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 @@ -108,7 +109,7 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
if err != nil {
return "", fmt.Errorf("cannot parse reference to builder target: %w", err)
}
desc, err := remote.Head(ref, remote.WithAuthFromKeychain(DefaultKeychain))
desc, err := remote.Head(ref, remote.WithAuthFromKeychain(DefaultKeychain), remote.WithContext(ctx))
if err == nil {
fmt.Fprintln(os.Stderr, "The image has been already built.")
return newBuilderImage + "@" + desc.Digest.String(), nil
Expand All @@ -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 @@ -261,6 +264,7 @@ func buildBuilderImageMultiArch(ctx context.Context, variant string) error {

remoteOpts := []remote.Option{
remote.WithAuthFromKeychain(DefaultKeychain),
remote.WithContext(ctx),
}

idx := mutate.IndexMediaType(empty.Index, types.DockerManifestList)
Expand Down Expand Up @@ -768,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 1122bf7

Please sign in to comment.