From 1bf5bdc689b94c3dd39eb1b5843a037ff4e4e1b6 Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Thu, 6 Apr 2023 14:04:37 -0400 Subject: [PATCH] Read kube_generate_type from containers.conf Use the kube_generate_type from the containers.conf as the default value for the --type flag for kube generate. Override the default when userexplicitly sets the --type flag. Signed-off-by: Urvashi Mohnani --- cmd/podman/kube/generate.go | 25 +++++++----------- pkg/api/handlers/libpod/generate.go | 15 ++++++++--- test/e2e/config/containers.conf | 1 + test/system/710-kube.bats | 41 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/cmd/podman/kube/generate.go b/cmd/podman/kube/generate.go index 046f697bef..ab8e83c498 100644 --- a/cmd/podman/kube/generate.go +++ b/cmd/podman/kube/generate.go @@ -4,14 +4,12 @@ import ( "fmt" "io" "os" - "strings" "github.com/containers/common/pkg/completion" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/generate" "github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/utils" - "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/pkg/domain/entities" "github.com/spf13/cobra" ) @@ -53,16 +51,16 @@ func init() { Command: generateKubeCmd, Parent: generate.GenerateCmd, }) - generateFlags(generateKubeCmd) + generateFlags(generateKubeCmd, registry.PodmanConfig()) registry.Commands = append(registry.Commands, registry.CliCommand{ Command: kubeGenerateCmd, Parent: kubeCmd, }) - generateFlags(kubeGenerateCmd) + generateFlags(kubeGenerateCmd, registry.PodmanConfig()) } -func generateFlags(cmd *cobra.Command) { +func generateFlags(cmd *cobra.Command, podmanConfig *entities.PodmanConfig) { flags := cmd.Flags() flags.BoolVarP(&generateOptions.Service, "service", "s", false, "Generate YAML for a Kubernetes service object") @@ -70,9 +68,13 @@ func generateFlags(cmd *cobra.Command) { flags.StringVarP(&generateFile, filenameFlagName, "f", "", "Write output to the specified path") _ = cmd.RegisterFlagCompletionFunc(filenameFlagName, completion.AutocompleteDefault) - // TODO: default should be configurable in containers.conf typeFlagName := "type" - flags.StringVarP(&generateOptions.Type, typeFlagName, "t", define.K8sKindPod, "Generate YAML for the given Kubernetes kind") + // If remote, don't read the client's containers.conf file + defaultGenerateType := "" + if !registry.IsRemote() { + defaultGenerateType = podmanConfig.ContainersConfDefaultsRO.Engine.KubeGenerateType + } + flags.StringVarP(&generateOptions.Type, typeFlagName, "t", defaultGenerateType, "Generate YAML for the given Kubernetes kind") _ = cmd.RegisterFlagCompletionFunc(typeFlagName, completion.AutocompleteNone) replicasFlagName := "replicas" @@ -83,15 +85,6 @@ func generateFlags(cmd *cobra.Command) { } func generateKube(cmd *cobra.Command, args []string) error { - typeVal, err := cmd.Flags().GetString("type") - if err != nil { - return err - } - typeVal = strings.ToLower(typeVal) - if typeVal != define.K8sKindPod && typeVal != define.K8sKindDeployment { - return fmt.Errorf("invalid type given, only supported types are pod and deployment") - } - report, err := registry.ContainerEngine().GenerateKube(registry.GetContext(), args, generateOptions) if err != nil { return err diff --git a/pkg/api/handlers/libpod/generate.go b/pkg/api/handlers/libpod/generate.go index af4ec89222..439bc60da9 100644 --- a/pkg/api/handlers/libpod/generate.go +++ b/pkg/api/handlers/libpod/generate.go @@ -5,7 +5,6 @@ import ( "net/http" "github.com/containers/podman/v4/libpod" - "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/pkg/api/handlers/utils" api "github.com/containers/podman/v4/pkg/api/types" "github.com/containers/podman/v4/pkg/domain/entities" @@ -96,7 +95,6 @@ func GenerateKube(w http.ResponseWriter, r *http.Request) { Replicas int32 `schema:"replicas"` }{ // Defaults would go here. - Type: define.K8sKindPod, Replicas: 1, } @@ -105,8 +103,19 @@ func GenerateKube(w http.ResponseWriter, r *http.Request) { return } + // Read the default kubeGenerateType from containers.conf it the user doesn't specify it + generateType := query.Type + if generateType == "" { + config, err := runtime.GetConfigNoCopy() + if err != nil { + utils.Error(w, http.StatusInternalServerError, err) + return + } + generateType = config.Engine.KubeGenerateType + } + containerEngine := abi.ContainerEngine{Libpod: runtime} - options := entities.GenerateKubeOptions{Service: query.Service, Type: query.Type, Replicas: query.Replicas} + options := entities.GenerateKubeOptions{Service: query.Service, Type: generateType, Replicas: query.Replicas} report, err := containerEngine.GenerateKube(r.Context(), query.Names, options) if err != nil { utils.Error(w, http.StatusInternalServerError, fmt.Errorf("generating YAML: %w", err)) diff --git a/test/e2e/config/containers.conf b/test/e2e/config/containers.conf index 77c7c3fd6c..f4415e99a3 100644 --- a/test/e2e/config/containers.conf +++ b/test/e2e/config/containers.conf @@ -62,6 +62,7 @@ no_hosts=true network_cmd_options=["allow_host_loopback=true"] service_timeout=1234 +kube_generate_type="deployment" volume_plugin_timeout = 15 diff --git a/test/system/710-kube.bats b/test/system/710-kube.bats index ce5edf4513..5408528ac7 100644 --- a/test/system/710-kube.bats +++ b/test/system/710-kube.bats @@ -135,4 +135,45 @@ status | = | null run_podman rmi $(pause_image) } +@test "podman kube generate - deployment" { + skip_if_remote "containersconf needs to be set on server side" + local pname=p$(random_string 15) + local cname1=c1$(random_string 15) + local cname2=c2$(random_string 15) + + run_podman pod create --name $pname + run_podman container create --name $cname1 --pod $pname $IMAGE top + run_podman container create --name $cname2 --pod $pname $IMAGE bottom + + containersconf=$PODMAN_TMPDIR/containers.conf + cat >$containersconf <