-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding targets and path flags when packaging an extension
- Loading branch information
Showing
4 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package commands | |
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
|
@@ -11,6 +12,7 @@ import ( | |
"github.com/buildpacks/pack/internal/config" | ||
"github.com/buildpacks/pack/internal/style" | ||
"github.com/buildpacks/pack/pkg/client" | ||
"github.com/buildpacks/pack/pkg/dist" | ||
"github.com/buildpacks/pack/pkg/image" | ||
"github.com/buildpacks/pack/pkg/logging" | ||
) | ||
|
@@ -19,8 +21,10 @@ import ( | |
type ExtensionPackageFlags struct { | ||
PackageTomlPath string | ||
Format string | ||
Targets []string | ||
Publish bool | ||
Policy string | ||
Path string | ||
} | ||
|
||
// ExtensionPackager packages extensions | ||
|
@@ -32,9 +36,15 @@ type ExtensionPackager interface { | |
func ExtensionPackage(logger logging.Logger, cfg config.Config, packager ExtensionPackager, packageConfigReader PackageConfigReader) *cobra.Command { | ||
var flags ExtensionPackageFlags | ||
cmd := &cobra.Command{ | ||
Use: "package <name> --config <config-path>", | ||
Short: "Package an extension in OCI format", | ||
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), | ||
Use: "package <name> --config <config-path>", | ||
Short: "Package an extension in OCI format", | ||
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), | ||
Example: "pack extension package /output/file.cnb --path /extracted/from/tgz/folder --format file\n pack extension package registy/image-name --path /extracted/from/tgz/folder --format image --publish", | ||
Check failure on line 42 in internal/commands/extension_package.go
|
||
Long: "extension package allows users to package (an) extension(s) into OCI format, which can then to be hosted in " + | ||
"image repositories or persisted on disk as a '.cnb' file." + | ||
"Packaged extensions can be used as inputs to `pack build` (using the `--extension` flag), " + | ||
"and they can be included in the configs used in `pack builder create` and `pack extension package`. For more " + | ||
"on how to package an extension, see: https://buildpacks.io/docs/buildpack-author-guide/package-a-buildpack/.", | ||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||
if err := validateExtensionPackageFlags(&flags); err != nil { | ||
return err | ||
|
@@ -51,6 +61,13 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi | |
} | ||
|
||
exPackageCfg := pubbldpkg.DefaultExtensionConfig() | ||
var exPath string | ||
if flags.Path != "" { | ||
if exPath, err = filepath.Abs(flags.Path); err != nil { | ||
return errors.Wrap(err, "resolving extension path") | ||
} | ||
exPackageCfg.Extension.URI = exPath | ||
} | ||
relativeBaseDir := "" | ||
if flags.PackageTomlPath != "" { | ||
exPackageCfg, err = packageConfigReader.Read(flags.PackageTomlPath) | ||
|
@@ -74,13 +91,36 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi | |
} | ||
} | ||
|
||
targets, err := processExtensionPackageTargets(flags.Path, packageConfigReader, exPackageCfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
daemon := !flags.Publish && flags.Format == "" | ||
multiArchCfg, err := processMultiArchitectureConfig(logger, flags.Targets, targets, daemon) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(multiArchCfg.Targets()) == 0 { | ||
logger.Infof("Pro tip: use --target flag OR [[targets]] in buildpack.toml to specify the desired platform (os/arch/variant); using os %s", style.Symbol(exPackageCfg.Platform.OS)) | ||
} else { | ||
// FIXME: Check if we can copy the config files during layers creation. | ||
filesToClean, err := multiArchCfg.CopyConfigFiles(exPath, "extension") | ||
if err != nil { | ||
return err | ||
} | ||
defer clean(filesToClean) | ||
} | ||
|
||
if err := packager.PackageExtension(cmd.Context(), client.PackageBuildpackOptions{ | ||
RelativeBaseDir: relativeBaseDir, | ||
Name: name, | ||
Format: flags.Format, | ||
Config: exPackageCfg, | ||
Publish: flags.Publish, | ||
PullPolicy: pullPolicy, | ||
Targets: multiArchCfg.Targets(), | ||
}); err != nil { | ||
return err | ||
} | ||
|
@@ -104,6 +144,14 @@ func ExtensionPackage(logger logging.Logger, cfg config.Config, packager Extensi | |
cmd.Flags().StringVarP(&flags.Format, "format", "f", "", `Format to save package as ("image" or "file")`) | ||
cmd.Flags().BoolVar(&flags.Publish, "publish", false, `Publish the extension directly to the container registry specified in <name>, instead of the daemon (applies to "--format=image" only).`) | ||
cmd.Flags().StringVar(&flags.Policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always") | ||
cmd.Flags().StringVarP(&flags.Path, "path", "p", "", "Path to the Extension that needs to be packaged") | ||
cmd.Flags().StringSliceVarP(&flags.Targets, "target", "t", nil, | ||
`Target platforms to build for. | ||
Targets should be in the format '[os][/arch][/variant]:[distroname@osversion@anotherversion];[distroname@osversion]'. | ||
- To specify two different architectures: '--target "linux/amd64" --target "linux/arm64"' | ||
- To specify the distribution version: '--target "linux/arm/v6:[email protected]"' | ||
- To specify multiple distribution versions: '--target "linux/arm/v6:[email protected]" --target "linux/arm/v6:[email protected]"' | ||
`) | ||
AddHelpFlag(cmd, "package") | ||
return cmd | ||
} | ||
|
@@ -114,3 +162,20 @@ func validateExtensionPackageFlags(p *ExtensionPackageFlags) error { | |
} | ||
return nil | ||
} | ||
|
||
// processExtensionPackageTargets returns the list of targets defined on the extension.toml | ||
func processExtensionPackageTargets(path string, packageConfigReader PackageConfigReader, bpPackageCfg pubbldpkg.Config) ([]dist.Target, error) { | ||
var targets []dist.Target | ||
|
||
// Read targets from buildpack.toml | ||
pathToExtensionToml := filepath.Join(path, "extension.toml") | ||
if _, err := os.Stat(pathToExtensionToml); err == nil { | ||
buildpackCfg, err := packageConfigReader.ReadBuildpackDescriptor(pathToExtensionToml) | ||
if err != nil { | ||
return nil, err | ||
} | ||
targets = buildpackCfg.Targets() | ||
} | ||
|
||
return targets, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters