chart.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "context"
  16. "fmt"
  17. "os"
  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. log.Logf("installing chart %s", c.ReleaseName)
  69. cmd := exec.Command("helm", args...)
  70. cmd.Stdout = os.Stdout
  71. cmd.Stderr = os.Stderr
  72. return cmd.Run()
  73. }
  74. // Uninstall removes the chart aswell as the repo.
  75. func (c *HelmChart) Uninstall() error {
  76. args := []string{"delete", "--namespace", c.Namespace, c.ReleaseName}
  77. cmd := exec.Command("helm", args...)
  78. cmd.Stdout = os.Stdout
  79. cmd.Stderr = os.Stderr
  80. err := cmd.Run()
  81. if err != nil {
  82. return err
  83. }
  84. return c.removeRepo()
  85. }
  86. func (c *HelmChart) addRepo() error {
  87. if c.Repo.Name == "" || c.Repo.URL == "" {
  88. return nil
  89. }
  90. log.Logf("adding repo %s", c.Repo.Name)
  91. args := []string{"repo", "add", c.Repo.Name, c.Repo.URL}
  92. cmd := exec.Command("helm", args...)
  93. cmd.Stdout = os.Stdout
  94. cmd.Stderr = os.Stderr
  95. return cmd.Run()
  96. }
  97. func (c *HelmChart) removeRepo() error {
  98. if c.Repo.Name == "" || c.Repo.URL == "" {
  99. return nil
  100. }
  101. args := []string{"repo", "remove", c.Repo.Name}
  102. cmd := exec.Command("helm", args...)
  103. cmd.Stdout = os.Stdout
  104. cmd.Stderr = os.Stderr
  105. return cmd.Run()
  106. }
  107. // Logs fetches the logs from all pods managed by this release
  108. // and prints them out.
  109. func (c *HelmChart) Logs() error {
  110. kc := c.config.KubeClientSet
  111. podList, err := kc.CoreV1().Pods(c.Namespace).List(
  112. context.TODO(),
  113. metav1.ListOptions{LabelSelector: "app.kubernetes.io/instance=" + c.ReleaseName})
  114. if err != nil {
  115. return err
  116. }
  117. log.Logf("logs: found %d pods", len(podList.Items))
  118. for i := range podList.Items {
  119. pod := podList.Items[i]
  120. for _, con := range pod.Spec.Containers {
  121. for _, b := range []bool{true, false} {
  122. resp := kc.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
  123. Container: con.Name,
  124. Previous: b,
  125. }).Do(context.TODO())
  126. err := resp.Error()
  127. if err != nil {
  128. continue
  129. }
  130. logs, err := resp.Raw()
  131. if err != nil {
  132. continue
  133. }
  134. log.Logf("[%s]: %s", c.ReleaseName, string(logs))
  135. }
  136. }
  137. }
  138. return nil
  139. }