Skip to content

Commit

Permalink
Fix robot account names which contains namespace name
Browse files Browse the repository at this point in the history
  • Loading branch information
steadyk committed Mar 6, 2024
1 parent 24ad297 commit 41f6b86
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/harbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,25 @@ async def sync_registries(target_registries: [Registry]):
await client.create_registry(registry=target_registry)


async def construct_target_robot_name(target_robot: Robot) -> str:
if namespace := target_robot.permissions[0].namespace is not '*':
return f'{robot_name_prefix}{namespace}+{target_robot.name}'
else:
return f'{robot_name_prefix}{target_robot.name}'


async def sync_robot_accounts(target_robots: [Robot]):
current_robots = await client.get_robots()
current_robot_names = [
current_robot.name for current_robot in current_robots
]
current_robot_id = [current_robot.id for current_robot in current_robots]

# Harbor appends a prefix to all robot account names
# Harbor appends a prefix and namespace if present to all
# robot account names.
# To compare against our target robot names, we have to add the prefix
target_robot_names_with_prefix = [
robot_name_prefix + target_robot["name"]
construct_target_robot_name(target_robot)
for target_robot in target_robots
]

Expand All @@ -178,26 +186,26 @@ async def sync_robot_accounts(target_robots: [Robot]):
target_robot.name.upper().replace("-", "_")
)
# Modify existing robot
if robot_name_prefix + target_robot.name in current_robot_names:
if robot_name in current_robot_names:
robot_id = current_robot_id[
current_robot_names.index(
robot_name_prefix + target_robot.name
construct_target_robot_name(target_robot)
)
]
target_robot.name = robot_name_prefix + target_robot.name
target_robot.name = construct_target_robot_name(target_robot)
print(f'- Syncing robot "{target_robot.name}".')
await client.update_robot(robot_id=robot_id, robot=target_robot)
# Create new robot
else:
print(
"- Creating new robot"
f' "{robot_name_prefix + target_robot.name}"'
f' "{construct_target_robot_name(target_robot)}"'
)
try:
await client.create_robot(robot=target_robot)
except Conflict as e:
print(
f''' => ERROR: "{robot_name_prefix + target_robot.name}"
f''' => "{construct_target_robot_name(target_robot)}"
Harbor Conflict Error: {e}'''
)
except BadRequest as e:
Expand Down

0 comments on commit 41f6b86

Please sign in to comment.