-
Notifications
You must be signed in to change notification settings - Fork 63
/
runner_package.go
193 lines (171 loc) · 5.26 KB
/
runner_package.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package run
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"k8s.io/apimachinery/pkg/util/uuid"
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/config"
"kraftkit.sh/initrd"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/paraprogress"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/target"
)
// runnerPackage is a runner for a package defined through a respective
// compatible package manager. Utilizing the PackageManger interface,
// determination of whether the provided positional argument represents a
// package. Typically this is used in the OCI usecase where a compatible image
// is referenced which contains a pre-built Unikraft unikernel. E.g.:
//
// $ kraft run unikraft.org/helloworld:latest
type runnerPackage struct {
packName string
args []string
pm packmanager.PackageManager
}
// String implements Runner.
func (runner *runnerPackage) String() string {
if runner.pm != nil {
return runner.pm.Format().String()
}
return "package"
}
// Runnable implements Runner.
func (runner *runnerPackage) Runnable(ctx context.Context, opts *Run, args ...string) (bool, error) {
if len(args) == 0 {
return false, fmt.Errorf("no arguments supplied")
}
runner.packName = args[0]
runner.args = args[1:]
if runner.pm == nil {
runner.pm = packmanager.G(ctx)
}
pm, compatible, err := runner.pm.IsCompatible(ctx,
runner.packName,
packmanager.WithArchitecture(opts.Architecture),
packmanager.WithPlatform(opts.platform.String()),
packmanager.WithUpdate(true),
)
if err == nil && compatible {
runner.pm = pm
return true, nil
} else if err != nil {
return false, err
}
return false, nil
}
// Prepare implements Runner.
func (runner *runnerPackage) Prepare(ctx context.Context, opts *Run, machine *machineapi.Machine, args ...string) error {
// First try the local cache of the catalog
packs, err := runner.pm.Catalog(ctx,
packmanager.WithTypes(unikraft.ComponentTypeApp),
packmanager.WithName(runner.packName),
packmanager.WithUpdate(false),
packmanager.WithPlatform(opts.platform.String()),
packmanager.WithArchitecture(opts.Architecture),
packmanager.WithUpdate(true),
)
if err != nil {
return err
}
if len(packs) > 1 {
return fmt.Errorf("could not determine what to run: too many options")
} else if len(packs) == 0 {
// Second, try accessing the remote catalog
packs, err = runner.pm.Catalog(ctx,
packmanager.WithTypes(unikraft.ComponentTypeApp),
packmanager.WithName(runner.packName),
packmanager.WithUpdate(true),
packmanager.WithPlatform(opts.platform.String()),
packmanager.WithArchitecture(opts.Architecture),
)
if err != nil {
return err
}
if len(packs) > 1 {
return fmt.Errorf("could not determine what to run: too many options")
} else if len(packs) == 0 {
return fmt.Errorf("not found: %s", runner.packName)
}
}
// Pre-emptively prepare the UID so that we can extract the kernel to the
// defined state directory.
machine.ObjectMeta.UID = uuid.NewUUID()
machine.Status.StateDir = filepath.Join(config.G[config.KraftKit](ctx).RuntimeDir, string(machine.ObjectMeta.UID))
if err := os.MkdirAll(machine.Status.StateDir, fs.ModeSetgid|0o775); err != nil {
return err
}
// Clean up the package directory if an error occurs before returning.
defer func() {
if err != nil {
os.RemoveAll(machine.Status.StateDir)
}
}()
paramodel, err := paraprogress.NewParaProgress(
ctx,
[]*paraprogress.Process{paraprogress.NewProcess(
fmt.Sprintf("pulling %s", runner.packName),
func(ctx context.Context, w func(progress float64)) error {
return packs[0].Pull(
ctx,
pack.WithPullProgressFunc(w),
pack.WithPullWorkdir(machine.Status.StateDir),
)
},
)},
paraprogress.IsParallel(false),
paraprogress.WithRenderer(
log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY,
),
paraprogress.WithFailFast(true),
)
if err != nil {
return err
}
if err := paramodel.Start(); err != nil {
return err
}
// Crucially, the catalog should return an interface that also implements
// target.Target. This demonstrates that the implementing package can
// resolve application kernels.
targ, ok := packs[0].(target.Target)
if !ok {
return fmt.Errorf("package does not convert to target")
}
machine.Spec.Architecture = targ.Architecture().Name()
machine.Spec.Platform = targ.Platform().Name()
machine.Spec.Kernel = fmt.Sprintf("%s://%s", runner.pm.Format(), runner.packName)
machine.Spec.ApplicationArgs = runner.args
// Set the path to the initramfs if present.
var ramfs initrd.Initrd
if opts.Rootfs == "" && targ.Initrd() != nil {
ramfs = targ.Initrd()
} else if len(opts.Rootfs) > 0 {
ramfs, err = initrd.New(ctx, opts.Rootfs)
if err != nil {
return err
}
}
if ramfs != nil {
machine.Status.InitrdPath, err = ramfs.Build(ctx)
if err != nil {
return err
}
}
// Use the symbolic debuggable kernel image?
if opts.WithKernelDbg {
machine.Status.KernelPath = targ.KernelDbg()
} else {
machine.Status.KernelPath = targ.Kernel()
}
return nil
}