chart.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package addon
  14. import (
  15. "bytes"
  16. "fmt"
  17. "os/exec"
  18. "path/filepath"
  19. . "github.com/onsi/ginkgo/v2"
  20. corev1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "github.com/external-secrets/external-secrets-e2e/framework/log"
  23. )
  24. // HelmChart installs the specified Chart into the cluster.
  25. type HelmChart struct {
  26. Namespace string
  27. ReleaseName string
  28. Chart string
  29. ChartVersion string
  30. Repo ChartRepo
  31. Vars []StringTuple
  32. Values []string
  33. Args []string
  34. // InstallTimeout overrides `helm install --timeout` (empty = default).
  35. InstallTimeout string
  36. config *Config
  37. }
  38. type ChartRepo struct {
  39. Name string
  40. URL string
  41. }
  42. type StringTuple struct {
  43. Key string
  44. Value string
  45. }
  46. // Setup stores the config in an internal field
  47. // to get access to the k8s api in orderto fetch logs.
  48. func (c *HelmChart) Setup(cfg *Config) error {
  49. c.config = cfg
  50. return nil
  51. }
  52. // defaultInstallTimeout is the `helm install --wait` timeout used when a chart
  53. // does not set InstallTimeout.
  54. const defaultInstallTimeout = "600s"
  55. // Install adds the chart repo and installs the helm chart.
  56. func (c *HelmChart) Install() error {
  57. args := []string{
  58. "dependency", "update", filepath.Join(AssetDir(), "deploy/charts/external-secrets"),
  59. }
  60. log.Logf("updating chart dependencies with args: %+q", args)
  61. cmd := exec.Command("helm", args...)
  62. output, err := cmd.CombinedOutput()
  63. if err != nil {
  64. return fmt.Errorf("unable to run update cmd: %w: %s", err, string(output))
  65. }
  66. err = c.addRepo()
  67. if err != nil {
  68. return err
  69. }
  70. timeout := c.InstallTimeout
  71. if timeout == "" {
  72. timeout = defaultInstallTimeout
  73. }
  74. args = []string{"install", c.ReleaseName, c.Chart,
  75. "--dependency-update",
  76. "--debug",
  77. "--wait",
  78. "--timeout", timeout,
  79. "-o", "yaml",
  80. "--namespace", c.Namespace,
  81. }
  82. if c.ChartVersion != "" {
  83. args = append(args, "--version", c.ChartVersion)
  84. }
  85. for _, v := range c.Values {
  86. args = append(args, "--values", v)
  87. }
  88. for _, s := range c.Vars {
  89. args = append(args, "--set", fmt.Sprintf("%s=%s", s.Key, s.Value))
  90. }
  91. args = append(args, c.Args...)
  92. log.Logf("installing chart with args: %+q", args)
  93. cmd = exec.Command("helm", args...)
  94. output, err = cmd.CombinedOutput()
  95. if err != nil {
  96. return fmt.Errorf("unable to run cmd: %w: %s", err, string(output))
  97. }
  98. log.Logf("finished running chart install")
  99. return nil
  100. }
  101. // Uninstall removes the chart aswell as the repo.
  102. func (c *HelmChart) Uninstall() error {
  103. args := []string{"uninstall", "--namespace", c.Namespace, c.ReleaseName, "--wait"}
  104. cmd := exec.Command("helm", args...)
  105. output, err := cmd.CombinedOutput()
  106. if err != nil {
  107. return fmt.Errorf("unable to uninstall helm release: %w: %s", err, string(output))
  108. }
  109. return c.removeRepo()
  110. }
  111. func (c *HelmChart) addRepo() error {
  112. if c.Repo.Name == "" || c.Repo.URL == "" {
  113. return nil
  114. }
  115. var sout, serr bytes.Buffer
  116. args := []string{"repo", "add", c.Repo.Name, c.Repo.URL}
  117. cmd := exec.Command("helm", args...)
  118. cmd.Stdout = &sout
  119. cmd.Stderr = &serr
  120. err := cmd.Run()
  121. if err != nil {
  122. return fmt.Errorf("unable to add helm repo: %w: %s, %s", err, sout.String(), serr.String())
  123. }
  124. return nil
  125. }
  126. func (c *HelmChart) removeRepo() error {
  127. if c.Repo.Name == "" || c.Repo.URL == "" {
  128. return nil
  129. }
  130. args := []string{"repo", "remove", c.Repo.Name}
  131. cmd := exec.Command("helm", args...)
  132. output, err := cmd.CombinedOutput()
  133. if err != nil {
  134. return fmt.Errorf("unable to remove repo: %w: %s", err, string(output))
  135. }
  136. return nil
  137. }
  138. // Logs fetches the logs from all pods managed by this release
  139. // and prints them out.
  140. func (c *HelmChart) Logs() error {
  141. kc := c.config.KubeClientSet
  142. podList, err := kc.CoreV1().Pods(c.Namespace).List(
  143. GinkgoT().Context(),
  144. metav1.ListOptions{LabelSelector: "app.kubernetes.io/instance=" + c.ReleaseName})
  145. if err != nil {
  146. return err
  147. }
  148. log.Logf("logs: found %d pods", len(podList.Items))
  149. tailLines := int64(200)
  150. for i := range podList.Items {
  151. pod := podList.Items[i]
  152. for _, con := range pod.Spec.Containers {
  153. for _, b := range []bool{true, false} {
  154. resp := kc.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
  155. Container: con.Name,
  156. Previous: b,
  157. TailLines: &tailLines,
  158. }).Do(GinkgoT().Context())
  159. err := resp.Error()
  160. if err != nil {
  161. continue
  162. }
  163. logs, err := resp.Raw()
  164. if err != nil {
  165. continue
  166. }
  167. log.Logf("[%s]: %s", c.ReleaseName, string(logs))
  168. }
  169. }
  170. }
  171. return nil
  172. }
  173. func (c *HelmChart) HasVar(key, value string) bool {
  174. for _, v := range c.Vars {
  175. if v.Key == key && v.Value == value {
  176. return true
  177. }
  178. }
  179. return false
  180. }