addon.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "os"
  16. "path/filepath"
  17. "github.com/onsi/ginkgo/v2"
  18. "github.com/onsi/gomega"
  19. "k8s.io/client-go/kubernetes"
  20. "k8s.io/client-go/rest"
  21. crclient "sigs.k8s.io/controller-runtime/pkg/client"
  22. "github.com/external-secrets/external-secrets-e2e/framework/log"
  23. "github.com/external-secrets/external-secrets-e2e/framework/util"
  24. )
  25. var globalAddons []Addon
  26. func init() {
  27. globalAddons = make([]Addon, 0)
  28. }
  29. type Config struct {
  30. // KubeConfig which was used to create the connection.
  31. KubeConfig *rest.Config
  32. // Kubernetes API clientsets
  33. KubeClientSet kubernetes.Interface
  34. // controller-runtime client for newer controllers
  35. CRClient crclient.Client
  36. }
  37. type Addon interface {
  38. Setup(*Config) error
  39. Install() error
  40. Logs() error
  41. Uninstall() error
  42. }
  43. func InstallGlobalAddon(addon Addon) {
  44. globalAddons = append(globalAddons, addon)
  45. cfg := &Config{}
  46. cfg.KubeConfig, cfg.KubeClientSet, cfg.CRClient = util.NewConfig()
  47. ginkgo.By("installing global addon")
  48. err := addon.Setup(cfg)
  49. gomega.Expect(err).NotTo(gomega.HaveOccurred())
  50. err = addon.Install()
  51. if err != nil {
  52. addon.Logs() // Print logs in case installation fails
  53. }
  54. gomega.Expect(err).NotTo(gomega.HaveOccurred())
  55. }
  56. func UninstallGlobalAddons() {
  57. for _, addon := range globalAddons {
  58. ginkgo.By("uninstalling addon")
  59. err := addon.Uninstall()
  60. gomega.Expect(err).NotTo(gomega.HaveOccurred())
  61. }
  62. }
  63. // AssetDir returns the path to the k8s asset directory
  64. // which holds the helm charts, vault and conjur configuration.
  65. // It starts at the cwd, and walks its way up to the root.
  66. // It returns /k8s as a fallback.
  67. // When running the e2e suite locally, this should return $REPO/e2e/k8s,
  68. // when ran in CI this returns /k8s because the tests run in a dedicated pod where
  69. // the assets are copied into the container.
  70. func AssetDir() string {
  71. // Start from current working directory
  72. currentDir, err := os.Getwd()
  73. if err != nil {
  74. return ""
  75. }
  76. // Traverse up the directory tree looking for "k8s" directory
  77. for {
  78. k8sPath := filepath.Join(currentDir, "k8s")
  79. // Check if "k8s" directory exists
  80. if info, err := os.Stat(k8sPath); err == nil && info.IsDir() {
  81. return k8sPath
  82. }
  83. // Get parent directory
  84. parentDir := filepath.Dir(currentDir)
  85. // If we've reached the root directory, stop searching
  86. if parentDir == currentDir {
  87. break
  88. }
  89. currentDir = parentDir
  90. }
  91. return "/k8s"
  92. }
  93. func PrintLogs() {
  94. for _, addon := range globalAddons {
  95. err := addon.Logs()
  96. if err != nil {
  97. log.Logf("error fetching logs: %s", err.Error())
  98. }
  99. }
  100. }