Skip to content

Commit d9ecc0e

Browse files
authored
chore: rework image-builder CLI (#151)
* chore: rework image-builder CLI - split into reusable packages - split image build stages into reusable actions --------- Signed-off-by: Angelos Kolaitis <neoaggelos@gmail.com>
1 parent 82f06f7 commit d9ecc0e

37 files changed

+792
-831
lines changed

cmd/exp/image-builder/cmd_haproxy.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

cmd/exp/image-builder/cmd_kubeadm.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

cmd/exp/image-builder/cmd_root.go

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package action
2+
3+
import "context"
4+
5+
type Action func(context.Context) error
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package action
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
// Chain is a meta Action that runs multiple Action one after the other.
9+
func Chain(actions ...Action) Action {
10+
return func(ctx context.Context) error {
11+
for idx, action := range actions {
12+
if err := action(ctx); err != nil {
13+
return fmt.Errorf("action %d/%d failed: %w", idx, len(actions), err)
14+
}
15+
}
16+
return nil
17+
}
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package action
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
incus "github.com/lxc/incus/v6/client"
9+
"github.com/lxc/incus/v6/shared/ioprogress"
10+
"sigs.k8s.io/controller-runtime/pkg/log"
11+
12+
"github.com/lxc/cluster-api-provider-incus/internal/lxc"
13+
)
14+
15+
// ExportImage is an Action that downloads a unified image tarball and saves to a local file.
16+
func ExportImage(lxcClient *lxc.Client, imageAliasName string, outputFile string) Action {
17+
return func(ctx context.Context) (rerr error) {
18+
image, _, err := lxcClient.GetImageAlias(imageAliasName)
19+
if err != nil {
20+
return fmt.Errorf("failed to find image for alias %q: %w", imageAliasName, err)
21+
}
22+
23+
output, err := os.Create(outputFile)
24+
if err != nil {
25+
return fmt.Errorf("failed to create output file: %w", err)
26+
}
27+
defer func() {
28+
_ = output.Close()
29+
if rerr != nil {
30+
_ = os.Remove(outputFile)
31+
}
32+
}()
33+
34+
log.FromContext(ctx).V(1).Info("Downloading image")
35+
resp, err := lxcClient.GetImageFile(image.Target, incus.ImageFileRequest{
36+
MetaFile: output,
37+
ProgressHandler: func(progress ioprogress.ProgressData) {
38+
log.FromContext(ctx).V(2).WithValues("progress", progress.Text).Info("Downloading image")
39+
},
40+
})
41+
if err != nil {
42+
return fmt.Errorf("failed to download image: %w", err)
43+
}
44+
45+
log.FromContext(ctx).V(1).WithValues("image", resp).Info("Downloaded image")
46+
if err := output.Truncate(resp.MetaSize); err != nil {
47+
return fmt.Errorf("failed to truncate output file: %w", err)
48+
}
49+
50+
return nil
51+
}
52+
}

0 commit comments

Comments
 (0)