eso.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  23. "github.com/external-secrets/external-secrets/e2e/framework/log"
  24. )
  25. // WaitForSecretValue waits until a secret comes into existence and compares the secret.Data
  26. // with the provided values.
  27. func (f *Framework) WaitForSecretValue(namespace, name string, expected *v1.Secret) (*v1.Secret, error) {
  28. secret := &v1.Secret{}
  29. err := wait.PollImmediate(time.Second*2, time.Minute*2, func() (bool, error) {
  30. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  31. Namespace: namespace,
  32. Name: name,
  33. }, secret)
  34. if apierrors.IsNotFound(err) {
  35. log.Logf("Secret Not Found. Expected: %+v, Got: %+v", expected, secret)
  36. return false, nil
  37. }
  38. return equalSecrets(expected, secret), nil
  39. })
  40. return secret, err
  41. }
  42. func equalSecrets(exp, ts *v1.Secret) bool {
  43. if exp.Type != ts.Type {
  44. return false
  45. }
  46. expLabels, _ := json.Marshal(exp.ObjectMeta.Labels)
  47. tsLabels, _ := json.Marshal(ts.ObjectMeta.Labels)
  48. if !bytes.Equal(expLabels, tsLabels) {
  49. return false
  50. }
  51. // secret contains data hash property which must be ignored
  52. delete(ts.ObjectMeta.Annotations, esv1alpha1.AnnotationDataHash)
  53. if len(ts.ObjectMeta.Annotations) == 0 {
  54. ts.ObjectMeta.Annotations = nil
  55. }
  56. expAnnotations, _ := json.Marshal(exp.ObjectMeta.Annotations)
  57. tsAnnotations, _ := json.Marshal(ts.ObjectMeta.Annotations)
  58. if !bytes.Equal(expAnnotations, tsAnnotations) {
  59. return false
  60. }
  61. expData, _ := json.Marshal(exp.Data)
  62. tsData, _ := json.Marshal(ts.Data)
  63. return bytes.Equal(expData, tsData)
  64. }