eso.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "time"
  19. //nolint
  20. . "github.com/onsi/gomega"
  21. v1 "k8s.io/api/core/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. "github.com/external-secrets/external-secrets-e2e/framework/log"
  27. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  28. )
  29. // WaitForSecretValue waits until a secret comes into existence and compares the secret.Data
  30. // with the provided values.
  31. func (f *Framework) WaitForSecretValue(namespace, name string, expected *v1.Secret) (*v1.Secret, error) {
  32. secret := &v1.Secret{}
  33. err := wait.PollImmediate(time.Second*10, time.Minute, func() (bool, error) {
  34. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  35. Namespace: namespace,
  36. Name: name,
  37. }, secret)
  38. if apierrors.IsNotFound(err) {
  39. return false, nil
  40. }
  41. return equalSecrets(expected, secret), nil
  42. })
  43. return secret, err
  44. }
  45. func (f *Framework) printESDebugLogs(esName, esNamespace string) {
  46. // fetch es and print status condition
  47. var es esv1.ExternalSecret
  48. err := f.CRClient.Get(context.Background(), types.NamespacedName{
  49. Name: esName,
  50. Namespace: esNamespace,
  51. }, &es)
  52. Expect(err).ToNot(HaveOccurred())
  53. log.Logf("resourceVersion=%s", es.Status.SyncedResourceVersion)
  54. for _, cond := range es.Status.Conditions {
  55. log.Logf("condition: status=%s type=%s reason=%s message=%s", cond.Status, cond.Type, cond.Reason, cond.Message)
  56. }
  57. // list events for given
  58. evs, err := f.KubeClientSet.CoreV1().Events(esNamespace).List(context.Background(), metav1.ListOptions{
  59. FieldSelector: "involvedObject.name=" + esName + ",involvedObject.kind=ExternalSecret",
  60. })
  61. Expect(err).ToNot(HaveOccurred())
  62. for _, ev := range evs.Items {
  63. log.Logf("ev reason=%s message=%s", ev.Reason, ev.Message)
  64. }
  65. // print most recent logs of default eso installation
  66. podList, err := f.KubeClientSet.CoreV1().Pods("default").List(
  67. context.Background(),
  68. metav1.ListOptions{LabelSelector: "app.kubernetes.io/instance=eso,app.kubernetes.io/name=external-secrets"})
  69. Expect(err).ToNot(HaveOccurred())
  70. numLines := int64(60)
  71. for i := range podList.Items {
  72. pod := podList.Items[i]
  73. for _, con := range pod.Spec.Containers {
  74. for _, b := range []bool{true, false} {
  75. resp := f.KubeClientSet.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{
  76. Container: con.Name,
  77. Previous: b,
  78. TailLines: &numLines,
  79. }).Do(context.TODO())
  80. err := resp.Error()
  81. if err != nil {
  82. continue
  83. }
  84. logs, err := resp.Raw()
  85. if err != nil {
  86. continue
  87. }
  88. log.Logf("[%s]: %s", "eso", string(logs))
  89. }
  90. }
  91. }
  92. }
  93. func equalSecrets(exp, ts *v1.Secret) bool {
  94. if exp.Type != ts.Type {
  95. return false
  96. }
  97. // secret contains labels which must be ignored
  98. delete(ts.ObjectMeta.Labels, esv1.LabelOwner)
  99. delete(ts.ObjectMeta.Labels, esv1.LabelManaged)
  100. if len(ts.ObjectMeta.Labels) == 0 {
  101. ts.ObjectMeta.Labels = nil
  102. }
  103. expLabels, _ := json.Marshal(exp.ObjectMeta.Labels)
  104. tsLabels, _ := json.Marshal(ts.ObjectMeta.Labels)
  105. if !bytes.Equal(expLabels, tsLabels) {
  106. return false
  107. }
  108. // secret contains data hash property which must be ignored
  109. delete(ts.ObjectMeta.Annotations, esv1.AnnotationDataHash)
  110. if len(ts.ObjectMeta.Annotations) == 0 {
  111. ts.ObjectMeta.Annotations = nil
  112. }
  113. expAnnotations, _ := json.Marshal(exp.ObjectMeta.Annotations)
  114. tsAnnotations, _ := json.Marshal(ts.ObjectMeta.Annotations)
  115. if !bytes.Equal(expAnnotations, tsAnnotations) {
  116. return false
  117. }
  118. expData, _ := json.Marshal(exp.Data)
  119. tsData, _ := json.Marshal(ts.Data)
  120. return bytes.Equal(expData, tsData)
  121. }