kube play: update the handling of PersistentVolumeClaim

Up - do not fail if volume already exists, use the existing one
Down - allow the user to remove the volume by passing --force
Add tests
Update the documentation

Signed-off-by: Ygal Blum <ygal.blum@gmail.com>
This commit is contained in:
Ygal Blum
2022-11-06 10:11:06 +02:00
parent afc84151fc
commit 1c8196a9ac
15 changed files with 248 additions and 31 deletions

View File

@ -3,16 +3,22 @@ package kube
import (
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/spf13/cobra"
)
type downKubeOptions struct {
Force bool
}
var (
downDescription = `Reads in a structured file of Kubernetes YAML.
Removes pods that have been based on the Kubernetes kind described in the YAML.`
downCmd = &cobra.Command{
Use: "down KUBEFILE|-",
Use: "down [options] KUBEFILE|-",
Short: "Remove pods based on Kubernetes YAML.",
Long: downDescription,
RunE: down,
@ -22,6 +28,8 @@ var (
cat nginx.yml | podman kube down -
podman kube down https://example.com/nginx.yml`,
}
downOptions = downKubeOptions{}
)
func init() {
@ -29,6 +37,14 @@ func init() {
Command: downCmd,
Parent: kubeCmd,
})
downFlags(downCmd)
}
func downFlags(cmd *cobra.Command) {
flags := cmd.Flags()
flags.SetNormalizeFunc(utils.AliasFlags)
flags.BoolVar(&downOptions.Force, "force", false, "remove volumes")
}
func down(cmd *cobra.Command, args []string) error {
@ -36,5 +52,5 @@ func down(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return teardown(reader)
return teardown(reader, entities.PlayKubeDownOptions{Force: downOptions.Force})
}

View File

@ -138,6 +138,7 @@ func playFlags(cmd *cobra.Command) {
flags.BoolVarP(&playOptions.Quiet, "quiet", "q", false, "Suppress output information when pulling images")
flags.BoolVar(&playOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries")
flags.BoolVar(&playOptions.StartCLI, "start", true, "Start the pod after creating it")
flags.BoolVar(&playOptions.Force, "force", false, "Remove volumes as part of --down")
authfileFlagName := "authfile"
flags.StringVar(&playOptions.Authfile, authfileFlagName, auth.GetDefaultAuthFile(), "Path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
@ -242,17 +243,21 @@ func play(cmd *cobra.Command, args []string) error {
playOptions.StaticMACs = append(playOptions.StaticMACs, m)
}
if playOptions.Force && !playOptions.Down {
return errors.New("--force may be specified only with --down")
}
reader, err := readerFromArg(args[0])
if err != nil {
return err
}
if playOptions.Down {
return teardown(reader)
return teardown(reader, entities.PlayKubeDownOptions{Force: playOptions.Force})
}
if playOptions.Replace {
if err := teardown(reader); err != nil && !errorhandling.Contains(err, define.ErrNoSuchPod) {
if err := teardown(reader, entities.PlayKubeDownOptions{Force: playOptions.Force}); err != nil && !errorhandling.Contains(err, define.ErrNoSuchPod) {
return err
}
if _, err := reader.Seek(0, 0); err != nil {
@ -302,13 +307,13 @@ func readerFromArg(fileName string) (*bytes.Reader, error) {
return bytes.NewReader(data), nil
}
func teardown(body io.Reader) error {
func teardown(body io.Reader, options entities.PlayKubeDownOptions) error {
var (
podStopErrors utils.OutputErrors
podRmErrors utils.OutputErrors
volRmErrors utils.OutputErrors
)
options := new(entities.PlayKubeDownOptions)
reports, err := registry.ContainerEngine().PlayKubeDown(registry.GetContext(), body, *options)
reports, err := registry.ContainerEngine().PlayKubeDown(registry.GetContext(), body, options)
if err != nil {
return err
}
@ -338,7 +343,22 @@ func teardown(body io.Reader) error {
}
}
return podRmErrors.PrintErrors()
lastPodRmError := podRmErrors.PrintErrors()
if lastPodRmError != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", lastPodRmError)
}
// Output rm'd volumes
fmt.Println("Volumes removed:")
for _, removed := range reports.VolumeRmReport {
if removed.Err == nil {
fmt.Println(removed.Id)
} else {
volRmErrors = append(volRmErrors, removed.Err)
}
}
return volRmErrors.PrintErrors()
}
func kubeplay(body io.Reader) error {