From b7353a740d806d0e3654098e9ef4817af02ae8f1 Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Mon, 8 Jul 2019 20:20:47 +0300 Subject: [PATCH 1/2] Add `ignite inspect ` to get information about an Object --- cmd/ignite/cmd/inspect.go | 44 ++++++++++++++++++++++++++++ cmd/ignite/cmd/root.go | 1 + cmd/ignite/run/inspect.go | 61 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 cmd/ignite/cmd/inspect.go create mode 100644 cmd/ignite/run/inspect.go diff --git a/cmd/ignite/cmd/inspect.go b/cmd/ignite/cmd/inspect.go new file mode 100644 index 000000000..22bb7f9ae --- /dev/null +++ b/cmd/ignite/cmd/inspect.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "github.com/lithammer/dedent" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/weaveworks/ignite/cmd/ignite/run" + "github.com/weaveworks/ignite/pkg/errutils" + "io" +) + +// NewCmdInspect inspects an Ignite Object +func NewCmdInspect(out io.Writer) *cobra.Command { + i := &run.InspectFlags{} + + cmd := &cobra.Command{ + Use: "inspect ", + Short: "Inspect an Ignite Object", + Long: dedent.Dedent(` + Retrieve information about the given object of the given kind. + The kind can be "image", "kernel" or "vm". The object is matched + by prefix based on its ID and name. Outputs JSON by default, can + be overridden to YAML with the yaml flag (-y, --yaml). + `), + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + errutils.Check(func() error { + io, err := i.NewInspectOptions(args[0], args[1]) + if err != nil { + return err + } + + return run.Inspect(io) + }()) + }, + } + + addInspectFlags(cmd.Flags(), i) + return cmd +} + +func addInspectFlags(fs *pflag.FlagSet, i *run.InspectFlags) { + fs.BoolVarP(&i.YAMLOutput, "yaml", "y", false, "Output the object in YAML format instead of JSON") +} diff --git a/cmd/ignite/cmd/root.go b/cmd/ignite/cmd/root.go index 6e423edc4..15bbbe92f 100644 --- a/cmd/ignite/cmd/root.go +++ b/cmd/ignite/cmd/root.go @@ -82,6 +82,7 @@ func NewIgniteCommand(in io.Reader, out, err io.Writer) *cobra.Command { root.AddCommand(NewCmdKill(os.Stdout)) root.AddCommand(NewCmdLogs(os.Stdout)) root.AddCommand(NewCmdGitOps(os.Stdout)) + root.AddCommand(NewCmdInspect(os.Stdout)) root.AddCommand(NewCmdPs(os.Stdout)) root.AddCommand(NewCmdRm(os.Stdout)) root.AddCommand(NewCmdRmi(os.Stdout)) diff --git a/cmd/ignite/run/inspect.go b/cmd/ignite/run/inspect.go new file mode 100644 index 000000000..89a581162 --- /dev/null +++ b/cmd/ignite/run/inspect.go @@ -0,0 +1,61 @@ +package run + +import ( + "bytes" + "fmt" + "github.com/weaveworks/ignite/pkg/apis/ignite/scheme" + "github.com/weaveworks/ignite/pkg/client" + "github.com/weaveworks/ignite/pkg/filter" + + meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1" +) + +type InspectFlags struct { + YAMLOutput bool +} + +type inspectOptions struct { + *InspectFlags + object meta.Object +} + +func (i *InspectFlags) NewInspectOptions(k, objectMatch string) (*inspectOptions, error) { + var err error + var kind meta.Kind + io := &inspectOptions{InspectFlags: i} + + switch k { + case meta.KindImage.Lower(): + kind = meta.KindImage + case meta.KindKernel.Lower(): + kind = meta.KindKernel + case meta.KindVM.Lower(): + kind = meta.KindVM + default: + return nil, fmt.Errorf("unrecognized kind: %s", k) + } + + if io.object, err = client.Dynamic(kind).Find(filter.NewIDNameFilter(objectMatch)); err != nil { + return nil, err + } + + return io, nil +} + +func Inspect(io *inspectOptions) error { + // Choose the encoder + encodeFunc := scheme.Serializer.EncodeJSON + if io.YAMLOutput { + encodeFunc = scheme.Serializer.EncodeYAML + } + + // Encode the object with the selected encoder + b, err := encodeFunc(io.object) + if err != nil { + return err + } + + // Print the encoded object + fmt.Println(string(bytes.TrimSpace(b))) + return nil +} From c6622cf4396201283a9ed27a2f3f0d8521a2d101 Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Mon, 8 Jul 2019 20:40:53 +0300 Subject: [PATCH 2/2] Generate `inspect` docs, replace --yaml flag with generic --output flag Run `make tidy` as well which fixes the import ordering. --- api/ignite.md | 53 +++++++++++++++++++++++++--- cmd/ignite/cmd/inspect.go | 7 ++-- cmd/ignite/run/inspect.go | 25 ++++++++----- docs/cli/ignite.md | 1 + docs/cli/ignite_inspect.md | 34 ++++++++++++++++++ pkg/apis/ignite/v1alpha1/defaults.go | 3 +- pkg/apis/ignite/v1alpha1/helpers.go | 2 +- pkg/storage/cache.go | 3 +- 8 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 docs/cli/ignite_inspect.md diff --git a/api/ignite.md b/api/ignite.md index 5f110286c..0e22ebf99 100644 --- a/api/ignite.md +++ b/api/ignite.md @@ -17,8 +17,11 @@ ## Index * [Constants](#pkg-constants) * [Variables](#pkg-variables) +* [func SetDefaults_Image(obj *Image)](#SetDefaults_Image) +* [func SetDefaults_Kernel(obj *Kernel)](#SetDefaults_Kernel) * [func SetDefaults_OCIImageClaim(obj *OCIImageClaim)](#SetDefaults_OCIImageClaim) * [func SetDefaults_PoolSpec(obj *PoolSpec)](#SetDefaults_PoolSpec) +* [func SetDefaults_VM(obj *VM)](#SetDefaults_VM) * [func SetDefaults_VMKernelSpec(obj *VMKernelSpec)](#SetDefaults_VMKernelSpec) * [func SetDefaults_VMSpec(obj *VMSpec)](#SetDefaults_VMSpec) * [func SetDefaults_VMStatus(obj *VMStatus)](#SetDefaults_VMStatus) @@ -42,6 +45,8 @@ * [type PoolStatus](#PoolStatus) * [type SSH](#SSH) * [type VM](#VM) + * [func (vm *VM) SetImage(image *Image)](#VM.SetImage) + * [func (vm *VM) SetKernel(kernel *Kernel)](#VM.SetKernel) * [type VMImageSource](#VMImageSource) * [type VMImageSpec](#VMImageSpec) * [type VMKernelSpec](#VMKernelSpec) @@ -93,31 +98,51 @@ SchemeGroupVersion is group version used to register these objects -## func [SetDefaults_OCIImageClaim](/pkg/apis/ignite/v1alpha1/defaults.go?s=263:313#L13) +## func [SetDefaults_Image](/pkg/apis/ignite/v1alpha1/defaults.go?s=2025:2059#L83) +``` go +func SetDefaults_Image(obj *Image) +``` + + +## func [SetDefaults_Kernel](/pkg/apis/ignite/v1alpha1/defaults.go?s=2083:2119#L87) +``` go +func SetDefaults_Kernel(obj *Kernel) +``` + + +## func [SetDefaults_OCIImageClaim](/pkg/apis/ignite/v1alpha1/defaults.go?s=274:324#L14) ``` go func SetDefaults_OCIImageClaim(obj *OCIImageClaim) ``` -## func [SetDefaults_PoolSpec](/pkg/apis/ignite/v1alpha1/defaults.go?s=353:393#L17) +## func [SetDefaults_PoolSpec](/pkg/apis/ignite/v1alpha1/defaults.go?s=364:404#L18) ``` go func SetDefaults_PoolSpec(obj *PoolSpec) ``` -## func [SetDefaults_VMKernelSpec](/pkg/apis/ignite/v1alpha1/defaults.go?s=1315:1363#L57) +## func [SetDefaults_VM](/pkg/apis/ignite/v1alpha1/defaults.go?s=1973:2001#L79) +``` go +func SetDefaults_VM(obj *VM) +``` +TODO: Temporary hacks to populate TypeMeta until we get the generator working + + + +## func [SetDefaults_VMKernelSpec](/pkg/apis/ignite/v1alpha1/defaults.go?s=1326:1374#L58) ``` go func SetDefaults_VMKernelSpec(obj *VMKernelSpec) ``` -## func [SetDefaults_VMSpec](/pkg/apis/ignite/v1alpha1/defaults.go?s=919:955#L39) +## func [SetDefaults_VMSpec](/pkg/apis/ignite/v1alpha1/defaults.go?s=930:966#L40) ``` go func SetDefaults_VMSpec(obj *VMSpec) ``` -## func [SetDefaults_VMStatus](/pkg/apis/ignite/v1alpha1/defaults.go?s=1449:1489#L63) +## func [SetDefaults_VMStatus](/pkg/apis/ignite/v1alpha1/defaults.go?s=1460:1500#L64) ``` go func SetDefaults_VMStatus(obj *VMStatus) ``` @@ -548,6 +573,24 @@ These files are stored in /var/lib/firecracker/vm/{vm-id}/metadata.json +### func (\*VM) [SetImage](/pkg/apis/ignite/v1alpha1/helpers.go?s=658:694#L30) +``` go +func (vm *VM) SetImage(image *Image) +``` +SetImage populates relevant fields to an Image on the VM object + + + + +### func (\*VM) [SetKernel](/pkg/apis/ignite/v1alpha1/helpers.go?s=942:981#L38) +``` go +func (vm *VM) SetKernel(kernel *Kernel) +``` +SetKernel populates relevant fields to a Kernel on the VM object + + + + ## type [VMImageSource](/pkg/apis/ignite/v1alpha1/types.go?s=8568:8676#L232) ``` go type VMImageSource struct { diff --git a/cmd/ignite/cmd/inspect.go b/cmd/ignite/cmd/inspect.go index 22bb7f9ae..10d570eef 100644 --- a/cmd/ignite/cmd/inspect.go +++ b/cmd/ignite/cmd/inspect.go @@ -1,12 +1,13 @@ package cmd import ( + "io" + "github.com/lithammer/dedent" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/weaveworks/ignite/cmd/ignite/run" "github.com/weaveworks/ignite/pkg/errutils" - "io" ) // NewCmdInspect inspects an Ignite Object @@ -20,7 +21,7 @@ func NewCmdInspect(out io.Writer) *cobra.Command { Retrieve information about the given object of the given kind. The kind can be "image", "kernel" or "vm". The object is matched by prefix based on its ID and name. Outputs JSON by default, can - be overridden to YAML with the yaml flag (-y, --yaml). + be overridden with the output flag (-o, --output). `), Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { @@ -40,5 +41,5 @@ func NewCmdInspect(out io.Writer) *cobra.Command { } func addInspectFlags(fs *pflag.FlagSet, i *run.InspectFlags) { - fs.BoolVarP(&i.YAMLOutput, "yaml", "y", false, "Output the object in YAML format instead of JSON") + fs.StringVarP(&i.OutputFormat, "output", "o", "json", "Output the object in the specified format") } diff --git a/cmd/ignite/run/inspect.go b/cmd/ignite/run/inspect.go index 89a581162..297837b3b 100644 --- a/cmd/ignite/run/inspect.go +++ b/cmd/ignite/run/inspect.go @@ -3,6 +3,8 @@ package run import ( "bytes" "fmt" + "strings" + "github.com/weaveworks/ignite/pkg/apis/ignite/scheme" "github.com/weaveworks/ignite/pkg/client" "github.com/weaveworks/ignite/pkg/filter" @@ -11,7 +13,7 @@ import ( ) type InspectFlags struct { - YAMLOutput bool + OutputFormat string } type inspectOptions struct { @@ -24,7 +26,7 @@ func (i *InspectFlags) NewInspectOptions(k, objectMatch string) (*inspectOptions var kind meta.Kind io := &inspectOptions{InspectFlags: i} - switch k { + switch strings.ToLower(k) { case meta.KindImage.Lower(): kind = meta.KindImage case meta.KindKernel.Lower(): @@ -32,7 +34,7 @@ func (i *InspectFlags) NewInspectOptions(k, objectMatch string) (*inspectOptions case meta.KindVM.Lower(): kind = meta.KindVM default: - return nil, fmt.Errorf("unrecognized kind: %s", k) + return nil, fmt.Errorf("unrecognized kind: %q", k) } if io.object, err = client.Dynamic(kind).Find(filter.NewIDNameFilter(objectMatch)); err != nil { @@ -43,14 +45,19 @@ func (i *InspectFlags) NewInspectOptions(k, objectMatch string) (*inspectOptions } func Inspect(io *inspectOptions) error { - // Choose the encoder - encodeFunc := scheme.Serializer.EncodeJSON - if io.YAMLOutput { - encodeFunc = scheme.Serializer.EncodeYAML + var b []byte + var err error + + // Select the encoder and encode the object with it + switch io.OutputFormat { + case "json": + b, err = scheme.Serializer.EncodeJSON(io.object) + case "yaml": + b, err = scheme.Serializer.EncodeYAML(io.object) + default: + err = fmt.Errorf("unrecognized output format: %q", io.OutputFormat) } - // Encode the object with the selected encoder - b, err := encodeFunc(io.object) if err != nil { return err } diff --git a/docs/cli/ignite.md b/docs/cli/ignite.md index f8d2bb18b..0082341aa 100644 --- a/docs/cli/ignite.md +++ b/docs/cli/ignite.md @@ -44,6 +44,7 @@ Example usage: * [ignite create](ignite_create.md) - Create a new VM without starting it * [ignite gitops](ignite_gitops.md) - Run the GitOps feature of Ignite * [ignite image](ignite_image.md) - Manage base images for VMs +* [ignite inspect](ignite_inspect.md) - Inspect an Ignite Object * [ignite kernel](ignite_kernel.md) - Manage VM kernels * [ignite kill](ignite_kill.md) - Kill running VMs * [ignite logs](ignite_logs.md) - Get the logs for a running VM diff --git a/docs/cli/ignite_inspect.md b/docs/cli/ignite_inspect.md new file mode 100644 index 000000000..5a58753b4 --- /dev/null +++ b/docs/cli/ignite_inspect.md @@ -0,0 +1,34 @@ +## ignite inspect + +Inspect an Ignite Object + +### Synopsis + + +Retrieve information about the given object of the given kind. +The kind can be "image", "kernel" or "vm". The object is matched +by prefix based on its ID and name. Outputs JSON by default, can +be overridden to YAML with the yaml flag (-y, --yaml). + + +``` +ignite inspect [flags] +``` + +### Options + +``` + -h, --help help for inspect + -o, --output string Output the object in the specified format (default "json") +``` + +### Options inherited from parent commands + +``` + -q, --quiet The quiet mode allows for machine-parsable output, by printing only IDs +``` + +### SEE ALSO + +* [ignite](ignite.md) - ignite: easily run Firecracker VMs + diff --git a/pkg/apis/ignite/v1alpha1/defaults.go b/pkg/apis/ignite/v1alpha1/defaults.go index 04562312c..05df39f90 100644 --- a/pkg/apis/ignite/v1alpha1/defaults.go +++ b/pkg/apis/ignite/v1alpha1/defaults.go @@ -1,10 +1,11 @@ package v1alpha1 import ( + "reflect" + meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1" "github.com/weaveworks/ignite/pkg/constants" "k8s.io/apimachinery/pkg/runtime" - "reflect" ) func addDefaultingFuncs(scheme *runtime.Scheme) error { diff --git a/pkg/apis/ignite/v1alpha1/helpers.go b/pkg/apis/ignite/v1alpha1/helpers.go index f044391bf..45b3601a2 100644 --- a/pkg/apis/ignite/v1alpha1/helpers.go +++ b/pkg/apis/ignite/v1alpha1/helpers.go @@ -40,4 +40,4 @@ func (vm *VM) SetKernel(kernel *Kernel) { vm.Status.Kernel.OCIImageSource = kernel.Status.OCISource // TODO: Remove this vm.Status.Kernel.UID = kernel.GetUID() -} \ No newline at end of file +} diff --git a/pkg/storage/cache.go b/pkg/storage/cache.go index 89fbae88e..14e6e80d1 100644 --- a/pkg/storage/cache.go +++ b/pkg/storage/cache.go @@ -1,8 +1,9 @@ package storage import ( - meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1" "log" + + meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1" ) // Cache is an intermediate caching layer, which conforms to Storage