config.go 4.1 KB

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