aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/kube/generate.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2022-08-16 13:04:41 +0000
committerGitHub <noreply@github.com>2022-08-16 13:04:41 +0000
commitd0567dd078982fcdb937868c9d80887a5bf73026 (patch)
treef60ea57840bd26def5b97abaa63968e454dfbd9d /cmd/podman/kube/generate.go
parent311b5946f74c94c5d6024c904abf6976c75e4d8f (diff)
parentd856fb43e11b3a4a93b423d330ce3435afddde4b (diff)
downloadpodman-d0567dd078982fcdb937868c9d80887a5bf73026.tar.gz
podman-d0567dd078982fcdb937868c9d80887a5bf73026.tar.bz2
podman-d0567dd078982fcdb937868c9d80887a5bf73026.zip
Merge pull request #15270 from nicrowe00/kubegenerate
Add "podman kube generate" command
Diffstat (limited to 'cmd/podman/kube/generate.go')
-rw-r--r--cmd/podman/kube/generate.go109
1 files changed, 109 insertions, 0 deletions
diff --git a/cmd/podman/kube/generate.go b/cmd/podman/kube/generate.go
new file mode 100644
index 000000000..6df4b55fc
--- /dev/null
+++ b/cmd/podman/kube/generate.go
@@ -0,0 +1,109 @@
+package kube
+
+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/generate"
+ "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 (
+ generateOptions = entities.GenerateKubeOptions{}
+ generateFile = ""
+ generateDescription = `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.`
+
+ generateKubeCmd = &cobra.Command{
+ Use: "generate [options] {CONTAINER...|POD...|VOLUME...}",
+ Short: "Generate Kubernetes YAML from containers, pods or volumes.",
+ Long: generateDescription,
+ RunE: generateKube,
+ Args: cobra.MinimumNArgs(1),
+ ValidArgsFunction: common.AutocompleteForGenerate,
+ Example: `podman kube generate ctrID
+ podman kube generate podID
+ podman kube generate --service podID
+ podman kube generate volumeName
+ podman kube generate ctrID podID volumeName --service`,
+ }
+ kubeGenerateDescription = generateDescription
+
+ kubeGenerateCmd = &cobra.Command{
+ Use: "kube [options] {CONTAINER...|POD...|VOLUME...}",
+ Short: "Generate Kubernetes YAML from containers, pods or volumes.",
+ Long: kubeGenerateDescription,
+ RunE: kubeGenerate,
+ Args: cobra.MinimumNArgs(1),
+ ValidArgsFunction: common.AutocompleteForGenerate,
+ Example: `podman kube generate ctrID
+ podman kube generate podID
+ podman kube generate --service podID
+ podman kube generate volumeName
+ podman kube generate ctrID podID volumeName --service`,
+ }
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Command: generateKubeCmd,
+ Parent: kubeCmd,
+ })
+ generateFlags(generateKubeCmd)
+
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Command: kubeGenerateCmd,
+ Parent: generate.GenerateCmd,
+ })
+ generateFlags(kubeGenerateCmd)
+}
+
+func generateFlags(cmd *cobra.Command) {
+ flags := cmd.Flags()
+ flags.BoolVarP(&generateOptions.Service, "service", "s", false, "Generate YAML for a Kubernetes service object")
+
+ filenameFlagName := "filename"
+ flags.StringVarP(&generateFile, filenameFlagName, "f", "", "Write output to the specified path")
+ _ = cmd.RegisterFlagCompletionFunc(filenameFlagName, completion.AutocompleteDefault)
+
+ flags.SetNormalizeFunc(utils.AliasFlags)
+}
+
+func generateKube(cmd *cobra.Command, args []string) error {
+ report, err := registry.ContainerEngine().GenerateKube(registry.GetContext(), args, generateOptions)
+ 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(generateFile); err == nil {
+ return fmt.Errorf("cannot write to %q; file exists", generateFile)
+ }
+ if err := ioutil.WriteFile(generateFile, content, 0644); err != nil {
+ return fmt.Errorf("cannot write to %q: %w", generateFile, err)
+ }
+ return nil
+ }
+
+ fmt.Println(string(content))
+ return nil
+}
+
+func kubeGenerate(cmd *cobra.Command, args []string) error {
+ return generateKube(cmd, args)
+}