config.go 3.1 KB

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