helm.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/cli/cmd/config"
  11. "github.com/spf13/cobra"
  12. )
  13. func registerCommand_Helm(cliConf config.CLIConfig) *cobra.Command {
  14. depMsg := `This command is no longer available. Please consult documentation of the respective cloud provider to get access to the kubeconfig of the cluster.
  15. Note that any change made directly on the kubernetes cluster under the hood can degrade the performance and reliability of the cluster, and Porter will
  16. automatically reconcile any changes that pose a threat to the uptime of the cluster to its original state. Porter is not responsible for the issues that
  17. arise due to the change implemented directly on the Kubernetes cluster via kubectl.`
  18. helmCmd := &cobra.Command{
  19. Use: "helm",
  20. Short: "Use helm to interact with a Porter cluster",
  21. Deprecated: depMsg,
  22. Run: func(cmd *cobra.Command, args []string) {
  23. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runHelm)
  24. if err != nil {
  25. os.Exit(1)
  26. }
  27. },
  28. }
  29. return helmCmd
  30. }
  31. func runHelm(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
  32. // this will never error because it just ran
  33. user, _ := client.AuthCheck(ctx)
  34. if !strings.HasSuffix(user.Email, "@porter.run") {
  35. return fmt.Errorf("Forbidden")
  36. }
  37. _, err := exec.LookPath("helm")
  38. if err != nil {
  39. return fmt.Errorf("error finding helm: %w", err)
  40. }
  41. tmpFile, err := downloadTempKubeconfig(ctx, client, cliConf)
  42. if err != nil {
  43. return err
  44. }
  45. defer func() {
  46. os.Remove(tmpFile)
  47. }()
  48. os.Setenv("KUBECONFIG", tmpFile)
  49. execCommand := exec.Command("helm", args...)
  50. execCommand.Stdin = os.Stdin
  51. execCommand.Stdout = os.Stdout
  52. execCommand.Stderr = os.Stderr
  53. err = execCommand.Run()
  54. if err != nil {
  55. return fmt.Errorf("error running helm: %w", err)
  56. }
  57. return nil
  58. }