podman kube play/down --read from URL

`podman kube play` can create pods and containers from YAML
read from a URL poiniting to a YAML file.
For example: `podman kube play https://example.com/demo.yml`.
`podman kube down` can also teardown pods and containers created
from that YAML file by also reading YAML from a URL, provided the
YAML file the URL points to has not been changed or altered since
it was used to create pods and containers

Closes #14955
Signed-off-by: Niall Crowe <nicrowe@redhat.com>
This commit is contained in:
Niall Crowe 2022-08-16 15:38:59 +01:00
parent 498fe67ef7
commit 5f719b533e
5 changed files with 92 additions and 9 deletions

View File

@ -19,7 +19,8 @@ var (
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
ValidArgsFunction: common.AutocompleteDefaultOneArg, ValidArgsFunction: common.AutocompleteDefaultOneArg,
Example: `podman kube down nginx.yml 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`,
} }
) )

View File

@ -5,7 +5,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http"
"os" "os"
"strings" "strings"
@ -13,6 +15,7 @@ import (
"github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/completion"
"github.com/containers/image/v5/types" "github.com/containers/image/v5/types"
"github.com/containers/podman/v4/cmd/podman/common" "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/registry"
"github.com/containers/podman/v4/cmd/podman/utils" "github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/define"
@ -52,7 +55,8 @@ var (
ValidArgsFunction: common.AutocompleteDefaultOneArg, ValidArgsFunction: common.AutocompleteDefaultOneArg,
Example: `podman kube play nginx.yml Example: `podman kube play nginx.yml
cat nginx.yml | podman kube play - 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`,
} }
) )
@ -67,7 +71,8 @@ var (
ValidArgsFunction: common.AutocompleteDefaultOneArg, ValidArgsFunction: common.AutocompleteDefaultOneArg,
Example: `podman play kube nginx.yml Example: `podman play kube nginx.yml
cat nginx.yml | podman play kube - 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`,
} }
) )
@ -167,7 +172,7 @@ func playFlags(cmd *cobra.Command) {
_ = cmd.RegisterFlagCompletionFunc(contextDirFlagName, completion.AutocompleteDefault) _ = cmd.RegisterFlagCompletionFunc(contextDirFlagName, completion.AutocompleteDefault)
// NOTE: The service-container flag is marked as hidden as it // 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. // It is not something users should need to know or care about.
// //
// Having a flag rather than an env variable is cleaner. // Having a flag rather than an env variable is cleaner.
@ -255,6 +260,7 @@ func play(cmd *cobra.Command, args []string) error {
return err return err
} }
} }
return kubeplay(reader) return kubeplay(reader)
} }
@ -263,6 +269,7 @@ func playKube(cmd *cobra.Command, args []string) error {
} }
func readerFromArg(fileName string) (*bytes.Reader, error) { func readerFromArg(fileName string) (*bytes.Reader, error) {
errURL := parse.ValidURL(fileName)
if fileName == "-" { // Read from stdin if fileName == "-" { // Read from stdin
data, err := io.ReadAll(os.Stdin) data, err := io.ReadAll(os.Stdin)
if err != nil { if err != nil {
@ -270,6 +277,19 @@ func readerFromArg(fileName string) (*bytes.Reader, error) {
} }
return bytes.NewReader(data), nil 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) f, err := os.Open(fileName)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -4,10 +4,14 @@
podman-kube-down - Remove containers and pods based on Kubernetes YAML podman-kube-down - Remove containers and pods based on Kubernetes YAML
## SYNOPSIS ## SYNOPSIS
**podman kube down** *file.yml|-* **podman kube down** *file.yml|-|https://website.io/file.yml*
## DESCRIPTION ## 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 ## EXAMPLES
@ -30,14 +34,31 @@ spec:
Remove the pod and containers as described in the `demo.yml` file Remove the pod and containers as described in the `demo.yml` file
``` ```
$ podman kube down demo.yml $ podman kube down demo.yml
Pods stopped:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
Pods removed:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6 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 - $ cat demo.yml | podman kube play -
Pods stopped:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
Pods removed:
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6 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 ## 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)** **[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)**

View File

@ -4,13 +4,14 @@
podman-kube-play - Create containers, pods and volumes based on Kubernetes YAML podman-kube-play - Create containers, pods and volumes based on Kubernetes YAML
## SYNOPSIS ## SYNOPSIS
**podman kube play** [*options*] *file.yml|-* **podman kube play** [*options*] *file.yml|-|https://website.io/file.yml*
## DESCRIPTION ## 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. **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 `--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. 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. 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: Currently, the supported Kubernetes kinds are:
- Pod - Pod
@ -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 $ podman kube play demo.yml --network net1:ip=10.89.1.5 --network net2:ip=10.89.10.10
52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6 52182811df2b1e73f36476003a66ec872101ea59034ac0d4d3a7b40903b955a6
``` ```
Please take into account that networks must be created first using podman-network-create(1). 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 ## 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)** **[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)**

View File

@ -361,3 +361,28 @@ status: {}
run_podman pod rm -a run_podman pod rm -a
run_podman 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
}