agent.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package helm
  2. import (
  3. "io/ioutil"
  4. "github.com/porter-dev/porter/internal/config"
  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/kube"
  9. "helm.sh/helm/v3/pkg/release"
  10. "helm.sh/helm/v3/pkg/storage"
  11. "helm.sh/helm/v3/pkg/chartutil"
  12. kubefake "helm.sh/helm/v3/pkg/kube/fake"
  13. )
  14. // Agent is a Helm agent for performing helm operations
  15. type Agent struct {
  16. ActionConfig *action.Configuration
  17. }
  18. // Form represents the options for connecting to a cluster and
  19. // creating a Helm agent
  20. type Form struct {
  21. KubeConfig []byte
  22. AllowedContexts []string
  23. Context string `json:"context" form:"required"`
  24. Storage string `json:"storage" form:"oneof=secret configmap memory"`
  25. Namespace string `json:"namespace"`
  26. }
  27. // ToAgent uses the Form to generate an agent. Setting testing=true will create
  28. // a test agent with in-memory storage
  29. func (h *Form) ToAgent(
  30. l *logger.Logger,
  31. helmConf *config.HelmGlobalConf,
  32. storage *storage.Storage,
  33. ) (*Agent, error) {
  34. if helmConf.IsTesting {
  35. testStorage := storage
  36. if testStorage == nil {
  37. testStorage = StorageMap["memory"](nil, h.Namespace, nil)
  38. }
  39. return &Agent{&action.Configuration{
  40. Releases: testStorage,
  41. KubeClient: &kubefake.FailingKubeClient{
  42. PrintingKubeClient: kubefake.PrintingKubeClient{
  43. Out: ioutil.Discard,
  44. },
  45. },
  46. Capabilities: chartutil.DefaultCapabilities,
  47. Log: l.Printf,
  48. }}, nil
  49. }
  50. // create a kubernetes agent
  51. k8sForm := &kubernetes.Form{
  52. KubeConfig: h.KubeConfig,
  53. AllowedContexts: h.AllowedContexts,
  54. Context: h.Context,
  55. }
  56. k8sAgent, err := k8sForm.ToAgent()
  57. if err != nil {
  58. return nil, err
  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[h.Storage](l, h.Namespace, k8sAgent.Clientset),
  65. Log: l.Printf,
  66. }}, nil
  67. }
  68. // ListReleases lists releases based on a ListFilter
  69. func (a *Agent) ListReleases(
  70. namespace string,
  71. filter *ListFilter,
  72. ) ([]*release.Release, error) {
  73. cmd := action.NewList(a.ActionConfig)
  74. filter.apply(cmd)
  75. return cmd.Run()
  76. }
  77. // GetRelease returns the info of a release.
  78. func (a *Agent) GetRelease(
  79. name string,
  80. version int,
  81. ) (*release.Release, error) {
  82. // Namespace is already known by the RESTClientGetter.
  83. cmd := action.NewGet(a.ActionConfig)
  84. cmd.Version = version
  85. return cmd.Run(name)
  86. }