eso_v2_mutators.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 WithV2ProviderServiceAccount(providerName, serviceAccountName string) MutationFunc {
  51. return func(eso *ESO) {
  52. index := findProviderIndex(eso.HelmChart, providerName)
  53. if index < 0 {
  54. panic("provider entry must exist before overriding service account")
  55. }
  56. prefix := "providers.list[" + strconv.Itoa(index) + "].serviceAccount"
  57. setOrAppendVar(eso.HelmChart, StringTuple{Key: prefix + ".create", Value: "false"})
  58. setOrAppendVar(eso.HelmChart, StringTuple{Key: prefix + ".name", Value: serviceAccountName})
  59. }
  60. }
  61. func setOrAppendVar(chart *HelmChart, variable StringTuple) {
  62. for i := range chart.Vars {
  63. if chart.Vars[i].Key == variable.Key {
  64. chart.Vars[i].Value = variable.Value
  65. return
  66. }
  67. }
  68. chart.Vars = append(chart.Vars, variable)
  69. }
  70. func ensureV2ProviderConfig(chart *HelmChart) {
  71. requiredVars := []StringTuple{
  72. {Key: "v2.enabled", Value: "true"},
  73. {Key: "crds.createProvider", Value: "true"},
  74. {Key: "crds.createClusterProvider", Value: "true"},
  75. {Key: "providers.enabled", Value: "true"},
  76. }
  77. for _, variable := range requiredVars {
  78. setOrAppendVar(chart, variable)
  79. }
  80. defaultVars := []StringTuple{
  81. {Key: "replicaCount", Value: "1"},
  82. {Key: "providerDefaults.replicaCount", Value: "1"},
  83. }
  84. for _, variable := range defaultVars {
  85. setVarIfMissing(chart, variable)
  86. }
  87. }
  88. func setVarIfMissing(chart *HelmChart, variable StringTuple) {
  89. for i := range chart.Vars {
  90. if chart.Vars[i].Key == variable.Key {
  91. return
  92. }
  93. }
  94. chart.Vars = append(chart.Vars, variable)
  95. }
  96. func setProvider(chart *HelmChart, name, providerType, imageRepository, imageTag string) {
  97. index := findProviderIndex(chart, name)
  98. if index < 0 {
  99. index = nextProviderIndex(chart)
  100. }
  101. prefix := "providers.list[" + strconv.Itoa(index) + "]"
  102. vars := []StringTuple{
  103. {Key: prefix + ".name", Value: name},
  104. {Key: prefix + ".type", Value: providerType},
  105. {Key: prefix + ".enabled", Value: "true"},
  106. {Key: prefix + ".replicaCount", Value: "1"},
  107. {Key: prefix + ".image.repository", Value: imageRepository},
  108. {Key: prefix + ".image.tag", Value: imageTag},
  109. {Key: prefix + ".image.pullPolicy", Value: "IfNotPresent"},
  110. }
  111. for _, variable := range vars {
  112. setOrAppendVar(chart, variable)
  113. }
  114. }
  115. func findProviderIndex(chart *HelmChart, name string) int {
  116. const prefix = "providers.list["
  117. const suffix = "].name"
  118. for _, variable := range chart.Vars {
  119. if !strings.HasPrefix(variable.Key, prefix) || !strings.HasSuffix(variable.Key, suffix) {
  120. continue
  121. }
  122. if variable.Value != name {
  123. continue
  124. }
  125. indexStr := strings.TrimSuffix(strings.TrimPrefix(variable.Key, prefix), suffix)
  126. index, err := strconv.Atoi(indexStr)
  127. if err == nil {
  128. return index
  129. }
  130. }
  131. return -1
  132. }
  133. func nextProviderIndex(chart *HelmChart) int {
  134. const prefix = "providers.list["
  135. maxIndex := -1
  136. for _, variable := range chart.Vars {
  137. if !strings.HasPrefix(variable.Key, prefix) {
  138. continue
  139. }
  140. remainder := strings.TrimPrefix(variable.Key, prefix)
  141. closingBracket := strings.Index(remainder, "]")
  142. if closingBracket < 0 {
  143. continue
  144. }
  145. index, err := strconv.Atoi(remainder[:closingBracket])
  146. if err != nil {
  147. continue
  148. }
  149. if index > maxIndex {
  150. maxIndex = index
  151. }
  152. }
  153. return maxIndex + 1
  154. }
  155. func containsArg(args []string, target string) bool {
  156. for _, arg := range args {
  157. if arg == target {
  158. return true
  159. }
  160. }
  161. return false
  162. }