From 1e176923b15360559d859000f2fb0c0a3e30f792 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Mon, 16 Aug 2021 09:37:50 -0500 Subject: teardown play kube add the ability for play kube to tear down based on the yaml used to play it. it is indicated by --down in the play kube command. volumes are NOT deleted during the teardown. pods and their containers are stopped and removed. Signed-off-by: Brent Baude --- cmd/podman/play/kube.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'cmd/podman') diff --git a/cmd/podman/play/kube.go b/cmd/podman/play/kube.go index 2eebd9f86..9308371d2 100644 --- a/cmd/podman/play/kube.go +++ b/cmd/podman/play/kube.go @@ -86,6 +86,9 @@ func init() { flags.StringVar(&kubeOptions.Authfile, authfileFlagName, auth.GetDefaultAuthFile(), "Path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") _ = kubeCmd.RegisterFlagCompletionFunc(authfileFlagName, completion.AutocompleteDefault) + downFlagName := "down" + flags.BoolVar(&kubeOptions.Down, downFlagName, false, "Stop pods defined in the YAML file") + if !registry.IsRemote() { certDirFlagName := "cert-dir" flags.StringVar(&kubeOptions.CertDir, certDirFlagName, "", "`Pathname` of a directory containing TLS certificates and keys") @@ -144,12 +147,55 @@ func kube(cmd *cobra.Command, args []string) error { } kubeOptions.StaticMACs = append(kubeOptions.StaticMACs, m) } + if kubeOptions.Down { + return teardown(yamlfile) + } + return playkube(yamlfile) +} - report, err := registry.ContainerEngine().PlayKube(registry.GetContext(), yamlfile, kubeOptions.PlayKubeOptions) +func teardown(yamlfile string) error { + var ( + podStopErrors utils.OutputErrors + podRmErrors utils.OutputErrors + ) + options := new(entities.PlayKubeDownOptions) + reports, err := registry.ContainerEngine().PlayKubeDown(registry.GetContext(), yamlfile, *options) if err != nil { return err } + // Output stopped pods + fmt.Println("Pods stopped:") + for _, stopped := range reports.StopReport { + if len(stopped.Errs) == 0 { + fmt.Println(stopped.Id) + } else { + podStopErrors = append(podStopErrors, stopped.Errs...) + } + } + // Dump any stop errors + lastStopError := podStopErrors.PrintErrors() + if lastStopError != nil { + fmt.Fprintf(os.Stderr, "Error: %s\n", lastStopError) + } + + // Output rm'd pods + fmt.Println("Pods removed:") + for _, removed := range reports.RmReport { + if removed.Err == nil { + fmt.Println(removed.Id) + } else { + podRmErrors = append(podRmErrors, removed.Err) + } + } + return podRmErrors.PrintErrors() +} + +func playkube(yamlfile string) error { + report, err := registry.ContainerEngine().PlayKube(registry.GetContext(), yamlfile, kubeOptions.PlayKubeOptions) + if err != nil { + return err + } // Print volumes report for i, volume := range report.Volumes { if i == 0 { -- cgit v1.2.3-54-g00ecf