Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.

Update the FindTopLevelOwner function to extract only controllers.#95

Merged
supriyagarg merged 4 commits intoStackdriver:masterfrom
supriyagarg:controller_fix
Mar 28, 2018
Merged

Update the FindTopLevelOwner function to extract only controllers.#95
supriyagarg merged 4 commits intoStackdriver:masterfrom
supriyagarg:controller_fix

Conversation

@supriyagarg
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread src/kubernetes.cc Outdated
LOG(DEBUG) << "FindTopLevelController: refs is " << *refs;
#endif
if (refs->size() > 1) {
std::vector<json::Object> controller_refs;
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/kubernetes.cc Outdated
return object;
}
if (controller_refs.size() > 1) {
LOG(WARNING) << "Found multiple owner references for " << *obj
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/owner/controller/, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. done.

Comment thread 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
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread 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;
Copy link
Copy Markdown
Contributor

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 <<).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread 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")) {
Copy link
Copy Markdown
Contributor

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.

Comment thread src/kubernetes.cc Outdated
#endif
if (controller_refs.size() == 0) {
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelController: no controller owner references in " << *metadata;
Copy link
Copy Markdown
Contributor

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 <<).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/kubernetes.cc Outdated
#endif
return object;
}
if (controller_refs.size() > 1) {
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread 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);
Copy link
Copy Markdown
Contributor

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.

Comment thread src/kubernetes.cc Outdated
<< " picking the first one arbitrarily.";
}
const json::value& ref = (*refs)[0];
const json::Object controller_ref = *controller_refs[0];
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

Comment thread 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
Copy link
Copy Markdown
Contributor

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);

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/kubernetes.cc Outdated
#endif
return object;
}
if (controller_refs.size() > 1) {
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a 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.

Comment thread src/kubernetes.cc Outdated
return object;
}
const json::Array* refs = metadata->Get<json::Array>("ownerReferences");

Copy link
Copy Markdown
Contributor

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread 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.
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

Copy link
Copy Markdown
Contributor

@bmoyles0117 bmoyles0117 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@supriyagarg supriyagarg merged commit 6a72500 into Stackdriver:master Mar 28, 2018
@supriyagarg supriyagarg deleted the controller_fix branch March 28, 2018 22:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants