mirror of https://github.com/knative/func.git
Refactor builder (#2779)
* Cleanup: remove unnecessary code Signed-off-by: Matej Vašek <mvasek@redhat.com> * Cleanup: moved code outside of a loop Signed-off-by: Matej Vašek <mvasek@redhat.com> --------- Signed-off-by: Matej Vašek <mvasek@redhat.com>
This commit is contained in:
parent
910e862804
commit
b67d8d4405
|
@ -77,39 +77,10 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
func buildBuilderImage(ctx context.Context, variant, arch string) (string, error) {
|
||||
buildDir, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot create temporary build directory: %w", err)
|
||||
}
|
||||
defer func(path string) {
|
||||
_ = os.RemoveAll(path)
|
||||
}(buildDir)
|
||||
func buildBuilderImage(ctx context.Context, variant, version, arch, builderTomlPath string) (string, error) {
|
||||
|
||||
ghClient := newGHClient(ctx)
|
||||
listOpts := &github.ListOptions{Page: 0, PerPage: 1}
|
||||
releases, ghResp, err := ghClient.Repositories.ListReleases(ctx, "paketo-buildpacks", "builder-jammy-"+variant, listOpts)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot get upstream builder release: %w", err)
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(ghResp.Body)
|
||||
|
||||
if len(releases) <= 0 {
|
||||
return "", fmt.Errorf("cannot get latest release")
|
||||
}
|
||||
|
||||
release := releases[0]
|
||||
|
||||
if release.Name == nil {
|
||||
return "", fmt.Errorf("the name of the release is not defined")
|
||||
}
|
||||
if release.TarballURL == nil {
|
||||
return "", fmt.Errorf("the tarball url of the release is not defined")
|
||||
}
|
||||
newBuilderImage := "ghcr.io/knative/builder-jammy-" + variant
|
||||
newBuilderImageTagged := newBuilderImage + ":" + *release.Name + "-" + arch
|
||||
newBuilderImageTagged := newBuilderImage + ":" + version + "-" + arch
|
||||
|
||||
ref, err := name.ParseReference(newBuilderImageTagged)
|
||||
if err != nil {
|
||||
|
@ -121,12 +92,6 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
|
|||
return newBuilderImage + "@" + desc.Digest.String(), nil
|
||||
}
|
||||
|
||||
builderTomlPath := filepath.Join(buildDir, "builder.toml")
|
||||
err = downloadBuilderToml(ctx, *release.TarballURL, builderTomlPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot download builder toml: %w", err)
|
||||
}
|
||||
|
||||
builderConfig, _, err := builder.ReadConfig(builderTomlPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot parse builder.toml: %w", err)
|
||||
|
@ -165,7 +130,7 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
|
|||
}
|
||||
|
||||
createBuilderOpts := pack.CreateBuilderOptions{
|
||||
RelativeBaseDir: buildDir,
|
||||
RelativeBaseDir: filepath.Dir(builderTomlPath),
|
||||
Targets: []dist.Target{
|
||||
{
|
||||
OS: "linux",
|
||||
|
@ -181,7 +146,7 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
|
|||
"org.opencontainers.image.source": "https://github.com/knative/func",
|
||||
"org.opencontainers.image.vendor": "https://github.com/knative/func",
|
||||
"org.opencontainers.image.url": "https://github.com/knative/func/pkgs/container/builder-jammy-" + variant,
|
||||
"org.opencontainers.image.version": *release.Name,
|
||||
"org.opencontainers.image.version": version,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -284,6 +249,20 @@ func buildBuilderImageMultiArch(ctx context.Context, variant string) error {
|
|||
return fmt.Errorf("the tarball url of the release is not defined")
|
||||
}
|
||||
|
||||
buildDir, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create temporary build directory: %w", err)
|
||||
}
|
||||
defer func(path string) {
|
||||
_ = os.RemoveAll(path)
|
||||
}(buildDir)
|
||||
|
||||
builderTomlPath := filepath.Join(buildDir, "builder.toml")
|
||||
err = downloadBuilderToml(ctx, *release.TarballURL, builderTomlPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot download builder toml: %w", err)
|
||||
}
|
||||
|
||||
remoteOpts := []remote.Option{
|
||||
remote.WithAuthFromKeychain(DefaultKeychain),
|
||||
remote.WithContext(ctx),
|
||||
|
@ -305,7 +284,7 @@ func buildBuilderImageMultiArch(ctx context.Context, variant string) error {
|
|||
|
||||
var imgName string
|
||||
|
||||
imgName, err = buildBuilderImage(ctx, variant, arch)
|
||||
imgName, err = buildBuilderImage(ctx, variant, release.GetName(), arch, builderTomlPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue