eso.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. )
  23. // WaitForSecretValue waits until a secret comes into existence and compares the secret.Data
  24. // with the provided values.
  25. func (f *Framework) WaitForSecretValue(namespace, name string, expected *v1.Secret) (*v1.Secret, error) {
  26. secret := &v1.Secret{}
  27. err := wait.PollImmediate(time.Second*2, time.Minute*2, func() (bool, error) {
  28. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  29. Namespace: namespace,
  30. Name: name,
  31. }, secret)
  32. if apierrors.IsNotFound(err) {
  33. return false, nil
  34. }
  35. return equalSecrets(expected, secret), nil
  36. })
  37. return secret, err
  38. }
  39. func equalSecrets(exp, ts *v1.Secret) bool {
  40. if exp.Type != ts.Type {
  41. return false
  42. }
  43. expLabels, _ := json.Marshal(exp.ObjectMeta.Labels)
  44. tsLabels, _ := json.Marshal(ts.ObjectMeta.Labels)
  45. if !bytes.Equal(expLabels, tsLabels) {
  46. return false
  47. }
  48. expAnnotations, _ := json.Marshal(exp.ObjectMeta.Annotations)
  49. tsAnnotations, _ := json.Marshal(ts.ObjectMeta.Annotations)
  50. if !bytes.Equal(expAnnotations, tsAnnotations) {
  51. return false
  52. }
  53. expData, _ := json.Marshal(exp.Data)
  54. tsData, _ := json.Marshal(ts.Data)
  55. return bytes.Equal(expData, tsData)
  56. }