115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/go-containerregistry/pkg/authn"
|
|
"github.com/google/go-containerregistry/pkg/crane"
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
pkg "github.com/google/go-containerregistry/pkg/v1"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
|
)
|
|
|
|
type ImageDescr struct {
|
|
ConfigFile *pkg.ConfigFile `json:"configFile,omitempty"`
|
|
Manifest *pkg.Manifest `json:"manifest,omitempty"`
|
|
Tags []string `json:"avilableTags,omitempty"`
|
|
}
|
|
|
|
func (util *ImageUtil) imageInfo(params *ImageInfoParams) (*ImageInfoResult, error) {
|
|
var err error
|
|
res := &ImageInfoResult{}
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), time.Duration(params.Timeout)*time.Second)
|
|
|
|
options := make([]crane.Option, 0)
|
|
options = append(options, crane.WithContext(ctx))
|
|
|
|
ref, err := name.ParseReference(params.Imagepath)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
repo := ref.Context()
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
if params.Username != "" && params.Password != "" {
|
|
defaultTransport := &roundTripper{}
|
|
scopes := []string{repo.Scope(transport.PullScope)}
|
|
|
|
regName := repo.RegistryStr()
|
|
reg, err := name.NewRegistry(regName)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
basicAuth := &authn.Basic{
|
|
Username: params.Username,
|
|
Password: params.Password,
|
|
}
|
|
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
options = append(options, crane.WithTransport(authTransport))
|
|
} else {
|
|
transport := &roundTripper{}
|
|
options = append(options, crane.WithTransport(transport))
|
|
}
|
|
|
|
image, err := crane.Pull(params.Imagepath, options...)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
config, err := image.ConfigFile()
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.ImageInfo.ConfigFile = config
|
|
|
|
manifest, err := image.Manifest()
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.ImageInfo.Manifest = manifest
|
|
|
|
return res, err
|
|
|
|
remoteOptions := make([]remote.Option, 0)
|
|
remoteOptions = append(remoteOptions, remote.WithContext(ctx))
|
|
|
|
if params.Username != "" && params.Password != "" {
|
|
defaultTransport := &roundTripper{}
|
|
scopes := []string{repo.Scope(transport.PullScope)}
|
|
|
|
regName := repo.RegistryStr()
|
|
reg, err := name.NewRegistry(regName)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
basicAuth := &authn.Basic{
|
|
Username: params.Username,
|
|
Password: params.Password,
|
|
}
|
|
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
remoteOptions = append(remoteOptions, remote.WithTransport(authTransport))
|
|
} else {
|
|
transport := &roundTripper{}
|
|
options = append(options, crane.WithTransport(transport))
|
|
}
|
|
|
|
tags, err := remote.List(repo, remoteOptions...)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.ImageInfo.Tags = tags
|
|
|
|
return res, err
|
|
}
|