eso.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package addon
  13. import (
  14. "os"
  15. "time"
  16. // nolint
  17. . "github.com/onsi/ginkgo/v2"
  18. )
  19. type ESO struct {
  20. *HelmChart
  21. }
  22. const (
  23. installCRDsVar = "installCRDs"
  24. esoImage = "ghcr.io/external-secrets/external-secrets"
  25. )
  26. func NewESO(mutators ...MutationFunc) *ESO {
  27. eso := &ESO{
  28. &HelmChart{
  29. Namespace: "default",
  30. ReleaseName: "eso",
  31. Chart: "/k8s/deploy/charts/external-secrets",
  32. Vars: []StringTuple{
  33. {
  34. Key: "webhook.port",
  35. Value: "9443",
  36. },
  37. {
  38. Key: "webhook.image.tag",
  39. Value: os.Getenv("VERSION"),
  40. },
  41. {
  42. Key: "webhook.image.repository",
  43. Value: esoImage,
  44. },
  45. {
  46. Key: "certController.image.tag",
  47. Value: os.Getenv("VERSION"),
  48. },
  49. {
  50. Key: "certController.image.repository",
  51. Value: esoImage,
  52. },
  53. {
  54. Key: "image.tag",
  55. Value: os.Getenv("VERSION"),
  56. },
  57. {
  58. Key: "image.repository",
  59. Value: esoImage,
  60. },
  61. {
  62. Key: "extraArgs.loglevel",
  63. Value: "debug",
  64. },
  65. {
  66. Key: installCRDsVar,
  67. Value: "false",
  68. },
  69. {
  70. Key: "concurrent",
  71. Value: "100",
  72. },
  73. {
  74. Key: "extraArgs.experimental-enable-vault-token-cache",
  75. Value: "true",
  76. },
  77. {
  78. Key: "extraArgs.experimental-enable-aws-session-cache",
  79. Value: "true",
  80. },
  81. },
  82. },
  83. }
  84. for _, f := range mutators {
  85. f(eso)
  86. }
  87. return eso
  88. }
  89. type MutationFunc func(eso *ESO)
  90. func WithReleaseName(name string) MutationFunc {
  91. return func(eso *ESO) {
  92. eso.HelmChart.ReleaseName = name
  93. }
  94. }
  95. func WithNamespace(namespace string) MutationFunc {
  96. return func(eso *ESO) {
  97. eso.HelmChart.Namespace = namespace
  98. }
  99. }
  100. func WithNamespaceScope(namespace string) MutationFunc {
  101. return func(eso *ESO) {
  102. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  103. Key: "scopedNamespace",
  104. Value: namespace,
  105. })
  106. }
  107. }
  108. func WithoutWebhook() MutationFunc {
  109. return func(eso *ESO) {
  110. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  111. Key: "webhook.create",
  112. Value: "false",
  113. })
  114. }
  115. }
  116. func WithoutCertController() MutationFunc {
  117. return func(eso *ESO) {
  118. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  119. Key: "certController.create",
  120. Value: "false",
  121. })
  122. }
  123. }
  124. func WithServiceAccount(saName string) MutationFunc {
  125. return func(eso *ESO) {
  126. eso.HelmChart.Vars = append(eso.HelmChart.Vars, []StringTuple{
  127. {
  128. Key: "serviceAccount.create",
  129. Value: "false",
  130. },
  131. {
  132. Key: "serviceAccount.name",
  133. Value: saName,
  134. },
  135. }...)
  136. }
  137. }
  138. func WithControllerClass(class string) MutationFunc {
  139. return func(eso *ESO) {
  140. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  141. Key: "extraArgs.controller-class",
  142. Value: class,
  143. })
  144. }
  145. }
  146. // By default ESO is installed without CRDs
  147. // when using WithCRDs() the CRDs will be installed before
  148. // and uninstalled after use.
  149. func WithCRDs() MutationFunc {
  150. return func(eso *ESO) {
  151. for i, v := range eso.HelmChart.Vars {
  152. if v.Key == installCRDsVar {
  153. eso.HelmChart.Vars[i].Value = "true"
  154. }
  155. }
  156. }
  157. }
  158. func (l *ESO) Install() error {
  159. By("Installing eso\n")
  160. err := l.HelmChart.Install()
  161. if err != nil {
  162. return err
  163. }
  164. return nil
  165. }
  166. func (l *ESO) Uninstall() error {
  167. // uninstalling CRDs will trigger the deletion of all CRs. They block the deletion of the CRDs if
  168. // a finalizer is present.
  169. // We must uninstall the CRDs before the eso chart,
  170. // otherwise ESO will not remove the finalizer.
  171. if l.HelmChart.HasVar(installCRDsVar, "true") {
  172. By("Uninstalling eso CRDs")
  173. err := uninstallCRDs(l.config)
  174. if err != nil {
  175. return err
  176. }
  177. // Give ESO a grace period to clean up the CRs
  178. <-time.After(time.Minute)
  179. }
  180. By("Uninstalling eso")
  181. err := l.HelmChart.Uninstall()
  182. if err != nil {
  183. return err
  184. }
  185. return nil
  186. }