128 lines
3.0 KiB
Go
128 lines
3.0 KiB
Go
// https://iximiuz.com/en/posts/working-with-container-images-in-go/
|
|
// package main
|
|
//
|
|
// import (
|
|
//
|
|
// "context"
|
|
// "nf-repo/internal/util/tools"
|
|
// "os"
|
|
//
|
|
// "github.com/containers/image/copy"
|
|
// "github.com/containers/image/signature"
|
|
// "github.com/containers/image/storage"
|
|
// "github.com/containers/image/transports/alltransports"
|
|
// "github.com/containers/image/types"
|
|
//
|
|
// )
|
|
//
|
|
// func main() {
|
|
// imageName := "docker://alpine:latest"
|
|
// srcRef, _ := alltransports.ParseImageName(imageName)
|
|
//
|
|
// // Carries various default locations.
|
|
// systemCtx := &types.SystemContext{}
|
|
// policy, _ := signature.DefaultPolicy(systemCtx)
|
|
// policyCtx, _ := signature.NewPolicyContext(policy)
|
|
//
|
|
// dstName := imageName
|
|
// if srcRef.DockerReference() != nil {
|
|
// dstName = srcRef.DockerReference().String()
|
|
// }
|
|
// store := createStore() // see previous section
|
|
// dstRef, _ := storage.Transport.ParseStoreReference(store, dstName)
|
|
//
|
|
// copyOptions := ©.Options{ReportWriter: os.Stdout}
|
|
// manifest, _ := copy.Image(
|
|
// context.Background(),
|
|
// policyCtx,
|
|
// dstRef,
|
|
// srcRef,
|
|
// copyOptions,
|
|
// )
|
|
// println(string(manifest))
|
|
// }
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/containers/image/v5/pkg/blobinfocache"
|
|
"github.com/containers/image/v5/transports/alltransports"
|
|
"github.com/containers/image/v5/types"
|
|
digest "github.com/opencontainers/go-digest"
|
|
"log"
|
|
"nf-repo/internal/util/tools"
|
|
"runtime"
|
|
)
|
|
|
|
type M struct {
|
|
Manifests []struct {
|
|
Digest string `json:"digest"`
|
|
MediaType string `json:"mediaType"`
|
|
Platform struct {
|
|
Architecture string `json:"architecture"`
|
|
Os string `json:"os"`
|
|
Variant string `json:"variant,omitempty"`
|
|
} `json:"platform"`
|
|
Size int `json:"size"`
|
|
} `json:"manifests"`
|
|
MediaType string `json:"mediaType"`
|
|
SchemaVersion int `json:"schemaVersion"`
|
|
}
|
|
|
|
func main() {
|
|
imageName := "docker://alpine:latest"
|
|
srcRef, _ := alltransports.ParseImageName(imageName)
|
|
|
|
// Carries various default locations.
|
|
systemCtx := &types.SystemContext{
|
|
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
|
|
DockerDisableV1Ping: true,
|
|
}
|
|
|
|
log.Printf("[NewImageSource] start!!!")
|
|
imgSrc, err := srcRef.NewImageSource(tools.Timeout(60), systemCtx)
|
|
if err != nil {
|
|
log.Panic(1, err)
|
|
}
|
|
|
|
log.Printf("[GetManifest] start!!!")
|
|
bs, str, err := imgSrc.GetManifest(tools.Timeout(5), nil)
|
|
if err != nil {
|
|
log.Panic(2, err)
|
|
}
|
|
|
|
log.Printf("[manifest] bs=%s str=%s", string(bs), str)
|
|
|
|
var (
|
|
m = new(M)
|
|
d string
|
|
)
|
|
if err = json.Unmarshal(bs, m); err != nil {
|
|
log.Panic(3, err)
|
|
}
|
|
|
|
for _, item := range m.Manifests {
|
|
if item.Platform.Os == "linux" && item.Platform.Architecture == runtime.GOARCH {
|
|
d = item.Digest
|
|
break
|
|
}
|
|
}
|
|
|
|
if d == "" {
|
|
log.Panicf("[4] empty digest: %s - %s", runtime.GOOS, runtime.GOARCH)
|
|
}
|
|
|
|
rc, size, err := imgSrc.GetBlob(tools.Timeout(60),
|
|
types.BlobInfo{
|
|
Digest: digest.Digest(d),
|
|
}, blobinfocache.DefaultCache(systemCtx))
|
|
if err != nil {
|
|
log.Panic(5, err)
|
|
}
|
|
|
|
log.Printf("[GetBlob] size=%d", size)
|
|
|
|
rc.Close()
|
|
}
|