Permalink
Browse files

Merge pull request #1928 from baude/podtokube

generate kube
  • Loading branch information...
openshift-merge-robot committed Dec 7, 2018
2 parents d4af59d + c8436b4 commit 49d9a8f9518dd3d38219f5ead6782d4919433cfc
@@ -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,20 +36,21 @@ 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() {
return errors.Wrapf(libpod.ErrNotImplemented, "rootless users")
}
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))

This file was deleted.

Oops, something went wrong.
@@ -16,6 +16,7 @@
| [podman-diff(1)](/docs/podman-diff.1.md) | Inspect changes on a container or image's filesystem |[![...](/docs/play.png)](https://asciinema.org/a/FXfWB9CKYFwYM4EfqW3NSZy1G)|
| [podman-exec(1)](/docs/podman-exec.1.md) | Execute a command in a running container
| [podman-export(1)](/docs/podman-export.1.md) | Export container's filesystem contents as a tar archive |[![...](/docs/play.png)](https://asciinema.org/a/913lBIRAg5hK8asyIhhkQVLtV)|
| [podman-generate(1)](/docs/podman-generate.1.md) | Generate structured output based on Podman containers and pods | |
| [podman-history(1)](/docs/podman-history.1.md) | Shows the history of an image |[![...](/docs/play.png)](https://asciinema.org/a/bCvUQJ6DkxInMELZdc5DinNSx)|
| [podman-image(1)](/docs/podman-image.1.md) | Manage Images||
| [podman-images(1)](/docs/podman-images.1.md) | List images in local storage |[![...](/docs/play.png)](https://asciinema.org/a/133649)|
@@ -876,6 +876,25 @@ _podman_container_wait() {
_podman_wait
}

_podman_generate() {
local boolean_options="
--help
-h
"
subcommands="
kube
"
__podman_subcommands "$subcommands $aliases" && return

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
;;
*)
COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
;;
esac
}
_podman_container() {
local boolean_options="
--help
@@ -2219,6 +2238,14 @@ _podman_logout() {
_complete_ "$options_with_args" "$boolean_options"
}

_podman_generate_kube() {
local options_with_args=""

local boolean_options="
-s
--service
"

_podman_container_runlabel() {
local options_with_args="
--authfile
@@ -2702,6 +2729,7 @@ _podman_podman() {
diff
exec
export
generate
history
images
import
@@ -0,0 +1,119 @@
% podman-generate Podman Man Pages
% Brent Baude
% December 2018
# NAME
podman-generate-kube - Generate Kubernetes YAML

# SYNOPSIS
**podman generate kube **
[**-h**|**--help**]
[**-s**][**--service**]
CONTAINER|POD

# DESCRIPTION
**podman generate kube** will generate 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.

The **service** option can be used to generate a Service specification for the corresponding Pod ouput. In particular,
if the object has portmap bindings, the service specification will include a NodePort declaration to expose the service. A
random port is assigned by Podman in the specification.

# OPTIONS:

**s** **--service**
Generate a service file for the resulting Pod YAML.

## Examples ##

Create Kubernetes Pod YAML for a container called `some-mariadb` .
```
$ sudo podman generate kube some-mariadb
# 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-0.11.2-dev
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: 2018-12-03T19:07:59Z
labels:
app: some-mariadb
name: some-mariadb-libpod
spec:
containers:
- command:
- docker-entrypoint.sh
- mysqld
env:
- name: PATH
value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: TERM
value: xterm
- name: HOSTNAME
- name: container
value: podman
- name: GOSU_VERSION
value: "1.10"
- name: GPG_KEYS
value: "199369E5404BD5FC7D2FE43BCBCB082A1BB943DB \t177F4010FE56CA3336300305F1656F24C74CD1D8
\t430BDF5C56E7C94E848EE60C1C4CBDCDCD2EFD2A \t4D1BB29D63D98E422B2113B19334A25F8507EFA5"
- name: MARIADB_MAJOR
value: "10.3"
- name: MARIADB_VERSION
value: 1:10.3.10+maria~bionic
- name: MYSQL_ROOT_PASSWORD
value: x
image: quay.io/baude/demodb:latest
name: some-mariadb
ports:
- containerPort: 3306
hostPort: 36533
protocol: TCP
resources: {}
securityContext:
allowPrivilegeEscalation: true
privileged: false
readOnlyRootFilesystem: false
tty: true
workingDir: /
status: {}
```

Create Kubernetes service YAML for a container called `some-mariabdb`
```
$ sudo podman generate kube -s some-mariadb
# 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-0.11.2-dev
apiVersion: v1
kind: Service
metadata:
creationTimestamp: 2018-12-03T19:08:24Z
labels:
app: some-mariadb
name: some-mariadb-libpod
spec:
ports:
- name: "3306"
nodePort: 30929
port: 3306
protocol: TCP
targetPort: 0
selector:
app: some-mariadb
type: NodePort
status:
loadBalancer: {}
```

## SEE ALSO
podman(1), podman-container, podman-pod

# HISTORY
Decemeber 2018, Originally compiled by Brent Baude (bbaude at redhat dot com)
@@ -0,0 +1,19 @@
% podman-generate(1)

## NAME
podman\-container - generate structured data based for a containers and pods

## SYNOPSIS
**podman generate** *subcommand*

## DESCRIPTION
The generate command will create structured output (like YAML) based on a container or pod.

## COMMANDS

| Command | Man Page | Description |
| ------- | --------------------------------------------------- | ---------------------------------------------------------------------------- |
| kube | [podman-generate-kube(1)](podman-generate-kube.1.md) | Generate Kubernetes YAML based on a pod or container

## SEE ALSO
podman, podman-pod, podman-container
Oops, something went wrong.

0 comments on commit 49d9a8f

Please sign in to comment.