summaryrefslogtreecommitdiff
path: root/cmd/podman/generate
diff options
context:
space:
mode:
authorNiall Crowe <nicrowe@redhat.com>2022-08-04 09:02:54 +0100
committerNiall Crowe <nicrowe@redhat.com>2022-08-11 16:58:24 +0100
commitd856fb43e11b3a4a93b423d330ce3435afddde4b (patch)
treeb5533938756de79d6340e3a5802ac1a1700fd142 /cmd/podman/generate
parent92bbae40de3c48ee6b4692ab9e4d7cc14db242bb (diff)
downloadpodman-d856fb43e11b3a4a93b423d330ce3435afddde4b.tar.gz
podman-d856fb43e11b3a4a93b423d330ce3435afddde4b.tar.bz2
podman-d856fb43e11b3a4a93b423d330ce3435afddde4b.zip
Add "podman kube generate" command
"podman kube generate" creates Kubernetes YAML from Podman containers, pods or volumes. Users will still be able to use "podman generate kube" as an alias of "kube generate". Signed-off-by: Niall Crowe <nicrowe@redhat.com>
Diffstat (limited to 'cmd/podman/generate')
-rw-r--r--cmd/podman/generate/generate.go6
-rw-r--r--cmd/podman/generate/kube.go80
-rw-r--r--cmd/podman/generate/spec.go4
-rw-r--r--cmd/podman/generate/systemd.go4
4 files changed, 7 insertions, 87 deletions
diff --git a/cmd/podman/generate/generate.go b/cmd/podman/generate/generate.go
index 0a12961f4..f1dea6816 100644
--- a/cmd/podman/generate/generate.go
+++ b/cmd/podman/generate/generate.go
@@ -1,4 +1,4 @@
-package pods
+package generate
import (
"github.com/containers/podman/v4/cmd/podman/registry"
@@ -9,7 +9,7 @@ import (
var (
// Command: podman _generate_
- generateCmd = &cobra.Command{
+ GenerateCmd = &cobra.Command{
Use: "generate",
Short: "Generate structured data based on containers, pods or volumes",
Long: "Generate structured data (e.g., Kubernetes YAML or systemd units) based on containers, pods or volumes.",
@@ -20,6 +20,6 @@ var (
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
- Command: generateCmd,
+ Command: GenerateCmd,
})
}
diff --git a/cmd/podman/generate/kube.go b/cmd/podman/generate/kube.go
deleted file mode 100644
index 7bfc3dcf7..000000000
--- a/cmd/podman/generate/kube.go
+++ /dev/null
@@ -1,80 +0,0 @@
-package pods
-
-import (
- "fmt"
- "io"
- "io/ioutil"
- "os"
-
- "github.com/containers/common/pkg/completion"
- "github.com/containers/podman/v4/cmd/podman/common"
- "github.com/containers/podman/v4/cmd/podman/registry"
- "github.com/containers/podman/v4/cmd/podman/utils"
- "github.com/containers/podman/v4/pkg/domain/entities"
- "github.com/spf13/cobra"
-)
-
-var (
- kubeOptions = entities.GenerateKubeOptions{}
- kubeFile = ""
- kubeDescription = `Command generates Kubernetes Pod, Service or PersistenVolumeClaim YAML (v1 specification) from Podman containers, pods or volumes.
-
- Whether the input is for a container or pod, Podman will always generate the specification as a pod.`
-
- kubeCmd = &cobra.Command{
- Use: "kube [options] {CONTAINER...|POD...|VOLUME...}",
- Short: "Generate Kubernetes YAML from containers, pods or volumes.",
- Long: kubeDescription,
- RunE: kube,
- Args: cobra.MinimumNArgs(1),
- ValidArgsFunction: common.AutocompleteForKube,
- Example: `podman generate kube ctrID
- podman generate kube podID
- podman generate kube --service podID
- podman generate kube volumeName
- podman generate kube ctrID podID volumeName --service`,
- }
-)
-
-func init() {
- registry.Commands = append(registry.Commands, registry.CliCommand{
- Command: kubeCmd,
- Parent: generateCmd,
- })
- flags := kubeCmd.Flags()
- flags.BoolVarP(&kubeOptions.Service, "service", "s", false, "Generate YAML for a Kubernetes service object")
-
- filenameFlagName := "filename"
- flags.StringVarP(&kubeFile, filenameFlagName, "f", "", "Write output to the specified path")
- _ = kubeCmd.RegisterFlagCompletionFunc(filenameFlagName, completion.AutocompleteDefault)
-
- flags.SetNormalizeFunc(utils.AliasFlags)
-}
-
-func kube(cmd *cobra.Command, args []string) error {
- report, err := registry.ContainerEngine().GenerateKube(registry.GetContext(), args, kubeOptions)
- if err != nil {
- return err
- }
-
- content, err := ioutil.ReadAll(report.Reader)
- if err != nil {
- return err
- }
- if r, ok := report.Reader.(io.ReadCloser); ok {
- defer r.Close()
- }
-
- if cmd.Flags().Changed("filename") {
- if _, err := os.Stat(kubeFile); err == nil {
- return fmt.Errorf("cannot write to %q; file exists", kubeFile)
- }
- if err := ioutil.WriteFile(kubeFile, content, 0644); err != nil {
- return fmt.Errorf("cannot write to %q: %w", kubeFile, err)
- }
- return nil
- }
-
- fmt.Println(string(content))
- return nil
-}
diff --git a/cmd/podman/generate/spec.go b/cmd/podman/generate/spec.go
index 1cf967424..bf451ebc5 100644
--- a/cmd/podman/generate/spec.go
+++ b/cmd/podman/generate/spec.go
@@ -1,4 +1,4 @@
-package pods
+package generate
import (
"fmt"
@@ -31,7 +31,7 @@ var (
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: specCmd,
- Parent: generateCmd,
+ Parent: GenerateCmd,
})
opts = &entities.GenerateSpecOptions{}
flags := specCmd.Flags()
diff --git a/cmd/podman/generate/systemd.go b/cmd/podman/generate/systemd.go
index 1ece64a30..e40416534 100644
--- a/cmd/podman/generate/systemd.go
+++ b/cmd/podman/generate/systemd.go
@@ -1,4 +1,4 @@
-package pods
+package generate
import (
"encoding/json"
@@ -57,7 +57,7 @@ var (
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: systemdCmd,
- Parent: generateCmd,
+ Parent: GenerateCmd,
})
flags := systemdCmd.Flags()
flags.BoolVarP(&systemdOptions.Name, "name", "n", false, "Use container/pod names instead of IDs")