Update the FindTopLevelOwner function to extract only controllers.#95
Conversation
| LOG(DEBUG) << "FindTopLevelController: refs is " << *refs; | ||
| #endif | ||
| if (refs->size() > 1) { | ||
| std::vector<json::Object> controller_refs; |
There was a problem hiding this comment.
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.
| return object; | ||
| } | ||
| if (controller_refs.size() > 1) { | ||
| LOG(WARNING) << "Found multiple owner references for " << *obj |
There was a problem hiding this comment.
s/owner/controller/, right?
| 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.
Let's fit this in 80 characters again.
| 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.
Let's fit in 80 characters (line break before the <<).
| 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.
Let's fit this in 80 characters again.
| #endif | ||
| if (controller_refs.size() == 0) { | ||
| #ifdef VERBOSE | ||
| LOG(DEBUG) << "FindTopLevelController: no controller owner references in " << *metadata; |
There was a problem hiding this comment.
Let's fit in 80 characters (line break before the <<).
61aa635 to
fd148ce
Compare
fd148ce to
00bd7c2
Compare
| #endif | ||
| return object; | ||
| } | ||
| if (controller_refs.size() > 1) { |
There was a problem hiding this comment.
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.
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.
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.
| 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.
Just push_back should be sufficient now.
| << " 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.
The comment about not copying JSON values applies here too. Just pass the pointers around.
There was a problem hiding this comment.
this comment is no longer applicable.
| // 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.
How about:
json::value FindTopLevelController(const std::string& ns, json::value object)
const throw(QueryException, json::Exception);?
| #endif | ||
| return object; | ||
| } | ||
| if (controller_refs.size() > 1) { |
There was a problem hiding this comment.
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.
A couple of comments.
| return object; | ||
| } | ||
| const json::Array* refs = metadata->Get<json::Array>("ownerReferences"); | ||
|
|
There was a problem hiding this comment.
Let's move this blank line to after the #endif (so the log message is next to the declaration).
| 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.
This is no longer true.
There was a problem hiding this comment.
It implicitly is - but yes confusing. Removed the line.
No description provided.