config.go 3.7 KB

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