Skip to content

Commit

Permalink
WIP: added client logic for manifest cli
Browse files Browse the repository at this point in the history
* added client logic for manifest cli

* added manifest push

Signed-off-by: WYGIN <[email protected]>

---------

Signed-off-by: WYGIN <[email protected]>
  • Loading branch information
SaiKiranSparkApps authored Nov 2, 2023
1 parent 87f67fd commit e7cd44b
Show file tree
Hide file tree
Showing 26 changed files with 685 additions and 87 deletions.
3 changes: 1 addition & 2 deletions internal/builder/testmocks/mock_lifecycle.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ type PackClient interface {
InspectImage(string, bool) (*client.ImageInfo, error)
Rebase(context.Context, client.RebaseOptions) error
CreateBuilder(context.Context, client.CreateBuilderOptions) error
CreateManifest(context.Context, string, []string) (imageID string, err error)
AnnotateManifest() error
AddManifest() error
DeleteManifest() error
RemoveManifest() error
PushManifest() error
InspectManifest() error
CreateManifest(ctx context.Context, name string, images []string, opts client.CreateManifestOptions) (imageID string, err error)
AnnotateManifest(ctx context.Context, name string, image string, opts client.ManifestAnnotateOptions) error
ExistsManifest(ctx context.Context, image string) error
AddManifest(ctx context.Context, index string, images string, opts client.ManifestAddOptions) (imageID string, err error)
DeleteManifest(ctx context.Context, name []string) error
RemoveManifest(ctx context.Context, name string, images []string) error
PushManifest(ctx context.Context, index string, opts client.PushManifestOptions) (imageID string, err error)
InspectManifest(ctx context.Context, name string, opts client.InspectManifestOptions) error
NewBuildpack(context.Context, client.NewBuildpackOptions) error
PackageBuildpack(ctx context.Context, opts client.PackageBuildpackOptions) error
PackageExtension(ctx context.Context, opts client.PackageBuildpackOptions) error
Expand Down
23 changes: 23 additions & 0 deletions internal/commands/manifest_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"github.com/spf13/cobra"

// "github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/logging"
)

Expand All @@ -26,6 +27,24 @@ func ManifestAdd(logger logging.Logger, pack PackClient) *cobra.Command {
When a manifest list exits locally, user can add a new image to the manifest list using this command`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
// imageIndex := args[0]
// manifests := args[1:]
// if err := validateManifestAddFlags(flags); err != nil {
// return err
// }

// imageID, err := pack.AddManifest(cmd.Context(), imageIndex, manifests, client.ManifestAddOptions{
// OS: flags.os,
// Arch: flags.arch,
// Variant: flags.variant,
// All: flags.all,
// })

// if err != nil {
// return err
// }

// logger.Infof(imageID)
return nil
}),
}
Expand All @@ -38,3 +57,7 @@ func ManifestAdd(logger logging.Logger, pack PackClient) *cobra.Command {
AddHelpFlag(cmd, "add")
return cmd
}

func validateManifestAddFlags(flags ManifestAddFlags) error {
return nil
}
10 changes: 8 additions & 2 deletions internal/commands/manifest_annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (

// ManifestAnnotateFlags define flags provided to the ManifestAnnotate
type ManifestAnnotateFlags struct {
os, arch, variant string
os, arch, variant, osVersion string
features, osFeatures, annotations []string
}

// ManifestAnnotate modifies a manifest list (Image index) and update the platform information for an image included in the manifest list.
func ManifestAnnotate(logger logging.Logger, pack PackClient) *cobra.Command {
var flags ManifestAnnotateFlags

cmd := &cobra.Command{
Use: "pack manifest annotate [OPTIONS] <manifest-list> <manifest> [flags]",
Use: "pack manifest annotate [OPTIONS] <manifest-list> <manifest> [<manifest>...] [flags]",
Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs),
Short: "manifest annotate modifies a manifest list (Image index) and update the platform information for an image included in the manifest list.",
Example: `pack manifest annotate cnbs/sample-package:hello-universe-multiarch \
Expand All @@ -32,6 +33,11 @@ func ManifestAnnotate(logger logging.Logger, pack PackClient) *cobra.Command {
cmd.Flags().StringVar(&flags.os, "os", "", "Set the architecture")
cmd.Flags().StringVar(&flags.arch, "arch", "", "Set the architecture")
cmd.Flags().StringVar(&flags.variant, "variant", "", "Set the architecture")
cmd.Flags().StringVar(&flags.osVersion, "os-version", "", "override the os `version` of the specified image")
cmd.Flags().StringSliceVar(&flags.features, "features", nil, "override the `features` of the specified image")
cmd.Flags().StringSliceVar(&flags.osFeatures, "os-features", nil, "override the os `features` of the specified image")
cmd.Flags().StringSliceVar(&flags.annotations, "annotations", nil, "set an `annotation` for the specified image")


AddHelpFlag(cmd, "annotate")
return cmd
Expand Down
69 changes: 53 additions & 16 deletions internal/commands/manifest_create.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"

"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/logging"
)

// ManifestCreateFlags define flags provided to the ManifestCreate
type ManifestCreateFlags struct {
format, registry string
insecure, publish bool
format, registry, os, arch string
insecure, publish, all, amend bool
}

// ManifestCreate creates an image-index/image-list for a multi-arch image
Expand All @@ -23,33 +26,67 @@ func ManifestCreate(logger logging.Logger, pack PackClient) *cobra.Command {
Example: `pack manifest create cnbs/sample-package:hello-multiarch-universe \
cnbs/sample-package:hello-universe \
cnbs/sample-package:hello-universe-windows`,
Long: `Create a manifest list or manifest index for the image to support muti architecture for the image, it create a new ManifestList or ManifestIndex with the given name/repoName and adds the list of Manifests to the newly created ManifestIndex or ManifestList
Long: `Create a manifest list or image index for the image to support muti architecture for the image, it create a new ManifestList or ImageIndex with the given name and adds the list of Manifests to the newly created ImageIndex or ManifestList
If the <manifest-list> already exists in the registry: pack will save a local copy of the remote manifest list,
If the <manifest-list> doestn't exist in a registry: pack will create a local representation of the manifest list that will only save on the remote registry if the user publish it`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
// manifestList := args[0]
// manifests := args[1:]
imageIndex := args[0]
manifests := args[1:]
cmdFlags := cmd.Flags()

if err := validateManifestCreateFlags(flags); err != nil {
return err
}

// if cmd.Flags().Changed("insecure") {
// flags.insecure = !flags.insecure
// }
if cmdFlags.Changed("insecure") {
flags.insecure = !flags.insecure
}

// if cmd.Flags().Changed("publish") {
// flags.publish = !flags.publish
// }
if cmdFlags.Changed("publish") {
flags.publish = !flags.publish
}

// id, err := pack.CreateManifest()
id, err := pack.CreateManifest(cmd.Context(), imageIndex, manifests, client.CreateManifestOptions{
Format: flags.format,
Registry: flags.registry,
Insecure: flags.insecure,
Publish: flags.publish,
})

if err != nil {
return err
}
logger.Infof("Successfully created ImageIndex/ManifestList with imageID: '%s'", id)

return nil
}),
}

cmd.Flags().StringVarP(&flags.format, "format", "f", "", "Format to save image index as ('OCI' or 'V2S2') (default 'v2s2')")
cmd.Flags().BoolVar(&flags.insecure, "insecure", false, "Allow publishing to insecure registry")
cmd.Flags().BoolVar(&flags.publish, "publish", false, "Publish to registry")
cmd.Flags().StringVarP(&flags.registry, "registry", "r", "", "Publish to registry")
cmdFlags := cmd.Flags()

cmdFlags.StringVarP(&flags.format, "format", "f", "v2s2", "Format to save image index as ('OCI' or 'V2S2') (default 'v2s2')")
cmdFlags.StringVarP(&flags.registry, "registry", "r", "", "Publish to registry")
cmdFlags.StringVar(&flags.os, "os", "", "If any of the specified images is a list/index, choose the one for `os`")
if err := cmdFlags.MarkHidden("os"); err != nil {
panic(fmt.Sprintf("error marking --os as hidden: %v", err))
}
cmdFlags.StringVar(&flags.arch, "arch", "", "If any of the specified images is a list/index, choose the one for `arch`")
if err := cmdFlags.MarkHidden("arch"); err != nil {
panic(fmt.Sprintf("error marking --arch as hidden: %v", err))
}
cmdFlags.BoolVar(&flags.insecure, "insecure", false, "Allow publishing to insecure registry")
if err := cmdFlags.MarkHidden("insecure"); err != nil {
panic(fmt.Sprintf("error marking insecure as hidden: %v", err))
}
cmdFlags.BoolVar(&flags.publish, "publish", false, "Publish to registry")
cmdFlags.BoolVar(&flags.all, "all", false, "Add all of the list's images if the images to add are lists/index")
cmdFlags.BoolVar(&flags.amend, "amend", false, "Modify an existing list/index if one with the desired name already exists")

AddHelpFlag(cmd, "create")
return cmd
}

func validateManifestCreateFlags(flags ManifestCreateFlags) error {
return nil
}
33 changes: 33 additions & 0 deletions internal/commands/manifest_exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"github.com/spf13/cobra"

"github.com/buildpacks/pack/pkg/logging"
)

// ManifestDeleteFlags define flags provided to the ManifestDelete
// type ManifestDeleteFlags struct {
// }

// ManifestExists checks if a manifest list exists in local storage
func ManifestExists(logger logging.Logger, pack PackClient) *cobra.Command {
// var flags ManifestDeleteFlags

cmd := &cobra.Command{
Use: "pack manifest exists [manifest-list]",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Short: "Delete one or more manifest lists from local storage",
Example: `pack manifest exists cnbs/sample-package:hello-multiarch-universe`,
Long: `Checks if a manifest list exists in local storage`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
if err := pack.ExistsManifest(cmd.Context(), args[0]); err != nil {
return err
}
return nil
}),
}

AddHelpFlag(cmd, "remove")
return cmd
}
1 change: 1 addition & 0 deletions internal/commands/manifest_exists_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package commands_test
24 changes: 23 additions & 1 deletion internal/commands/manifest_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package commands
import (
"github.com/spf13/cobra"

"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/logging"
)

// ManifestPushFlags define flags provided to the ManifestPush
type ManifestPushFlags struct {
format string
insecure, purge bool
insecure, purge, all, quite bool
}

// ManifestPush pushes a manifest list (Image index) to a registry.
Expand All @@ -25,14 +26,35 @@ func ManifestPush(logger logging.Logger, pack PackClient) *cobra.Command {
Once a manifest list is ready to be published into the registry, the push command can be used`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
if err := parseFalgs(flags); err != nil {
return err
}

imageID, err := pack.PushManifest(cmd.Context(), args[0], client.PushManifestOptions{
Format: flags.format,
Insecure: flags.insecure,
Purge: flags.purge,
})

if err != nil {
return err
}

logger.Infof(imageID)
return nil
}),
}

cmd.Flags().StringVarP(&flags.format, "format", "f", "", "Format to save image index as ('OCI' or 'V2S2') (default 'v2s2')")
cmd.Flags().BoolVar(&flags.insecure, "insecure", false, "Allow publishing to insecure registry")
cmd.Flags().BoolVar(&flags.purge, "purge", false, "Delete the manifest list or image index from local storage if pushing succeeds")
cmd.Flags().BoolVar(&flags.all, "all", false, "Also push the images in the list")
cmd.Flags().BoolVarP(&flags.quite, "quite", "q", false, "Also push the images in the list")

AddHelpFlag(cmd, "push")
return cmd
}

func parseFalgs(flags ManifestPushFlags) error {
return nil
}
3 changes: 3 additions & 0 deletions internal/commands/manifest_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func ManifestDelete(logger logging.Logger, pack PackClient) *cobra.Command {
When a manifest list exits locally, users can remove existing images from a manifest list`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
if err := pack.DeleteManifest(cmd.Context(), args); err != nil {
return err
}
return nil
}),
}
Expand Down
3 changes: 3 additions & 0 deletions internal/commands/manifest_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func ManifestRemove(logger logging.Logger, pack PackClient) *cobra.Command {
Sometimes users can just experiment with the feature locally and they want to discard all the local information created by pack. 'rm' command just delete the local manifest list`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
if err := pack.RemoveManifest(cmd.Context(), args[0], args[1:]); err != nil {
return err
}
return nil
}),
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e7cd44b

Please sign in to comment.