github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/generate_kube.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 8 "github.com/containers/libpod/cmd/podman/cliconfig" 9 "github.com/containers/libpod/pkg/adapter" 10 podmanVersion "github.com/containers/libpod/version" 11 "github.com/ghodss/yaml" 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 ) 15 16 var ( 17 containerKubeCommand cliconfig.GenerateKubeValues 18 containerKubeDescription = `Command generates Kubernetes Pod YAML (v1 specification) from a podman container or pod. 19 20 Whether the input is for a container or pod, Podman will always generate the specification as a Pod. The input may be in the form of a pod or container name or ID.` 21 _containerKubeCommand = &cobra.Command{ 22 Use: "kube [flags] CONTAINER | POD", 23 Short: "Generate Kubernetes pod YAML from a container or pod", 24 Long: containerKubeDescription, 25 RunE: func(cmd *cobra.Command, args []string) error { 26 containerKubeCommand.InputArgs = args 27 containerKubeCommand.GlobalFlags = MainGlobalOpts 28 containerKubeCommand.Remote = remoteclient 29 return generateKubeYAMLCmd(&containerKubeCommand) 30 }, 31 Example: `podman generate kube ctrID 32 podman generate kube podID 33 podman generate kube --service podID`, 34 } 35 ) 36 37 func init() { 38 containerKubeCommand.Command = _containerKubeCommand 39 containerKubeCommand.SetHelpTemplate(HelpTemplate()) 40 containerKubeCommand.SetUsageTemplate(UsageTemplate()) 41 flags := containerKubeCommand.Flags() 42 flags.BoolVarP(&containerKubeCommand.Service, "service", "s", false, "Generate YAML for kubernetes service object") 43 flags.StringVarP(&containerKubeCommand.Filename, "filename", "f", "", "Filename to output to") 44 } 45 46 func generateKubeYAMLCmd(c *cliconfig.GenerateKubeValues) error { 47 var ( 48 //podYAML *v1.Pod 49 err error 50 output []byte 51 //pod *libpod.Pod 52 marshalledPod []byte 53 marshalledService []byte 54 ) 55 56 args := c.InputArgs 57 if len(args) != 1 { 58 return errors.Errorf("you must provide exactly one container|pod ID or name") 59 } 60 61 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 62 if err != nil { 63 return errors.Wrapf(err, "could not get runtime") 64 } 65 defer runtime.DeferredShutdown(false) 66 67 podYAML, serviceYAML, err := runtime.GenerateKube(c) 68 if err != nil { 69 return err 70 } 71 // Marshall the results 72 marshalledPod, err = yaml.Marshal(podYAML) 73 if err != nil { 74 return err 75 } 76 if c.Service { 77 marshalledService, err = yaml.Marshal(serviceYAML) 78 if err != nil { 79 return err 80 } 81 } 82 header := `# Generation of Kubernetes YAML is still under development! 83 # 84 # Save the output of this file and use kubectl create -f to import 85 # it into Kubernetes. 86 # 87 # Created with podman-%s 88 ` 89 output = append(output, []byte(fmt.Sprintf(header, podmanVersion.Version))...) 90 output = append(output, marshalledPod...) 91 if c.Bool("service") { 92 output = append(output, []byte("---\n")...) 93 output = append(output, marshalledService...) 94 } 95 96 if c.Filename != "" { 97 if _, err := os.Stat(c.Filename); err == nil { 98 return errors.Errorf("cannot write to %q - file exists", c.Filename) 99 } 100 101 if err := ioutil.WriteFile(c.Filename, output, 0644); err != nil { 102 return err 103 } 104 } else { 105 // Output the v1.Pod with the v1.Container 106 fmt.Println(string(output)) 107 } 108 109 return nil 110 }