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) {
|
func buildBuilderImage(ctx context.Context, variant, version, arch, builderTomlPath 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)
|
|
||||||
|
|
||||||
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
|
newBuilderImage := "ghcr.io/knative/builder-jammy-" + variant
|
||||||
newBuilderImageTagged := newBuilderImage + ":" + *release.Name + "-" + arch
|
newBuilderImageTagged := newBuilderImage + ":" + version + "-" + arch
|
||||||
|
|
||||||
ref, err := name.ParseReference(newBuilderImageTagged)
|
ref, err := name.ParseReference(newBuilderImageTagged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -121,12 +92,6 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
|
||||||
return newBuilderImage + "@" + desc.Digest.String(), nil
|
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)
|
builderConfig, _, err := builder.ReadConfig(builderTomlPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("cannot parse builder.toml: %w", err)
|
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{
|
createBuilderOpts := pack.CreateBuilderOptions{
|
||||||
RelativeBaseDir: buildDir,
|
RelativeBaseDir: filepath.Dir(builderTomlPath),
|
||||||
Targets: []dist.Target{
|
Targets: []dist.Target{
|
||||||
{
|
{
|
||||||
OS: "linux",
|
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.source": "https://github.com/knative/func",
|
||||||
"org.opencontainers.image.vendor": "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.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")
|
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{
|
remoteOpts := []remote.Option{
|
||||||
remote.WithAuthFromKeychain(DefaultKeychain),
|
remote.WithAuthFromKeychain(DefaultKeychain),
|
||||||
remote.WithContext(ctx),
|
remote.WithContext(ctx),
|
||||||
|
@ -305,7 +284,7 @@ func buildBuilderImageMultiArch(ctx context.Context, variant string) error {
|
||||||
|
|
||||||
var imgName string
|
var imgName string
|
||||||
|
|
||||||
imgName, err = buildBuilderImage(ctx, variant, arch)
|
imgName, err = buildBuilderImage(ctx, variant, release.GetName(), arch, builderTomlPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue