-
Notifications
You must be signed in to change notification settings - Fork 11
Update the FindTopLevelOwner function to extract only controllers. #95
Conversation
src/kubernetes.cc
Outdated
| LOG(DEBUG) << "FindTopLevelController: refs is " << *refs; | ||
| #endif | ||
| if (refs->size() > 1) { | ||
| std::vector<json::Object> controller_refs; |
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.
Let's not copy JSON values. Given that the refs array will live to the end of this function, I would suggest just storing const json::Object pointers in the array.
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.
done
src/kubernetes.cc
Outdated
| return object; | ||
| } | ||
| if (controller_refs.size() > 1) { | ||
| LOG(WARNING) << "Found multiple owner references for " << *obj |
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.
s/owner/controller/, 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.
correct. done.
src/kubernetes.h
Outdated
| json::value FindTopLevelOwner(const std::string& ns, json::value object) const | ||
| // For a given object, returns the top-level controller object. | ||
| // When there are multiple controller references, follows the first one. | ||
| json::value FindTopLevelController(const std::string& ns, json::value object) const |
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.
Let's fit this in 80 characters again.
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.
done
src/kubernetes.cc
Outdated
| if (!metadata->Has("ownerReferences")) { | ||
| #ifdef VERBOSE | ||
| LOG(DEBUG) << "FindTopLevelOwner: no owner references in " << *metadata; | ||
| LOG(DEBUG) << "FindTopLevelController: no owner references in " << *metadata; |
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.
Let's fit in 80 characters (line break before the <<).
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.
done
src/kubernetes.cc
Outdated
| std::vector<json::Object> controller_refs; | ||
| for (const json::value& ref : *refs) { | ||
| const json::Object* ref_obj = ref->As<json::Object>(); | ||
| if (ref_obj->Has("controller") && ref_obj->Get<json::Boolean>("controller")) { |
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.
Let's fit this in 80 characters again.
src/kubernetes.cc
Outdated
| #endif | ||
| if (controller_refs.size() == 0) { | ||
| #ifdef VERBOSE | ||
| LOG(DEBUG) << "FindTopLevelController: no controller owner references in " << *metadata; |
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.
Let's fit in 80 characters (line break before the <<).
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.
done
61aa635 to
fd148ce
Compare
fd148ce to
00bd7c2
Compare
src/kubernetes.cc
Outdated
| #endif | ||
| return object; | ||
| } | ||
| if (controller_refs.size() > 1) { |
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.
Being that we're specifically looking for controllers now, this is from the Kubernetes documentation https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#objectmeta-v1-meta.
"There cannot be more than one managing controller."
How do you feel about removing this check?
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.
Correct - this was more for defensive programming, just in case.
If we want to ignore any controller after the first one, I can simplify the for loop above to store just the first controller, and break after it finds one. WDYT?
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.
Yes, let's just declare a single const json::Object* controller_ref = nullptr with a comment that Kubernetes objects are supposed to have at most one and a link to the doc that Bryan referenced. Then break out of the loop when you find one.
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.
Done
src/kubernetes.cc
Outdated
| const json::Object* ref_obj = ref->As<json::Object>(); | ||
| if (ref_obj->Has("controller") && | ||
| ref_obj->Get<json::Boolean>("controller")) { | ||
| controller_refs.emplace_back(ref_obj); |
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 push_back should be sufficient now.
src/kubernetes.cc
Outdated
| << " picking the first one arbitrarily."; | ||
| } | ||
| const json::value& ref = (*refs)[0]; | ||
| const json::Object controller_ref = *controller_refs[0]; |
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 comment about not copying JSON values applies here too. Just pass the pointers around.
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.
this comment is no longer applicable.
src/kubernetes.h
Outdated
| // For a given object, returns the top-level controller object. | ||
| // When there are multiple controller references, follows the first one. | ||
| json::value FindTopLevelController( | ||
| const std::string& ns, json::value object) const |
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.
How about:
json::value FindTopLevelController(const std::string& ns, json::value object)
const throw(QueryException, json::Exception);?
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.
Done
src/kubernetes.cc
Outdated
| #endif | ||
| return object; | ||
| } | ||
| if (controller_refs.size() > 1) { |
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.
Yes, let's just declare a single const json::Object* controller_ref = nullptr with a comment that Kubernetes objects are supposed to have at most one and a link to the doc that Bryan referenced. Then break out of the loop when you find one.
igorpeshansky
left a comment
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.
A couple of comments.
src/kubernetes.cc
Outdated
| return object; | ||
| } | ||
| const json::Array* refs = metadata->Get<json::Array>("ownerReferences"); | ||
|
|
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.
Let's move this blank line to after the #endif (so the log message is next to the declaration).
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.
done
src/kubernetes.h
Outdated
| json::value FindTopLevelOwner(const std::string& ns, json::value object) const | ||
| throw(QueryException, json::Exception); | ||
| // For a given object, returns the top-level controller object. | ||
| // When there are multiple controller references, follows the first one. |
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.
This is no longer true.
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 implicitly is - but yes confusing. Removed the line.
igorpeshansky
left a comment
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 ![]()
bmoyles0117
left a comment
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
No description provided.