eso_v2_mutators.go 4.2 KB

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