config.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package helm
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "github.com/porter-dev/porter/internal/kubernetes"
  6. "github.com/porter-dev/porter/internal/logger"
  7. "helm.sh/helm/v3/pkg/action"
  8. "helm.sh/helm/v3/pkg/chartutil"
  9. "helm.sh/helm/v3/pkg/kube"
  10. kubefake "helm.sh/helm/v3/pkg/kube/fake"
  11. "helm.sh/helm/v3/pkg/storage"
  12. k8s "k8s.io/client-go/kubernetes"
  13. )
  14. // Form represents the options for connecting to a cluster and
  15. // creating a Helm agent
  16. type Form struct {
  17. KubeConfig []byte
  18. AllowedContexts []string
  19. Context string `json:"context" form:"required"`
  20. Storage string `json:"storage" form:"oneof=secret configmap memory"`
  21. Namespace string `json:"namespace"`
  22. }
  23. // GetAgentOutOfClusterConfig creates a new Agent from outside the cluster using
  24. // the underlying kubernetes.GetAgentOutOfClusterConfig method
  25. func GetAgentOutOfClusterConfig(form *Form, l *logger.Logger) (*Agent, error) {
  26. // create a kubernetes agent
  27. conf := &kubernetes.OutOfClusterConfig{
  28. KubeConfig: form.KubeConfig,
  29. AllowedContexts: form.AllowedContexts,
  30. Context: form.Context,
  31. }
  32. k8sAgent, err := kubernetes.GetAgentOutOfClusterConfig(conf)
  33. if err != nil {
  34. return nil, err
  35. }
  36. clientset, ok := k8sAgent.Clientset.(*k8s.Clientset)
  37. if !ok {
  38. return nil, errors.New("Agent Clientset was not of type *(k8s.io/client-go/kubernetes).Clientset")
  39. }
  40. // use k8s agent to create Helm agent
  41. return &Agent{&action.Configuration{
  42. RESTClientGetter: k8sAgent.RESTClientGetter,
  43. KubeClient: kube.New(k8sAgent.RESTClientGetter),
  44. Releases: StorageMap[form.Storage](l, clientset.CoreV1(), form.Namespace),
  45. Log: l.Printf,
  46. }}, nil
  47. }
  48. // GetAgentInClusterConfig creates a new Agent from inside the cluster using
  49. // the underlying kubernetes.GetAgentInClusterConfig method
  50. func GetAgentInClusterConfig(form *Form, l *logger.Logger) (*Agent, error) {
  51. // create a kubernetes agent
  52. k8sAgent, err := kubernetes.GetAgentInClusterConfig()
  53. if err != nil {
  54. return nil, err
  55. }
  56. clientset, ok := k8sAgent.Clientset.(*k8s.Clientset)
  57. if !ok {
  58. return nil, errors.New("Agent Clientset was not of type *(k8s.io/client-go/kubernetes).Clientset")
  59. }
  60. // use k8s agent to create Helm agent
  61. return &Agent{&action.Configuration{
  62. RESTClientGetter: k8sAgent.RESTClientGetter,
  63. KubeClient: kube.New(k8sAgent.RESTClientGetter),
  64. Releases: StorageMap[form.Storage](l, clientset.CoreV1(), form.Namespace),
  65. Log: l.Printf,
  66. }}, nil
  67. }
  68. // GetAgentTesting creates a new Agent using an optional existing storage class
  69. func GetAgentTesting(form *Form, storage *storage.Storage, l *logger.Logger) *Agent {
  70. testStorage := storage
  71. if testStorage == nil {
  72. testStorage = StorageMap["memory"](nil, nil, "")
  73. }
  74. return &Agent{&action.Configuration{
  75. Releases: testStorage,
  76. KubeClient: &kubefake.FailingKubeClient{
  77. PrintingKubeClient: kubefake.PrintingKubeClient{
  78. Out: ioutil.Discard,
  79. },
  80. },
  81. Capabilities: chartutil.DefaultCapabilities,
  82. Log: l.Printf,
  83. }}
  84. }