Skip to content

Commit 2f51621

Browse files
authored
Get Tenant Secret From Tenant CR (#323)
We were assuming the Tenant Credentials Secret instead of reading it from it's .spec.credsSecret this commit addresses that
1 parent 7e6e64c commit 2f51621

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

restapi/admin_tenants.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,12 @@ func GetTenantServiceURL(mi *operator.Tenant) (svcURL string) {
230230
return fmt.Sprintf("%s://%s", scheme, net.JoinHostPort(svc, strconv.Itoa(port)))
231231
}
232232

233-
func getTenantAdminClient(ctx context.Context, client K8sClientI, namespace, tenantName, svcURL string, insecure bool) (*madmin.AdminClient, error) {
233+
func getTenantAdminClient(ctx context.Context, client K8sClientI, tenant *operator.Tenant, svcURL string, insecure bool) (*madmin.AdminClient, error) {
234+
if tenant == nil || tenant.Spec.CredsSecret == nil {
235+
return nil, errors.New("invalid arguments")
236+
}
234237
// get admin credentials from secret
235-
creds, err := client.getSecret(ctx, namespace, fmt.Sprintf("%s-secret", tenantName), metav1.GetOptions{})
238+
creds, err := client.getSecret(ctx, tenant.Namespace, tenant.Spec.CredsSecret.Name, metav1.GetOptions{})
236239
if err != nil {
237240
return nil, err
238241
}
@@ -1047,8 +1050,7 @@ func getTenantUsageResponse(session *models.Principal, params admin_api.GetTenan
10471050
mAdmin, err := getTenantAdminClient(
10481051
ctx,
10491052
k8sClient,
1050-
params.Namespace,
1051-
params.Tenant,
1053+
minTenant,
10521054
svcURL,
10531055
true)
10541056
if err != nil {

restapi/admin_tenants_test.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
8989
type args struct {
9090
ctx context.Context
9191
client K8sClientI
92-
namespace string
93-
tenantName string
92+
tenant v1.Tenant
9493
serviceURL string
9594
insecure bool
9695
}
@@ -104,10 +103,15 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
104103
{
105104
name: "Return Tenant Admin, no errors",
106105
args: args{
107-
ctx: ctx,
108-
client: kClient,
109-
namespace: "default",
110-
tenantName: "tenant-1",
106+
ctx: ctx,
107+
client: kClient,
108+
tenant: v1.Tenant{
109+
ObjectMeta: metav1.ObjectMeta{
110+
Namespace: "default",
111+
Name: "tenant-1",
112+
},
113+
Spec: v1.TenantSpec{CredsSecret: &corev1.LocalObjectReference{Name: "secret-name"}},
114+
},
111115
serviceURL: "http://service-1.default.svc.cluster.local:80",
112116
},
113117
mockGetSecret: func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
@@ -132,10 +136,14 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
132136
{
133137
name: "Access key not stored on secrets",
134138
args: args{
135-
ctx: ctx,
136-
client: kClient,
137-
namespace: "default",
138-
tenantName: "tenant-1",
139+
ctx: ctx,
140+
client: kClient,
141+
tenant: v1.Tenant{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Namespace: "default",
144+
Name: "tenant-1",
145+
},
146+
},
139147
serviceURL: "http://service-1.default.svc.cluster.local:80",
140148
},
141149
mockGetSecret: func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
@@ -159,10 +167,14 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
159167
{
160168
name: "Secret key not stored on secrets",
161169
args: args{
162-
ctx: ctx,
163-
client: kClient,
164-
namespace: "default",
165-
tenantName: "tenant-1",
170+
ctx: ctx,
171+
client: kClient,
172+
tenant: v1.Tenant{
173+
ObjectMeta: metav1.ObjectMeta{
174+
Namespace: "default",
175+
Name: "tenant-1",
176+
},
177+
},
166178
serviceURL: "http://service-1.default.svc.cluster.local:80",
167179
},
168180
mockGetSecret: func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
@@ -186,10 +198,14 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
186198
{
187199
name: "Handle error on getService",
188200
args: args{
189-
ctx: ctx,
190-
client: kClient,
191-
namespace: "default",
192-
tenantName: "tenant-1",
201+
ctx: ctx,
202+
client: kClient,
203+
tenant: v1.Tenant{
204+
ObjectMeta: metav1.ObjectMeta{
205+
Namespace: "default",
206+
Name: "tenant-1",
207+
},
208+
},
193209
serviceURL: "http://service-1.default.svc.cluster.local:80",
194210
},
195211
mockGetSecret: func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
@@ -209,10 +225,14 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
209225
{
210226
name: "Handle error on getSecret",
211227
args: args{
212-
ctx: ctx,
213-
client: kClient,
214-
namespace: "default",
215-
tenantName: "tenant-1",
228+
ctx: ctx,
229+
client: kClient,
230+
tenant: v1.Tenant{
231+
ObjectMeta: metav1.ObjectMeta{
232+
Namespace: "default",
233+
Name: "tenant-1",
234+
},
235+
},
216236
serviceURL: "http://service-1.default.svc.cluster.local:80",
217237
},
218238
mockGetSecret: func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
@@ -233,7 +253,7 @@ func Test_TenantInfoTenantAdminClient(t *testing.T) {
233253
k8sclientGetSecretMock = tt.mockGetSecret
234254
k8sclientGetServiceMock = tt.mockGetService
235255
t.Run(tt.name, func(t *testing.T) {
236-
got, err := getTenantAdminClient(tt.args.ctx, tt.args.client, tt.args.namespace, tt.args.tenantName, tt.args.serviceURL, tt.args.insecure)
256+
got, err := getTenantAdminClient(tt.args.ctx, tt.args.client, &tt.args.tenant, tt.args.serviceURL, tt.args.insecure)
237257
if err != nil {
238258
if tt.wantErr {
239259
return

0 commit comments

Comments
 (0)