eso.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 framework
  13. import (
  14. "bytes"
  15. "context"
  16. "encoding/json"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  23. )
  24. // WaitForSecretValue waits until a secret comes into existence and compares the secret.Data
  25. // with the provided values.
  26. func (f *Framework) WaitForSecretValue(namespace, name string, expected *v1.Secret) (*v1.Secret, error) {
  27. secret := &v1.Secret{}
  28. err := wait.PollImmediate(time.Second*10, time.Minute, func() (bool, error) {
  29. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  30. Namespace: namespace,
  31. Name: name,
  32. }, secret)
  33. if apierrors.IsNotFound(err) {
  34. return false, nil
  35. }
  36. return equalSecrets(expected, secret), nil
  37. })
  38. return secret, err
  39. }
  40. func equalSecrets(exp, ts *v1.Secret) bool {
  41. if exp.Type != ts.Type {
  42. return false
  43. }
  44. expLabels, _ := json.Marshal(exp.ObjectMeta.Labels)
  45. tsLabels, _ := json.Marshal(ts.ObjectMeta.Labels)
  46. if !bytes.Equal(expLabels, tsLabels) {
  47. return false
  48. }
  49. // secret contains data hash property which must be ignored
  50. delete(ts.ObjectMeta.Annotations, esv1beta1.AnnotationDataHash)
  51. if len(ts.ObjectMeta.Annotations) == 0 {
  52. ts.ObjectMeta.Annotations = nil
  53. }
  54. expAnnotations, _ := json.Marshal(exp.ObjectMeta.Annotations)
  55. tsAnnotations, _ := json.Marshal(ts.ObjectMeta.Annotations)
  56. if !bytes.Equal(expAnnotations, tsAnnotations) {
  57. return false
  58. }
  59. expData, _ := json.Marshal(exp.Data)
  60. tsData, _ := json.Marshal(ts.Data)
  61. return bytes.Equal(expData, tsData)
  62. }