mirror of
https://github.com/containers/podman.git
synced 2025-12-11 09:18:34 +08:00
23
cmd/podman/generate.go
Normal file
23
cmd/podman/generate.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
generateSubCommands = []cli.Command{
|
||||
containerKubeCommand,
|
||||
}
|
||||
|
||||
generateDescription = "generate structured data based for a containers and pods"
|
||||
kubeCommand = cli.Command{
|
||||
Name: "generate",
|
||||
Usage: "generated structured data",
|
||||
Description: generateDescription,
|
||||
ArgsUsage: "",
|
||||
Subcommands: generateSubCommands,
|
||||
UseShortOptionHandling: true,
|
||||
OnUsageError: usageErrorHandler,
|
||||
Hidden: true,
|
||||
}
|
||||
)
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||
"github.com/containers/libpod/libpod"
|
||||
"github.com/containers/libpod/pkg/rootless"
|
||||
podmanVersion "github.com/containers/libpod/version"
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
"k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -18,16 +19,15 @@ var (
|
||||
Name: "service, s",
|
||||
Usage: "only generate YAML for kubernetes service object",
|
||||
},
|
||||
LatestFlag,
|
||||
}
|
||||
containerKubeDescription = "Generate Kubernetes Pod YAML"
|
||||
containerKubeCommand = cli.Command{
|
||||
Name: "generate",
|
||||
Usage: "Generate Kubernetes pod YAML for a container",
|
||||
Name: "kube",
|
||||
Usage: "Generate Kubernetes pod YAML for a container or pod",
|
||||
Description: containerKubeDescription,
|
||||
Flags: sortFlags(containerKubeFlags),
|
||||
Action: generateKubeYAMLCmd,
|
||||
ArgsUsage: "CONTAINER-NAME",
|
||||
ArgsUsage: "CONTAINER|POD-NAME",
|
||||
UseShortOptionHandling: true,
|
||||
OnUsageError: usageErrorHandler,
|
||||
}
|
||||
@@ -36,9 +36,13 @@ var (
|
||||
// generateKubeYAMLCmdgenerates or replays kube
|
||||
func generateKubeYAMLCmd(c *cli.Context) error {
|
||||
var (
|
||||
container *libpod.Container
|
||||
err error
|
||||
output []byte
|
||||
podYAML *v1.Pod
|
||||
container *libpod.Container
|
||||
err error
|
||||
output []byte
|
||||
pod *libpod.Pod
|
||||
mashalledBytes []byte
|
||||
servicePorts []v1.ServicePort
|
||||
)
|
||||
|
||||
if rootless.IsRootless() {
|
||||
@@ -46,10 +50,7 @@ func generateKubeYAMLCmd(c *cli.Context) error {
|
||||
}
|
||||
args := c.Args()
|
||||
if len(args) > 1 || (len(args) < 1 && !c.Bool("latest")) {
|
||||
return errors.Errorf("you must provide one container ID or name or --latest")
|
||||
}
|
||||
if c.Bool("service") {
|
||||
return errors.Wrapf(libpod.ErrNotImplemented, "service generation")
|
||||
return errors.Errorf("you must provide one container|pod ID or name or --latest")
|
||||
}
|
||||
|
||||
runtime, err := libpodruntime.GetRuntime(c)
|
||||
@@ -59,33 +60,43 @@ func generateKubeYAMLCmd(c *cli.Context) error {
|
||||
defer runtime.Shutdown(false)
|
||||
|
||||
// Get the container in question
|
||||
if c.Bool("latest") {
|
||||
container, err = runtime.GetLatestContainer()
|
||||
container, err = runtime.LookupContainer(args[0])
|
||||
if err != nil {
|
||||
pod, err = runtime.LookupPod(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
podYAML, servicePorts, err = pod.GenerateForKube()
|
||||
} else {
|
||||
container, err = runtime.LookupContainer(args[0])
|
||||
if len(container.Dependencies()) > 0 {
|
||||
return errors.Wrapf(libpod.ErrNotImplemented, "containers with dependencies")
|
||||
}
|
||||
podYAML, err = container.GenerateForKube()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(container.Dependencies()) > 0 {
|
||||
return errors.Wrapf(libpod.ErrNotImplemented, "containers with dependencies")
|
||||
if c.Bool("service") {
|
||||
serviceYAML := libpod.GenerateKubeServiceFromV1Pod(podYAML, servicePorts)
|
||||
mashalledBytes, err = yaml.Marshal(serviceYAML)
|
||||
} else {
|
||||
// Marshall the results
|
||||
mashalledBytes, err = yaml.Marshal(podYAML)
|
||||
}
|
||||
|
||||
podYAML, err := container.InspectForKube()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
developmentComment := []byte("# Generation of Kubenetes YAML is still under development!\n")
|
||||
logrus.Warn("This function is still under heavy development.")
|
||||
// Marshall the results
|
||||
b, err := yaml.Marshal(podYAML)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
output = append(output, developmentComment...)
|
||||
output = append(output, b...)
|
||||
header := `# Generation of Kubenetes 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, mashalledBytes...)
|
||||
// Output the v1.Pod with the v1.Container
|
||||
fmt.Println(string(output))
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
kubeSubCommands = []cli.Command{
|
||||
containerKubeCommand,
|
||||
}
|
||||
|
||||
kubeDescription = "Work with Kubernetes objects"
|
||||
kubeCommand = cli.Command{
|
||||
Name: "kube",
|
||||
Usage: "Import and export Kubernetes objections from and to Podman",
|
||||
Description: containerDescription,
|
||||
ArgsUsage: "",
|
||||
Subcommands: kubeSubCommands,
|
||||
UseShortOptionHandling: true,
|
||||
OnUsageError: usageErrorHandler,
|
||||
Hidden: true,
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user