2
0

kubectl.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/cli/cmd/config"
  10. "github.com/spf13/cobra"
  11. )
  12. func registerCommand_Kubectl(cliConf config.CLIConfig) *cobra.Command {
  13. kubectlCmd := &cobra.Command{
  14. Use: "kubectl",
  15. Short: "Use kubectl to interact with a Porter cluster",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runKubectl)
  18. if err != nil {
  19. os.Exit(1)
  20. }
  21. },
  22. }
  23. var printKubeconfig bool
  24. kubectlCmd.Flags().BoolVar(&printKubeconfig, "print-kubeconfig", false, "Print an authenticated kubeconfig to the console with a 15 minute expiry")
  25. return kubectlCmd
  26. }
  27. func runKubectl(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
  28. _, err := exec.LookPath("kubectl")
  29. if err != nil {
  30. return fmt.Errorf("error finding kubectl: %w", err)
  31. }
  32. printKubeconfig, err := cmd.Flags().GetBool("print-kubeconfig")
  33. if err != nil {
  34. return fmt.Errorf("error when retrieving print-kubeconfig flag")
  35. }
  36. tmpFile, err := downloadTempKubeconfig(ctx, client, cliConf)
  37. if err != nil {
  38. return err
  39. }
  40. defer func() {
  41. os.Remove(tmpFile)
  42. }()
  43. if printKubeconfig {
  44. kc, err := os.ReadFile(tmpFile) //nolint:gosec
  45. if err != nil {
  46. return fmt.Errorf("erro reading downloaded kubeconfig for printing: %w", err)
  47. }
  48. fmt.Println(string(kc))
  49. return nil
  50. }
  51. err = os.Setenv("KUBECONFIG", tmpFile)
  52. if err != nil {
  53. return fmt.Errorf("unable to set KUBECONFIG env var: %w", err)
  54. }
  55. execCommand := exec.Command("kubectl", args...)
  56. execCommand.Stdout = os.Stdout
  57. execCommand.Stderr = os.Stderr
  58. err = execCommand.Run()
  59. if err != nil {
  60. return fmt.Errorf("error running helm: %w", err)
  61. }
  62. return nil
  63. }
  64. func downloadTempKubeconfig(ctx context.Context, client api.Client, cliConf config.CLIConfig) (string, error) {
  65. tmpFile, err := os.CreateTemp("", "porter_kubeconfig_*.yaml")
  66. if err != nil {
  67. return "", fmt.Errorf("error creating temp file for kubeconfig: %w", err)
  68. }
  69. defer tmpFile.Close()
  70. resp, err := client.GetKubeconfig(ctx, cliConf.Project, cliConf.Cluster, cliConf.Kubeconfig)
  71. if err != nil {
  72. return "", fmt.Errorf("error fetching kubeconfig for cluster: %w", err)
  73. }
  74. _, err = tmpFile.Write(resp.Kubeconfig)
  75. if err != nil {
  76. return "", fmt.Errorf("error writing kubeconfig to temp file: %w", err)
  77. }
  78. return tmpFile.Name(), nil
  79. }