Skip to content

Commit

Permalink
Merge pull request #15426 from nicrowe00/14955
Browse files Browse the repository at this point in the history
podman kube play/down --read from URL
  • Loading branch information
openshift-merge-robot authored Aug 23, 2022
2 parents 8cfbcfe + 5f719b5 commit 5dea121
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/podman/kube/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var (
Args: cobra.ExactArgs(1),
ValidArgsFunction: common.AutocompleteDefaultOneArg,
Example: `podman kube down nginx.yml
cat nginx.yml | podman kube down -`,
cat nginx.yml | podman kube down -
podman kube down https://example.com/nginx.yml`,
}
)

Expand Down
26 changes: 23 additions & 3 deletions cmd/podman/kube/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"strings"

"github.com/containers/common/pkg/auth"
"github.com/containers/common/pkg/completion"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/parse"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/libpod/define"
Expand Down Expand Up @@ -52,7 +55,8 @@ var (
ValidArgsFunction: common.AutocompleteDefaultOneArg,
Example: `podman kube play nginx.yml
cat nginx.yml | podman kube play -
podman kube play --creds user:password --seccomp-profile-root /custom/path apache.yml`,
podman kube play --creds user:password --seccomp-profile-root /custom/path apache.yml
podman kube play https://example.com/nginx.yml`,
}
)

Expand All @@ -67,7 +71,8 @@ var (
ValidArgsFunction: common.AutocompleteDefaultOneArg,
Example: `podman play kube nginx.yml
cat nginx.yml | podman play kube -
podman play kube --creds user:password --seccomp-profile-root /custom/path apache.yml`,
podman play kube --creds user:password --seccomp-profile-root /custom/path apache.yml
podman play kube https://example.com/nginx.yml`,
}
)

Expand Down Expand Up @@ -167,7 +172,7 @@ func playFlags(cmd *cobra.Command) {
_ = cmd.RegisterFlagCompletionFunc(contextDirFlagName, completion.AutocompleteDefault)

// NOTE: The service-container flag is marked as hidden as it
// is purely designed for running kube-play in systemd units.
// is purely designed for running kube-play or play-kube in systemd units.
// It is not something users should need to know or care about.
//
// Having a flag rather than an env variable is cleaner.
Expand Down Expand Up @@ -255,6 +260,7 @@ func play(cmd *cobra.Command, args []string) error {
return err
}
}

return kubeplay(reader)
}

Expand All @@ -263,13 +269,27 @@ func playKube(cmd *cobra.Command, args []string) error {
}

func readerFromArg(fileName string) (*bytes.Reader, error) {
errURL := parse.ValidURL(fileName)
if fileName == "-" { // Read from stdin
data, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
if errURL == nil {
response, err := http.Get(fileName)
if err != nil {
return nil, err
}
defer response.Body.Close()

data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
f, err := os.Open(fileName)
if err != nil {
return nil, err
Expand Down
27 changes: 24 additions & 3 deletions docs/source/markdown/podman-kube-down.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
podman-kube-down - Remove containers and pods based on Kubernetes YAML

## SYNOPSIS
**podman kube down** *file.yml|-*
**podman kube down** *file.yml|-|https://website.io/file.yml*

## DESCRIPTION
**podman kube down** reads a specified Kubernetes YAML file, tearing down pods that were created by the `podman kube play` command via the same Kubernetes YAML file. Any volumes that were created by the previous `podman kube play` command remain intact. If the YAML file is specified as `-`, `podman kube down` reads the YAML from stdin.
**podman kube down** reads a specified Kubernetes YAML file, tearing down pods that were created by the `podman kube play` command via the same Kubernetes YAML
file. Any volumes that were created by the previous `podman kube play` command remain intact. If the YAML file is specified as `-`, `podman kube down` reads the
YAML from stdin. The input can also be a URL that points to a YAML file such as https://podman.io/demo.yml. `podman kube down` will then teardown the pods and
containers created by `podman kube play` via the same Kubernetes YAML from the URL. However, `podman kube down` will not work with a URL if the YAML file the URL
points to has been changed or altered since the creation of the pods and containers using `podman kube play`.

## EXAMPLES

Expand All @@ -30,14 +34,31 @@ spec:
Remove the pod and containers as described in the `demo.yml` file
```
$ podman kube down demo.yml
Pods stopped:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
Pods removed:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
```

Remove the pod and containers as described in the`demo.yml` file YAML sent to stdin
Remove the pod and containers as described in the `demo.yml` file YAML sent to stdin
```
$ cat demo.yml | podman kube play -
Pods stopped:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
Pods removed:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
```

Remove the pods and containers as described in the `demo.yml` file YAML read from a URL
```
$ podman kube down https://podman.io/demo.yml
Pods stopped:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
Pods removed:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
```
`podman kube down` will not work with a URL if the YAML file the URL points to has been changed
or altered since it was used to create the pods and containers.

## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-kube(1)](podman-kube.1.md)**, **[podman-kube-play(1)](podman-kube-play.1.md)**, **[podman-kube-generate(1)](podman-kube-generate.1.md)**, **[containers-certs.d(5)](https://github.com/containers/image/blob/main/docs/containers-certs.d.5.md)**
20 changes: 18 additions & 2 deletions docs/source/markdown/podman-kube-play.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
podman-kube-play - Create containers, pods and volumes based on Kubernetes YAML

## SYNOPSIS
**podman kube play** [*options*] *file.yml|-*
**podman kube play** [*options*] *file.yml|-|https://website.io/file.yml*

## DESCRIPTION
**podman kube play** will read in a structured file of Kubernetes YAML. It will then recreate the containers, pods or volumes described in the YAML. Containers within a pod are then started and the ID of the new Pod or the name of the new Volume is output. If the yaml file is specified as "-" then `podman kube play` will read the YAML file from stdin.
Using the `--down` command line option, it is also capable of tearing down the pods created by a previous run of `podman kube play`.
Using the `--replace` command line option, it will tear down the pods(if any) created by a previous run of `podman kube play` and recreate the pods with the Kubernetes YAML file.
Ideally the input file would be one created by Podman (see podman-kube-generate(1)). This would guarantee a smooth import and expected results.
The input can also be a URL that points to a YAML file such as https://podman.io/demo.yml. `podman kube play` will read the YAML from the URL and create pods and containers from it.

Currently, the supported Kubernetes kinds are:
- Pod
Expand Down Expand Up @@ -300,8 +301,23 @@ Create a pod connected to two networks (called net1 and net2) with a static ip
$ podman kube play demo.yml --network net1:ip=10.89.1.5 --network net2:ip=10.89.10.10
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
```

Please take into account that networks must be created first using podman-network-create(1).

Create and teardown from a URL pointing to a YAML file
```
$ podman kube play https://podman.io/demo.yml
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6

$ podman kube play --down https://podman.io/demo.yml
Pods stopped:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
Pods removed:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
```
`podman kube play --down` will not work with a URL if the YAML file the URL points to
has been changed or altered.



## SEE ALSO
**[podman(1)](podman.1.md)**, **[podman-kube(1)](podman-kube.1.md)**, **[podman-kube-down(1)](podman-kube-down.1.md)**, **[podman-network-create(1)](podman-network-create.1.md)**, **[podman-kube-generate(1)](podman-kube-generate.1.md)**, **[containers-certs.d(5)](https://github.com/containers/image/blob/main/docs/containers-certs.d.5.md)**
25 changes: 25 additions & 0 deletions test/system/700-play.bats
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,28 @@ status: {}
run_podman pod rm -a
run_podman rm -a
}

@test "podman kube play - URL" {
TESTDIR=$PODMAN_TMPDIR/testdir
mkdir -p $TESTDIR
echo "$testYaml" | sed "s|TESTDIR|${TESTDIR}|g" > $PODMAN_TMPDIR/test.yaml

HOST_PORT=$(random_free_port)
SERVER=http://127.0.0.1:$HOST_PORT

run_podman run -d --name myyaml -p "$HOST_PORT:80" \
-v $PODMAN_TMPDIR/test.yaml:/var/www/testpod.yaml:Z \
-w /var/www \
$IMAGE /bin/busybox-extras httpd -f -p 80

run_podman kube play $SERVER/testpod.yaml
run_podman inspect test_pod-test --format "{{.State.Running}}"
is "$output" "true"
run_podman kube down $SERVER/testpod.yaml
run_podman 125 inspect test_pod-test
is "$output" ".*Error: inspecting object: no such object: \"test_pod-test\""

run_podman pod rm -a -f
run_podman rm -a -f
run_podman rm -f -t0 myyaml
}

0 comments on commit 5dea121

Please sign in to comment.