chart.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 2020 The cert-manager 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. http://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. "context"
  17. "fmt"
  18. "os/exec"
  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. "--debug",
  57. "--wait",
  58. "--timeout", "600s",
  59. "-o", "yaml",
  60. "--namespace", c.Namespace,
  61. }
  62. if c.ChartVersion != "" {
  63. args = append(args, "--version", c.ChartVersion)
  64. }
  65. for _, v := range c.Values {
  66. args = append(args, "--values", v)
  67. }
  68. for _, s := range c.Vars {
  69. args = append(args, "--set", fmt.Sprintf("%s=%s", s.Key, s.Value))
  70. }
  71. args = append(args, c.Args...)
  72. var sout, serr bytes.Buffer
  73. log.Logf("installing chart with args: %+q", args)
  74. cmd := exec.Command("helm", args...)
  75. cmd.Stdout = &sout
  76. cmd.Stderr = &serr
  77. err = cmd.Run()
  78. if err != nil {
  79. return fmt.Errorf("unable to run cmd: %w: %s %s", err, sout.String(), serr.String())
  80. }
  81. return nil
  82. }
  83. // Uninstall removes the chart aswell as the repo.
  84. func (c *HelmChart) Uninstall() error {
  85. var sout, serr bytes.Buffer
  86. args := []string{"delete", "--namespace", c.Namespace, c.ReleaseName}
  87. cmd := exec.Command("helm", args...)
  88. cmd.Stdout = &sout
  89. cmd.Stderr = &serr
  90. err := cmd.Run()
  91. if err != nil {
  92. return fmt.Errorf("unable to delete helm release: %w: %s, %s", err, sout.String(), serr.String())
  93. }
  94. return c.removeRepo()
  95. }
  96. func (c *HelmChart) addRepo() error {
  97. if c.Repo.Name == "" || c.Repo.URL == "" {
  98. return nil
  99. }
  100. var sout, serr bytes.Buffer
  101. args := []string{"repo", "add", c.Repo.Name, c.Repo.URL}
  102. cmd := exec.Command("helm", args...)
  103. cmd.Stdout = &sout
  104. cmd.Stderr = &serr
  105. err := cmd.Run()
  106. if err != nil {
  107. return fmt.Errorf("unable to add helm repo: %w: %s, %s", err, sout.String(), serr.String())
  108. }
  109. return nil
  110. }
  111. func (c *HelmChart) removeRepo() error {
  112. if c.Repo.Name == "" || c.Repo.URL == "" {
  113. return nil
  114. }
  115. var sout, serr bytes.Buffer
  116. args := []string{"repo", "remove", c.Repo.Name}
  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 remove repo: %w: %s, %s", err, sout.String(), serr.String())
  123. }
  124. return nil
  125. }
  126. // Logs fetches the logs from all pods managed by this release
  127. // and prints them out.
  128. func (c *HelmChart) Logs() error {
  129. kc := c.config.KubeClientSet
  130. podList, err := kc.CoreV1().Pods(c.Namespace).List(
  131. context.TODO(),
  132. metav1.ListOptions{LabelSelector: "app.kubernetes.io/instance=" + c.ReleaseName})
  133. if err != nil {
  134. return err
  135. }
  136. log.Logf("logs: found %d pods", len(podList.Items))
  137. tailLines := int64(200)
  138. for i := range podList.Items {
  139. pod := podList.Items[i]
  140. for _, con := range pod.Spec.Containers {
  141. for _, b := range []bool{true, false} {
  142. resp := kc.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
  143. Container: con.Name,
  144. Previous: b,
  145. TailLines: &tailLines,
  146. }).Do(context.TODO())
  147. err := resp.Error()
  148. if err != nil {
  149. continue
  150. }
  151. logs, err := resp.Raw()
  152. if err != nil {
  153. continue
  154. }
  155. log.Logf("[%s]: %s", c.ReleaseName, string(logs))
  156. }
  157. }
  158. }
  159. return nil
  160. }
  161. func (c *HelmChart) HasVar(key, value string) bool {
  162. for _, v := range c.Vars {
  163. if v.Key == key && v.Value == value {
  164. return true
  165. }
  166. }
  167. return false
  168. }