summaryrefslogtreecommitdiff
path: root/cmd/podman/generate_kube.go
blob: 318dd07719341a14bdda65a6c9789155dde92691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main

import (
	"fmt"
	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/pkg/adapter"
	podmanVersion "github.com/containers/libpod/version"
	"github.com/ghodss/yaml"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	containerKubeCommand     cliconfig.GenerateKubeValues
	containerKubeDescription = `Command generates Kubernetes Pod YAML (v1 specification) from a podman container or pod.

  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.`
	_containerKubeCommand = &cobra.Command{
		Use:   "kube [flags] CONTAINER | POD",
		Short: "Generate Kubernetes pod YAML from a container or pod",
		Long:  containerKubeDescription,
		RunE: func(cmd *cobra.Command, args []string) error {
			containerKubeCommand.InputArgs = args
			containerKubeCommand.GlobalFlags = MainGlobalOpts
			containerKubeCommand.Remote = remoteclient
			return generateKubeYAMLCmd(&containerKubeCommand)
		},
		Example: `podman generate kube ctrID
  podman generate kube podID
  podman generate kube --service podID`,
	}
)

func init() {
	containerKubeCommand.Command = _containerKubeCommand
	containerKubeCommand.SetHelpTemplate(HelpTemplate())
	containerKubeCommand.SetUsageTemplate(UsageTemplate())
	flags := containerKubeCommand.Flags()
	flags.BoolVarP(&containerKubeCommand.Service, "service", "s", false, "Generate YAML for kubernetes service object")
}

func generateKubeYAMLCmd(c *cliconfig.GenerateKubeValues) error {
	var (
		//podYAML           *v1.Pod
		err    error
		output []byte
		//pod               *libpod.Pod
		marshalledPod     []byte
		marshalledService []byte
	)

	args := c.InputArgs
	if len(args) != 1 {
		return errors.Errorf("you must provide exactly one container|pod ID or name")
	}

	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.Shutdown(false)

	podYAML, serviceYAML, err := runtime.GenerateKube(c)
	if err != nil {
		return err
	}
	// Marshall the results
	marshalledPod, err = yaml.Marshal(podYAML)
	if err != nil {
		return err
	}
	if c.Service {
		marshalledService, err = yaml.Marshal(serviceYAML)
		if err != nil {
			return err
		}
	}
	header := `# Generation of Kubernetes YAML is still under development!
#
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-%s
`
	output = append(output, []byte(fmt.Sprintf(header, podmanVersion.Version))...)
	output = append(output, marshalledPod...)
	if c.Bool("service") {
		output = append(output, []byte("---\n")...)
		output = append(output, marshalledService...)
	}
	// Output the v1.Pod with the v1.Container
	fmt.Println(string(output))

	return nil
}