Skip to content

Commit 3aa0e1a

Browse files
committed
feat: Introduce new readme element to Kraftfile
Simultaneously propagate this information to the OCI manifest. Signed-off-by: Alexander Jung <[email protected]>
1 parent d75d3e1 commit 3aa0e1a

File tree

11 files changed

+73
-0
lines changed

11 files changed

+73
-0
lines changed

internal/cli/kraft/pkg/packager_kraftfile_runtime.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ func (p *packagerKraftfileRuntime) Pack(ctx context.Context, opts *PkgOptions, a
332332
return nil, err
333333
}
334334

335+
if len(opts.Readme) == 0 {
336+
opts.Readme = opts.Project.Readme()
337+
} else {
338+
if readmeBytes, err := os.ReadFile(opts.Readme); err == nil {
339+
opts.Readme = string(readmeBytes)
340+
}
341+
}
342+
335343
var result []pack.Package
336344
norender := log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY
337345

@@ -352,6 +360,7 @@ func (p *packagerKraftfileRuntime) Pack(ctx context.Context, opts *PkgOptions, a
352360
packmanager.PackKConfig(!opts.NoKConfig),
353361
packmanager.PackName(opts.Name),
354362
packmanager.PackOutput(opts.Output),
363+
packmanager.PackReadme(opts.Readme),
355364
)
356365

357366
if ukversion, ok := targ.KConfig().Get(unikraft.UK_FULLVERSION); ok {

internal/cli/kraft/pkg/packager_kraftfile_unikraft.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package pkg
88
import (
99
"context"
1010
"fmt"
11+
"os"
1112
"strings"
1213

1314
"github.com/mattn/go-shellwords"
@@ -117,6 +118,14 @@ func (p *packagerKraftfileUnikraft) Pack(ctx context.Context, opts *PkgOptions,
117118
return nil, err
118119
}
119120

121+
if len(opts.Readme) == 0 {
122+
opts.Readme = opts.Project.Readme()
123+
} else {
124+
if readmeBytes, err := os.ReadFile(opts.Readme); err == nil {
125+
opts.Readme = string(readmeBytes)
126+
}
127+
}
128+
120129
// When i > 0, we have already applied the merge strategy. Now, for all
121130
// targets, we actually do wish to merge these because they are part of
122131
// the same execution lifecycle.
@@ -136,6 +145,7 @@ func (p *packagerKraftfileUnikraft) Pack(ctx context.Context, opts *PkgOptions,
136145
packmanager.PackKConfig(!opts.NoKConfig),
137146
packmanager.PackName(opts.Name),
138147
packmanager.PackOutput(opts.Output),
148+
packmanager.PackReadme(opts.Readme),
139149
)
140150

141151
if ukversion, ok := targ.KConfig().Get(unikraft.UK_FULLVERSION); ok {

internal/cli/kraft/pkg/pkg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type PkgOptions struct {
4848
Platform string `local:"true" long:"plat" short:"p" usage:"Filter the creation of the package by platform of known targets"`
4949
Project app.Application `noattribute:"true"`
5050
Push bool `local:"true" long:"push" short:"P" usage:"Push the package on if successfully packaged"`
51+
Readme string `local:"true" long:"readme" usage:"Verbatim text or path to a README file to include in the package"`
5152
Rootfs string `local:"true" long:"rootfs" usage:"Specify a path to use as root file system (can be volume or initramfs)"`
5253
Strategy packmanager.MergeStrategy `noattribute:"true"`
5354
Target string `local:"true" long:"target" short:"t" usage:"Package a particular known target"`

oci/annotations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const (
1111
AnnotationURL = "org.unikraft.image.url"
1212
AnnotationCreated = "org.unikraft.image.created"
1313
AnnotationDescription = "org.unikraft.image.description"
14+
AnnotationReadme = "org.unikraft.image.readme"
1415
AnnotationKernelPath = "org.unikraft.kernel.image"
1516
AnnotationKernelVersion = "org.unikraft.kernel.version"
1617
AnnotationKernelInitrdPath = "org.unikraft.kernel.initrd"

oci/pack.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"context"
1010
"crypto/tls"
11+
"encoding/base64"
1112
"encoding/json"
1213
"errors"
1314
"fmt"
@@ -72,6 +73,7 @@ type ociPackage struct {
7273
kernelDbg string
7374
initrd initrd.Initrd
7475
command []string
76+
readme string
7577

7678
original *ociPackage
7779
}
@@ -100,6 +102,7 @@ func NewPackageFromTarget(ctx context.Context, targ target.Target, opts ...packm
100102
kernel: targ.Kernel(),
101103
kernelDbg: targ.KernelDbg(),
102104
command: popts.Args(),
105+
readme: popts.Readme(),
103106
}
104107

105108
// It is possible that `NewPackageFromTarget` is called with an existing
@@ -380,6 +383,11 @@ func NewPackageFromTarget(ctx context.Context, targ target.Target, opts ...packm
380383
return nil, fmt.Errorf("could not add manifest to index: %w", err)
381384
}
382385

386+
if len(ocipack.readme) > 0 {
387+
encoded := base64.StdEncoding.EncodeToString([]byte(ocipack.readme))
388+
ocipack.manifest.SetAnnotation(ctx, AnnotationReadme, encoded)
389+
}
390+
383391
if _, err = ocipack.index.Save(ctx, ocipack.ref.String(), nil); err != nil {
384392
return nil, fmt.Errorf("could not save index: %w", err)
385393
}

packmanager/pack_options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type PackOptions struct {
1717
kernelSourceFiles bool
1818
kernelVersion string
1919
name string
20+
readme string
2021
output string
2122
mergeStrategy MergeStrategy
2223
}
@@ -81,6 +82,11 @@ func (popts *PackOptions) Name() string {
8182
return popts.name
8283
}
8384

85+
// Readme returns the readme of the package.
86+
func (popts *PackOptions) Readme() string {
87+
return popts.readme
88+
}
89+
8490
// Output returns the location of the package.
8591
func (popts *PackOptions) Output() string {
8692
return popts.output
@@ -167,6 +173,13 @@ func PackName(name string) PackOption {
167173
}
168174
}
169175

176+
// PackReadme sets the readme of the package.
177+
func PackReadme(readme string) PackOption {
178+
return func(popts *PackOptions) {
179+
popts.readme = readme
180+
}
181+
}
182+
170183
// PackOutput sets the location of the output artifact package.
171184
func PackOutput(output string) PackOption {
172185
return func(popts *PackOptions) {

schema/v0.6.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
"/^name$/": { "type": "string" },
1313

14+
"/^readme$/": { "type": "string" },
15+
1416
"/^outdir$/": { "type": "string" },
1517

1618
"/^template$/": {

unikraft/app/application.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import (
3939
type Application interface {
4040
component.Component
4141

42+
// Readme contains the application's README text.
43+
Readme() string
44+
4245
// WorkingDir returns the path to the application's working directory
4346
WorkingDir() string
4447

@@ -150,6 +153,7 @@ type Application interface {
150153

151154
type application struct {
152155
name string
156+
readme string
153157
version string
154158
source string
155159
path string
@@ -173,6 +177,10 @@ func (app application) Name() string {
173177
return app.name
174178
}
175179

180+
func (app application) Readme() string {
181+
return app.readme
182+
}
183+
176184
func (app application) String() string {
177185
return app.name
178186
}

unikraft/app/application_options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ func WithName(name string) ApplicationOption {
7070
}
7171
}
7272

73+
// WithReadme sets the application's readme
74+
func WithReadme(readme string) ApplicationOption {
75+
return func(ac *application) error {
76+
ac.readme = readme
77+
return nil
78+
}
79+
}
80+
7381
// WithVersion sets the application version
7482
func WithVersion(version string) ApplicationOption {
7583
return func(ac *application) error {

unikraft/app/loader.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package app
2020
import (
2121
"context"
2222
"fmt"
23+
"os"
2324
"strings"
2425

2526
interp "github.com/compose-spec/compose-go/interpolation"
@@ -43,6 +44,17 @@ func NewApplicationFromInterface(ctx context.Context, iface map[string]interface
4344
}
4445
}
4546

47+
if r, ok := iface["readme"]; ok {
48+
app.readme, ok = r.(string)
49+
if !ok {
50+
return nil, errors.New("readme must be a string")
51+
}
52+
53+
if readmeBytes, err := os.ReadFile(app.readme); err == nil {
54+
app.readme = string(readmeBytes)
55+
}
56+
}
57+
4658
app.name = name
4759
app.path = popts.workdir
4860

0 commit comments

Comments
 (0)