config.go 4.1 KB

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