Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up secrets created during the tests. #208

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions cmd/janitor/gcp_janitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,51 @@ def clean_gke_cluster(project, age, filt):
return len(errs) > 0


def clean_secrets(project, age, filt):
"""Clean up secrets from Google secret manager"""
os.environ['CLOUDSDK_API_ENDPOINT_OVERRIDES_SECRETMANAGER'] = 'https://secretmanager.googleapis.com/'
cmd = [
'gcloud', 'secrets', 'list',
'--project=%s' % project,
'--format=json(name,createTime)'
]
log('running %s' % cmd)

output = ''
try:
output = subprocess.check_output(cmd)
except subprocess.CalledProcessError as exc:
# expected error
log('Cannot list secrets')
return 1

for item in json.loads(output):
log('secret info: %r' % item)
if 'name' not in item or 'createTime' not in item:
raise ValueError('name and createTime must be present: %r' % item)

# createTime in format '2024-10-16T07:22:22.119486Z'
# Only `024-10-16T07:22:22` this part is required.
item['createTime'] = item['createTime'].split('+')[0]
created = datetime.datetime.strptime(
item['createTime'][:19], '%Y-%m-%dT%H:%M:%S')
if created < age:
log('Found stale gke cluster %r, created time = %r' %
dargudear-google marked this conversation as resolved.
Show resolved Hide resolved
(item['name'], item['createTime']))
delete = [
'gcloud', 'secrets', '-q', 'delete',
item['name'],
'--project=%s' % project,
]
try:
output = subprocess.check_output(delete)
except subprocess.CalledProcessError as exc:
# expected error
log('Cannot delete secret %r'%(item['name']))
continue
return 0


def activate_service_account(service_account):
print('[=== Activating service_account %s ===]' % service_account)
cmd = [
Expand Down Expand Up @@ -675,6 +720,13 @@ def main(project, days, hours, filt, rate_limit, service_account, additional_zon
err |= 1 # keep cleaning the other resource
print('Fail to clean up cluster from project %r' % project, file=sys.stderr)

# try to clean secrets created.
try:
err |= clean_secrets(project, age, filt)
except ValueError:
err |= 1 # keep cleaning the other resource
print('Fail to clean up secrets from project %r' % project, file=sys.stderr)

zones = BASE_ZONES + additional_zones
gkehub_apis = {'gkehub.googleapis.com', 'staging-gkehub.sandbox.googleapis.com', 'autopush-gkehub.sandbox.googleapis.com'}
for api, resources in RESOURCES_BY_API.items():
Expand Down