externalsecret_controller_test.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 externalsecret
  13. import (
  14. "context"
  15. "fmt"
  16. "time"
  17. . "github.com/onsi/ginkgo"
  18. . "github.com/onsi/ginkgo/extensions/table"
  19. . "github.com/onsi/gomega"
  20. dto "github.com/prometheus/client_model/go"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. "github.com/external-secrets/external-secrets/pkg/provider"
  28. "github.com/external-secrets/external-secrets/pkg/provider/fake"
  29. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  30. )
  31. var (
  32. fakeProvider *fake.Client
  33. metric dto.Metric
  34. timeout = time.Second * 10
  35. interval = time.Millisecond * 250
  36. )
  37. type testCase struct {
  38. secretStore *esv1alpha1.SecretStore
  39. externalSecret *esv1alpha1.ExternalSecret
  40. // checkCondition should return true if the externalSecret
  41. // has the expected condition
  42. checkCondition func(*esv1alpha1.ExternalSecret) bool
  43. // checkExternalSecret is called after the condition has been verified
  44. // use this to verify the externalSecret
  45. checkExternalSecret func(*esv1alpha1.ExternalSecret)
  46. // optional. use this to test the secret value
  47. checkSecret func(*esv1alpha1.ExternalSecret, *v1.Secret)
  48. }
  49. type testTweaks func(*testCase)
  50. var _ = Describe("ExternalSecret controller", func() {
  51. const (
  52. ExternalSecretName = "test-es"
  53. ExternalSecretStore = "test-store"
  54. ExternalSecretTargetSecretName = "test-secret"
  55. )
  56. var ExternalSecretNamespace string
  57. BeforeEach(func() {
  58. var err error
  59. ExternalSecretNamespace, err = CreateNamespace("test-ns", k8sClient)
  60. Expect(err).ToNot(HaveOccurred())
  61. metric.Reset()
  62. syncCallsTotal.Reset()
  63. syncCallsError.Reset()
  64. externalSecretCondition.Reset()
  65. })
  66. AfterEach(func() {
  67. Expect(k8sClient.Delete(context.Background(), &v1.Namespace{
  68. ObjectMeta: metav1.ObjectMeta{
  69. Name: ExternalSecretNamespace,
  70. },
  71. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  72. Expect(k8sClient.Delete(context.Background(), &esv1alpha1.SecretStore{
  73. ObjectMeta: metav1.ObjectMeta{
  74. Name: ExternalSecretStore,
  75. Namespace: ExternalSecretNamespace,
  76. },
  77. }, client.PropagationPolicy(metav1.DeletePropagationBackground)), client.GracePeriodSeconds(0)).To(Succeed())
  78. })
  79. const targetProp = "targetProperty"
  80. const remoteKey = "barz"
  81. const remoteProperty = "bang"
  82. makeDefaultTestcase := func() *testCase {
  83. return &testCase{
  84. // default condition: es should be ready
  85. checkCondition: func(es *esv1alpha1.ExternalSecret) bool {
  86. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  87. if cond == nil || cond.Status != v1.ConditionTrue {
  88. return false
  89. }
  90. return true
  91. },
  92. checkExternalSecret: func(es *esv1alpha1.ExternalSecret) {},
  93. secretStore: &esv1alpha1.SecretStore{
  94. ObjectMeta: metav1.ObjectMeta{
  95. Name: ExternalSecretStore,
  96. Namespace: ExternalSecretNamespace,
  97. },
  98. Spec: esv1alpha1.SecretStoreSpec{
  99. Provider: &esv1alpha1.SecretStoreProvider{
  100. AWS: &esv1alpha1.AWSProvider{
  101. Service: esv1alpha1.AWSServiceSecretsManager,
  102. },
  103. },
  104. },
  105. },
  106. externalSecret: &esv1alpha1.ExternalSecret{
  107. ObjectMeta: metav1.ObjectMeta{
  108. Name: ExternalSecretName,
  109. Namespace: ExternalSecretNamespace,
  110. },
  111. Spec: esv1alpha1.ExternalSecretSpec{
  112. SecretStoreRef: esv1alpha1.SecretStoreRef{
  113. Name: ExternalSecretStore,
  114. },
  115. Target: esv1alpha1.ExternalSecretTarget{
  116. Name: ExternalSecretTargetSecretName,
  117. },
  118. Data: []esv1alpha1.ExternalSecretData{
  119. {
  120. SecretKey: targetProp,
  121. RemoteRef: esv1alpha1.ExternalSecretDataRemoteRef{
  122. Key: remoteKey,
  123. Property: remoteProperty,
  124. },
  125. },
  126. },
  127. },
  128. },
  129. }
  130. }
  131. // labels and annotations from the Kind=ExternalSecret
  132. // should be copied over to the Kind=Secret
  133. syncLabelsAnnotations := func(tc *testCase) {
  134. const secretVal = "someValue"
  135. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  136. "fooobar": "bazz",
  137. }
  138. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  139. "hihihih": "hehehe",
  140. }
  141. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  142. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  143. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  144. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  145. Eventually(func() bool {
  146. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  147. return metric.GetCounter().GetValue() == 1.0
  148. }, timeout, interval).Should(BeTrue())
  149. // check value
  150. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  151. // check labels & annotations
  152. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  153. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.ObjectMeta.Annotations))
  154. // ownerRef must not not be set!
  155. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeTrue())
  156. }
  157. }
  158. // merge with existing secret using creationPolicy=Merge
  159. // it should NOT have a ownerReference
  160. // metadata.managedFields with the correct owner should be added to the secret
  161. mergeWithSecret := func(tc *testCase) {
  162. const secretVal = "someValue"
  163. const existingKey = "pre-existing-key"
  164. existingVal := "pre-existing-value"
  165. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  166. // create secret beforehand
  167. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  168. ObjectMeta: metav1.ObjectMeta{
  169. Name: "test-secret",
  170. Namespace: ExternalSecretNamespace,
  171. },
  172. Data: map[string][]byte{
  173. existingKey: []byte(existingVal),
  174. },
  175. }, client.FieldOwner("fake.manager"))).To(Succeed())
  176. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  177. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  178. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  179. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  180. Eventually(func() bool {
  181. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  182. return metric.GetCounter().GetValue() == 1.0
  183. }, timeout, interval).Should(BeTrue())
  184. // check value
  185. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  186. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  187. // check labels & annotations
  188. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.ObjectMeta.Labels))
  189. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.ObjectMeta.Annotations))
  190. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  191. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(2))
  192. Expect(hasFieldOwnership(secret.ObjectMeta, "external-secrets", "{\"f:data\":{\"f:targetProperty\":{}}}")).To(BeTrue())
  193. Expect(hasFieldOwnership(secret.ObjectMeta, "fake.manager", "{\"f:data\":{\".\":{},\"f:pre-existing-key\":{}},\"f:type\":{}}")).To(BeTrue())
  194. }
  195. }
  196. // should not merge with secret if it doesn't exist
  197. mergeWithSecretErr := func(tc *testCase) {
  198. const secretVal = "someValue"
  199. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  200. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  201. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  202. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  203. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  204. return false
  205. }
  206. return true
  207. }
  208. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  209. Eventually(func() bool {
  210. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  211. return metric.GetCounter().GetValue() >= 2.0
  212. }, timeout, interval).Should(BeTrue())
  213. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  214. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  215. }
  216. }
  217. // controller should not force override but
  218. // return an error on conflict
  219. mergeWithConflict := func(tc *testCase) {
  220. const secretVal = "someValue"
  221. // this should confict
  222. const existingKey = targetProp
  223. existingVal := "pre-existing-value"
  224. tc.externalSecret.Spec.Target.CreationPolicy = esv1alpha1.Merge
  225. // create secret beforehand
  226. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  227. ObjectMeta: metav1.ObjectMeta{
  228. Name: "test-secret",
  229. Namespace: ExternalSecretNamespace,
  230. },
  231. Data: map[string][]byte{
  232. existingKey: []byte(existingVal),
  233. },
  234. }, client.FieldOwner("fake.manager"))).To(Succeed())
  235. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  236. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  237. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  238. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  239. return false
  240. }
  241. return true
  242. }
  243. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  244. // check that value stays the same
  245. Expect(string(secret.Data[existingKey])).To(Equal(existingVal))
  246. Expect(string(secret.Data[targetProp])).ToNot(Equal(secretVal))
  247. // check owner/managedFields
  248. Expect(hasOwnerRef(secret.ObjectMeta, "ExternalSecret", ExternalSecretName)).To(BeFalse())
  249. Expect(secret.ObjectMeta.ManagedFields).To(HaveLen(1))
  250. Expect(hasFieldOwnership(secret.ObjectMeta, "fake.manager", "{\"f:data\":{\".\":{},\"f:targetProperty\":{}},\"f:type\":{}}")).To(BeTrue())
  251. }
  252. }
  253. // when using a template it should be used as a blueprint
  254. // to construct a new secret: labels, annotations and type
  255. syncWithTemplate := func(tc *testCase) {
  256. const secretVal = "someValue"
  257. const expectedSecretVal = "SOMEVALUE was templated"
  258. const tplStaticKey = "tplstatickey"
  259. const tplStaticVal = "tplstaticvalue"
  260. tc.externalSecret.ObjectMeta.Labels = map[string]string{
  261. "fooobar": "bazz",
  262. }
  263. tc.externalSecret.ObjectMeta.Annotations = map[string]string{
  264. "hihihih": "hehehe",
  265. }
  266. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  267. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  268. Labels: map[string]string{
  269. "foos": "ball",
  270. },
  271. Annotations: map[string]string{
  272. "hihi": "ga",
  273. },
  274. },
  275. Type: v1.SecretTypeOpaque,
  276. Data: map[string]string{
  277. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  278. tplStaticKey: tplStaticVal,
  279. },
  280. }
  281. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  282. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  283. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  284. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  285. Eventually(func() bool {
  286. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  287. return metric.GetCounter().GetValue() == 1.0
  288. }, timeout, interval).Should(BeTrue())
  289. // check values
  290. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  291. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  292. // labels/annotations should be taken from the template
  293. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  294. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  295. }
  296. }
  297. // secret should be synced with correct value precedence:
  298. // * template
  299. // * templateFrom
  300. // * data
  301. // * dataFrom
  302. syncWithTemplatePrecedence := func(tc *testCase) {
  303. const secretVal = "someValue"
  304. const expectedSecretVal = "SOMEVALUE was templated"
  305. const tplStaticKey = "tplstatickey"
  306. const tplStaticVal = "tplstaticvalue"
  307. const tplFromCMName = "template-cm"
  308. const tplFromSecretName = "template-secret"
  309. const tplFromKey = "tpl-from-key"
  310. const tplFromSecKey = "tpl-from-sec-key"
  311. const tplFromVal = "tpl-from-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  312. const tplFromSecVal = "tpl-from-sec-value: {{ .targetProperty | toString }} // {{ .bar | toString }}"
  313. Expect(k8sClient.Create(context.Background(), &v1.ConfigMap{
  314. ObjectMeta: metav1.ObjectMeta{
  315. Name: tplFromCMName,
  316. Namespace: ExternalSecretNamespace,
  317. },
  318. Data: map[string]string{
  319. tplFromKey: tplFromVal,
  320. },
  321. })).To(Succeed())
  322. Expect(k8sClient.Create(context.Background(), &v1.Secret{
  323. ObjectMeta: metav1.ObjectMeta{
  324. Name: tplFromSecretName,
  325. Namespace: ExternalSecretNamespace,
  326. },
  327. Data: map[string][]byte{
  328. tplFromSecKey: []byte(tplFromSecVal),
  329. },
  330. })).To(Succeed())
  331. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  332. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{},
  333. Type: v1.SecretTypeOpaque,
  334. TemplateFrom: []esv1alpha1.TemplateFrom{
  335. {
  336. ConfigMap: &esv1alpha1.TemplateRef{
  337. Name: tplFromCMName,
  338. Items: []esv1alpha1.TemplateRefItem{
  339. {
  340. Key: tplFromKey,
  341. },
  342. },
  343. },
  344. },
  345. {
  346. Secret: &esv1alpha1.TemplateRef{
  347. Name: tplFromSecretName,
  348. Items: []esv1alpha1.TemplateRefItem{
  349. {
  350. Key: tplFromSecKey,
  351. },
  352. },
  353. },
  354. },
  355. },
  356. Data: map[string]string{
  357. // this should be the data value, not dataFrom
  358. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  359. // this should use the value from the map
  360. "bar": "value from map: {{ .bar | toString }}",
  361. // just a static value
  362. tplStaticKey: tplStaticVal,
  363. },
  364. }
  365. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  366. {
  367. Key: "datamap",
  368. },
  369. }
  370. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  371. fakeProvider.WithGetSecretMap(map[string][]byte{
  372. "targetProperty": []byte("map-foo-value"),
  373. "bar": []byte("map-bar-value"),
  374. }, nil)
  375. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  376. // check values
  377. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  378. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  379. Expect(string(secret.Data["bar"])).To(Equal("value from map: map-bar-value"))
  380. Expect(string(secret.Data[tplFromKey])).To(Equal("tpl-from-value: someValue // map-bar-value"))
  381. Expect(string(secret.Data[tplFromSecKey])).To(Equal("tpl-from-sec-value: someValue // map-bar-value"))
  382. }
  383. }
  384. refreshWithTemplate := func(tc *testCase) {
  385. const secretVal = "someValue"
  386. const expectedSecretVal = "SOMEVALUE was templated"
  387. const tplStaticKey = "tplstatickey"
  388. const tplStaticVal = "tplstaticvalue"
  389. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  390. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  391. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  392. Labels: map[string]string{"foo": "bar"},
  393. Annotations: map[string]string{"foo": "bar"},
  394. },
  395. Type: v1.SecretTypeOpaque,
  396. Data: map[string]string{
  397. targetProp: "{{ .targetProperty | toString | upper }} was templated",
  398. tplStaticKey: tplStaticVal,
  399. },
  400. }
  401. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  402. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  403. // check values
  404. Expect(string(secret.Data[targetProp])).To(Equal(expectedSecretVal))
  405. Expect(string(secret.Data[tplStaticKey])).To(Equal(tplStaticVal))
  406. // labels/annotations should be taken from the template
  407. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  408. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  409. cleanEs := tc.externalSecret.DeepCopy()
  410. // now update ExternalSecret
  411. tc.externalSecret.Spec.Target.Template.Metadata.Annotations["fuzz"] = "buzz"
  412. tc.externalSecret.Spec.Target.Template.Metadata.Labels["fuzz"] = "buzz"
  413. tc.externalSecret.Spec.Target.Template.Data["new"] = "value"
  414. Expect(k8sClient.Patch(context.Background(), tc.externalSecret, client.MergeFrom(cleanEs))).To(Succeed())
  415. // wait for secret
  416. sec := &v1.Secret{}
  417. secretLookupKey := types.NamespacedName{
  418. Name: ExternalSecretTargetSecretName,
  419. Namespace: ExternalSecretNamespace,
  420. }
  421. Eventually(func() bool {
  422. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  423. if err != nil {
  424. return false
  425. }
  426. // ensure new data value exist
  427. return string(sec.Data["new"]) == "value"
  428. }, time.Second*10, time.Millisecond*200).Should(BeTrue())
  429. // also check labels/annotations have been updated
  430. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  431. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  432. }
  433. }
  434. onlyMetadataFromTemplate := func(tc *testCase) {
  435. const secretVal = "someValue"
  436. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  437. tc.externalSecret.Spec.Target.Template = &esv1alpha1.ExternalSecretTemplate{
  438. Metadata: esv1alpha1.ExternalSecretTemplateMetadata{
  439. Labels: map[string]string{"foo": "bar"},
  440. Annotations: map[string]string{"foo": "bar"},
  441. },
  442. }
  443. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  444. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  445. // check values
  446. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  447. // labels/annotations should be taken from the template
  448. Expect(secret.ObjectMeta.Labels).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Labels))
  449. Expect(secret.ObjectMeta.Annotations).To(BeEquivalentTo(es.Spec.Target.Template.Metadata.Annotations))
  450. }
  451. }
  452. // when the provider secret changes the Kind=Secret value
  453. // must change, too.
  454. refreshSecretValue := func(tc *testCase) {
  455. const targetProp = "targetProperty"
  456. const secretVal = "someValue"
  457. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  458. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Second}
  459. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  460. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  461. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  462. Eventually(func() bool {
  463. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  464. return metric.GetCounter().GetValue() == 1.0
  465. }, timeout, interval).Should(BeTrue())
  466. // check values
  467. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  468. // update provider secret
  469. newValue := "NEW VALUE"
  470. sec := &v1.Secret{}
  471. fakeProvider.WithGetSecret([]byte(newValue), nil)
  472. secretLookupKey := types.NamespacedName{
  473. Name: ExternalSecretTargetSecretName,
  474. Namespace: ExternalSecretNamespace,
  475. }
  476. Eventually(func() bool {
  477. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  478. if err != nil {
  479. return false
  480. }
  481. v := sec.Data[targetProp]
  482. return string(v) == newValue
  483. }, timeout, interval).Should(BeTrue())
  484. }
  485. }
  486. refreshintervalZero := func(tc *testCase) {
  487. const targetProp = "targetProperty"
  488. const secretVal = "someValue"
  489. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  490. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: 0}
  491. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  492. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  493. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  494. Eventually(func() bool {
  495. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  496. return metric.GetCounter().GetValue() == 1.0
  497. }, timeout, interval).Should(BeTrue())
  498. // check values
  499. Expect(string(secret.Data[targetProp])).To(Equal(secretVal))
  500. // update provider secret
  501. newValue := "NEW VALUE"
  502. sec := &v1.Secret{}
  503. fakeProvider.WithGetSecret([]byte(newValue), nil)
  504. secretLookupKey := types.NamespacedName{
  505. Name: ExternalSecretTargetSecretName,
  506. Namespace: ExternalSecretNamespace,
  507. }
  508. Consistently(func() bool {
  509. err := k8sClient.Get(context.Background(), secretLookupKey, sec)
  510. if err != nil {
  511. return false
  512. }
  513. v := sec.Data[targetProp]
  514. return string(v) == secretVal
  515. }, time.Second*10, time.Second).Should(BeTrue())
  516. }
  517. }
  518. // with dataFrom all properties from the specified secret
  519. // should be put into the secret
  520. syncWithDataFrom := func(tc *testCase) {
  521. tc.externalSecret.Spec.Data = nil
  522. tc.externalSecret.Spec.DataFrom = []esv1alpha1.ExternalSecretDataRemoteRef{
  523. {
  524. Key: remoteKey,
  525. },
  526. }
  527. fakeProvider.WithGetSecretMap(map[string][]byte{
  528. "foo": []byte("map-foo-value"),
  529. "bar": []byte("map-bar-value"),
  530. }, nil)
  531. tc.checkSecret = func(es *esv1alpha1.ExternalSecret, secret *v1.Secret) {
  532. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  533. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 1.0)).To(BeTrue())
  534. Eventually(func() bool {
  535. Expect(syncCallsTotal.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  536. return metric.GetCounter().GetValue() == 1.0
  537. }, timeout, interval).Should(BeTrue())
  538. // check values
  539. Expect(string(secret.Data["foo"])).To(Equal("map-foo-value"))
  540. Expect(string(secret.Data["bar"])).To(Equal("map-bar-value"))
  541. }
  542. }
  543. // when a provider errors in a GetSecret call
  544. // a error condition must be set.
  545. providerErrCondition := func(tc *testCase) {
  546. const secretVal = "foobar"
  547. fakeProvider.WithGetSecret(nil, fmt.Errorf("boom"))
  548. tc.externalSecret.Spec.RefreshInterval = &metav1.Duration{Duration: time.Millisecond * 100}
  549. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  550. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  551. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  552. return false
  553. }
  554. return true
  555. }
  556. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  557. Eventually(func() bool {
  558. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  559. return metric.GetCounter().GetValue() >= 2.0
  560. }, timeout, interval).Should(BeTrue())
  561. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  562. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  563. // es condition should reflect recovered provider error
  564. fakeProvider.WithGetSecret([]byte(secretVal), nil)
  565. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  566. Eventually(func() bool {
  567. err := k8sClient.Get(context.Background(), esKey, es)
  568. if err != nil {
  569. return false
  570. }
  571. // condition must now be true!
  572. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  573. if cond == nil && cond.Status != v1.ConditionTrue {
  574. return false
  575. }
  576. return true
  577. }, timeout, interval).Should(BeTrue())
  578. }
  579. }
  580. // When a ExternalSecret references an non-existing SecretStore
  581. // a error condition must be set.
  582. storeMissingErrCondition := func(tc *testCase) {
  583. tc.externalSecret.Spec.SecretStoreRef.Name = "nonexistent"
  584. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  585. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  586. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  587. return false
  588. }
  589. return true
  590. }
  591. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  592. Eventually(func() bool {
  593. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  594. return metric.GetCounter().GetValue() >= 2.0
  595. }, timeout, interval).Should(BeTrue())
  596. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  597. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  598. }
  599. }
  600. // when the provider constructor errors (e.g. invalid configuration)
  601. // a SecretSyncedError status condition must be set
  602. storeConstructErrCondition := func(tc *testCase) {
  603. fakeProvider.WithNew(func(context.Context, esv1alpha1.GenericStore, client.Client,
  604. string) (provider.SecretsClient, error) {
  605. return nil, fmt.Errorf("artificial constructor error")
  606. })
  607. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  608. // condition must be false
  609. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  610. if cond == nil || cond.Status != v1.ConditionFalse || cond.Reason != esv1alpha1.ConditionReasonSecretSyncedError {
  611. return false
  612. }
  613. return true
  614. }
  615. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  616. Eventually(func() bool {
  617. Expect(syncCallsError.WithLabelValues(ExternalSecretName, ExternalSecretNamespace).Write(&metric)).To(Succeed())
  618. return metric.GetCounter().GetValue() >= 2.0
  619. }, timeout, interval).Should(BeTrue())
  620. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 1.0)).To(BeTrue())
  621. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  622. }
  623. }
  624. // when a SecretStore has a controller field set which we don't care about
  625. // the externalSecret must not be touched
  626. ignoreMismatchController := func(tc *testCase) {
  627. tc.secretStore.Spec.Controller = "nop"
  628. tc.checkCondition = func(es *esv1alpha1.ExternalSecret) bool {
  629. cond := GetExternalSecretCondition(es.Status, esv1alpha1.ExternalSecretReady)
  630. return cond == nil
  631. }
  632. tc.checkExternalSecret = func(es *esv1alpha1.ExternalSecret) {
  633. // Condition True and False should be 0, since the Condition was not created
  634. Eventually(func() float64 {
  635. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionTrue)).Write(&metric)).To(Succeed())
  636. return metric.GetGauge().GetValue()
  637. }, timeout, interval).Should(Equal(0.0))
  638. Eventually(func() float64 {
  639. Expect(externalSecretCondition.WithLabelValues(ExternalSecretName, ExternalSecretNamespace, string(esv1alpha1.ExternalSecretReady), string(v1.ConditionFalse)).Write(&metric)).To(Succeed())
  640. return metric.GetGauge().GetValue()
  641. }, timeout, interval).Should(Equal(0.0))
  642. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionFalse, 0.0)).To(BeTrue())
  643. Expect(externalSecretConditionShouldBe(ExternalSecretName, ExternalSecretNamespace, esv1alpha1.ExternalSecretReady, v1.ConditionTrue, 0.0)).To(BeTrue())
  644. }
  645. }
  646. DescribeTable("When reconciling an ExternalSecret",
  647. func(tweaks ...testTweaks) {
  648. tc := makeDefaultTestcase()
  649. for _, tweak := range tweaks {
  650. tweak(tc)
  651. }
  652. ctx := context.Background()
  653. By("creating a secret store and external secret")
  654. Expect(k8sClient.Create(ctx, tc.secretStore)).To(Succeed())
  655. Expect(k8sClient.Create(ctx, tc.externalSecret)).Should(Succeed())
  656. esKey := types.NamespacedName{Name: ExternalSecretName, Namespace: ExternalSecretNamespace}
  657. createdES := &esv1alpha1.ExternalSecret{}
  658. By("checking the es condition")
  659. Eventually(func() bool {
  660. err := k8sClient.Get(ctx, esKey, createdES)
  661. if err != nil {
  662. return false
  663. }
  664. return tc.checkCondition(createdES)
  665. }, timeout, interval).Should(BeTrue())
  666. tc.checkExternalSecret(createdES)
  667. // this must be optional so we can test faulty es configuration
  668. if tc.checkSecret != nil {
  669. syncedSecret := &v1.Secret{}
  670. secretLookupKey := types.NamespacedName{
  671. Name: ExternalSecretTargetSecretName,
  672. Namespace: ExternalSecretNamespace,
  673. }
  674. Eventually(func() bool {
  675. err := k8sClient.Get(ctx, secretLookupKey, syncedSecret)
  676. return err == nil
  677. }, timeout, interval).Should(BeTrue())
  678. tc.checkSecret(createdES, syncedSecret)
  679. }
  680. },
  681. Entry("should set the condition eventually", syncLabelsAnnotations),
  682. Entry("should merge with existing secret using creationPolicy=Merge", mergeWithSecret),
  683. Entry("should error if sceret doesn't exist when using creationPolicy=Merge", mergeWithSecretErr),
  684. Entry("should not resolve conflicts with creationPolicy=Merge", mergeWithConflict),
  685. Entry("should sync with template", syncWithTemplate),
  686. Entry("should sync template with correct value precedence", syncWithTemplatePrecedence),
  687. Entry("should refresh secret from template", refreshWithTemplate),
  688. Entry("should be able to use only metadata from template", onlyMetadataFromTemplate),
  689. Entry("should refresh secret value when provider secret changes", refreshSecretValue),
  690. Entry("should not refresh secret value when provider secret changes but refreshInterval is zero", refreshintervalZero),
  691. Entry("should fetch secret using dataFrom", syncWithDataFrom),
  692. Entry("should set error condition when provider errors", providerErrCondition),
  693. Entry("should set an error condition when store does not exist", storeMissingErrCondition),
  694. Entry("should set an error condition when store provider constructor fails", storeConstructErrCondition),
  695. Entry("should not process store with mismatching controller field", ignoreMismatchController),
  696. )
  697. })
  698. var _ = Describe("ExternalSecret refresh logic", func() {
  699. Context("secret refresh", func() {
  700. It("should refresh when resource version does not match", func() {
  701. Expect(shouldRefresh(esv1alpha1.ExternalSecret{
  702. Status: esv1alpha1.ExternalSecretStatus{
  703. SyncedResourceVersion: "some resource version",
  704. },
  705. })).To(BeTrue())
  706. })
  707. It("should refresh when labels change", func() {
  708. es := esv1alpha1.ExternalSecret{
  709. ObjectMeta: metav1.ObjectMeta{
  710. Generation: 1,
  711. Labels: map[string]string{
  712. "foo": "bar",
  713. },
  714. },
  715. Spec: esv1alpha1.ExternalSecretSpec{
  716. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  717. },
  718. Status: esv1alpha1.ExternalSecretStatus{
  719. RefreshTime: metav1.Now(),
  720. },
  721. }
  722. es.Status.SyncedResourceVersion = getResourceVersion(es)
  723. // this should not refresh, rv matches object
  724. Expect(shouldRefresh(es)).To(BeFalse())
  725. // change labels without changing the syncedResourceVersion and expect refresh
  726. es.ObjectMeta.Labels["new"] = "w00t"
  727. Expect(shouldRefresh(es)).To(BeTrue())
  728. })
  729. It("should refresh when annotations change", func() {
  730. es := esv1alpha1.ExternalSecret{
  731. ObjectMeta: metav1.ObjectMeta{
  732. Generation: 1,
  733. Annotations: map[string]string{
  734. "foo": "bar",
  735. },
  736. },
  737. Spec: esv1alpha1.ExternalSecretSpec{
  738. RefreshInterval: &metav1.Duration{Duration: time.Minute},
  739. },
  740. Status: esv1alpha1.ExternalSecretStatus{
  741. RefreshTime: metav1.Now(),
  742. },
  743. }
  744. es.Status.SyncedResourceVersion = getResourceVersion(es)
  745. // this should not refresh, rv matches object
  746. Expect(shouldRefresh(es)).To(BeFalse())
  747. // change annotations without changing the syncedResourceVersion and expect refresh
  748. es.ObjectMeta.Annotations["new"] = "w00t"
  749. Expect(shouldRefresh(es)).To(BeTrue())
  750. })
  751. It("should refresh when generation has changed", func() {
  752. es := esv1alpha1.ExternalSecret{
  753. ObjectMeta: metav1.ObjectMeta{
  754. Generation: 1,
  755. },
  756. Spec: esv1alpha1.ExternalSecretSpec{
  757. RefreshInterval: &metav1.Duration{Duration: 0},
  758. },
  759. Status: esv1alpha1.ExternalSecretStatus{
  760. RefreshTime: metav1.Now(),
  761. },
  762. }
  763. es.Status.SyncedResourceVersion = getResourceVersion(es)
  764. Expect(shouldRefresh(es)).To(BeFalse())
  765. // update gen -> refresh
  766. es.ObjectMeta.Generation = 2
  767. Expect(shouldRefresh(es)).To(BeTrue())
  768. })
  769. It("should skip refresh when refreshInterval is 0", func() {
  770. es := esv1alpha1.ExternalSecret{
  771. ObjectMeta: metav1.ObjectMeta{
  772. Generation: 1,
  773. },
  774. Spec: esv1alpha1.ExternalSecretSpec{
  775. RefreshInterval: &metav1.Duration{Duration: 0},
  776. },
  777. Status: esv1alpha1.ExternalSecretStatus{},
  778. }
  779. // resource version matches
  780. es.Status.SyncedResourceVersion = getResourceVersion(es)
  781. Expect(shouldRefresh(es)).To(BeFalse())
  782. })
  783. It("should refresh when refresh interval has passed", func() {
  784. es := esv1alpha1.ExternalSecret{
  785. ObjectMeta: metav1.ObjectMeta{
  786. Generation: 1,
  787. },
  788. Spec: esv1alpha1.ExternalSecretSpec{
  789. RefreshInterval: &metav1.Duration{Duration: time.Second},
  790. },
  791. Status: esv1alpha1.ExternalSecretStatus{
  792. RefreshTime: metav1.NewTime(metav1.Now().Add(-time.Second * 5)),
  793. },
  794. }
  795. // resource version matches
  796. es.Status.SyncedResourceVersion = getResourceVersion(es)
  797. Expect(shouldRefresh(es)).To(BeTrue())
  798. })
  799. It("should refresh when no refresh time was set", func() {
  800. es := esv1alpha1.ExternalSecret{
  801. ObjectMeta: metav1.ObjectMeta{
  802. Generation: 1,
  803. },
  804. Spec: esv1alpha1.ExternalSecretSpec{
  805. RefreshInterval: &metav1.Duration{Duration: time.Second},
  806. },
  807. Status: esv1alpha1.ExternalSecretStatus{},
  808. }
  809. // resource version matches
  810. es.Status.SyncedResourceVersion = getResourceVersion(es)
  811. Expect(shouldRefresh(es)).To(BeTrue())
  812. })
  813. })
  814. Context("objectmeta hash", func() {
  815. It("should produce different hashes for different k/v pairs", func() {
  816. h1 := hashMeta(metav1.ObjectMeta{
  817. Generation: 1,
  818. Annotations: map[string]string{
  819. "foo": "bar",
  820. },
  821. })
  822. h2 := hashMeta(metav1.ObjectMeta{
  823. Generation: 1,
  824. Annotations: map[string]string{
  825. "foo": "bing",
  826. },
  827. })
  828. Expect(h1).ToNot(Equal(h2))
  829. })
  830. It("should produce different hashes for different generations but same label/annotations", func() {
  831. h1 := hashMeta(metav1.ObjectMeta{
  832. Generation: 1,
  833. Annotations: map[string]string{
  834. "foo": "bar",
  835. },
  836. Labels: map[string]string{
  837. "foo": "bar",
  838. },
  839. })
  840. h2 := hashMeta(metav1.ObjectMeta{
  841. Generation: 2,
  842. Annotations: map[string]string{
  843. "foo": "bar",
  844. },
  845. Labels: map[string]string{
  846. "foo": "bar",
  847. },
  848. })
  849. Expect(h1).To(Equal(h2))
  850. })
  851. It("should produce the same hash for the same k/v pairs", func() {
  852. h1 := hashMeta(metav1.ObjectMeta{
  853. Generation: 1,
  854. })
  855. h2 := hashMeta(metav1.ObjectMeta{
  856. Generation: 1,
  857. })
  858. Expect(h1).To(Equal(h2))
  859. h1 = hashMeta(metav1.ObjectMeta{
  860. Generation: 1,
  861. Annotations: map[string]string{
  862. "foo": "bar",
  863. },
  864. })
  865. h2 = hashMeta(metav1.ObjectMeta{
  866. Generation: 1,
  867. Annotations: map[string]string{
  868. "foo": "bar",
  869. },
  870. })
  871. Expect(h1).To(Equal(h2))
  872. })
  873. })
  874. })
  875. // CreateNamespace creates a new namespace in the cluster.
  876. func CreateNamespace(baseName string, c client.Client) (string, error) {
  877. genName := fmt.Sprintf("ctrl-test-%v", baseName)
  878. ns := &v1.Namespace{
  879. ObjectMeta: metav1.ObjectMeta{
  880. GenerateName: genName,
  881. },
  882. }
  883. var err error
  884. err = wait.Poll(time.Second, 10*time.Second, func() (bool, error) {
  885. err = c.Create(context.Background(), ns)
  886. if err != nil {
  887. return false, nil
  888. }
  889. return true, nil
  890. })
  891. if err != nil {
  892. return "", err
  893. }
  894. return ns.Name, nil
  895. }
  896. func hasOwnerRef(meta metav1.ObjectMeta, kind, name string) bool {
  897. for _, ref := range meta.OwnerReferences {
  898. if ref.Kind == kind && ref.Name == name {
  899. return true
  900. }
  901. }
  902. return false
  903. }
  904. func hasFieldOwnership(meta metav1.ObjectMeta, mgr, rawFields string) bool {
  905. for _, ref := range meta.ManagedFields {
  906. if ref.Manager == mgr && string(ref.FieldsV1.Raw) == rawFields {
  907. return true
  908. }
  909. }
  910. return false
  911. }
  912. func externalSecretConditionShouldBe(name, ns string, ct esv1alpha1.ExternalSecretConditionType, cs v1.ConditionStatus, v float64) bool {
  913. return Eventually(func() float64 {
  914. Expect(externalSecretCondition.WithLabelValues(name, ns, string(ct), string(cs)).Write(&metric)).To(Succeed())
  915. return metric.GetGauge().GetValue()
  916. }, timeout, interval).Should(Equal(v))
  917. }
  918. func init() {
  919. fakeProvider = fake.New()
  920. schema.ForceRegister(fakeProvider, &esv1alpha1.SecretStoreProvider{
  921. AWS: &esv1alpha1.AWSProvider{
  922. Service: esv1alpha1.AWSServiceSecretsManager,
  923. },
  924. })
  925. }