eso.go 4.3 KB

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