Skip to content

Commit

Permalink
Merge pull request #18140 from umohnani8/deployments
Browse files Browse the repository at this point in the history
Read kube_generate_type from containers.conf
  • Loading branch information
openshift-merge-robot authored Apr 11, 2023
2 parents 8c4838f + 1bf5bdc commit cf3374e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
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)

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

0 comments on commit cf3374e

Please sign in to comment.