1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package kube
import (
"fmt"
"io"
"io/ioutil"
"os"
"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/pkg/domain/entities"
"github.com/spf13/cobra"
)
var (
generateOptions = entities.GenerateKubeOptions{}
generateFile = ""
generateDescription = `Command generates Kubernetes Pod, Service or PersistenVolumeClaim YAML (v1 specification) from Podman containers, pods or volumes.
Whether the input is for a container or pod, Podman will always generate the specification as a pod.`
kubeGenerateCmd = &cobra.Command{
Use: "generate [options] {CONTAINER...|POD...|VOLUME...}",
Short: "Generate Kubernetes YAML from containers, pods or volumes.",
Long: generateDescription,
RunE: generateKube,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: common.AutocompleteForGenerate,
Example: `podman kube generate ctrID
podman kube generate podID
podman kube generate --service podID
podman kube generate volumeName
podman kube generate ctrID podID volumeName --service`,
}
generateKubeCmd = &cobra.Command{
Use: "kube [options] {CONTAINER...|POD...|VOLUME...}",
Short: kubeGenerateCmd.Short,
Long: kubeGenerateCmd.Long,
RunE: kubeGenerateCmd.RunE,
Args: kubeGenerateCmd.Args,
ValidArgsFunction: kubeGenerateCmd.ValidArgsFunction,
Example: kubeGenerateCmd.Example,
}
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: generateKubeCmd,
Parent: generate.GenerateCmd,
})
generateFlags(generateKubeCmd)
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: kubeGenerateCmd,
Parent: kubeCmd,
})
generateFlags(kubeGenerateCmd)
}
func generateFlags(cmd *cobra.Command) {
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)
flags.SetNormalizeFunc(utils.AliasFlags)
}
func generateKube(cmd *cobra.Command, args []string) error {
report, err := registry.ContainerEngine().GenerateKube(registry.GetContext(), args, generateOptions)
if err != nil {
return err
}
content, err := ioutil.ReadAll(report.Reader)
if err != nil {
return err
}
if r, ok := report.Reader.(io.ReadCloser); ok {
defer r.Close()
}
if cmd.Flags().Changed("filename") {
if _, err := os.Stat(generateFile); err == nil {
return fmt.Errorf("cannot write to %q; file exists", generateFile)
}
if err := ioutil.WriteFile(generateFile, content, 0644); err != nil {
return fmt.Errorf("cannot write to %q: %w", generateFile, err)
}
return nil
}
fmt.Println(string(content))
return nil
}
|