-
Notifications
You must be signed in to change notification settings - Fork 10
mkctr: add flag to save image/images to disk #25
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
Open
kradalby
wants to merge
1
commit into
main
Choose a base branch
from
kradalby/image-to-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
"log" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
@@ -27,6 +28,7 @@ import ( | |
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/daemon" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/layout" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"github.com/google/go-containerregistry/pkg/v1/tarball" | ||
|
@@ -79,6 +81,7 @@ type buildParams struct { | |
staticFiles map[string]string | ||
imageRefs []name.Tag | ||
publish bool | ||
outPath string | ||
ldflags string | ||
gotags string | ||
target string | ||
|
@@ -96,6 +99,7 @@ func main() { | |
ldflagsArg = flag.String("ldflags", "", "the --ldflags value to pass to go") | ||
gotags = flag.String("gotags", "", "the --tags value to pass to go") | ||
push = flag.Bool("push", false, "publish the image") | ||
outPath = flag.String("out", "", "writes image(s) to a given folder") | ||
target = flag.String("target", "", "build for a specific env (options: flyio, local)") | ||
verbose = flag.Bool("v", false, "verbose build output") | ||
annotations = flag.String("annotations", "", `OCI image annotations https://github.com/opencontainers/image-spec/blob/main/annotations.md. | ||
|
@@ -140,6 +144,7 @@ func main() { | |
staticFiles: staticFiles, | ||
imageRefs: refs, | ||
publish: *push, | ||
outPath: *outPath, | ||
ldflags: *ldflagsArg, | ||
gotags: *gotags, | ||
target: *target, | ||
|
@@ -198,6 +203,34 @@ func verifyPlatform(p v1.Platform, target string) error { | |
return nil | ||
} | ||
|
||
func createOutDirectory(path string) error { | ||
fi, err := os.Stat(path) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return fmt.Errorf("checking out path: %w", err) | ||
} | ||
} | ||
if fi != nil && !fi.IsDir() { | ||
return fmt.Errorf("out must be a directory: %s", path) | ||
} | ||
if err = os.MkdirAll(path, 0755); err != nil { | ||
return fmt.Errorf("creating out directory: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func writeImageToFile(img v1.Image, imgRef name.Reference, p string) error { | ||
err := createOutDirectory(p) | ||
if err != nil { | ||
return err | ||
} | ||
if err := tarball.WriteToFile(path.Join(p, "image.tar"), imgRef, img); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func fetchAndBuild(bp *buildParams) error { | ||
ctx := context.Background() | ||
logf := log.Printf | ||
|
@@ -243,25 +276,30 @@ func fetchAndBuild(bp *buildParams) error { | |
if err != nil { | ||
return err | ||
} | ||
if !bp.publish { | ||
logf("not pushing") | ||
return nil | ||
} | ||
|
||
img = mutate.Annotations(img, bp.annotations).(v1.Image) // OCI annotations | ||
switch { | ||
case bp.publish: | ||
Comment on lines
+280
to
+281
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new |
||
img = mutate.Annotations(img, bp.annotations).(v1.Image) // OCI annotations | ||
|
||
for _, r := range bp.imageRefs { | ||
if bp.target == "local" { | ||
if err := loadLocalImage(logf, r, img); err != nil { | ||
for _, r := range bp.imageRefs { | ||
if bp.target == "local" { | ||
if err := loadLocalImage(logf, r, img); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
logf("pushing to %v", r) | ||
if err := remote.Write(r, img, remoteOpts...); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
logf("pushing to %v", r) | ||
if err := remote.Write(r, img, remoteOpts...); err != nil { | ||
return err | ||
} | ||
return nil | ||
|
||
case bp.outPath != "": | ||
return writeImageToFile(img, bp.imageRefs[0], bp.outPath) | ||
} | ||
logf("not pushing or writing to file") | ||
|
||
return nil | ||
case types.OCIImageIndex, types.DockerManifestList: | ||
// baseRef is a multi-platform index, rest of the method handles this. | ||
|
@@ -336,23 +374,28 @@ func fetchAndBuild(bp *buildParams) error { | |
return err | ||
} | ||
logf("image digest: %v", d) | ||
if !bp.publish { | ||
logf("not pushing") | ||
return nil | ||
} | ||
|
||
for _, r := range bp.imageRefs { | ||
if bp.target == "local" { | ||
if err := loadLocalImage(logf, r, img); err != nil { | ||
switch { | ||
case bp.publish: | ||
for _, r := range bp.imageRefs { | ||
if bp.target == "local" { | ||
if err := loadLocalImage(logf, r, img); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
logf("pushing to %v", r) | ||
if err := remote.Write(r, img, remoteOpts...); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
logf("pushing to %v", r) | ||
if err := remote.Write(r, img, remoteOpts...); err != nil { | ||
return err | ||
} | ||
return nil | ||
|
||
case bp.outPath != "": | ||
return writeImageToFile(img, bp.imageRefs[0], bp.outPath) | ||
} | ||
logf("not pushing or writing to file") | ||
|
||
return nil | ||
} | ||
if bp.target == "local" { | ||
|
@@ -371,17 +414,30 @@ func fetchAndBuild(bp *buildParams) error { | |
idx = mutate.Annotations(idx, bp.annotations).(v1.ImageIndex) | ||
|
||
logf("index digest: %v", d) | ||
if !bp.publish { | ||
logf("not pushing") | ||
|
||
switch { | ||
case bp.publish: | ||
for _, r := range bp.imageRefs { | ||
logf("pushing to %v", r) | ||
if err := remote.WriteIndex(r, idx, remoteOpts...); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
for _, r := range bp.imageRefs { | ||
logf("pushing to %v", r) | ||
if err := remote.WriteIndex(r, idx, remoteOpts...); err != nil { | ||
case bp.outPath != "": | ||
err := createOutDirectory(bp.outPath) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := layout.Write(bp.outPath, idx); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
logf("not pushing or writing to file") | ||
|
||
return nil | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(drive-by) Are the stat and error and type checks necessary here?
os.MkdirAll
already handles the case where the directory exists, and correctly errors when the path exists but is not a directory. https://go.dev/play/p/2C56cHeDKBV