eso_v2_mutators.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "strconv"
  17. "strings"
  18. )
  19. const (
  20. v2HelmNamespace = "external-secrets-system"
  21. v2HelmReleaseName = "external-secrets"
  22. )
  23. func WithV2Namespace() MutationFunc {
  24. return func(eso *ESO) {
  25. eso.HelmChart.Namespace = v2HelmNamespace
  26. eso.HelmChart.ReleaseName = v2HelmReleaseName
  27. if !containsArg(eso.HelmChart.Args, "--create-namespace") {
  28. eso.HelmChart.Args = append(eso.HelmChart.Args, "--create-namespace")
  29. }
  30. }
  31. }
  32. func WithV2KubernetesProvider() MutationFunc {
  33. return func(eso *ESO) {
  34. ensureV2ProviderConfig(eso.HelmChart)
  35. setProvider(eso.HelmChart, "kubernetes", "kubernetes", "ghcr.io/external-secrets/provider-kubernetes", os.Getenv("VERSION"))
  36. }
  37. }
  38. func WithV2FakeProvider() MutationFunc {
  39. return func(eso *ESO) {
  40. ensureV2ProviderConfig(eso.HelmChart)
  41. setProvider(eso.HelmChart, "fake", "fake", "ghcr.io/external-secrets/provider-fake", os.Getenv("VERSION"))
  42. }
  43. }
  44. func WithV2AWSProvider() MutationFunc {
  45. return func(eso *ESO) {
  46. ensureV2ProviderConfig(eso.HelmChart)
  47. setProvider(eso.HelmChart, "aws", "aws", "ghcr.io/external-secrets/provider-aws", os.Getenv("VERSION"))
  48. }
  49. }
  50. func WithV2GCPProvider() MutationFunc {
  51. return func(eso *ESO) {
  52. ensureV2ProviderConfig(eso.HelmChart)
  53. setProvider(eso.HelmChart, "gcp", "gcp", "ghcr.io/external-secrets/provider-gcp", os.Getenv("VERSION"))
  54. }
  55. }
  56. func WithV2ProviderServiceAccount(providerName, serviceAccountName string) MutationFunc {
  57. return func(eso *ESO) {
  58. index := findProviderIndex(eso.HelmChart, providerName)
  59. if index < 0 {
  60. panic("provider entry must exist before overriding service account")
  61. }
  62. prefix := "providers.list[" + strconv.Itoa(index) + "].serviceAccount"
  63. setOrAppendVar(eso.HelmChart, StringTuple{Key: prefix + ".create", Value: "false"})
  64. setOrAppendVar(eso.HelmChart, StringTuple{Key: prefix + ".name", Value: serviceAccountName})
  65. }
  66. }
  67. func setOrAppendVar(chart *HelmChart, variable StringTuple) {
  68. for i := range chart.Vars {
  69. if chart.Vars[i].Key == variable.Key {
  70. chart.Vars[i].Value = variable.Value
  71. return
  72. }
  73. }
  74. chart.Vars = append(chart.Vars, variable)
  75. }
  76. func ensureV2ProviderConfig(chart *HelmChart) {
  77. requiredVars := []StringTuple{
  78. {Key: "v2.enabled", Value: "true"},
  79. {Key: "crds.createProvider", Value: "true"},
  80. {Key: "crds.createClusterProvider", Value: "true"},
  81. {Key: "providers.enabled", Value: "true"},
  82. }
  83. for _, variable := range requiredVars {
  84. setOrAppendVar(chart, variable)
  85. }
  86. defaultVars := []StringTuple{
  87. {Key: "replicaCount", Value: "1"},
  88. {Key: "providerDefaults.replicaCount", Value: "1"},
  89. }
  90. for _, variable := range defaultVars {
  91. setVarIfMissing(chart, variable)
  92. }
  93. }
  94. func setVarIfMissing(chart *HelmChart, variable StringTuple) {
  95. for i := range chart.Vars {
  96. if chart.Vars[i].Key == variable.Key {
  97. return
  98. }
  99. }
  100. chart.Vars = append(chart.Vars, variable)
  101. }
  102. func setProvider(chart *HelmChart, name, providerType, imageRepository, imageTag string) {
  103. index := findProviderIndex(chart, name)
  104. if index < 0 {
  105. index = nextProviderIndex(chart)
  106. }
  107. prefix := "providers.list[" + strconv.Itoa(index) + "]"
  108. vars := []StringTuple{
  109. {Key: prefix + ".name", Value: name},
  110. {Key: prefix + ".type", Value: providerType},
  111. {Key: prefix + ".enabled", Value: "true"},
  112. {Key: prefix + ".replicaCount", Value: "1"},
  113. {Key: prefix + ".image.repository", Value: imageRepository},
  114. {Key: prefix + ".image.tag", Value: imageTag},
  115. {Key: prefix + ".image.pullPolicy", Value: "IfNotPresent"},
  116. }
  117. for _, variable := range vars {
  118. setOrAppendVar(chart, variable)
  119. }
  120. }
  121. func findProviderIndex(chart *HelmChart, name string) int {
  122. const prefix = "providers.list["
  123. const suffix = "].name"
  124. for _, variable := range chart.Vars {
  125. if !strings.HasPrefix(variable.Key, prefix) || !strings.HasSuffix(variable.Key, suffix) {
  126. continue
  127. }
  128. if variable.Value != name {
  129. continue
  130. }
  131. indexStr := strings.TrimSuffix(strings.TrimPrefix(variable.Key, prefix), suffix)
  132. index, err := strconv.Atoi(indexStr)
  133. if err == nil {
  134. return index
  135. }
  136. }
  137. return -1
  138. }
  139. func nextProviderIndex(chart *HelmChart) int {
  140. const prefix = "providers.list["
  141. maxIndex := -1
  142. for _, variable := range chart.Vars {
  143. if !strings.HasPrefix(variable.Key, prefix) {
  144. continue
  145. }
  146. remainder := strings.TrimPrefix(variable.Key, prefix)
  147. closingBracket := strings.Index(remainder, "]")
  148. if closingBracket < 0 {
  149. continue
  150. }
  151. index, err := strconv.Atoi(remainder[:closingBracket])
  152. if err != nil {
  153. continue
  154. }
  155. if index > maxIndex {
  156. maxIndex = index
  157. }
  158. }
  159. return maxIndex + 1
  160. }
  161. func containsArg(args []string, target string) bool {
  162. for _, arg := range args {
  163. if arg == target {
  164. return true
  165. }
  166. }
  167. return false
  168. }