|
| 1 | +package kube |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/containers/common/pkg/completion" |
| 10 | + "github.com/containers/podman/v4/cmd/podman/common" |
| 11 | + "github.com/containers/podman/v4/cmd/podman/registry" |
| 12 | + "github.com/containers/podman/v4/cmd/podman/utils" |
| 13 | + "github.com/containers/podman/v4/pkg/domain/entities" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + applyOptions = entities.ApplyOptions{} |
| 19 | + applyDescription = `Command applies a podman container, pod, volume, or kube yaml to a Kubernetes cluster when a kubeconfig file is given.` |
| 20 | + |
| 21 | + applyCmd = &cobra.Command{ |
| 22 | + Use: "apply [options] [CONTAINER...|POD...|VOLUME...]", |
| 23 | + Short: "Deploy a podman container, pod, volume, or Kubernetes yaml to a Kubernetes cluster", |
| 24 | + Long: applyDescription, |
| 25 | + RunE: apply, |
| 26 | + ValidArgsFunction: common.AutocompleteForKube, |
| 27 | + Example: `podman kube apply ctrName volName |
| 28 | + podman kube apply --namespace project -f fileName`, |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + registry.Commands = append(registry.Commands, registry.CliCommand{ |
| 34 | + Command: applyCmd, |
| 35 | + Parent: kubeCmd, |
| 36 | + }) |
| 37 | + applyFlags(applyCmd) |
| 38 | +} |
| 39 | + |
| 40 | +func applyFlags(cmd *cobra.Command) { |
| 41 | + flags := cmd.Flags() |
| 42 | + flags.SetNormalizeFunc(utils.AliasFlags) |
| 43 | + |
| 44 | + kubeconfigFlagName := "kubeconfig" |
| 45 | + flags.StringVarP(&applyOptions.Kubeconfig, kubeconfigFlagName, "k", os.Getenv("KUBECONFIG"), "Path to the kubeconfig file for the Kubernetes cluster") |
| 46 | + _ = cmd.RegisterFlagCompletionFunc(kubeconfigFlagName, completion.AutocompleteDefault) |
| 47 | + |
| 48 | + namespaceFlagName := "ns" |
| 49 | + flags.StringVarP(&applyOptions.Namespace, namespaceFlagName, "", "", "The namespace to deploy the workload to on the Kubernetes cluster") |
| 50 | + _ = cmd.RegisterFlagCompletionFunc(namespaceFlagName, completion.AutocompleteNone) |
| 51 | + |
| 52 | + caCertFileFlagName := "ca-cert-file" |
| 53 | + flags.StringVarP(&applyOptions.CACertFile, caCertFileFlagName, "", "", "Path to the CA cert file for the Kubernetes cluster.") |
| 54 | + _ = cmd.RegisterFlagCompletionFunc(caCertFileFlagName, completion.AutocompleteDefault) |
| 55 | + |
| 56 | + fileFlagName := "file" |
| 57 | + flags.StringVarP(&applyOptions.File, fileFlagName, "f", "", "Path to the Kubernetes yaml file to deploy.") |
| 58 | + _ = cmd.RegisterFlagCompletionFunc(fileFlagName, completion.AutocompleteDefault) |
| 59 | + |
| 60 | + serviceFlagName := "service" |
| 61 | + flags.BoolVarP(&applyOptions.Service, serviceFlagName, "s", false, "Create a service object for the container being deployed.") |
| 62 | +} |
| 63 | + |
| 64 | +func apply(cmd *cobra.Command, args []string) error { |
| 65 | + if cmd.Flags().Changed("file") && cmd.Flags().Changed("service") { |
| 66 | + return errors.New("cannot set --service and --file at the same time") |
| 67 | + } |
| 68 | + |
| 69 | + kubeconfig, err := cmd.Flags().GetString("kubeconfig") |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + if kubeconfig == "" { |
| 74 | + return errors.New("kubeconfig not given, unable to connect to cluster") |
| 75 | + } |
| 76 | + |
| 77 | + var reader io.Reader |
| 78 | + if cmd.Flags().Changed("file") { |
| 79 | + yamlFile := applyOptions.File |
| 80 | + if yamlFile == "-" { |
| 81 | + yamlFile = os.Stdin.Name() |
| 82 | + } |
| 83 | + |
| 84 | + f, err := os.Open(yamlFile) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + defer f.Close() |
| 89 | + reader = f |
| 90 | + } else { |
| 91 | + generateOptions.Service = applyOptions.Service |
| 92 | + report, err := registry.ContainerEngine().GenerateKube(registry.GetContext(), args, generateOptions) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + if r, ok := report.Reader.(io.ReadCloser); ok { |
| 97 | + defer r.Close() |
| 98 | + } |
| 99 | + reader = report.Reader |
| 100 | + } |
| 101 | + |
| 102 | + fmt.Println("Deploying to cluster...") |
| 103 | + |
| 104 | + if err = registry.ContainerEngine().KubeApply(registry.GetContext(), reader, applyOptions); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + fmt.Println("Successfully deployed workloads to cluster!") |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
0 commit comments