-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement distribution artifact pulling
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
- Loading branch information
1 parent
d698cca
commit a2fd0f2
Showing
15 changed files
with
319 additions
and
55 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
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,48 @@ | ||
// Copyright 2024 Stefan Prodan. | ||
// SPDX-License-Identifier: AGPL-3.0 | ||
|
||
package builder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/fluxcd/pkg/tar" | ||
"github.com/google/go-containerregistry/pkg/crane" | ||
) | ||
|
||
// PullArtifact downloads an artifact from an OCI repository and extracts the content | ||
// of the first tgz layer to the given destination directory. | ||
// It returns the digest of the artifact. | ||
func PullArtifact(ctx context.Context, ociURL, dstDir string) (string, error) { | ||
img, err := crane.Pull(strings.TrimPrefix(ociURL, "oci://"), crane.WithContext(ctx)) | ||
if err != nil { | ||
return "", fmt.Errorf("pulling artifact %s failed: %w", ociURL, err) | ||
} | ||
|
||
digest, err := img.Digest() | ||
if err != nil { | ||
return "", fmt.Errorf("parsing digest for artifact %s failed: %w", ociURL, err) | ||
} | ||
|
||
layers, err := img.Layers() | ||
if err != nil { | ||
return "", fmt.Errorf("listing layers in artifact %s failed: %w", ociURL, err) | ||
} | ||
|
||
if len(layers) < 1 { | ||
return "", fmt.Errorf("no layers found in artifact %s", ociURL) | ||
} | ||
|
||
blob, err := layers[0].Compressed() | ||
if err != nil { | ||
return "", fmt.Errorf("extracting layer from artifact %s failed: %w", ociURL, err) | ||
} | ||
|
||
if err = tar.Untar(blob, dstDir, tar.WithMaxUntarSize(-1)); err != nil { | ||
return "", fmt.Errorf("extracting layer from artifact %s failed: %w", ociURL, err) | ||
} | ||
|
||
return digest.String(), nil | ||
} |
Oops, something went wrong.