v2_support.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright © The ESO Authors
  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 common
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. . "github.com/onsi/gomega"
  19. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  20. corev1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. "github.com/external-secrets/external-secrets-e2e/framework"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  26. )
  27. const (
  28. StaticAccessKeyIDKey = "kid"
  29. StaticSecretAccessKeyKey = "sak"
  30. StaticSessionTokenKey = "st"
  31. )
  32. type V2ClusterProviderScenario struct {
  33. AuthScope esv1.AuthenticationScope
  34. ConfigName string
  35. ConfigNamespace string
  36. NamePrefix string
  37. ProviderNamespace string
  38. ProviderRefNamespace string
  39. WorkloadNamespace string
  40. }
  41. func CredentialsSecretName(name string) string {
  42. return name + "-credentials"
  43. }
  44. func StaticCredentialsSecretData(kid, sak, st string) map[string]string {
  45. return map[string]string{
  46. StaticAccessKeyIDKey: kid,
  47. StaticSecretAccessKeyKey: sak,
  48. StaticSessionTokenKey: st,
  49. }
  50. }
  51. func ProviderConfigNamespace(authScope esv1.AuthenticationScope, providerNamespace, workloadNamespace string) string {
  52. if authScope == esv1.AuthenticationScopeProviderNamespace {
  53. return providerNamespace
  54. }
  55. return workloadNamespace
  56. }
  57. func ProviderReferenceNamespace(authScope esv1.AuthenticationScope, providerNamespace string) string {
  58. if authScope == esv1.AuthenticationScopeProviderNamespace {
  59. return providerNamespace
  60. }
  61. return ""
  62. }
  63. func NewV2ClusterProviderScenario(workloadNamespace, prefix string, authScope esv1.AuthenticationScope, createProviderNamespace func(prefix string) string) V2ClusterProviderScenario {
  64. providerNamespace := workloadNamespace
  65. if authScope == esv1.AuthenticationScopeProviderNamespace && createProviderNamespace != nil {
  66. providerNamespace = createProviderNamespace(prefix + "-provider")
  67. }
  68. return V2ClusterProviderScenario{
  69. AuthScope: authScope,
  70. ConfigName: fmt.Sprintf("%s-config", prefix),
  71. ConfigNamespace: ProviderConfigNamespace(authScope, providerNamespace, workloadNamespace),
  72. NamePrefix: fmt.Sprintf("%s-%s", workloadNamespace, prefix),
  73. ProviderNamespace: providerNamespace,
  74. ProviderRefNamespace: ProviderReferenceNamespace(authScope, providerNamespace),
  75. WorkloadNamespace: workloadNamespace,
  76. }
  77. }
  78. func (s V2ClusterProviderScenario) ClusterProviderName() string {
  79. return fmt.Sprintf("%s-cluster-provider", s.NamePrefix)
  80. }
  81. func WaitForPushSecretStatus(f *framework.Framework, namespace, name string, status corev1.ConditionStatus) {
  82. Eventually(func(g Gomega) {
  83. var ps esv1alpha1.PushSecret
  84. g.Expect(f.CRClient.Get(context.Background(), types.NamespacedName{Name: name, Namespace: namespace}, &ps)).To(Succeed())
  85. g.Expect(ps.Status.Conditions).NotTo(BeEmpty())
  86. for _, condition := range ps.Status.Conditions {
  87. if condition.Type == esv1alpha1.PushSecretReady && condition.Status == status {
  88. return
  89. }
  90. }
  91. g.Expect(false).To(BeTrue())
  92. }, time.Minute, 5*time.Second).Should(Succeed())
  93. }
  94. func ExpectPushSecretEventMessage(f *framework.Framework, namespace, objectName, expectedMessage string) {
  95. Eventually(func() string {
  96. events, err := f.KubeClientSet.CoreV1().Events(namespace).List(context.Background(), metav1.ListOptions{
  97. FieldSelector: "involvedObject.name=" + objectName + ",involvedObject.kind=PushSecret",
  98. })
  99. Expect(err).NotTo(HaveOccurred())
  100. messages := make([]string, 0, len(events.Items))
  101. for _, event := range events.Items {
  102. if event.Message != "" {
  103. messages = append(messages, event.Message)
  104. }
  105. }
  106. return fmt.Sprintf("%v", messages)
  107. }, time.Minute, 5*time.Second).Should(ContainSubstring(expectedMessage))
  108. }
  109. func PushSecretMetadataWithRemoteNamespace(namespace string) *apiextensionsv1.JSON {
  110. return &apiextensionsv1.JSON{Raw: []byte(fmt.Sprintf(`{"apiVersion":"kubernetes.external-secrets.io/v1alpha1","kind":"PushSecretMetadata","spec":{"remoteNamespace":"%s"}}`, namespace))}
  111. }