chart.go 4.2 KB

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