chart.go 4.6 KB

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