chart.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. config *Config
  35. }
  36. type ChartRepo struct {
  37. Name string
  38. URL string
  39. }
  40. type StringTuple struct {
  41. Key string
  42. Value string
  43. }
  44. // Setup stores the config in an internal field
  45. // to get access to the k8s api in orderto fetch logs.
  46. func (c *HelmChart) Setup(cfg *Config) error {
  47. c.config = cfg
  48. return nil
  49. }
  50. // Install adds the chart repo and installs the helm chart.
  51. func (c *HelmChart) Install() error {
  52. args := []string{
  53. "dependency", "update", filepath.Join(AssetDir(), "deploy/charts/external-secrets"),
  54. }
  55. log.Logf("updating chart dependencies with args: %+q", args)
  56. cmd := exec.Command("helm", args...)
  57. output, err := cmd.CombinedOutput()
  58. if err != nil {
  59. return fmt.Errorf("unable to run update cmd: %w: %s", err, string(output))
  60. }
  61. err = c.addRepo()
  62. if err != nil {
  63. return err
  64. }
  65. args = []string{"install", c.ReleaseName, c.Chart,
  66. "--dependency-update",
  67. "--debug",
  68. "--wait",
  69. "--timeout", "600s",
  70. "-o", "yaml",
  71. "--namespace", c.Namespace,
  72. }
  73. if c.ChartVersion != "" {
  74. args = append(args, "--version", c.ChartVersion)
  75. }
  76. for _, v := range c.Values {
  77. args = append(args, "--values", v)
  78. }
  79. for _, s := range c.Vars {
  80. args = append(args, "--set", fmt.Sprintf("%s=%s", s.Key, s.Value))
  81. }
  82. args = append(args, c.Args...)
  83. log.Logf("installing chart with args: %+q", args)
  84. cmd = exec.Command("helm", args...)
  85. output, err = cmd.CombinedOutput()
  86. if err != nil {
  87. return fmt.Errorf("unable to run cmd: %w: %s", err, string(output))
  88. }
  89. log.Logf("finished running chart install")
  90. return nil
  91. }
  92. // Uninstall removes the chart aswell as the repo.
  93. func (c *HelmChart) Uninstall() error {
  94. args := []string{"uninstall", "--namespace", c.Namespace, c.ReleaseName, "--wait"}
  95. cmd := exec.Command("helm", args...)
  96. output, err := cmd.CombinedOutput()
  97. if err != nil {
  98. return fmt.Errorf("unable to uninstall helm release: %w: %s", err, string(output))
  99. }
  100. return c.removeRepo()
  101. }
  102. func (c *HelmChart) addRepo() error {
  103. if c.Repo.Name == "" || c.Repo.URL == "" {
  104. return nil
  105. }
  106. var sout, serr bytes.Buffer
  107. args := []string{"repo", "add", c.Repo.Name, c.Repo.URL}
  108. cmd := exec.Command("helm", args...)
  109. cmd.Stdout = &sout
  110. cmd.Stderr = &serr
  111. err := cmd.Run()
  112. if err != nil {
  113. return fmt.Errorf("unable to add helm repo: %w: %s, %s", err, sout.String(), serr.String())
  114. }
  115. return nil
  116. }
  117. func (c *HelmChart) removeRepo() error {
  118. if c.Repo.Name == "" || c.Repo.URL == "" {
  119. return nil
  120. }
  121. args := []string{"repo", "remove", c.Repo.Name}
  122. cmd := exec.Command("helm", args...)
  123. output, err := cmd.CombinedOutput()
  124. if err != nil {
  125. return fmt.Errorf("unable to remove repo: %w: %s", err, string(output))
  126. }
  127. return nil
  128. }
  129. // Logs fetches the logs from all pods managed by this release
  130. // and prints them out.
  131. func (c *HelmChart) Logs() error {
  132. kc := c.config.KubeClientSet
  133. podList, err := kc.CoreV1().Pods(c.Namespace).List(
  134. GinkgoT().Context(),
  135. metav1.ListOptions{LabelSelector: "app.kubernetes.io/instance=" + c.ReleaseName})
  136. if err != nil {
  137. return err
  138. }
  139. log.Logf("logs: found %d pods", len(podList.Items))
  140. tailLines := int64(200)
  141. for i := range podList.Items {
  142. pod := podList.Items[i]
  143. for _, con := range pod.Spec.Containers {
  144. for _, b := range []bool{true, false} {
  145. resp := kc.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
  146. Container: con.Name,
  147. Previous: b,
  148. TailLines: &tailLines,
  149. }).Do(GinkgoT().Context())
  150. err := resp.Error()
  151. if err != nil {
  152. continue
  153. }
  154. logs, err := resp.Raw()
  155. if err != nil {
  156. continue
  157. }
  158. log.Logf("[%s]: %s", c.ReleaseName, string(logs))
  159. }
  160. }
  161. }
  162. return nil
  163. }
  164. func (c *HelmChart) HasVar(key, value string) bool {
  165. for _, v := range c.Vars {
  166. if v.Key == key && v.Value == value {
  167. return true
  168. }
  169. }
  170. return false
  171. }