agent.go 2.5 KB

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