Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buildpack builder script #2629

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading