forked from apptainer/apptainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add progressbar support for apptainer push
Signed-off-by: jason yang <jasonyangshadow@gmail.com>
- Loading branch information
1 parent
1f4fba4
commit 0d8948b
Showing
5 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package oras | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/apptainer/apptainer/pkg/syfs" | ||
"github.com/apptainer/apptainer/pkg/sylog" | ||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/remotes" | ||
"github.com/containerd/containerd/remotes/docker" | ||
ocitypes "github.com/containers/image/v5/types" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/schollz/progressbar/v3" | ||
oras_docker "oras.land/oras-go/pkg/auth/docker" | ||
) | ||
|
||
type OrasResolver struct { | ||
remotes.Resolver | ||
} | ||
|
||
type orasPusher struct { | ||
remotes.Pusher | ||
} | ||
|
||
type contentWriter struct { | ||
content.Writer | ||
multiWriter io.Writer | ||
} | ||
|
||
func NewResolver(options docker.ResolverOptions) remotes.Resolver { | ||
return &OrasResolver{docker.NewResolver(options)} | ||
} | ||
|
||
func getOrasResolver(ctx context.Context, ociAuth *ocitypes.DockerAuthConfig, noHTTPS, push bool) (remotes.Resolver, error) { | ||
opts := docker.ResolverOptions{Credentials: genCredfn(ociAuth), PlainHTTP: noHTTPS} | ||
if ociAuth != nil && (ociAuth.Username != "" || ociAuth.Password != "") { | ||
return NewResolver(opts), nil | ||
} | ||
|
||
cli, err := oras_docker.NewClient(syfs.DockerConf()) | ||
if err != nil { | ||
sylog.Warningf("Couldn't load auth credential file: %s", err) | ||
return NewResolver(opts), nil | ||
} | ||
httpClient := &http.Client{} | ||
|
||
if push { | ||
httpClient.Transport = newOrasUploadTransport() | ||
} | ||
|
||
resolver, err := cli.Resolver(ctx, httpClient, noHTTPS) | ||
if err != nil { | ||
return &OrasResolver{}, err | ||
} | ||
|
||
return &OrasResolver{resolver}, nil | ||
} | ||
|
||
func (r *OrasResolver) Resolve(ctx context.Context, ref string) (name string, desc ocispec.Descriptor, err error) { | ||
return r.Resolver.Resolve(ctx, ref) | ||
} | ||
|
||
func (r *OrasResolver) Fetcher(ctx context.Context, ref string) (remotes.Fetcher, error) { | ||
return r.Resolver.Fetcher(ctx, ref) | ||
} | ||
|
||
func (r *OrasResolver) Pusher(ctx context.Context, ref string) (remotes.Pusher, error) { | ||
pusher, err := r.Resolver.Pusher(ctx, ref) | ||
if err != nil { | ||
return &orasPusher{}, err | ||
} | ||
|
||
return &orasPusher{pusher}, nil | ||
} | ||
|
||
func (p *orasPusher) Push(ctx context.Context, desc ocispec.Descriptor) (content.Writer, error) { | ||
writer, err := p.Pusher.Push(ctx, desc) | ||
if err != nil { | ||
return &contentWriter{}, err | ||
} | ||
|
||
bar := progressbar.DefaultBytes(desc.Size) | ||
mwriter := io.MultiWriter(writer, bar) | ||
return &contentWriter{ | ||
multiWriter: mwriter, | ||
Writer: writer, | ||
}, nil | ||
} | ||
|
||
func (w *contentWriter) Write(p []byte) (n int, err error) { | ||
return w.multiWriter.Write(p) | ||
} |