diff --git a/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness.go b/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness.go index fd5a3ab9d1a..8c6d2d12c6d 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness.go +++ b/staging/src/k8s.io/apiserver/pkg/server/filters/priority-and-fairness.go @@ -186,7 +186,7 @@ func WithPriorityAndFairness( served = true setResponseHeaders(classification, w) - forgetWatch = fcIfc.RegisterWatch(requestInfo) + forgetWatch = fcIfc.RegisterWatch(r) // Notify the main thread that we're ready to start the watch. close(shouldStartWatchCh) diff --git a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker.go b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker.go index 4dc7c131eb0..18f89950676 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker.go +++ b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker.go @@ -17,6 +17,7 @@ limitations under the License. package flowcontrol import ( + "net/http" "sync" "k8s.io/apimachinery/pkg/util/sets" @@ -51,10 +52,10 @@ type ForgetWatchFunc func() // of watches in the system for the purpose of estimating the // cost of incoming mutating requests. type WatchTracker interface { - // RegisterWatch reqisters a watch with the provided requestInfo + // RegisterWatch reqisters a watch based on the provided http.Request // in the tracker. It returns the function that should be called // to forget the watcher once it is finished. - RegisterWatch(requestInfo *request.RequestInfo) ForgetWatchFunc + RegisterWatch(r *http.Request) ForgetWatchFunc // GetInterestedWatchCount returns the number of watches that are // potentially interested in a request with a given RequestInfo @@ -77,8 +78,9 @@ func NewWatchTracker() WatchTracker { } // RegisterWatch implements WatchTracker interface. -func (w *watchTracker) RegisterWatch(requestInfo *request.RequestInfo) ForgetWatchFunc { - if requestInfo == nil || requestInfo.Verb != "watch" { +func (w *watchTracker) RegisterWatch(r *http.Request) ForgetWatchFunc { + requestInfo, ok := request.RequestInfoFrom(r.Context()) + if !ok || requestInfo == nil || requestInfo.Verb != "watch" { return nil } diff --git a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker_test.go b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker_test.go index fafccbe4255..3448b65b0f2 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker_test.go +++ b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/watch_tracker_test.go @@ -17,6 +17,7 @@ limitations under the License. package flowcontrol import ( + "context" "net/http" "net/url" "testing" @@ -107,8 +108,10 @@ func TestRegisterWatch(t *testing.T) { if err != nil { t.Fatalf("unexpected error from requestInfo creation: %#v", err) } + ctx := request.WithRequestInfo(context.Background(), requestInfo) + r := testCase.request.WithContext(ctx) - forget := watchTracker.RegisterWatch(requestInfo) + forget := watchTracker.RegisterWatch(r) if testCase.expected == nil { if forget != nil { t.Errorf("unexpected watch registered: %#v", watchTracker.watchCount) @@ -151,7 +154,8 @@ func TestGetInterestedWatchCount(t *testing.T) { if err != nil { t.Fatalf("unexpected error from requestInfo creation: %#v", err) } - if forget := watchTracker.RegisterWatch(requestInfo); forget == nil { + r := req.WithContext(request.WithRequestInfo(context.Background(), requestInfo)) + if forget := watchTracker.RegisterWatch(r); forget == nil { t.Errorf("watch wasn't registered: %#v", requestInfo) } }