Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read kube_generate_type from containers.conf #18140

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions cmd/podman/kube/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -53,26 +51,30 @@ 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")

filenameFlagName := "filename"
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"
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions pkg/api/handlers/libpod/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}

Expand All @@ -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))
Expand Down
1 change: 1 addition & 0 deletions test/e2e/config/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions test/system/710-kube.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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)
umohnani8 marked this conversation as resolved.
Show resolved Hide resolved

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 <<EOF
[engine]
kube_generate_type="deployment"
EOF
CONTAINERS_CONF_OVERRIDE=$containersconf run_podman kube generate $pname

json=$(yaml2json <<<"$output")
# For debugging purposes in the event we regress, we can see the generate output to know what went wrong
jq . <<<"$json"

# See container test above for description of this table
expect="
apiVersion | = | apps/v1
kind | = | Deployment

metadata.creationTimestamp | =~ | [0-9T:-]\\+Z
metadata.labels.app | = | ${pname}
metadata.name | = | ${pname}-deployment
"

while read key op expect; do
actual=$(jq -r -c ".$key" <<<"$json")
assert "$actual" $op "$expect" ".$key"
done < <(parse_table "$expect")

run_podman rm $cname1 $cname2
run_podman pod rm $pname
run_podman rmi $(pause_image)
}

# vim: filetype=sh