-
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.
- Loading branch information
Showing
13 changed files
with
816 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package kubepkg | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
kubepkgv1alpha1 "github.com/octohelm/kubepkgspec/pkg/apis/kubepkg/v1alpha1" | ||
specv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"iter" | ||
) | ||
|
||
func Push(i ImageIter, options ...remote.Option) error { | ||
for ref, img := range i.Images() { | ||
if err := remote.Put(ref, img, options...); err != nil { | ||
return err | ||
} | ||
} | ||
return i.Err() | ||
} | ||
|
||
func KubePkg(idx v1.ImageIndex) (*kubepkgv1alpha1.KubePkg, error) { | ||
indexManifest, err := idx.IndexManifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, m := range indexManifest.Manifests { | ||
if m.ArtifactType == ArtifactType { | ||
img, err := idx.Image(m.Digest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawConfig, err := img.RawConfigFile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
kpkg := &kubepkgv1alpha1.KubePkg{} | ||
if err := json.Unmarshal(rawConfig, kpkg); err != nil { | ||
return nil, err | ||
} | ||
return kpkg, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func WithRenamer(renamer Renamer) ImageIterOption { | ||
return func(i *imageIter) { | ||
i.renamer = renamer | ||
} | ||
} | ||
|
||
func WithRegistry(r Registry) ImageIterOption { | ||
return func(i *imageIter) { | ||
i.registry = r | ||
} | ||
} | ||
|
||
type ImageIterOption func(i *imageIter) | ||
|
||
func NewImageIter(idx v1.ImageIndex, options ...ImageIterOption) ImageIter { | ||
i := &imageIter{ | ||
ImageIndex: idx, | ||
} | ||
|
||
for _, o := range options { | ||
o(i) | ||
} | ||
|
||
return i | ||
} | ||
|
||
type ImageIter interface { | ||
Images() iter.Seq2[name.Reference, remote.Taggable] | ||
Err() error | ||
} | ||
|
||
type imageIter struct { | ||
ImageIndex v1.ImageIndex | ||
|
||
registry Registry | ||
renamer Renamer | ||
|
||
err error | ||
} | ||
|
||
func (r *imageIter) Err() error { | ||
return r.err | ||
} | ||
|
||
func (r *imageIter) Done(err error) { | ||
r.err = err | ||
} | ||
|
||
func (p *imageIter) Repository(repoName string) (name.Repository, error) { | ||
if r := p.registry; r != nil { | ||
return r.Repo(repoName), nil | ||
} | ||
return name.NewRepository(repoName) | ||
} | ||
|
||
func (r *imageIter) Images() iter.Seq2[name.Reference, remote.Taggable] { | ||
indexManifest, err := r.ImageIndex.IndexManifest() | ||
if err != nil { | ||
r.Done(err) | ||
|
||
return func(yield func(name.Reference, remote.Taggable) bool) { | ||
|
||
} | ||
} | ||
|
||
return func(yield func(name.Reference, remote.Taggable) bool) { | ||
for _, d := range indexManifest.Manifests { | ||
if d.Annotations == nil { | ||
continue | ||
} | ||
|
||
imageName := d.Annotations[specv1.AnnotationBaseImageName] | ||
if imageName == "" { | ||
continue | ||
} | ||
|
||
tag := d.Annotations[specv1.AnnotationRefName] | ||
if tag == "" { | ||
continue | ||
} | ||
|
||
repo, err := r.Repository(imageName) | ||
if err != nil { | ||
r.Done(err) | ||
return | ||
} | ||
|
||
if r.renamer != nil { | ||
renamed, err := r.Repository(r.renamer.Rename(repo)) | ||
if err != nil { | ||
r.Done(err) | ||
return | ||
} | ||
repo = renamed | ||
} | ||
|
||
ref := repo.Tag(tag) | ||
|
||
if d.MediaType.IsImage() { | ||
img, err := r.ImageIndex.Image(d.Digest) | ||
if err != nil { | ||
r.Done(err) | ||
return | ||
} | ||
|
||
if !yield(ref, img) { | ||
return | ||
} | ||
} else if d.MediaType.IsIndex() { | ||
index, err := r.ImageIndex.ImageIndex(d.Digest) | ||
if err != nil { | ||
r.Done(err) | ||
return | ||
} | ||
if !yield(ref, index) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.