-
Notifications
You must be signed in to change notification settings - Fork 82
[NC | DBS3] Add support for reserved bucket tags #8967
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
Merged
tangledbytes
merged 1 commit into
noobaa:master
from
tangledbytes:utkarsh/feat/nc-cli-bucket-tags
May 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -39,6 +39,8 @@ const { throw_cli_error, get_bucket_owner_account_by_name, | |||||
const manage_nsfs_validations = require('../manage_nsfs/manage_nsfs_validations'); | ||||||
const nc_mkm = require('../manage_nsfs/nc_master_key_manager').get_instance(); | ||||||
const notifications_util = require('../util/notifications_util'); | ||||||
const BucketSpaceFS = require('../sdk/bucketspace_fs'); | ||||||
tangledbytes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const NoobaaEvent = require('../manage_nsfs/manage_nsfs_events_utils').NoobaaEvent; | ||||||
|
||||||
/////////////// | ||||||
//// GENERAL // | ||||||
|
@@ -135,7 +137,6 @@ async function fetch_bucket_data(action, user_input) { | |||||
force_md5_etag: user_input.force_md5_etag === undefined || user_input.force_md5_etag === '' ? user_input.force_md5_etag : get_boolean_or_string_value(user_input.force_md5_etag), | ||||||
notifications: user_input.notifications | ||||||
}; | ||||||
|
||||||
if (user_input.bucket_policy !== undefined) { | ||||||
if (typeof user_input.bucket_policy === 'string') { | ||||||
// bucket_policy deletion specified with empty string '' | ||||||
|
@@ -154,6 +155,27 @@ async function fetch_bucket_data(action, user_input) { | |||||
data = await merge_new_and_existing_config_data(data); | ||||||
} | ||||||
|
||||||
if ((action === ACTIONS.UPDATE && user_input.tag) || (action === ACTIONS.ADD)) { | ||||||
const tags = JSON.parse(user_input.tag || '[]'); | ||||||
data.tag = BucketSpaceFS._merge_reserved_tags( | ||||||
data.tag || BucketSpaceFS._default_bucket_tags(), | ||||||
tags, | ||||||
action === ACTIONS.ADD ? true : await _is_bucket_empty(data), | ||||||
); | ||||||
} | ||||||
|
||||||
if ((action === ACTIONS.UPDATE && user_input.merge_tag) || (action === ACTIONS.ADD)) { | ||||||
const merge_tags = JSON.parse(user_input.merge_tag || '[]'); | ||||||
data.tag = _.merge( | ||||||
data.tag, | ||||||
BucketSpaceFS._merge_reserved_tags( | ||||||
data.tag || BucketSpaceFS._default_bucket_tags(), | ||||||
merge_tags, | ||||||
action === ACTIONS.ADD ? true : await _is_bucket_empty(data), | ||||||
) | ||||||
); | ||||||
} | ||||||
|
||||||
//if we're updating the owner, needs to override owner in file with the owner from user input. | ||||||
//if we're adding a bucket, need to set its owner id field | ||||||
if ((action === ACTIONS.UPDATE && user_input.owner) || (action === ACTIONS.ADD)) { | ||||||
|
@@ -200,7 +222,14 @@ async function add_bucket(data) { | |||||
data._id = mongo_utils.mongoObjectId(); | ||||||
const parsed_bucket_data = await config_fs.create_bucket_config_file(data); | ||||||
await set_bucker_owner(parsed_bucket_data); | ||||||
return { code: ManageCLIResponse.BucketCreated, detail: parsed_bucket_data, event_arg: { bucket: data.name }}; | ||||||
|
||||||
const [reserved_tag_event_args] = BucketSpaceFS._generate_reserved_tag_event_args({}, data.tag); | ||||||
|
||||||
return { | ||||||
code: ManageCLIResponse.BucketCreated, | ||||||
detail: parsed_bucket_data, | ||||||
event_arg: { ...(reserved_tag_event_args || {}), bucket: data.name, account: parsed_bucket_data.bucket_owner }, | ||||||
}; | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -256,25 +285,14 @@ async function update_bucket(data) { | |||||
*/ | ||||||
async function delete_bucket(data, force) { | ||||||
try { | ||||||
const temp_dir_name = native_fs_utils.get_bucket_tmpdir_name(data._id); | ||||||
const bucket_empty = await _is_bucket_empty(data); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is a bit confusing, suggesting to change it to:
Suggested change
From what I understand the operation is to empty the bucket and return |
||||||
if (!bucket_empty && !force) { | ||||||
throw_cli_error(ManageCLIError.BucketDeleteForbiddenHasObjects, data.name); | ||||||
} | ||||||
|
||||||
const bucket_temp_dir_path = native_fs_utils.get_bucket_tmpdir_full_path(data.path, data._id); | ||||||
// fs_contexts for bucket temp dir (storage path) | ||||||
const fs_context_fs_backend = native_fs_utils.get_process_fs_context(data.fs_backend); | ||||||
let entries; | ||||||
try { | ||||||
entries = await nb_native().fs.readdir(fs_context_fs_backend, data.path); | ||||||
} catch (err) { | ||||||
dbg.warn(`delete_bucket: bucket name ${data.name},` + | ||||||
`got an error on readdir with path: ${data.path}`, err); | ||||||
// if the bucket's path was deleted first (encounter ENOENT error) - continue deletion | ||||||
if (err.code !== 'ENOENT') throw err; | ||||||
} | ||||||
if (entries) { | ||||||
const object_entries = entries.filter(element => !element.name.endsWith(temp_dir_name)); | ||||||
if (object_entries.length > 0 && !force) { | ||||||
throw_cli_error(ManageCLIError.BucketDeleteForbiddenHasObjects, data.name); | ||||||
} | ||||||
} | ||||||
|
||||||
await native_fs_utils.folder_delete(bucket_temp_dir_path, fs_context_fs_backend, true); | ||||||
await config_fs.delete_bucket_config_file(data.name); | ||||||
return { code: ManageCLIResponse.BucketDeleted, detail: { name: data.name }, event_arg: { bucket: data.name } }; | ||||||
|
@@ -340,6 +358,33 @@ async function list_bucket_config_files(wide, filters = {}) { | |||||
return config_files_list; | ||||||
} | ||||||
|
||||||
/** | ||||||
* _is_bucket_empty returns true if the given bucket is empty | ||||||
* | ||||||
* @param {*} data | ||||||
* @returns {Promise<boolean>} | ||||||
*/ | ||||||
async function _is_bucket_empty(data) { | ||||||
tangledbytes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const temp_dir_name = native_fs_utils.get_bucket_tmpdir_name(data._id); | ||||||
// fs_contexts for bucket temp dir (storage path) | ||||||
const fs_context_fs_backend = native_fs_utils.get_process_fs_context(data.fs_backend); | ||||||
let entries; | ||||||
try { | ||||||
entries = await nb_native().fs.readdir(fs_context_fs_backend, data.path); | ||||||
} catch (err) { | ||||||
dbg.warn(`_is_bucket_empty: bucket name ${data.name},` + | ||||||
`got an error on readdir with path: ${data.path}`, err); | ||||||
// if the bucket's path was deleted first (encounter ENOENT error) - continue deletion | ||||||
if (err.code !== 'ENOENT') throw err; | ||||||
} | ||||||
if (entries) { | ||||||
const object_entries = entries.filter(element => !element.name.endsWith(temp_dir_name)); | ||||||
return object_entries.length === 0; | ||||||
} | ||||||
|
||||||
return true; | ||||||
} | ||||||
|
||||||
/** | ||||||
* bucket_management does the following - | ||||||
* 1. fetches the bucket data if this is not a list operation | ||||||
|
@@ -361,7 +406,24 @@ async function bucket_management(action, user_input) { | |||||
} else if (action === ACTIONS.STATUS) { | ||||||
response = await get_bucket_status(data); | ||||||
} else if (action === ACTIONS.UPDATE) { | ||||||
response = await update_bucket(data); | ||||||
const bucket_path = config_fs.get_bucket_path_by_name(user_input.name); | ||||||
const bucket_lock_file = `${bucket_path}.lock`; | ||||||
await native_fs_utils.lock_and_run(config_fs.fs_context, bucket_lock_file, async () => { | ||||||
tangledbytes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
const prev_bucket_info = await fetch_bucket_data(action, _.omit(user_input, ['tag', 'merge_tag'])); | ||||||
const bucket_info = await fetch_bucket_data(action, user_input); | ||||||
|
||||||
const tagging_object = BucketSpaceFS._objectify_tagging_arr(prev_bucket_info.tag); | ||||||
const [ | ||||||
reserved_tag_event_args, | ||||||
reserved_tag_modified, | ||||||
] = BucketSpaceFS._generate_reserved_tag_event_args(tagging_object, bucket_info.tag); | ||||||
|
||||||
response = await update_bucket(bucket_info); | ||||||
if (reserved_tag_modified) { | ||||||
new NoobaaEvent(NoobaaEvent.BUCKET_RESERVED_TAG_MODIFIED) | ||||||
.create_event(undefined, { ...reserved_tag_event_args, bucket_name: user_input.name }); | ||||||
} | ||||||
}); | ||||||
} else if (action === ACTIONS.DELETE) { | ||||||
const force = get_boolean_or_string_value(user_input.force); | ||||||
response = await delete_bucket(data, force); | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this structure is an overkill. Isn't a simple array of reserved tag names sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to keep it simple but at the same time I didn't want to write hard-coded things.
This structure just made sure that I don't have to hard-code few things like -
Do you have a suggestion for a simpler structure but make sure that we don't have some specific code (because this generic code ain't too complicated)?