github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/remotesecret/remote_secrets.go (about) 1 package remotesecret 2 3 import ( 4 "context" 5 "time" 6 7 rs "github.com/redhat-appstudio/remote-secret/api/v1beta1" 8 v1 "k8s.io/api/core/v1" 9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "k8s.io/apimachinery/pkg/types" 11 ) 12 13 // CreateRemoteSecret creates a RemoteSecret object 14 func (s *RemoteSecretController) CreateRemoteSecret(name, namespace string, targets []rs.RemoteSecretTarget, secretType v1.SecretType, labels map[string]string) (*rs.RemoteSecret, error) { 15 remoteSecret := rs.RemoteSecret{ 16 ObjectMeta: metav1.ObjectMeta{ 17 Name: name, 18 Namespace: namespace, 19 Labels: labels, 20 }, 21 Spec: rs.RemoteSecretSpec{ 22 Secret: rs.LinkableSecretSpec{ 23 GenerateName: "some-secret-", 24 Type: secretType, 25 }, 26 }, 27 } 28 29 remoteSecret.Spec.Targets = targets 30 31 ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) 32 defer cancel() 33 err := s.KubeRest().Create(ctx, &remoteSecret) 34 if err != nil { 35 return nil, err 36 } 37 return &remoteSecret, nil 38 } 39 40 // CreateRemoteSecretWithLabels creates a RemoteSecret object with specified labels and annotations 41 func (s *RemoteSecretController) CreateRemoteSecretWithLabelsAndAnnotations(name, namespace string, targetSecretName string, labels map[string]string, annotations map[string]string) (*rs.RemoteSecret, error) { 42 remoteSecret := rs.RemoteSecret{ 43 ObjectMeta: metav1.ObjectMeta{ 44 Name: name, 45 Namespace: namespace, 46 Labels: labels, 47 }, 48 Spec: rs.RemoteSecretSpec{ 49 Secret: rs.LinkableSecretSpec{ 50 Name: targetSecretName, 51 }, 52 }, 53 } 54 55 remoteSecret.ObjectMeta.Labels = labels 56 remoteSecret.ObjectMeta.Annotations = annotations 57 58 err := s.KubeRest().Create(context.Background(), &remoteSecret) 59 if err != nil { 60 return nil, err 61 } 62 return &remoteSecret, nil 63 } 64 65 // GetRemoteSecret returns the requested RemoteSecret object 66 func (s *RemoteSecretController) GetRemoteSecret(name, namespace string) (*rs.RemoteSecret, error) { 67 namespacedName := types.NamespacedName{ 68 Name: name, 69 Namespace: namespace, 70 } 71 72 remoteSecret := rs.RemoteSecret{} 73 74 err := s.KubeRest().Get(context.Background(), namespacedName, &remoteSecret) 75 if err != nil { 76 return nil, err 77 } 78 return &remoteSecret, nil 79 } 80 81 // GetTargetSecretName gets the target secret name from a given namespace 82 func (s *RemoteSecretController) GetTargetSecretName(targets []rs.TargetStatus, targetNamespace string) string { 83 targetSecretName := "" 84 85 for _, t := range targets { 86 if t.Namespace == targetNamespace { 87 return t.DeployedSecret.Name 88 } 89 } 90 91 return targetSecretName 92 } 93 94 // CreateUploadSecret creates an Upload secret object to inject data in a Remote Secret 95 func (s *RemoteSecretController) CreateUploadSecret(name, namespace string, remoteSecretName string, secretType v1.SecretType, stringData map[string]string) (*v1.Secret, error) { 96 uploadSecret := v1.Secret{ 97 ObjectMeta: metav1.ObjectMeta{ 98 Name: name, 99 Namespace: namespace, 100 Labels: map[string]string{ 101 rs.UploadSecretLabel: "remotesecret", 102 }, 103 Annotations: map[string]string{ 104 rs.RemoteSecretNameAnnotation: remoteSecretName, 105 }, 106 }, 107 Type: secretType, 108 StringData: stringData, 109 } 110 111 err := s.KubeRest().Create(context.Background(), &uploadSecret) 112 if err != nil { 113 return nil, err 114 } 115 return &uploadSecret, nil 116 } 117 118 // GetImageRepositoryRemoteSecret returns the requested image pull RemoteSecret object 119 func (s *RemoteSecretController) GetImageRepositoryRemoteSecret(name, applicationName, componentName, namespace string) (*rs.RemoteSecret, error) { 120 namespacedName := types.NamespacedName{ 121 Name: name, 122 Namespace: namespace, 123 } 124 125 remoteSecret := rs.RemoteSecret{ 126 ObjectMeta: metav1.ObjectMeta{ 127 Labels: map[string]string{ 128 "appstudio.redhat.com/application": applicationName, 129 "appstudio.redhat.com/component": componentName, 130 "appstudio.redhat.com/internal": "true", 131 }, 132 }, 133 } 134 135 err := s.KubeRest().Get(context.Background(), namespacedName, &remoteSecret) 136 if err != nil { 137 return nil, err 138 } 139 return &remoteSecret, nil 140 } 141 142 // RemoteSecretTargetHasNamespace checks whether the RemoteSecret targets contains a namespace 143 func (s *RemoteSecretController) RemoteSecretTargetsContainsNamespace(name string, rs *rs.RemoteSecret) bool { 144 for _, target := range rs.Status.Targets { 145 if target.Namespace == name { 146 return true 147 } 148 } 149 return false 150 }