-
Notifications
You must be signed in to change notification settings - Fork 184
ore/aws: Handle missing SnapshotID
#3896
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
Conversation
mantle/cmd/ore/aws/delete-image.go
Outdated
|
||
if snapshotID != "" { | ||
err := API.RemoveBySnapshotTag(snapshotID, allowMissing) | ||
err := API.RemoveBySnapshotTag(snapshotID, amiID, allowMissing) |
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 think rather than changing this function we should change the input --snapshot
to accept a new special value for snapshot ID of detectFromAMI
.
If this value was passed in then we would:
- First determine if AMI exists
- If AMI exists determine snapshot ID from it and set that value
then we can continue with the rest of the code
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.
we would then change the calling GC code to pass in --snapshot detectFromAMI
if no snapshot was set in the json.
b7db303
to
436a64b
Compare
src/cmd-cloud-prune
Outdated
region_name = ami.get("name") | ||
ami_id = ami.get("hvm") | ||
snapshot_id = ami.get("snapshot") | ||
snapshot_id = ami.get("snapshot") or "detectFromAMI" |
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.
just provide a default value to .get()
and it will do what you want here.
>>> { 'name': 'abcdefg' }.get("name", 'default')
'abcdefg'
>>> { 'name': 'abcdefg' }.get("blank", 'default')
'default'
let's also add a comment here with a little more explanation on what we are doing.. i.e. If we are dealing with an old manifest where the snapshot ID isn't stored then let's instruct ore to detect the snapshot ID from the AMI.
mantle/cmd/ore/aws/delete-image.go
Outdated
} | ||
|
||
if snapshotID != "" { | ||
if snapshotID == "detectFromAMI" { |
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.
maybe this should be
if snapshotID == "detectFromAMI" { | |
if snapshotID == "detectFromAMI" && amiID != "" { |
mantle/cmd/ore/aws/delete-image.go
Outdated
// Let's try to extract the snapshotID from AMI | ||
snapshot, err := API.FindSnapshot(amiID) | ||
// FindSnapshot returns nil if not found. | ||
if err == nil { |
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 think we need to check the err != nil
case, right?
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.
The function FindSnapshot
has multiple scenarios. Basically, its either snapshot is nil
or err!=nil
if nothing is found.
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.
the FindSnapshot
function has several errors that it can throw:
unable to describe snapshots:
found multiple matching snapshots
unable to describe import tasks
couldn't describe snapshot from import task
found multiple matching import tasks
in all of those cases you can't assume there is no snapshot. You can only say there is definitely no snapshot if snapshot
and err
are both nil
.
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.
Understood, but they all mean we are not getting any snapshots from the AMI i.e. we won't be handling each scenario differently right?
From what I see, the scenario when we get a snapshot from AMI, the snapshot!=nil
and err==nil
. If anything else, it doesn't help us.
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 can possibly update the error message if needed. Something less aggressive like Failed to find any snapshot for ami-123
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.
It depends on how we want to handle it. If there was an AMI then there definitely is a snapshot and if we can't find a snapshot ID then we probably just want to error here. I mean we don't expect for an error to happen so bubbling that up to the user would be useful? or at least printing a warning (probably the better option).
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.
basically we should handle the err != nil
case even if it's only informational:
if err != nil {
print warning; do nothing
} else if snapshot == nil {
print log message; do nothing
} else {
snapshotID = snapshot
}
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.
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: Unable to find any snapshot for %s.\n", amiID)
os.Exit(0)
} else if snapshot == nil {
fmt.Fprintf(os.Stderr, "No valid snapshot found for %s.\n", amiID)
os.Exit(0)
}
snapshotID = snapshot.SnapshotID
Something like this works? We shouldn't need the else
statement since that would just mean that we have a valid snapshot.
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.
that looks pretty good. I would actually say that we hit an error but we are going to continue. fmt.Fprintf(os.Stderr, "Warning: Encountered error when searching for snapshot for %s: %s. Continuing..\n", amiID, err)
also I wouldn't do os.Stderr
for the non warning log message.
436a64b
to
f97df9d
Compare
Added a check for when the snapshotID is None by giving it the value of detectFromAMI. Then we aim to determine the snapshot by seeing if we have any associated with the AMI-ID. If nothing shows up, we define it as pruned
f97df9d
to
64fd2f7
Compare
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.
LGTM
Added a check for when the
snapshotID
is None in theRemoveBySnapshotTag
function. IfsnapshotID
is None, the function now attempts to extract the snapshot ID from the associated AMI. If the AMI cannot be described, it is logged as pruned, and the function returns without error.