Skip to content

Commit

Permalink
feat!: Replace dynamic OCI arguments with generic options (#660)
Browse files Browse the repository at this point in the history
Approved-by: Alexander Jung <[email protected]>
  • Loading branch information
nderjung authored Jul 24, 2023
2 parents 20225fb + 1773573 commit 8ea7051
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 55 deletions.
5 changes: 3 additions & 2 deletions cmd/kraft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func New() *cobra.Command {
`, "`"),
Example: heredoc.Doc(`
# Package a project as an OCI archive and embed the target's KConfig.
$ kraft pkg --as oci --oci-tag unikraft.org/nginx:latest --with-kconfig`),
$ kraft pkg --as oci --name unikraft.org/nginx:latest --with-kconfig`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "pkg",
},
Expand Down Expand Up @@ -204,9 +204,10 @@ func (opts *Pkg) Run(cmd *cobra.Command, args []string) error {

popts := []packmanager.PackOption{
packmanager.PackArgs(cmdShellArgs...),
packmanager.PackInitrd(opts.Initrd),
packmanager.PackKConfig(opts.WithKConfig),
packmanager.PackName(opts.Name),
packmanager.PackOutput(opts.Output),
packmanager.PackInitrd(opts.Initrd),
}

if ukversion, ok := targ.KConfig().Get(unikraft.UK_FULLVERSION); ok {
Expand Down
21 changes: 0 additions & 21 deletions oci/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package oci

import (
"kraftkit.sh/cmdfactory"
"kraftkit.sh/packmanager"
)

Expand All @@ -19,24 +18,4 @@ func init() {
WithDefaultRegistries(),
WithDetectHandler(),
)

// Register additional command-line flags
cmdfactory.RegisterFlag(
"kraft pkg",
cmdfactory.StringVar(
&flagTag,
"oci-tag",
"",
"Set the OCI image tag.",
),
)
cmdfactory.RegisterFlag(
"kraft pkg",
cmdfactory.BoolVar(
&flagUseMediaTypes,
"oci-use-media-types",
false,
"Use media types as opposed to well-known paths (experimental).",
),
)
}
5 changes: 0 additions & 5 deletions oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
// You may not use this file except in compliance with the License.
package oci

var (
flagTag string
flagUseMediaTypes bool
)

const (
DefaultRegistry = "unikraft.org"
DefaultNamespace = "default"
Expand Down
40 changes: 13 additions & 27 deletions oci/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func NewPackageFromTarget(ctx context.Context, targ target.Target, opts ...packm
command: popts.Args(),
}

if flagTag != "" {
ocipack.ref, err = name.ParseReference(flagTag,
if popts.Name() != "" {
ocipack.ref, err = name.ParseReference(popts.Name(),
name.WithDefaultRegistry(DefaultRegistry),
)
} else {
Expand Down Expand Up @@ -147,32 +147,18 @@ func NewPackageFromTarget(ctx context.Context, targ target.Target, opts ...packm
"dest": WellKnownKernelPath,
}).Debug("oci: including kernel")

if flagUseMediaTypes {
blob, err := NewBlobFromFile(ctx,
MediaTypeImageKernel,
ocipack.Kernel(),
)
if err != nil {
return nil, err
}

if _, err := image.AddBlob(ctx, blob); err != nil {
return nil, err
}
} else {
layer, err := NewLayerFromFile(ctx,
ocispec.MediaTypeImageLayer,
ocipack.Kernel(),
WellKnownKernelPath,
WithLayerAnnotation(AnnotationKernelPath, WellKnownKernelPath),
)
if err != nil {
return nil, err
}
layer, err := NewLayerFromFile(ctx,
ocispec.MediaTypeImageLayer,
ocipack.Kernel(),
WellKnownKernelPath,
WithLayerAnnotation(AnnotationKernelPath, WellKnownKernelPath),
)
if err != nil {
return nil, err
}

if _, err := image.AddLayer(ctx, layer); err != nil {
return nil, err
}
if _, err := image.AddLayer(ctx, layer); err != nil {
return nil, err
}

if popts.Initrd() != "" {
Expand Down
13 changes: 13 additions & 0 deletions packmanager/pack_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type PackOptions struct {
kernelLibraryObjects bool
kernelSourceFiles bool
kernelVersion string
name string
output string
}

Expand Down Expand Up @@ -60,6 +61,11 @@ func (popts *PackOptions) KernelVersion() string {
return popts.kernelVersion
}

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

// Output returns the location of the package.
func (popts *PackOptions) Output() string {
return popts.output
Expand Down Expand Up @@ -127,6 +133,13 @@ func PackWithKernelVersion(version string) PackOption {
}
}

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

// PackOutput sets the location of the output artifact package.
func PackOutput(output string) PackOption {
return func(popts *PackOptions) {
Expand Down

0 comments on commit 8ea7051

Please sign in to comment.