addon.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright © The ESO 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. 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. "fmt"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "github.com/onsi/ginkgo/v2"
  21. "github.com/onsi/gomega"
  22. "k8s.io/client-go/kubernetes"
  23. "k8s.io/client-go/rest"
  24. crclient "sigs.k8s.io/controller-runtime/pkg/client"
  25. "github.com/external-secrets/external-secrets-e2e/framework/log"
  26. "github.com/external-secrets/external-secrets-e2e/framework/util"
  27. )
  28. var globalAddons []Addon
  29. func init() {
  30. globalAddons = make([]Addon, 0)
  31. }
  32. type Config struct {
  33. // KubeConfig which was used to create the connection.
  34. KubeConfig *rest.Config
  35. // Kubernetes API clientsets
  36. KubeClientSet kubernetes.Interface
  37. // controller-runtime client for newer controllers
  38. CRClient crclient.Client
  39. }
  40. type Addon interface {
  41. Setup(*Config) error
  42. Install() error
  43. Logs() error
  44. Uninstall() error
  45. }
  46. func InstallGlobalAddon(addon Addon) {
  47. globalAddons = append(globalAddons, addon)
  48. cfg := &Config{}
  49. cfg.KubeConfig, cfg.KubeClientSet, cfg.CRClient = util.NewConfig()
  50. ginkgo.By("installing global addon")
  51. err := addon.Setup(cfg)
  52. gomega.Expect(err).NotTo(gomega.HaveOccurred())
  53. err = addon.Install()
  54. if err != nil {
  55. addon.Logs() // Print logs in case installation fails
  56. }
  57. gomega.Expect(err).NotTo(gomega.HaveOccurred())
  58. }
  59. func UninstallGlobalAddons() {
  60. for _, addon := range globalAddons {
  61. ginkgo.By("uninstalling addon")
  62. err := addon.Uninstall()
  63. gomega.Expect(err).NotTo(gomega.HaveOccurred())
  64. }
  65. }
  66. const skipGlobalTeardownVar = "E2E_SKIP_GLOBAL_TEARDOWN"
  67. // SkipGlobalTeardown reports whether to leave the global addons installed, for a
  68. // cluster that is about to be discarded. Off unless asked for, and refused when
  69. // several suites share the cluster, since two of them install the same release.
  70. func SkipGlobalTeardown() bool {
  71. raw, ok := os.LookupEnv(skipGlobalTeardownVar)
  72. if !ok || raw == "" {
  73. return false
  74. }
  75. skip, err := strconv.ParseBool(raw)
  76. if err != nil {
  77. // Failing here would unwind the whole AfterSuite, losing the teardown
  78. // and the logs, so fall back to tearing down and say so.
  79. teardownLogf("%s is not a boolean (%q), so the teardown will run: %v",
  80. skipGlobalTeardownVar, raw, err)
  81. return false
  82. }
  83. if !skip {
  84. return false
  85. }
  86. // Only sees this process. Two separate single-suite runs against one cluster
  87. // would still collide.
  88. if suites := strings.Fields(os.Getenv("TEST_SUITES")); len(suites) > 1 {
  89. teardownLogf("%s ignored: suites %q share one cluster, so the global "+
  90. "addons have to come out between them", skipGlobalTeardownVar,
  91. strings.Join(suites, " "))
  92. return false
  93. }
  94. teardownLogf("%s set: leaving the global addons installed for the cluster to "+
  95. "be discarded with", skipGlobalTeardownVar)
  96. return true
  97. }
  98. // teardownLogf logs to stderr, not log.Logf: ginkgo drops GinkgoWriter output
  99. // for a passing node without -v, and these lines must survive a green run.
  100. func teardownLogf(format string, args ...any) {
  101. fmt.Fprintf(os.Stderr, format+"\n", args...)
  102. }
  103. // AssetDir returns the path to the k8s asset directory
  104. // which holds the helm charts, vault and conjur configuration.
  105. // It starts at the cwd, and walks its way up to the root.
  106. // It returns /k8s as a fallback.
  107. // When running the e2e suite locally, this should return $REPO/e2e/k8s,
  108. // when ran in CI this returns /k8s because the tests run in a dedicated pod where
  109. // the assets are copied into the container.
  110. func AssetDir() string {
  111. // Start from current working directory
  112. currentDir, err := os.Getwd()
  113. if err != nil {
  114. return ""
  115. }
  116. // Traverse up the directory tree looking for "k8s" directory
  117. for {
  118. k8sPath := filepath.Join(currentDir, "k8s")
  119. // Check if "k8s" directory exists
  120. if info, err := os.Stat(k8sPath); err == nil && info.IsDir() {
  121. return k8sPath
  122. }
  123. // Get parent directory
  124. parentDir := filepath.Dir(currentDir)
  125. // If we've reached the root directory, stop searching
  126. if parentDir == currentDir {
  127. break
  128. }
  129. currentDir = parentDir
  130. }
  131. return "/k8s"
  132. }
  133. func PrintLogs() {
  134. for _, addon := range globalAddons {
  135. err := addon.Logs()
  136. if err != nil {
  137. log.Logf("error fetching logs: %s", err.Error())
  138. }
  139. }
  140. }