eso.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. //nolint
  19. . "github.com/onsi/gomega"
  20. v1 "k8s.io/api/core/v1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/wait"
  25. "github.com/external-secrets/external-secrets-e2e/framework/log"
  26. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. )
  28. // WaitForSecretValue waits until a secret comes into existence and compares the secret.Data
  29. // with the provided values.
  30. func (f *Framework) WaitForSecretValue(namespace, name string, expected *v1.Secret) (*v1.Secret, error) {
  31. secret := &v1.Secret{}
  32. err := wait.PollImmediate(time.Second*10, time.Minute, func() (bool, error) {
  33. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  34. Namespace: namespace,
  35. Name: name,
  36. }, secret)
  37. if apierrors.IsNotFound(err) {
  38. return false, nil
  39. }
  40. return equalSecrets(expected, secret), nil
  41. })
  42. return secret, err
  43. }
  44. func (f *Framework) printESDebugLogs(esName, esNamespace string) {
  45. // fetch es and print status condition
  46. var es esv1beta1.ExternalSecret
  47. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  48. Name: esName,
  49. Namespace: esNamespace,
  50. }, &es)
  51. Expect(err).ToNot(HaveOccurred())
  52. log.Logf("resourceVersion=%s", es.Status.SyncedResourceVersion)
  53. for _, cond := range es.Status.Conditions {
  54. log.Logf("condition: status=%s type=%s reason=%s message=%s", cond.Status, cond.Type, cond.Reason, cond.Message)
  55. }
  56. // list events for given
  57. evs, err := f.KubeClientSet.CoreV1().Events(esNamespace).List(context.Background(), metav1.ListOptions{
  58. FieldSelector: "involvedObject.name=" + esName + ",involvedObject.kind=ExternalSecret",
  59. })
  60. Expect(err).ToNot(HaveOccurred())
  61. for _, ev := range evs.Items {
  62. log.Logf("ev reason=%s message=%s", ev.Reason, ev.Message)
  63. }
  64. // print most recent logs of default eso installation
  65. podList, err := f.KubeClientSet.CoreV1().Pods("default").List(
  66. context.Background(),
  67. metav1.ListOptions{LabelSelector: "app.kubernetes.io/instance=eso,app.kubernetes.io/name=external-secrets"})
  68. Expect(err).ToNot(HaveOccurred())
  69. numLines := int64(60)
  70. for i := range podList.Items {
  71. pod := podList.Items[i]
  72. for _, con := range pod.Spec.Containers {
  73. for _, b := range []bool{true, false} {
  74. resp := f.KubeClientSet.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{
  75. Container: con.Name,
  76. Previous: b,
  77. TailLines: &numLines,
  78. }).Do(context.TODO())
  79. err := resp.Error()
  80. if err != nil {
  81. continue
  82. }
  83. logs, err := resp.Raw()
  84. if err != nil {
  85. continue
  86. }
  87. log.Logf("[%s]: %s", "eso", string(logs))
  88. }
  89. }
  90. }
  91. }
  92. func equalSecrets(exp, ts *v1.Secret) bool {
  93. if exp.Type != ts.Type {
  94. return false
  95. }
  96. expLabels, _ := json.Marshal(exp.ObjectMeta.Labels)
  97. tsLabels, _ := json.Marshal(ts.ObjectMeta.Labels)
  98. if !bytes.Equal(expLabels, tsLabels) {
  99. return false
  100. }
  101. // secret contains data hash property which must be ignored
  102. delete(ts.ObjectMeta.Annotations, esv1beta1.AnnotationDataHash)
  103. if len(ts.ObjectMeta.Annotations) == 0 {
  104. ts.ObjectMeta.Annotations = nil
  105. }
  106. expAnnotations, _ := json.Marshal(exp.ObjectMeta.Annotations)
  107. tsAnnotations, _ := json.Marshal(ts.ObjectMeta.Annotations)
  108. if !bytes.Equal(expAnnotations, tsAnnotations) {
  109. return false
  110. }
  111. expData, _ := json.Marshal(exp.Data)
  112. tsData, _ := json.Marshal(ts.Data)
  113. return bytes.Equal(expData, tsData)
  114. }