Skip to content

Commit e114f85

Browse files
committed
main: put ISO/disk code into proper functions
During the refactor the `manifestFromCobra()` helper became quite messy. This commit cleans this up by moving the parts that deal with disk images into their own helper and the part that deals with iso the same.
1 parent 7cbd50d commit e114f85

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

bib/cmd/bootc-image-builder/image.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ import (
2323
"github.com/sirupsen/logrus"
2424

2525
"github.com/osbuild/bootc-image-builder/bib/internal/distrodef"
26-
"github.com/osbuild/bootc-image-builder/bib/internal/imagetypes"
2726
)
2827

2928
type ManifestConfig struct {
3029
// OCI image path (without the transport, that is always docker://)
3130
Imgref string
3231
BuildImgref string
3332

34-
ImageTypes imagetypes.ImageTypes
35-
3633
// Build config
3734
Config *blueprint.Blueprint
3835

bib/cmd/bootc-image-builder/main.go

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/spf13/pflag"
2121
"golang.org/x/exp/slices"
2222

23+
"github.com/osbuild/blueprint/pkg/blueprint"
2324
repos "github.com/osbuild/images/data/repositories"
2425
"github.com/osbuild/images/pkg/arch"
2526
"github.com/osbuild/images/pkg/bib/blueprintload"
@@ -70,7 +71,7 @@ func inContainerOrUnknown() bool {
7071
return err == nil
7172
}
7273

73-
func makeManifest(c *ManifestConfig, solver *depsolvednf.Solver, cacheRoot string) (manifest.OSBuildManifest, map[string][]rpmmd.RepoConfig, error) {
74+
func makeISOManifest(c *ManifestConfig, solver *depsolvednf.Solver, cacheRoot string) (manifest.OSBuildManifest, map[string][]rpmmd.RepoConfig, error) {
7475
rng := createRand()
7576
mani, err := manifestForISO(c, rng)
7677
if err != nil {
@@ -217,55 +218,63 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
217218
pbar.SetPulseMsgf("Manifest generation step")
218219
pbar.Start()
219220

220-
// For now shortcut here and build ding "images" for anything
221-
// that is not the iso
222-
if !imageTypes.BuildsISO() {
223-
distro, err := bootc.NewBootcDistro(imgref)
224-
if err != nil {
225-
return nil, nil, err
226-
}
227-
if err := distro.SetBuildContainer(buildImgref); err != nil {
228-
return nil, nil, err
229-
}
230-
if err := distro.SetDefaultFs(rootFs); err != nil {
231-
return nil, nil, err
232-
}
233-
// XXX: consider target-arch
234-
archi, err := distro.GetArch(cntArch.String())
235-
if err != nil {
236-
return nil, nil, err
237-
}
238-
// XXX: how to generate for all image types
239-
imgType, err := archi.GetImageType(imgTypes[0])
240-
if err != nil {
241-
return nil, nil, err
242-
}
221+
// Note that we only need to pass a single imgType here into the manifest generation because:
222+
// 1. the bootc disk manifests contains exports for all supported image types
223+
// 2. the bootc iso is always a single build
224+
imgType := imgTypes[0]
225+
if imageTypes.BuildsISO() {
226+
return manifestFromCobraForISO(imgref, buildImgref, imgType, rootFs, rpmCacheRoot, config, useLibrepo, cntArch)
227+
}
228+
return manifestFromCobraForDisk(imgref, buildImgref, imgType, rootFs, rpmCacheRoot, config, useLibrepo, cntArch)
229+
}
243230

244-
var buf bytes.Buffer
245-
repos, err := reporegistry.New(nil, []fs.FS{repos.FS})
246-
if err != nil {
247-
return nil, nil, err
248-
}
249-
mg, err := manifestgen.New(repos, &manifestgen.Options{
250-
Output: &buf,
251-
// XXX: hack to skip repo loading for the bootc image.
252-
// We need to add a SkipRepositories or similar to
253-
// manifestgen instead to make this clean
254-
OverrideRepos: []rpmmd.RepoConfig{
255-
{
256-
BaseURLs: []string{"https://example.com/not-used"},
257-
},
231+
func manifestFromCobraForDisk(imgref, buildImgref, imgTypeStr, rootFs, rpmCacheRoot string, config *blueprint.Blueprint, useLibrepo bool, cntArch arch.Arch) ([]byte, *mTLSConfig, error) {
232+
distro, err := bootc.NewBootcDistro(imgref)
233+
if err != nil {
234+
return nil, nil, err
235+
}
236+
if err := distro.SetBuildContainer(buildImgref); err != nil {
237+
return nil, nil, err
238+
}
239+
if err := distro.SetDefaultFs(rootFs); err != nil {
240+
return nil, nil, err
241+
}
242+
archi, err := distro.GetArch(cntArch.String())
243+
if err != nil {
244+
return nil, nil, err
245+
}
246+
imgType, err := archi.GetImageType(imgTypeStr)
247+
if err != nil {
248+
return nil, nil, err
249+
}
250+
251+
var buf bytes.Buffer
252+
repos, err := reporegistry.New(nil, []fs.FS{repos.FS})
253+
if err != nil {
254+
return nil, nil, err
255+
}
256+
mg, err := manifestgen.New(repos, &manifestgen.Options{
257+
Output: &buf,
258+
// XXX: hack to skip repo loading for the bootc image.
259+
// We need to add a SkipRepositories or similar to
260+
// manifestgen instead to make this clean
261+
OverrideRepos: []rpmmd.RepoConfig{
262+
{
263+
BaseURLs: []string{"https://example.com/not-used"},
258264
},
259-
})
260-
if err != nil {
261-
return nil, nil, err
262-
}
263-
if err := mg.Generate(config, distro, imgType, archi, nil); err != nil {
264-
return nil, nil, err
265-
}
266-
return buf.Bytes(), nil, nil
265+
},
266+
})
267+
if err != nil {
268+
return nil, nil, err
269+
}
270+
if err := mg.Generate(config, distro, imgType, archi, nil); err != nil {
271+
return nil, nil, err
267272
}
273+
return buf.Bytes(), nil, nil
274+
275+
}
268276

277+
func manifestFromCobraForISO(imgref, buildImgref, imgTypeStr, rootFs, rpmCacheRoot string, config *blueprint.Blueprint, useLibrepo bool, cntArch arch.Arch) ([]byte, *mTLSConfig, error) {
269278
container, err := podman_container.New(imgref)
270279
if err != nil {
271280
return nil, nil, err
@@ -335,7 +344,6 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
335344
manifestConfig := &ManifestConfig{
336345
Architecture: cntArch,
337346
Config: config,
338-
ImageTypes: imageTypes,
339347
Imgref: imgref,
340348
BuildImgref: buildImgref,
341349
DistroDefPaths: distroDefPaths,
@@ -345,7 +353,7 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
345353
UseLibrepo: useLibrepo,
346354
}
347355

348-
manifest, repos, err := makeManifest(manifestConfig, solver, rpmCacheRoot)
356+
manifest, repos, err := makeISOManifest(manifestConfig, solver, rpmCacheRoot)
349357
if err != nil {
350358
return nil, nil, err
351359
}

0 commit comments

Comments
 (0)