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

Fix #332: Use lru cache to expire entrypoint items #350

Merged
merged 1 commit into from
Jan 15, 2019
Merged

Fix #332: Use lru cache to expire entrypoint items #350

merged 1 commit into from
Jan 15, 2019

Conversation

mathspanda
Copy link
Contributor

@mathspanda mathspanda commented Dec 26, 2018

Fix issue #332

Use a lru cache that will only keep the most recently-used entrypoints cached and drop entrypoints haven't been used recently, in order to present a risk of DoS.

  1. replace internal map in original Cache with lru.Cache
  2. fix incorrect entrypoint_test package name

Signed-off-by: mathspanda [email protected]

@knative-prow-robot knative-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 26, 2018
Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

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

Thanks for this change! It's nice that it's so easy to use, and should help us avoid a DoS avenue.

Can you add some tests for this new behavior in this change? It should be fairly easy to simulate the calls to have something expire from the cache, particularly if you make the size a package level var and set it to something low in your test.

cache: make(map[string][]string),
lru, err := lru.New(defaultEntryPointCacheSize)
if err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't panic here, we should return the error from NewCache and update callers to handle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

THX for review. Has fixed code as you requested. @imjasonh

Copy link
Collaborator

@bobcatfish bobcatfish left a comment

Choose a reason for hiding this comment

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

Thanks for implementing this @mathspanda ! 🎉

I have a bit of feedback, mostly about mutating the value of entrypointCacheSize

Also before you merge this in, could you amend the commit and explain a bit more about why this change is being made in the commit message (you can probably reuse a lot of what @imjasonh wrote in #332 if you want), and add the issue number to the commit itself? See our commit message guidelines

Thanks!!!

// due to the possibility of a panic if two workers were to attempt to read and
// write to the internal map at the same time.
// getting the Entrypoint of a container image from a remote registry. The
// internal lru cache is thread-safe.
Copy link
Collaborator

Choose a reason for hiding this comment

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

cool!

pkg/reconciler/v1alpha1/taskrun/taskrun.go Outdated Show resolved Hide resolved
pkg/reconciler/v1alpha1/taskrun/entrypoint/entrypoint.go Outdated Show resolved Hide resolved
@mathspanda mathspanda changed the title Use lru cache to expire entrypoint items Fix #332: Use lru cache to expire entrypoint items Jan 8, 2019
Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

/lgtm

@@ -1,4 +1,4 @@
package entrypoint_test
package entrypoint
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to use entrypoint here (instead of entrypoint_test) ? I'm guessing for cacheSize 🙃
(I tend to really like when the test package is different from the current package as it forces to write test that tests the behavior more than the implementation)

Copy link
Member

Choose a reason for hiding this comment

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

I actually usually prefer to have tests in the same package, so that package-level vars/consts/funcs don't get exported just so they can be called from tests.

In some cases it's possible to only call funcs that are exported anyway, but sometimes those funcs get long and need to be split up into smaller focused funcs, and it's usually a good idea to avoid exporting those.

Copy link
Member

Choose a reason for hiding this comment

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

I actually usually prefer to have tests in the same package, so that package-level vars/consts/funcs don't get exported just so they can be called from tests.

Yeah, we should definitely not export var/const/func just for the sake of tests 😉

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jan 8, 2019
Copy link
Contributor

@shashwathi shashwathi left a comment

Choose a reason for hiding this comment

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

PR looks great. I have couple of small nits about test error msgs.

@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label Jan 8, 2019
@shashwathi
Copy link
Contributor

/ok-to-test

@knative-prow-robot knative-prow-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jan 9, 2019
Use a lru cache that will only keep the most recently-used entrypoints
cached and drop entrypoints haven't been used recently, in order to
present a risk of DoS.

1. replace internal map in original Cache with lru.Cache
2. fix incorrect entrypoint_test package name

Signed-off-by: mathspanda <[email protected]>
@knative-metrics-robot
Copy link

The following is the coverage report on pkg/.
Say /test pull-knative-build-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/reconciler/v1alpha1/taskrun/entrypoint/entrypoint.go 84.6% 83.8% -0.8
pkg/reconciler/v1alpha1/taskrun/taskrun.go 72.9% 72.7% -0.3

Copy link
Collaborator

@bobcatfish bobcatfish left a comment

Choose a reason for hiding this comment

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

Looking great @mathspanda ! I think we just need one more review from @imjasonh at this point

Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jan 15, 2019
@knative-prow-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ImJasonH, mathspanda

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 15, 2019
@knative-prow-robot knative-prow-robot merged commit 8e06fa0 into tektoncd:master Jan 15, 2019
afrittoli added a commit to afrittoli/pipeline that referenced this pull request Jan 22, 2019
Missing dep, probably after lru cache was implemented in
tektoncd#350
knative-prow-robot pushed a commit that referenced this pull request Jan 22, 2019
Missing dep, probably after lru cache was implemented in
#350
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants