Skip to content

Commit 4b8842b

Browse files
author
Richard Kuo (Onyx)
committed
Merge branch 'main' of https://github.com/onyx-dot-app/onyx into feature/helm-separate-workers
2 parents 90c9393 + 7965fd9 commit 4b8842b

File tree

6 files changed

+76
-24
lines changed

6 files changed

+76
-24
lines changed

.github/workflows/pr-helm-chart-testing.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ jobs:
3737
echo "changed=true" >> "$GITHUB_OUTPUT"
3838
fi
3939
40+
# uncomment to force run chart-testing
41+
# - name: Force run chart-testing (list-changed)
42+
# id: list-changed
43+
# run: echo "changed=true" >> $GITHUB_OUTPUT
44+
4045
# lint all charts if any changes were detected
4146
- name: Run chart-testing (lint)
4247
if: steps.list-changed.outputs.changed == 'true'

backend/ee/onyx/external_permissions/google_drive/group_sync.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class FolderInfo(BaseModel):
3434
permissions: list[GoogleDrivePermission]
3535

3636

37-
def _get_all_folders(google_drive_connector: GoogleDriveConnector) -> list[FolderInfo]:
37+
def _get_all_folders(
38+
google_drive_connector: GoogleDriveConnector, skip_folders_without_permissions: bool
39+
) -> list[FolderInfo]:
3840
"""Have to get all folders since the group syncing system assumes all groups
3941
are returned every time.
4042
@@ -73,6 +75,9 @@ def _get_all_folders(google_drive_connector: GoogleDriveConnector) -> list[Folde
7375
for permission in raw_permissions
7476
]
7577

78+
if not permissions and skip_folders_without_permissions:
79+
continue
80+
7681
all_folders.append(
7782
FolderInfo(
7883
id=folder_id,
@@ -286,7 +291,10 @@ def gdrive_group_sync(
286291
)
287292

288293
# Get all folder permissions
289-
folder_info = _get_all_folders(google_drive_connector)
294+
folder_info = _get_all_folders(
295+
google_drive_connector=google_drive_connector,
296+
skip_folders_without_permissions=True,
297+
)
290298

291299
# Map group emails to their members
292300
group_email_to_member_emails_map = _map_group_email_to_member_emails(

backend/onyx/db/auth.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
from collections.abc import Callable
33
from typing import Any
44
from typing import Dict
5+
from typing import TypeVar
56

67
from fastapi import Depends
78
from fastapi_users.models import ID
89
from fastapi_users.models import UP
910
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
1011
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyAccessTokenDatabase
1112
from sqlalchemy import func
13+
from sqlalchemy import Select
1214
from sqlalchemy.ext.asyncio import AsyncSession
1315
from sqlalchemy.future import select
1416
from sqlalchemy.orm import Session
1517

16-
from onyx.auth.invited_users import get_invited_users
1718
from onyx.auth.schemas import UserRole
1819
from onyx.db.api_key import get_api_key_email_pattern
1920
from onyx.db.engine import get_async_session
@@ -25,6 +26,8 @@
2526
fetch_versioned_implementation_with_fallback,
2627
)
2728

29+
T = TypeVar("T", bound=tuple[Any, ...])
30+
2831

2932
def get_default_admin_user_emails() -> list[str]:
3033
"""Returns a list of emails who should default to Admin role.
@@ -37,31 +40,44 @@ def get_default_admin_user_emails() -> list[str]:
3740
return get_default_admin_user_emails_fn()
3841

3942

40-
def get_total_users_count(db_session: Session) -> int:
43+
def _add_live_user_count_where_clause(
44+
select_stmt: Select[T],
45+
only_admin_users: bool,
46+
) -> Select[T]:
4147
"""
42-
Returns the total number of users in the system.
43-
This is the sum of users and invited users.
48+
Builds a SQL column expression that can be used to filter out
49+
users who should not be included in the live user count.
4450
"""
45-
user_count = (
46-
db_session.query(User)
47-
.filter(
48-
~User.email.endswith(get_api_key_email_pattern()), # type: ignore
49-
User.role != UserRole.EXT_PERM_USER,
50-
)
51-
.count()
51+
select_stmt = select_stmt.where(~User.email.endswith(get_api_key_email_pattern())) # type: ignore
52+
if only_admin_users:
53+
return select_stmt.where(User.role == UserRole.ADMIN)
54+
55+
return select_stmt.where(
56+
User.role != UserRole.EXT_PERM_USER,
5257
)
53-
invited_users = len(get_invited_users())
54-
return user_count + invited_users
58+
59+
60+
def get_live_users_count(db_session: Session) -> int:
61+
"""
62+
Returns the number of users in the system.
63+
This does NOT include invited users, "users" pulled in
64+
from external connectors, or API keys.
65+
"""
66+
count_stmt = func.count(User.id) # type: ignore
67+
select_stmt = select(count_stmt)
68+
select_stmt_w_filters = _add_live_user_count_where_clause(select_stmt, False)
69+
user_count = db_session.scalar(select_stmt_w_filters)
70+
if user_count is None:
71+
raise RuntimeError("Was not able to fetch the user count.")
72+
return user_count
5573

5674

5775
async def get_user_count(only_admin_users: bool = False) -> int:
5876
async with get_async_session_context_manager() as session:
5977
count_stmt = func.count(User.id) # type: ignore
6078
stmt = select(count_stmt)
61-
if only_admin_users:
62-
stmt = stmt.where(User.role == UserRole.ADMIN)
63-
result = await session.execute(stmt)
64-
user_count = result.scalar()
79+
stmt_w_filters = _add_live_user_count_where_clause(stmt, only_admin_users)
80+
user_count = await session.scalar(stmt_w_filters)
6581
if user_count is None:
6682
raise RuntimeError("Was not able to fetch the user count.")
6783
return user_count

backend/onyx/server/manage/users.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from onyx.configs.constants import AuthType
4545
from onyx.configs.constants import FASTAPI_USERS_AUTH_COOKIE_NAME
4646
from onyx.db.api_key import is_api_key_email_address
47-
from onyx.db.auth import get_total_users_count
47+
from onyx.db.auth import get_live_users_count
4848
from onyx.db.engine import get_session
4949
from onyx.db.models import AccessToken
5050
from onyx.db.models import User
@@ -343,7 +343,7 @@ def bulk_invite_users(
343343
logger.info("Registering tenant users")
344344
fetch_ee_implementation_or_noop(
345345
"onyx.server.tenants.billing", "register_tenant_users", None
346-
)(tenant_id, get_total_users_count(db_session))
346+
)(tenant_id, get_live_users_count(db_session))
347347

348348
return number_of_invited_users
349349
except Exception as e:
@@ -379,7 +379,7 @@ def remove_invited_user(
379379
if MULTI_TENANT and not DEV_MODE:
380380
fetch_ee_implementation_or_noop(
381381
"onyx.server.tenants.billing", "register_tenant_users", None
382-
)(tenant_id, get_total_users_count(db_session))
382+
)(tenant_id, get_live_users_count(db_session))
383383
except Exception:
384384
logger.error(
385385
"Request to update number of seats taken in control plane failed. "

deployment/helm/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Local testing
2+
3+
- One time setup
4+
- brew install kind
5+
- Ensure you have no config at ~/.kube/config
6+
- kind create cluster
7+
- mv ~/.kube/config ~/.kube/kind-config
8+
9+
- Command setup
10+
- export KUBECONFIG=~/.kube/kind-config
11+
- kubectl config use-context kind-kind
12+
- from source root run
13+
- ct install --all --helm-extra-set-args="--set=nginx.enabled=false" --debug --config ct.yaml

deployment/helm/charts/onyx/templates/tests/test-connection.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ spec:
1010
containers:
1111
- name: wget
1212
image: busybox
13-
command: ['wget']
14-
args: ['{{ include "onyx-stack.fullname" . }}-webserver:{{ .Values.webserver.service.servicePort }}']
13+
command:
14+
- /bin/sh
15+
- -c
16+
args:
17+
- |
18+
for i in $(seq 1 40); do
19+
echo "Attempt $i: wget {{ include "onyx-stack.fullname" . }}-webserver:{{ .Values.webserver.service.servicePort }}"
20+
wget {{ include "onyx-stack.fullname" . }}-webserver:{{ .Values.webserver.service.servicePort }} && exit 0
21+
sleep 15
22+
done
23+
echo "Service unavailable after 40 attempts"
24+
exit 1
1525
restartPolicy: Never

0 commit comments

Comments
 (0)