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

Conversation

@supriyagarg
Copy link
Contributor

No description provided.

LOG(DEBUG) << "FindTopLevelController: refs is " << *refs;
#endif
if (refs->size() > 1) {
std::vector<json::Object> controller_refs;
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

done

return object;
}
if (controller_refs.size() > 1) {
LOG(WARNING) << "Found multiple owner references for " << *obj
Copy link
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
Contributor Author

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
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

done

if (!metadata->Has("ownerReferences")) {
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelOwner: no owner references in " << *metadata;
LOG(DEBUG) << "FindTopLevelController: no owner references in " << *metadata;
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

done

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

#endif
if (controller_refs.size() == 0) {
#ifdef VERBOSE
LOG(DEBUG) << "FindTopLevelController: no controller owner references in " << *metadata;
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

done

#endif
return object;
}
if (controller_refs.size() > 1) {
Copy link
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

Done

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

<< " picking the first one arbitrarily.";
}
const json::value& ref = (*refs)[0];
const json::Object controller_ref = *controller_refs[0];
Copy link
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
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.

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
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
Contributor Author

Choose a reason for hiding this comment

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

Done

#endif
return object;
}
if (controller_refs.size() > 1) {
Copy link
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
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.

return object;
}
const json::Array* refs = metadata->Get<json::Array>("ownerReferences");

Copy link
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
Contributor Author

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.
Copy link
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
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
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
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