eso_v2_mutators.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.createClusterProviderClass", Value: "true"},
  74. {Key: "crds.createProviderStore", Value: "true"},
  75. {Key: "crds.createClusterProviderStore", Value: "true"},
  76. {Key: "providers.enabled", Value: "true"},
  77. }
  78. for _, variable := range requiredVars {
  79. setOrAppendVar(chart, variable)
  80. }
  81. defaultVars := []StringTuple{
  82. {Key: "replicaCount", Value: "1"},
  83. {Key: "providerDefaults.replicaCount", Value: "1"},
  84. }
  85. for _, variable := range defaultVars {
  86. setVarIfMissing(chart, variable)
  87. }
  88. }
  89. func setVarIfMissing(chart *HelmChart, variable StringTuple) {
  90. for i := range chart.Vars {
  91. if chart.Vars[i].Key == variable.Key {
  92. return
  93. }
  94. }
  95. chart.Vars = append(chart.Vars, variable)
  96. }
  97. func setProvider(chart *HelmChart, name, providerType, imageRepository, imageTag string) {
  98. index := findProviderIndex(chart, name)
  99. if index < 0 {
  100. index = nextProviderIndex(chart)
  101. }
  102. prefix := "providers.list[" + strconv.Itoa(index) + "]"
  103. vars := []StringTuple{
  104. {Key: prefix + ".name", Value: name},
  105. {Key: prefix + ".type", Value: providerType},
  106. {Key: prefix + ".enabled", Value: "true"},
  107. {Key: prefix + ".replicaCount", Value: "1"},
  108. {Key: prefix + ".image.repository", Value: imageRepository},
  109. {Key: prefix + ".image.tag", Value: imageTag},
  110. {Key: prefix + ".image.pullPolicy", Value: "IfNotPresent"},
  111. }
  112. for _, variable := range vars {
  113. setOrAppendVar(chart, variable)
  114. }
  115. }
  116. func findProviderIndex(chart *HelmChart, name string) int {
  117. const prefix = "providers.list["
  118. const suffix = "].name"
  119. for _, variable := range chart.Vars {
  120. if !strings.HasPrefix(variable.Key, prefix) || !strings.HasSuffix(variable.Key, suffix) {
  121. continue
  122. }
  123. if variable.Value != name {
  124. continue
  125. }
  126. indexStr := strings.TrimSuffix(strings.TrimPrefix(variable.Key, prefix), suffix)
  127. index, err := strconv.Atoi(indexStr)
  128. if err == nil {
  129. return index
  130. }
  131. }
  132. return -1
  133. }
  134. func nextProviderIndex(chart *HelmChart) int {
  135. const prefix = "providers.list["
  136. maxIndex := -1
  137. for _, variable := range chart.Vars {
  138. if !strings.HasPrefix(variable.Key, prefix) {
  139. continue
  140. }
  141. remainder := strings.TrimPrefix(variable.Key, prefix)
  142. closingBracket := strings.Index(remainder, "]")
  143. if closingBracket < 0 {
  144. continue
  145. }
  146. index, err := strconv.Atoi(remainder[:closingBracket])
  147. if err != nil {
  148. continue
  149. }
  150. if index > maxIndex {
  151. maxIndex = index
  152. }
  153. }
  154. return maxIndex + 1
  155. }
  156. func containsArg(args []string, target string) bool {
  157. for _, arg := range args {
  158. if arg == target {
  159. return true
  160. }
  161. }
  162. return false
  163. }