Skip to content

Commit

Permalink
feat: Introduce new readme element to Kraftfile
Browse files Browse the repository at this point in the history
Simultaneously propagate this information to the OCI manifest.

Signed-off-by: Alexander Jung <[email protected]>
  • Loading branch information
nderjung committed Feb 21, 2024
1 parent d75d3e1 commit 9a39700
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/cli/kraft/pkg/packager_kraftfile_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,14 @@ func (p *packagerKraftfileRuntime) Pack(ctx context.Context, opts *PkgOptions, a
return nil, err
}

if len(opts.Readme) == 0 {
opts.Readme = opts.Project.Readme()
} else {
if readmeBytes, err := os.ReadFile(opts.Readme); err == nil {
opts.Readme = string(readmeBytes)
}
}

var result []pack.Package
norender := log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY

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

if ukversion, ok := targ.KConfig().Get(unikraft.UK_FULLVERSION); ok {
Expand Down
10 changes: 10 additions & 0 deletions internal/cli/kraft/pkg/packager_kraftfile_unikraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package pkg
import (
"context"
"fmt"
"os"
"strings"

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

if len(opts.Readme) == 0 {
opts.Readme = opts.Project.Readme()
} else {
if readmeBytes, err := os.ReadFile(opts.Readme); err == nil {
opts.Readme = string(readmeBytes)
}
}

// When i > 0, we have already applied the merge strategy. Now, for all
// targets, we actually do wish to merge these because they are part of
// the same execution lifecycle.
Expand All @@ -136,6 +145,7 @@ func (p *packagerKraftfileUnikraft) Pack(ctx context.Context, opts *PkgOptions,
packmanager.PackKConfig(!opts.NoKConfig),
packmanager.PackName(opts.Name),
packmanager.PackOutput(opts.Output),
packmanager.PackReadme(opts.Readme),
)

if ukversion, ok := targ.KConfig().Get(unikraft.UK_FULLVERSION); ok {
Expand Down
1 change: 1 addition & 0 deletions internal/cli/kraft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type PkgOptions struct {
Platform string `local:"true" long:"plat" short:"p" usage:"Filter the creation of the package by platform of known targets"`
Project app.Application `noattribute:"true"`
Push bool `local:"true" long:"push" short:"P" usage:"Push the package on if successfully packaged"`
Readme string `local:"true" long:"readme" usage:"Verbatim text or path to a README file to include in the package"`
Rootfs string `local:"true" long:"rootfs" usage:"Specify a path to use as root file system (can be volume or initramfs)"`
Strategy packmanager.MergeStrategy `noattribute:"true"`
Target string `local:"true" long:"target" short:"t" usage:"Package a particular known target"`
Expand Down
1 change: 1 addition & 0 deletions oci/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
AnnotationURL = "org.unikraft.image.url"
AnnotationCreated = "org.unikraft.image.created"
AnnotationDescription = "org.unikraft.image.description"
AnnotationReadme = "org.unikraft.image.readme"
AnnotationKernelPath = "org.unikraft.kernel.image"
AnnotationKernelVersion = "org.unikraft.kernel.version"
AnnotationKernelInitrdPath = "org.unikraft.kernel.initrd"
Expand Down
8 changes: 8 additions & 0 deletions oci/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -72,6 +73,7 @@ type ociPackage struct {
kernelDbg string
initrd initrd.Initrd
command []string
readme string

original *ociPackage
}
Expand Down Expand Up @@ -100,6 +102,7 @@ func NewPackageFromTarget(ctx context.Context, targ target.Target, opts ...packm
kernel: targ.Kernel(),
kernelDbg: targ.KernelDbg(),
command: popts.Args(),
readme: popts.Readme(),
}

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

if len(ocipack.readme) > 0 {
encoded := base64.StdEncoding.EncodeToString([]byte(ocipack.readme))
ocipack.index.SetAnnotation(ctx, AnnotationReadme, encoded)
}

if _, err = ocipack.index.Save(ctx, ocipack.ref.String(), nil); err != nil {
return nil, fmt.Errorf("could not save index: %w", err)
}
Expand Down
13 changes: 13 additions & 0 deletions packmanager/pack_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PackOptions struct {
kernelSourceFiles bool
kernelVersion string
name string
readme string
output string
mergeStrategy MergeStrategy
}
Expand Down Expand Up @@ -81,6 +82,11 @@ func (popts *PackOptions) Name() string {
return popts.name
}

// Readme returns the readme of the package.
func (popts *PackOptions) Readme() string {
return popts.readme
}

// Output returns the location of the package.
func (popts *PackOptions) Output() string {
return popts.output
Expand Down Expand Up @@ -167,6 +173,13 @@ func PackName(name string) PackOption {
}
}

// PackReadme sets the readme of the package.
func PackReadme(readme string) PackOption {
return func(popts *PackOptions) {
popts.readme = readme
}
}

// PackOutput sets the location of the output artifact package.
func PackOutput(output string) PackOption {
return func(popts *PackOptions) {
Expand Down
2 changes: 2 additions & 0 deletions schema/v0.6.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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

"/^readme$/": { "type": "string" },

"/^outdir$/": { "type": "string" },

"/^template$/": {
Expand Down
8 changes: 8 additions & 0 deletions unikraft/app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import (
type Application interface {
component.Component

// Readme contains the application's README text.
Readme() string

// WorkingDir returns the path to the application's working directory
WorkingDir() string

Expand Down Expand Up @@ -150,6 +153,7 @@ type Application interface {

type application struct {
name string
readme string
version string
source string
path string
Expand All @@ -173,6 +177,10 @@ func (app application) Name() string {
return app.name
}

func (app application) Readme() string {
return app.readme
}

func (app application) String() string {
return app.name
}
Expand Down
8 changes: 8 additions & 0 deletions unikraft/app/application_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func WithName(name string) ApplicationOption {
}
}

// WithReadme sets the application's readme
func WithReadme(readme string) ApplicationOption {
return func(ac *application) error {
ac.readme = readme
return nil
}
}

// WithVersion sets the application version
func WithVersion(version string) ApplicationOption {
return func(ac *application) error {
Expand Down
12 changes: 12 additions & 0 deletions unikraft/app/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package app
import (
"context"
"fmt"
"os"
"strings"

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

if r, ok := iface["readme"]; ok {
app.readme, ok = r.(string)
if !ok {
return nil, errors.New("readme must be a string")
}

if readmeBytes, err := os.ReadFile(app.readme); err == nil {
app.readme = string(readmeBytes)
}
}

app.name = name
app.path = popts.workdir

Expand Down
1 change: 1 addition & 0 deletions unikraft/app/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func NewProjectFromOptions(ctx context.Context, opts ...ProjectOption) (Applicat

project, err := NewApplicationFromOptions(
WithName(projectName),
WithReadme(app.readme),
WithWorkingDir(popts.workdir),
WithFilename(app.filename),
WithOutDir(app.outDir),
Expand Down

0 comments on commit 9a39700

Please sign in to comment.