Skip to content
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

feat(NODE-5968): Container and Kubernetes Awareness #4005

Merged
merged 16 commits into from Mar 6, 2024

Conversation

aditi-khare-mongoDB
Copy link
Contributor

@aditi-khare-mongoDB aditi-khare-mongoDB commented Feb 27, 2024

Description

Track user usage of containers.

What is changing?

When kubernetes is in process.env, we add a {orchestrator: 'kubernetes'} field to the client.env field of the handshake document (if this does not make the document exceed 512 bytes).

When '/.dockerenv' exists, we add a {runtime: 'docker'} field to the client.env field of the handshake document (if this does not make the document exceed 512 bytes).

Note: We do not unit test docker, but I tested our logic for detecting docker on an EVG Host, ran Docker and detection succeeds for both root and non-root user cases.

Is there new documentation needed for these changes?

No, non-user facing change.

What is the motivation for this change?

Product wants to track user usage of docker and kubernetes.

Release Highlight

Container and Kubernetes Awareness

The Node.js driver now keeps track of container metadata in the client.env.container field of the handshake document.

If space allows, the following metadata will be included in client.env.container:

env?: { 
  container?: {
    orchestrator?: 'kubernetes' // if process.env.KUBERNETES_SERVICE_HOST is set
    runtime?: 'docker' // if the '/.dockerenv' file exists
  } 
}

Note: If neither Kubernetes nor Docker is present, client.env will not have the container property.

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@aditi-khare-mongoDB aditi-khare-mongoDB changed the title EOD commit feat(NODE-5968): Container and Kubernetes Awareness Feb 27, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB marked this pull request as ready for review February 29, 2024 19:10
@nbbeeken nbbeeken self-assigned this Feb 29, 2024
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Feb 29, 2024
@nbbeeken nbbeeken self-requested a review February 29, 2024 19:11
lint fix 2
@aditi-khare-mongoDB aditi-khare-mongoDB force-pushed the NODE-5968/6.x-container-and-kubernetes-awareness branch from 6da9072 to 7140454 Compare February 29, 2024 19:42
added tests - no docker tests

removed extraneous export and newline

removed extraneous export and newline

removed unnecesary helper funcs

removed unnecesary let, changed to const

added more tests to comply w kickoff - no docker tests still

cleared up logic
@aditi-khare-mongoDB aditi-khare-mongoDB force-pushed the NODE-5968/6.x-container-and-kubernetes-awareness branch from 7140454 to 03e5e13 Compare February 29, 2024 19:44
src/cmap/handshake/client_metadata.ts Outdated Show resolved Hide resolved
src/cmap/handshake/client_metadata.ts Show resolved Hide resolved
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
test/unit/cmap/connect.test.ts Show resolved Hide resolved
src/cmap/handshake/client_metadata.ts Outdated Show resolved Hide resolved
src/cmap/handshake/client_metadata.ts Outdated Show resolved Hide resolved
src/cmap/handshake/client_metadata.ts Outdated Show resolved Hide resolved
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
@nbbeeken nbbeeken added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Mar 4, 2024
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
test/unit/cmap/connect.test.ts Outdated Show resolved Hide resolved
@aditi-khare-mongoDB aditi-khare-mongoDB force-pushed the NODE-5968/6.x-container-and-kubernetes-awareness branch from 6355e4e to 25d1b44 Compare March 4, 2024 19:48
nbbeeken
nbbeeken previously approved these changes Mar 4, 2024
src/cmap/connect.ts Show resolved Hide resolved
src/cmap/connect.ts Outdated Show resolved Hide resolved
src/cmap/handshake/client_metadata.ts Outdated Show resolved Hide resolved
nbbeeken
nbbeeken previously approved these changes Mar 5, 2024
Copy link
Contributor

@baileympearson baileympearson left a comment

Choose a reason for hiding this comment

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

two small comments, otherwise LGTM.

@@ -551,6 +551,7 @@ export function parseOptions(
);

mongoOptions.metadata = makeClientMetadata(mongoOptions);
mongoOptions.extendedMetadata = addContainerMetadata(mongoOptions.metadata);
Copy link
Contributor

Choose a reason for hiding this comment

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

we might want to add a catch block, like Anna did when parallelizing dns lookup and srv resolution, so that this doesn't result in an unhandled promise rejection on the off chance it rejects

Copy link
Contributor

@nbbeeken nbbeeken Mar 6, 2024

Choose a reason for hiding this comment

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

Good catch.

Why are we calling this up here again? seems like addContainerMetadata has what it needs down in prepareHandshake no?

Could we pass the LimitedSizeDocument down on options a different key?

Copy link
Contributor

Choose a reason for hiding this comment

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

We cache the promise during options parsing so that we only construct the extended metadata once. Then we just await it during every call to connect. Aditi filed https://jira.mongodb.org/browse/NODE-5994 to make metadata private, remove extendedMetadata and just store its result in metadata in driver v7

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok, thanks for the context sorry I missed that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in the catch block!

Comment on lines 164 to 168
if (isDocker == null) {
dockerPromise ??= fs.access('/.dockerenv').then(
() => true,
() => false
);
isDocker = await dockerPromise;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

(optional) the original suggestion was intended to make it unnecessary to cache two variables. but we can leave it as-is too.

Suggested change
if (isDocker == null) {
dockerPromise ??= fs.access('/.dockerenv').then(
() => true,
() => false
);
isDocker = await dockerPromise;
}
dockerPromise ??= fs.access('/.dockerenv').then(
() => true,
() => false
);
const isDocker = await dockerPromise;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, made the change.

@aditi-khare-mongoDB aditi-khare-mongoDB force-pushed the NODE-5968/6.x-container-and-kubernetes-awareness branch 3 times, most recently from 9a2a74f to 2ac1971 Compare March 6, 2024 18:50
@aditi-khare-mongoDB aditi-khare-mongoDB force-pushed the NODE-5968/6.x-container-and-kubernetes-awareness branch from 2ac1971 to e33b33d Compare March 6, 2024 18:55
@nbbeeken nbbeeken merged commit 28b7040 into main Mar 6, 2024
22 of 26 checks passed
@nbbeeken nbbeeken deleted the NODE-5968/6.x-container-and-kubernetes-awareness branch March 6, 2024 23:18
aditi-khare-mongoDB added a commit that referenced this pull request Mar 8, 2024
synced new test files

added support for error response

added api docs

made MongoServerError.errorResponse required + casted resulting type errors

test(NODE-5992): fix env var restoration in tests (#4017)

refactor(NODE-5903): add newline to stdio logging (#4018)

fix(NODE-5985): throw Nodejs' certificate expired error when TLS fails to connect instead of `CERT_HAS_EXPIRED` (#4014)

test(NODE-5962): gossip cluster time in utr (#4019)

chore(NODE-5997): update saslprep to ^1.1.5 (#4023)

feat(NODE-5968): container and Kubernetes awareness in client metadata (#4005)

fix(NODE-5993): memory leak in the `Connection` class (#4022)

added TODO(NODE-XXXX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
4 participants