chart.go 4.2 KB

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