eso.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "time"
  18. // nolint
  19. . "github.com/onsi/ginkgo/v2"
  20. )
  21. type ESO struct {
  22. *HelmChart
  23. }
  24. const (
  25. installCRDsVar = "installCRDs"
  26. esoImage = "ghcr.io/external-secrets/external-secrets"
  27. )
  28. func NewESO(mutators ...MutationFunc) *ESO {
  29. eso := &ESO{
  30. &HelmChart{
  31. Namespace: "default",
  32. ReleaseName: "eso",
  33. Chart: filepath.Join(AssetDir(), "deploy/charts/external-secrets"),
  34. Vars: []StringTuple{
  35. {
  36. Key: "webhook.port",
  37. Value: "9443",
  38. },
  39. {
  40. Key: "webhook.image.tag",
  41. Value: os.Getenv("VERSION"),
  42. },
  43. {
  44. Key: "webhook.image.repository",
  45. Value: esoImage,
  46. },
  47. {
  48. Key: "certController.image.tag",
  49. Value: os.Getenv("VERSION"),
  50. },
  51. {
  52. Key: "certController.image.repository",
  53. Value: esoImage,
  54. },
  55. {
  56. Key: "image.tag",
  57. Value: os.Getenv("VERSION"),
  58. },
  59. {
  60. Key: "image.repository",
  61. Value: esoImage,
  62. },
  63. {
  64. Key: "extraArgs.loglevel",
  65. Value: "debug",
  66. },
  67. {
  68. Key: installCRDsVar,
  69. Value: "false",
  70. },
  71. {
  72. Key: "concurrent",
  73. Value: "100",
  74. },
  75. {
  76. Key: "extraArgs.experimental-enable-vault-token-cache",
  77. Value: "true",
  78. },
  79. },
  80. },
  81. }
  82. for _, f := range mutators {
  83. f(eso)
  84. }
  85. return eso
  86. }
  87. type MutationFunc func(eso *ESO)
  88. func WithReleaseName(name string) MutationFunc {
  89. return func(eso *ESO) {
  90. eso.HelmChart.ReleaseName = name
  91. }
  92. }
  93. func WithNamespace(namespace string) MutationFunc {
  94. return func(eso *ESO) {
  95. eso.HelmChart.Namespace = namespace
  96. }
  97. }
  98. func WithNamespaceScope(namespace string) MutationFunc {
  99. return func(eso *ESO) {
  100. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  101. Key: "scopedNamespace",
  102. Value: namespace,
  103. })
  104. }
  105. }
  106. func WithoutWebhook() MutationFunc {
  107. return func(eso *ESO) {
  108. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  109. Key: "webhook.create",
  110. Value: "false",
  111. })
  112. }
  113. }
  114. func WithoutCertController() MutationFunc {
  115. return func(eso *ESO) {
  116. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  117. Key: "certController.create",
  118. Value: "false",
  119. })
  120. }
  121. }
  122. func WithServiceAccount(saName string) MutationFunc {
  123. return func(eso *ESO) {
  124. eso.HelmChart.Vars = append(eso.HelmChart.Vars, []StringTuple{
  125. {
  126. Key: "serviceAccount.create",
  127. Value: "false",
  128. },
  129. {
  130. Key: "serviceAccount.name",
  131. Value: saName,
  132. },
  133. }...)
  134. }
  135. }
  136. func WithControllerClass(class string) MutationFunc {
  137. return func(eso *ESO) {
  138. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  139. Key: "extraArgs.controller-class",
  140. Value: class,
  141. })
  142. }
  143. }
  144. // By default ESO is installed without CRDs
  145. // when using WithCRDs() the CRDs will be installed before
  146. // and uninstalled after use.
  147. func WithCRDs() MutationFunc {
  148. return func(eso *ESO) {
  149. for i, v := range eso.HelmChart.Vars {
  150. if v.Key == installCRDsVar {
  151. eso.HelmChart.Vars[i].Value = "true"
  152. }
  153. }
  154. }
  155. }
  156. func WithAllowGenericTargets() MutationFunc {
  157. return func(eso *ESO) {
  158. eso.HelmChart.Vars = append(eso.HelmChart.Vars, StringTuple{
  159. Key: "genericTargets.enabled",
  160. Value: "true",
  161. }, StringTuple{
  162. Key: "genericTargets.resources[0].apiGroup",
  163. Value: "",
  164. }, StringTuple{
  165. Key: "genericTargets.resources[0].resources[0]",
  166. Value: "configmaps",
  167. }, StringTuple{
  168. Key: "genericTargets.resources[0].verbs[0]",
  169. Value: "create",
  170. }, StringTuple{
  171. Key: "genericTargets.resources[0].verbs[1]",
  172. Value: "delete",
  173. }, StringTuple{
  174. Key: "genericTargets.resources[0].verbs[2]",
  175. Value: "list",
  176. }, StringTuple{
  177. Key: "genericTargets.resources[0].verbs[3]",
  178. Value: "get",
  179. }, StringTuple{
  180. Key: "genericTargets.resources[0].verbs[4]",
  181. Value: "patch",
  182. }, StringTuple{
  183. Key: "genericTargets.resources[0].verbs[5]",
  184. Value: "watch",
  185. })
  186. }
  187. }
  188. func (l *ESO) Install() error {
  189. By("Installing eso\n")
  190. err := l.HelmChart.Install()
  191. if err != nil {
  192. return err
  193. }
  194. return nil
  195. }
  196. func (l *ESO) Uninstall() error {
  197. // uninstalling CRDs will trigger the deletion of all CRs. They block the deletion of the CRDs if
  198. // a finalizer is present.
  199. // We must uninstall the CRDs before the eso chart,
  200. // otherwise ESO will not remove the finalizer.
  201. if l.HelmChart.HasVar(installCRDsVar, "true") {
  202. By("Uninstalling eso CRDs")
  203. err := uninstallCRDs(l.config)
  204. if err != nil {
  205. return err
  206. }
  207. // Give ESO a grace period to clean up the CRs
  208. <-time.After(time.Minute)
  209. }
  210. By("Uninstalling eso")
  211. err := l.HelmChart.Uninstall()
  212. if err != nil {
  213. return err
  214. }
  215. return nil
  216. }