eso.go 4.3 KB

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