@@ -9,34 +9,42 @@ import (
9
9
"sigs.k8s.io/controller-runtime/pkg/client"
10
10
)
11
11
12
+ // getSecret retrieves a secret if it exists, returns an error if not
13
+ func (r * TrustyAIServiceReconciler ) getSecret (ctx context.Context , name , namespace string ) (* corev1.Secret , error ) {
14
+ secret := & corev1.Secret {}
15
+ err := r .Get (ctx , client.ObjectKey {Name : name , Namespace : namespace }, secret )
16
+ if err != nil {
17
+ if errors .IsNotFound (err ) {
18
+ return nil , fmt .Errorf ("secret %s not found in namespace %s: %w" , name , namespace , err )
19
+ }
20
+ return nil , fmt .Errorf ("failed to get secret %s in namespace %s: %w" , name , namespace , err )
21
+ }
22
+ return secret , nil
23
+ }
24
+
12
25
// findDatabaseSecret finds the DB configuration secret named (specified or default) in the same namespace as the CR
13
26
func (r * TrustyAIServiceReconciler ) findDatabaseSecret (ctx context.Context , instance * trustyaiopendatahubiov1alpha1.TrustyAIService ) (* corev1.Secret , error ) {
14
27
15
28
databaseConfigurationsName := instance .Spec .Storage .DatabaseConfigurations
16
29
defaultDatabaseConfigurationsName := instance .Name + dbCredentialsSuffix
17
30
18
- secret := & corev1.Secret {}
19
-
20
31
if databaseConfigurationsName != "" {
21
- secret := & corev1.Secret {}
22
- err := r .Get (ctx , client.ObjectKey {Name : databaseConfigurationsName , Namespace : instance .Namespace }, secret )
23
- if err == nil {
24
- return secret , nil
32
+ secret , err := r .getSecret (ctx , databaseConfigurationsName , instance .Namespace )
33
+ if err != nil {
34
+ return nil , err
25
35
}
26
- if ! errors . IsNotFound ( err ) {
27
- return nil , fmt . Errorf ( "failed to get secret %s in namespace %s: %w" , databaseConfigurationsName , instance . Namespace , err )
36
+ if secret != nil {
37
+ return secret , nil
28
38
}
29
39
} else {
30
40
// If specified not found, try the default
31
-
32
- err := r .Get (ctx , client.ObjectKey {Name : defaultDatabaseConfigurationsName , Namespace : instance .Namespace }, secret )
33
- if err == nil {
34
- return secret , nil
41
+ secret , err := r .getSecret (ctx , defaultDatabaseConfigurationsName , instance .Namespace )
42
+ if err != nil {
43
+ return nil , err
35
44
}
36
- if ! errors . IsNotFound ( err ) {
37
- return nil , fmt . Errorf ( "failed to get secret %s in namespace %s: %w" , defaultDatabaseConfigurationsName , instance . Namespace , err )
45
+ if secret != nil {
46
+ return secret , nil
38
47
}
39
-
40
48
}
41
49
42
50
return nil , fmt .Errorf ("neither secret %s nor %s found in namespace %s" , databaseConfigurationsName , defaultDatabaseConfigurationsName , instance .Namespace )
0 commit comments