mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Merge pull request #105026 from wojtek-t/migrate_clock_2
Unify towards k8s.io/utils/clock - part 2
This commit is contained in:
commit
9918aa1e03
@ -35,6 +35,7 @@
|
|||||||
allowedImports:
|
allowedImports:
|
||||||
- k8s.io/apimachinery
|
- k8s.io/apimachinery
|
||||||
- k8s.io/kube-openapi
|
- k8s.io/kube-openapi
|
||||||
|
- k8s.io/utils/clock
|
||||||
- k8s.io/utils/net
|
- k8s.io/utils/net
|
||||||
- k8s.io/klog
|
- k8s.io/klog
|
||||||
|
|
||||||
@ -79,6 +80,7 @@
|
|||||||
- k8s.io/apimachinery
|
- k8s.io/apimachinery
|
||||||
- k8s.io/client-go
|
- k8s.io/client-go
|
||||||
- k8s.io/klog
|
- k8s.io/klog
|
||||||
|
- k8s.io/utils
|
||||||
- baseImportPath: "./vendor/k8s.io/client-go/tools/"
|
- baseImportPath: "./vendor/k8s.io/client-go/tools/"
|
||||||
excludeTests: true
|
excludeTests: true
|
||||||
ignoredSubTrees:
|
ignoredSubTrees:
|
||||||
|
@ -21,17 +21,17 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewExpiring returns an initialized expiring cache.
|
// NewExpiring returns an initialized expiring cache.
|
||||||
func NewExpiring() *Expiring {
|
func NewExpiring() *Expiring {
|
||||||
return NewExpiringWithClock(utilclock.RealClock{})
|
return NewExpiringWithClock(clock.RealClock{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExpiringWithClock is like NewExpiring but allows passing in a custom
|
// NewExpiringWithClock is like NewExpiring but allows passing in a custom
|
||||||
// clock for testing.
|
// clock for testing.
|
||||||
func NewExpiringWithClock(clock utilclock.Clock) *Expiring {
|
func NewExpiringWithClock(clock clock.Clock) *Expiring {
|
||||||
return &Expiring{
|
return &Expiring{
|
||||||
clock: clock,
|
clock: clock,
|
||||||
cache: make(map[interface{}]entry),
|
cache: make(map[interface{}]entry),
|
||||||
@ -40,7 +40,7 @@ func NewExpiringWithClock(clock utilclock.Clock) *Expiring {
|
|||||||
|
|
||||||
// Expiring is a map whose entries expire after a per-entry timeout.
|
// Expiring is a map whose entries expire after a per-entry timeout.
|
||||||
type Expiring struct {
|
type Expiring struct {
|
||||||
clock utilclock.Clock
|
clock clock.Clock
|
||||||
|
|
||||||
// mu protects the below fields
|
// mu protects the below fields
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExpiringCache(t *testing.T) {
|
func TestExpiringCache(t *testing.T) {
|
||||||
@ -58,7 +58,7 @@ func TestExpiringCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExpiration(t *testing.T) {
|
func TestExpiration(t *testing.T) {
|
||||||
fc := &utilclock.FakeClock{}
|
fc := &testingclock.FakeClock{}
|
||||||
c := NewExpiringWithClock(fc)
|
c := NewExpiringWithClock(fc)
|
||||||
|
|
||||||
c.Set("a", "a", time.Second)
|
c.Set("a", "a", time.Second)
|
||||||
@ -104,7 +104,7 @@ func TestExpiration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGarbageCollection(t *testing.T) {
|
func TestGarbageCollection(t *testing.T) {
|
||||||
fc := &utilclock.FakeClock{}
|
fc := &testingclock.FakeClock{}
|
||||||
|
|
||||||
type entry struct {
|
type entry struct {
|
||||||
key, val string
|
key, val string
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func expectEntry(t *testing.T, c *LRUExpireCache, key interface{}, value interface{}) {
|
func expectEntry(t *testing.T, c *LRUExpireCache, key interface{}, value interface{}) {
|
||||||
@ -68,7 +68,7 @@ func TestSimpleRemove(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExpiredGet(t *testing.T) {
|
func TestExpiredGet(t *testing.T) {
|
||||||
fakeClock := clock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
c := NewLRUExpireCacheWithClock(10, fakeClock)
|
c := NewLRUExpireCacheWithClock(10, fakeClock)
|
||||||
c.Add("short-lived", "12345", 1*time.Millisecond)
|
c.Add("short-lived", "12345", 1*time.Millisecond)
|
||||||
// ensure the entry expired
|
// ensure the entry expired
|
||||||
|
@ -29,13 +29,13 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
utilcache "k8s.io/apimachinery/pkg/util/cache"
|
utilcache "k8s.io/apimachinery/pkg/util/cache"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apiserver/pkg/admission"
|
"k8s.io/apiserver/pkg/admission"
|
||||||
"k8s.io/apiserver/pkg/admission/initializer"
|
"k8s.io/apiserver/pkg/admission/initializer"
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
corelisters "k8s.io/client-go/listers/core/v1"
|
corelisters "k8s.io/client-go/listers/core/v1"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -28,7 +28,6 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
@ -38,6 +37,8 @@ import (
|
|||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/kubernetes/fake"
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
core "k8s.io/client-go/testing"
|
core "k8s.io/client-go/testing"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// newHandlerForTest returns a configured handler for testing.
|
// newHandlerForTest returns a configured handler for testing.
|
||||||
@ -240,7 +241,7 @@ func TestAdmissionNamespaceForceLiveLookup(t *testing.T) {
|
|||||||
return true, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}, Status: v1.NamespaceStatus{Phase: phases[namespace]}}, nil
|
return true, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}, Status: v1.NamespaceStatus{Phase: phases[namespace]}}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
fakeClock := clock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
|
|
||||||
handler, informerFactory, err := newHandlerForTestWithClock(mockClient, fakeClock)
|
handler, informerFactory, err := newHandlerForTestWithClock(mockClient, fakeClock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
utilcache "k8s.io/apimachinery/pkg/util/cache"
|
utilcache "k8s.io/apimachinery/pkg/util/cache"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
type simpleCache struct {
|
type simpleCache struct {
|
||||||
|
@ -24,9 +24,9 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apiserver/pkg/authentication/authenticator"
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
"k8s.io/apiserver/pkg/authentication/user"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSimpleCache(t *testing.T) {
|
func TestSimpleCache(t *testing.T) {
|
||||||
|
@ -33,12 +33,12 @@ import (
|
|||||||
"golang.org/x/sync/singleflight"
|
"golang.org/x/sync/singleflight"
|
||||||
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
||||||
"k8s.io/apiserver/pkg/audit"
|
"k8s.io/apiserver/pkg/audit"
|
||||||
"k8s.io/apiserver/pkg/authentication/authenticator"
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errAuthnCrash = apierrors.NewInternalError(errors.New("authentication failed unexpectedly"))
|
var errAuthnCrash = apierrors.NewInternalError(errors.New("authentication failed unexpectedly"))
|
||||||
@ -89,10 +89,10 @@ type cache interface {
|
|||||||
|
|
||||||
// New returns a token authenticator that caches the results of the specified authenticator. A ttl of 0 bypasses the cache.
|
// New returns a token authenticator that caches the results of the specified authenticator. A ttl of 0 bypasses the cache.
|
||||||
func New(authenticator authenticator.Token, cacheErrs bool, successTTL, failureTTL time.Duration) authenticator.Token {
|
func New(authenticator authenticator.Token, cacheErrs bool, successTTL, failureTTL time.Duration) authenticator.Token {
|
||||||
return newWithClock(authenticator, cacheErrs, successTTL, failureTTL, utilclock.RealClock{})
|
return newWithClock(authenticator, cacheErrs, successTTL, failureTTL, clock.RealClock{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWithClock(authenticator authenticator.Token, cacheErrs bool, successTTL, failureTTL time.Duration, clock utilclock.Clock) authenticator.Token {
|
func newWithClock(authenticator authenticator.Token, cacheErrs bool, successTTL, failureTTL time.Duration, clock clock.Clock) authenticator.Token {
|
||||||
randomCacheKey := make([]byte, 32)
|
randomCacheKey := make([]byte, 32)
|
||||||
if _, err := rand.Read(randomCacheKey); err != nil {
|
if _, err := rand.Read(randomCacheKey); err != nil {
|
||||||
panic(err) // rand should never fail
|
panic(err) // rand should never fail
|
||||||
|
@ -31,13 +31,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/uuid"
|
"k8s.io/apimachinery/pkg/util/uuid"
|
||||||
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
||||||
"k8s.io/apiserver/pkg/audit"
|
"k8s.io/apiserver/pkg/audit"
|
||||||
"k8s.io/apiserver/pkg/authentication/authenticator"
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
"k8s.io/apiserver/pkg/authentication/user"
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCachedTokenAuthenticator(t *testing.T) {
|
func TestCachedTokenAuthenticator(t *testing.T) {
|
||||||
@ -52,7 +53,7 @@ func TestCachedTokenAuthenticator(t *testing.T) {
|
|||||||
calledWithToken = append(calledWithToken, token)
|
calledWithToken = append(calledWithToken, token)
|
||||||
return &authenticator.Response{User: resultUsers[token]}, resultOk, resultErr
|
return &authenticator.Response{User: resultUsers[token]}, resultOk, resultErr
|
||||||
})
|
})
|
||||||
fakeClock := utilclock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
|
|
||||||
a := newWithClock(fakeAuth, true, time.Minute, 0, fakeClock)
|
a := newWithClock(fakeAuth, true, time.Minute, 0, fakeClock)
|
||||||
|
|
||||||
@ -126,7 +127,7 @@ func TestCachedTokenAuthenticatorWithAudiences(t *testing.T) {
|
|||||||
auds, _ := authenticator.AudiencesFrom(ctx)
|
auds, _ := authenticator.AudiencesFrom(ctx)
|
||||||
return &authenticator.Response{User: resultUsers[auds[0]+token]}, true, nil
|
return &authenticator.Response{User: resultUsers[auds[0]+token]}, true, nil
|
||||||
})
|
})
|
||||||
fakeClock := utilclock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
|
|
||||||
a := newWithClock(fakeAuth, true, time.Minute, 0, fakeClock)
|
a := newWithClock(fakeAuth, true, time.Minute, 0, fakeClock)
|
||||||
|
|
||||||
@ -546,7 +547,7 @@ func (s *singleBenchmark) bench(b *testing.B) {
|
|||||||
true,
|
true,
|
||||||
4*time.Second,
|
4*time.Second,
|
||||||
500*time.Millisecond,
|
500*time.Millisecond,
|
||||||
utilclock.RealClock{},
|
clock.RealClock{},
|
||||||
)
|
)
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
@ -22,11 +22,11 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apiserver/pkg/endpoints/metrics"
|
"k8s.io/apiserver/pkg/endpoints/metrics"
|
||||||
apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||||
"k8s.io/apiserver/pkg/server/httplog"
|
"k8s.io/apiserver/pkg/server/httplog"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
type requestFilterRecordKeyType int
|
type requestFilterRecordKeyType int
|
||||||
@ -55,13 +55,13 @@ func requestFilterRecordFrom(ctx context.Context) *requestFilterRecord {
|
|||||||
// TrackStarted measures the timestamp the given handler has started execution
|
// TrackStarted measures the timestamp the given handler has started execution
|
||||||
// by attaching a handler to the chain.
|
// by attaching a handler to the chain.
|
||||||
func TrackStarted(handler http.Handler, name string) http.Handler {
|
func TrackStarted(handler http.Handler, name string) http.Handler {
|
||||||
return trackStarted(handler, name, utilclock.RealClock{})
|
return trackStarted(handler, name, clock.RealClock{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrackCompleted measures the timestamp the given handler has completed execution and then
|
// TrackCompleted measures the timestamp the given handler has completed execution and then
|
||||||
// it updates the corresponding metric with the filter latency duration.
|
// it updates the corresponding metric with the filter latency duration.
|
||||||
func TrackCompleted(handler http.Handler) http.Handler {
|
func TrackCompleted(handler http.Handler) http.Handler {
|
||||||
return trackCompleted(handler, utilclock.RealClock{}, func(ctx context.Context, fr *requestFilterRecord, completedAt time.Time) {
|
return trackCompleted(handler, clock.RealClock{}, func(ctx context.Context, fr *requestFilterRecord, completedAt time.Time) {
|
||||||
latency := completedAt.Sub(fr.startedTimestamp)
|
latency := completedAt.Sub(fr.startedTimestamp)
|
||||||
metrics.RecordFilterLatency(ctx, fr.name, latency)
|
metrics.RecordFilterLatency(ctx, fr.name, latency)
|
||||||
if klog.V(3).Enabled() && latency > minFilterLatencyToLog {
|
if klog.V(3).Enabled() && latency > minFilterLatencyToLog {
|
||||||
@ -70,7 +70,7 @@ func TrackCompleted(handler http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func trackStarted(handler http.Handler, name string, clock utilclock.PassiveClock) http.Handler {
|
func trackStarted(handler http.Handler, name string, clock clock.PassiveClock) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
if fr := requestFilterRecordFrom(ctx); fr != nil {
|
if fr := requestFilterRecordFrom(ctx); fr != nil {
|
||||||
@ -90,7 +90,7 @@ func trackStarted(handler http.Handler, name string, clock utilclock.PassiveCloc
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func trackCompleted(handler http.Handler, clock utilclock.PassiveClock, action func(context.Context, *requestFilterRecord, time.Time)) http.Handler {
|
func trackCompleted(handler http.Handler, clock clock.PassiveClock, action func(context.Context, *requestFilterRecord, time.Time)) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// The previous filter has just completed.
|
// The previous filter has just completed.
|
||||||
completedAt := clock.Now()
|
completedAt := clock.Now()
|
||||||
|
@ -23,7 +23,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTrackStartedWithContextAlreadyHasFilterRecord(t *testing.T) {
|
func TestTrackStartedWithContextAlreadyHasFilterRecord(t *testing.T) {
|
||||||
@ -41,7 +41,7 @@ func TestTrackStartedWithContextAlreadyHasFilterRecord(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
requestFilterStarted := time.Now()
|
requestFilterStarted := time.Now()
|
||||||
wrapped := trackStarted(handler, filterName, utilclock.NewFakeClock(requestFilterStarted))
|
wrapped := trackStarted(handler, filterName, testingclock.NewFakeClock(requestFilterStarted))
|
||||||
|
|
||||||
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil)
|
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -84,7 +84,7 @@ func TestTrackStartedWithContextDoesNotHaveFilterRecord(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
requestFilterStarted := time.Now()
|
requestFilterStarted := time.Now()
|
||||||
wrapped := trackStarted(handler, filterName, utilclock.NewFakeClock(requestFilterStarted))
|
wrapped := trackStarted(handler, filterName, testingclock.NewFakeClock(requestFilterStarted))
|
||||||
|
|
||||||
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil)
|
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -121,7 +121,7 @@ func TestTrackCompletedContextHasFilterRecord(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
requestFilterEndedAt := time.Now()
|
requestFilterEndedAt := time.Now()
|
||||||
wrapped := trackCompleted(handler, utilclock.NewFakeClock(requestFilterEndedAt), func(_ context.Context, fr *requestFilterRecord, completedAt time.Time) {
|
wrapped := trackCompleted(handler, testingclock.NewFakeClock(requestFilterEndedAt), func(_ context.Context, fr *requestFilterRecord, completedAt time.Time) {
|
||||||
actionCallCount++
|
actionCallCount++
|
||||||
filterRecordGot = fr
|
filterRecordGot = fr
|
||||||
filterCompletedAtGot = completedAt
|
filterCompletedAtGot = completedAt
|
||||||
@ -157,7 +157,7 @@ func TestTrackCompletedContextDoesNotHaveFilterRecord(t *testing.T) {
|
|||||||
handlerCallCount++
|
handlerCallCount++
|
||||||
})
|
})
|
||||||
|
|
||||||
wrapped := trackCompleted(handler, utilclock.NewFakeClock(time.Now()), func(_ context.Context, _ *requestFilterRecord, _ time.Time) {
|
wrapped := trackCompleted(handler, testingclock.NewFakeClock(time.Now()), func(_ context.Context, _ *requestFilterRecord, _ time.Time) {
|
||||||
actionCallCount++
|
actionCallCount++
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -27,13 +27,13 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
||||||
"k8s.io/apiserver/pkg/audit"
|
"k8s.io/apiserver/pkg/audit"
|
||||||
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
|
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -48,11 +48,11 @@ const (
|
|||||||
// requestTimeoutMaximum specifies the default request timeout value.
|
// requestTimeoutMaximum specifies the default request timeout value.
|
||||||
func WithRequestDeadline(handler http.Handler, sink audit.Sink, policy audit.PolicyRuleEvaluator, longRunning request.LongRunningRequestCheck,
|
func WithRequestDeadline(handler http.Handler, sink audit.Sink, policy audit.PolicyRuleEvaluator, longRunning request.LongRunningRequestCheck,
|
||||||
negotiatedSerializer runtime.NegotiatedSerializer, requestTimeoutMaximum time.Duration) http.Handler {
|
negotiatedSerializer runtime.NegotiatedSerializer, requestTimeoutMaximum time.Duration) http.Handler {
|
||||||
return withRequestDeadline(handler, sink, policy, longRunning, negotiatedSerializer, requestTimeoutMaximum, utilclock.RealClock{})
|
return withRequestDeadline(handler, sink, policy, longRunning, negotiatedSerializer, requestTimeoutMaximum, clock.RealClock{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func withRequestDeadline(handler http.Handler, sink audit.Sink, policy audit.PolicyRuleEvaluator, longRunning request.LongRunningRequestCheck,
|
func withRequestDeadline(handler http.Handler, sink audit.Sink, policy audit.PolicyRuleEvaluator, longRunning request.LongRunningRequestCheck,
|
||||||
negotiatedSerializer runtime.NegotiatedSerializer, requestTimeoutMaximum time.Duration, clock utilclock.PassiveClock) http.Handler {
|
negotiatedSerializer runtime.NegotiatedSerializer, requestTimeoutMaximum time.Duration, clock clock.PassiveClock) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
ctx := req.Context()
|
ctx := req.Context()
|
||||||
|
|
||||||
|
@ -32,10 +32,10 @@ import (
|
|||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
auditinternal "k8s.io/apiserver/pkg/apis/audit"
|
||||||
"k8s.io/apiserver/pkg/audit/policy"
|
"k8s.io/apiserver/pkg/audit/policy"
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseTimeout(t *testing.T) {
|
func TestParseTimeout(t *testing.T) {
|
||||||
@ -227,7 +227,7 @@ func TestWithRequestDeadlineWithClock(t *testing.T) {
|
|||||||
// if the deadline filter uses the clock instead of using the request started timestamp from the context
|
// if the deadline filter uses the clock instead of using the request started timestamp from the context
|
||||||
// then we will see a request deadline of about a minute.
|
// then we will see a request deadline of about a minute.
|
||||||
receivedTimestampExpected := time.Now().Add(time.Minute)
|
receivedTimestampExpected := time.Now().Add(time.Minute)
|
||||||
fakeClock := utilclock.NewFakeClock(receivedTimestampExpected)
|
fakeClock := testingclock.NewFakeClock(receivedTimestampExpected)
|
||||||
|
|
||||||
fakeSink := &fakeAuditSink{}
|
fakeSink := &fakeAuditSink{}
|
||||||
fakeRuleEvaluator := policy.NewFakePolicyRuleEvaluator(auditinternal.LevelRequestResponse, nil)
|
fakeRuleEvaluator := policy.NewFakePolicyRuleEvaluator(auditinternal.LevelRequestResponse, nil)
|
||||||
|
@ -19,18 +19,18 @@ package filters
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WithRequestReceivedTimestamp attaches the ReceivedTimestamp (the time the request reached
|
// WithRequestReceivedTimestamp attaches the ReceivedTimestamp (the time the request reached
|
||||||
// the apiserver) to the context.
|
// the apiserver) to the context.
|
||||||
func WithRequestReceivedTimestamp(handler http.Handler) http.Handler {
|
func WithRequestReceivedTimestamp(handler http.Handler) http.Handler {
|
||||||
return withRequestReceivedTimestampWithClock(handler, utilclock.RealClock{})
|
return withRequestReceivedTimestampWithClock(handler, clock.RealClock{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// The clock is passed as a parameter, handy for unit testing.
|
// The clock is passed as a parameter, handy for unit testing.
|
||||||
func withRequestReceivedTimestampWithClock(handler http.Handler, clock utilclock.PassiveClock) http.Handler {
|
func withRequestReceivedTimestampWithClock(handler http.Handler, clock clock.PassiveClock) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
ctx := req.Context()
|
ctx := req.Context()
|
||||||
req = req.WithContext(request.WithReceivedTimestamp(ctx, clock.Now()))
|
req = req.WithContext(request.WithReceivedTimestamp(ctx, clock.Now()))
|
||||||
|
@ -22,8 +22,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
"k8s.io/apiserver/pkg/endpoints/request"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWithRequestReceivedTimestamp(t *testing.T) {
|
func TestWithRequestReceivedTimestamp(t *testing.T) {
|
||||||
@ -41,7 +41,7 @@ func TestWithRequestReceivedTimestamp(t *testing.T) {
|
|||||||
receivedTimestampGot, ok = request.ReceivedTimestampFrom(req.Context())
|
receivedTimestampGot, ok = request.ReceivedTimestampFrom(req.Context())
|
||||||
})
|
})
|
||||||
|
|
||||||
wrapped := withRequestReceivedTimestampWithClock(handler, utilclock.NewFakeClock(receivedTimestampExpected))
|
wrapped := withRequestReceivedTimestampWithClock(handler, testingclock.NewFakeClock(receivedTimestampExpected))
|
||||||
|
|
||||||
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil)
|
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -35,7 +35,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
|
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
|
||||||
"k8s.io/apimachinery/pkg/version"
|
"k8s.io/apimachinery/pkg/version"
|
||||||
@ -73,6 +72,7 @@ import (
|
|||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
openapicommon "k8s.io/kube-openapi/pkg/common"
|
openapicommon "k8s.io/kube-openapi/pkg/common"
|
||||||
"k8s.io/kube-openapi/pkg/validation/spec"
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
utilsnet "k8s.io/utils/net"
|
utilsnet "k8s.io/utils/net"
|
||||||
|
|
||||||
// install apis
|
// install apis
|
||||||
|
@ -25,12 +25,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
utilnet "k8s.io/apimachinery/pkg/util/net"
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
||||||
"k8s.io/apiserver/pkg/apis/apiserver"
|
"k8s.io/apiserver/pkg/apis/apiserver"
|
||||||
"k8s.io/apiserver/pkg/server/egressselector/metrics"
|
"k8s.io/apiserver/pkg/server/egressselector/metrics"
|
||||||
"k8s.io/component-base/metrics/legacyregistry"
|
"k8s.io/component-base/metrics/legacyregistry"
|
||||||
"k8s.io/component-base/metrics/testutil"
|
"k8s.io/component-base/metrics/testutil"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeEgressSelection struct {
|
type fakeEgressSelection struct {
|
||||||
@ -244,7 +244,7 @@ func TestMetrics(t *testing.T) {
|
|||||||
|
|
||||||
t.Run(tn, func(t *testing.T) {
|
t.Run(tn, func(t *testing.T) {
|
||||||
metrics.Metrics.Reset()
|
metrics.Metrics.Reset()
|
||||||
metrics.Metrics.SetClock(clock.NewFakeClock(time.Now()))
|
metrics.Metrics.SetClock(testingclock.NewFakeClock(time.Now()))
|
||||||
d := dialerCreator{
|
d := dialerCreator{
|
||||||
connector: &fakeProxyServerConnector{
|
connector: &fakeProxyServerConnector{
|
||||||
connectorErr: tc.connectorErr,
|
connectorErr: tc.connectorErr,
|
||||||
|
@ -19,9 +19,9 @@ package metrics
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/component-base/metrics"
|
"k8s.io/component-base/metrics"
|
||||||
"k8s.io/component-base/metrics/legacyregistry"
|
"k8s.io/component-base/metrics/legacyregistry"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -31,7 +31,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
|
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
|
||||||
"k8s.io/apimachinery/pkg/version"
|
"k8s.io/apimachinery/pkg/version"
|
||||||
@ -56,6 +55,7 @@ import (
|
|||||||
openapiutil "k8s.io/kube-openapi/pkg/util"
|
openapiutil "k8s.io/kube-openapi/pkg/util"
|
||||||
openapiproto "k8s.io/kube-openapi/pkg/util/proto"
|
openapiproto "k8s.io/kube-openapi/pkg/util/proto"
|
||||||
"k8s.io/kube-openapi/pkg/validation/spec"
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Info about an API group.
|
// Info about an API group.
|
||||||
|
@ -21,8 +21,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apiserver/pkg/server/healthz"
|
"k8s.io/apiserver/pkg/server/healthz"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddHealthChecks adds HealthCheck(s) to health endpoints (healthz, livez, readyz) but
|
// AddHealthChecks adds HealthCheck(s) to health endpoints (healthz, livez, readyz) but
|
||||||
|
@ -20,13 +20,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDelayedHealthCheck(t *testing.T) {
|
func TestDelayedHealthCheck(t *testing.T) {
|
||||||
t.Run("test that liveness check returns true until the delay has elapsed", func(t *testing.T) {
|
t.Run("test that liveness check returns true until the delay has elapsed", func(t *testing.T) {
|
||||||
t0 := time.Unix(0, 0)
|
t0 := time.Unix(0, 0)
|
||||||
c := clock.NewFakeClock(t0)
|
c := testingclock.NewFakeClock(t0)
|
||||||
doneCh := make(chan struct{})
|
doneCh := make(chan struct{})
|
||||||
|
|
||||||
healthCheck := delayedHealthCheck(postStartHookHealthz{"test", doneCh}, c, time.Duration(10)*time.Second)
|
healthCheck := delayedHealthCheck(postStartHookHealthz{"test", doneCh}, c, time.Duration(10)*time.Second)
|
||||||
@ -52,7 +52,7 @@ func TestDelayedHealthCheck(t *testing.T) {
|
|||||||
})
|
})
|
||||||
t.Run("test that liveness check does not toggle false even if done channel is closed early", func(t *testing.T) {
|
t.Run("test that liveness check does not toggle false even if done channel is closed early", func(t *testing.T) {
|
||||||
t0 := time.Unix(0, 0)
|
t0 := time.Unix(0, 0)
|
||||||
c := clock.NewFakeClock(t0)
|
c := testingclock.NewFakeClock(t0)
|
||||||
|
|
||||||
doneCh := make(chan struct{})
|
doneCh := make(chan struct{})
|
||||||
|
|
||||||
|
@ -31,7 +31,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/fields"
|
"k8s.io/apimachinery/pkg/fields"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
@ -41,6 +40,7 @@ import (
|
|||||||
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
utiltrace "k8s.io/utils/trace"
|
utiltrace "k8s.io/utils/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
@ -44,6 +43,8 @@ import (
|
|||||||
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
|
||||||
"k8s.io/apiserver/pkg/storage"
|
"k8s.io/apiserver/pkg/storage"
|
||||||
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -603,7 +604,7 @@ func TestTimeBucketWatchersBasic(t *testing.T) {
|
|||||||
return newCacheWatcher(0, filter, forget, testVersioner{}, deadline, true, objectType, "")
|
return newCacheWatcher(0, filter, forget, testVersioner{}, deadline, true, objectType, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
clock := clock.NewFakeClock(time.Now())
|
clock := testingclock.NewFakeClock(time.Now())
|
||||||
watchers := newTimeBucketWatchers(clock, defaultBookmarkFrequency)
|
watchers := newTimeBucketWatchers(clock, defaultBookmarkFrequency)
|
||||||
now := clock.Now()
|
now := clock.Now()
|
||||||
watchers.addWatcher(newWatcher(now.Add(10 * time.Second)))
|
watchers.addWatcher(newWatcher(now.Add(10 * time.Second)))
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -20,11 +20,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTimeBudget(t *testing.T) {
|
func TestTimeBudget(t *testing.T) {
|
||||||
fakeClock := clock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
|
|
||||||
budget := &timeBudgetImpl{
|
budget := &timeBudgetImpl{
|
||||||
clock: fakeClock,
|
clock: fakeClock,
|
||||||
|
@ -27,11 +27,11 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/fields"
|
"k8s.io/apimachinery/pkg/fields"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/apiserver/pkg/storage"
|
"k8s.io/apiserver/pkg/storage"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
utiltrace "k8s.io/utils/trace"
|
utiltrace "k8s.io/utils/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,13 +31,13 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/fields"
|
"k8s.io/apimachinery/pkg/fields"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/apiserver/pkg/apis/example"
|
"k8s.io/apiserver/pkg/apis/example"
|
||||||
"k8s.io/apiserver/pkg/storage"
|
"k8s.io/apiserver/pkg/storage"
|
||||||
"k8s.io/apiserver/pkg/storage/etcd3"
|
"k8s.io/apiserver/pkg/storage/etcd3"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeTestPod(name string, resourceVersion uint64) *v1.Pod {
|
func makeTestPod(name string, resourceVersion uint64) *v1.Pod {
|
||||||
@ -81,7 +81,7 @@ func newTestWatchCache(capacity int, indexers *cache.Indexers) *watchCache {
|
|||||||
}
|
}
|
||||||
versioner := etcd3.APIObjectVersioner{}
|
versioner := etcd3.APIObjectVersioner{}
|
||||||
mockHandler := func(*watchCacheEvent) {}
|
mockHandler := func(*watchCacheEvent) {}
|
||||||
wc := newWatchCache(keyFunc, mockHandler, getAttrsFunc, versioner, indexers, clock.NewFakeClock(time.Now()), reflect.TypeOf(&example.Pod{}))
|
wc := newWatchCache(keyFunc, mockHandler, getAttrsFunc, versioner, indexers, testingclock.NewFakeClock(time.Now()), reflect.TypeOf(&example.Pod{}))
|
||||||
// To preserve behavior of tests that assume a given capacity,
|
// To preserve behavior of tests that assume a given capacity,
|
||||||
// resize it to th expected size.
|
// resize it to th expected size.
|
||||||
wc.capacity = capacity
|
wc.capacity = capacity
|
||||||
@ -443,7 +443,7 @@ func TestWaitUntilFreshAndGet(t *testing.T) {
|
|||||||
|
|
||||||
func TestWaitUntilFreshAndListTimeout(t *testing.T) {
|
func TestWaitUntilFreshAndListTimeout(t *testing.T) {
|
||||||
store := newTestWatchCache(3, &cache.Indexers{})
|
store := newTestWatchCache(3, &cache.Indexers{})
|
||||||
fc := store.clock.(*clock.FakeClock)
|
fc := store.clock.(*testingclock.FakeClock)
|
||||||
|
|
||||||
// In background, step clock after the below call starts the timer.
|
// In background, step clock after the below call starts the timer.
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -36,7 +36,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
@ -52,6 +51,8 @@ import (
|
|||||||
"k8s.io/apiserver/pkg/storage/value"
|
"k8s.io/apiserver/pkg/storage/value"
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -409,7 +410,7 @@ func TestWatch(t *testing.T) {
|
|||||||
// Inject one list error to make sure we test the relist case.
|
// Inject one list error to make sure we test the relist case.
|
||||||
etcdStorage = &injectListError{errors: 1, Interface: etcdStorage}
|
etcdStorage = &injectListError{errors: 1, Interface: etcdStorage}
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
fakeClock := clock.NewFakeClock(time.Now())
|
fakeClock := testingclock.NewFakeClock(time.Now())
|
||||||
cacher, _, err := newTestCacherWithClock(etcdStorage, fakeClock)
|
cacher, _, err := newTestCacherWithClock(etcdStorage, fakeClock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Couldn't create cacher: %v", err)
|
t.Fatalf("Couldn't create cacher: %v", err)
|
||||||
|
@ -38,7 +38,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/client-go/pkg/apis/clientauthentication"
|
"k8s.io/client-go/pkg/apis/clientauthentication"
|
||||||
"k8s.io/client-go/pkg/apis/clientauthentication/install"
|
"k8s.io/client-go/pkg/apis/clientauthentication/install"
|
||||||
clientauthenticationv1 "k8s.io/client-go/pkg/apis/clientauthentication/v1"
|
clientauthenticationv1 "k8s.io/client-go/pkg/apis/clientauthentication/v1"
|
||||||
@ -49,6 +48,7 @@ import (
|
|||||||
"k8s.io/client-go/transport"
|
"k8s.io/client-go/transport"
|
||||||
"k8s.io/client-go/util/connrotation"
|
"k8s.io/client-go/util/connrotation"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const execInfoEnv = "KUBERNETES_EXEC_INFO"
|
const execInfoEnv = "KUBERNETES_EXEC_INFO"
|
||||||
|
@ -39,10 +39,10 @@ import (
|
|||||||
|
|
||||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/client-go/pkg/apis/clientauthentication"
|
"k8s.io/client-go/pkg/apis/clientauthentication"
|
||||||
"k8s.io/client-go/tools/clientcmd/api"
|
"k8s.io/client-go/tools/clientcmd/api"
|
||||||
"k8s.io/client-go/transport"
|
"k8s.io/client-go/transport"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -1421,7 +1421,7 @@ func TestInstallHintRateLimit(t *testing.T) {
|
|||||||
a.sometimes.threshold = test.threshold
|
a.sometimes.threshold = test.threshold
|
||||||
a.sometimes.interval = test.interval
|
a.sometimes.interval = test.interval
|
||||||
|
|
||||||
clock := clock.NewFakeClock(time.Now())
|
clock := testingclock.NewFakeClock(time.Now())
|
||||||
a.sometimes.clock = clock
|
a.sometimes.clock = clock
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
|
@ -39,13 +39,13 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
|
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
|
||||||
utilclock "k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/net"
|
"k8s.io/apimachinery/pkg/util/net"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
restclientwatch "k8s.io/client-go/rest/watch"
|
restclientwatch "k8s.io/client-go/rest/watch"
|
||||||
"k8s.io/client-go/tools/metrics"
|
"k8s.io/client-go/tools/metrics"
|
||||||
"k8s.io/client-go/util/flowcontrol"
|
"k8s.io/client-go/util/flowcontrol"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -619,12 +619,12 @@ type throttleSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type throttledLogger struct {
|
type throttledLogger struct {
|
||||||
clock utilclock.PassiveClock
|
clock clock.PassiveClock
|
||||||
settings []*throttleSettings
|
settings []*throttleSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
var globalThrottledLogger = &throttledLogger{
|
var globalThrottledLogger = &throttledLogger{
|
||||||
clock: utilclock.RealClock{},
|
clock: clock.RealClock{},
|
||||||
settings: []*throttleSettings{
|
settings: []*throttleSettings{
|
||||||
{
|
{
|
||||||
logLevel: 2,
|
logLevel: 2,
|
||||||
|
@ -45,7 +45,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
|
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/apimachinery/pkg/util/httpstream"
|
"k8s.io/apimachinery/pkg/util/httpstream"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
@ -2485,7 +2484,7 @@ func TestThrottledLogger(t *testing.T) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
globalThrottledLogger.clock = oldClock
|
globalThrottledLogger.clock = oldClock
|
||||||
}()
|
}()
|
||||||
clock := clock.NewFakeClock(now)
|
clock := testingclock.NewFakeClock(now)
|
||||||
globalThrottledLogger.clock = clock
|
globalThrottledLogger.clock = clock
|
||||||
|
|
||||||
logMessages := 0
|
logMessages := 0
|
||||||
|
@ -29,7 +29,6 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/json"
|
"k8s.io/apimachinery/pkg/util/json"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||||
@ -43,6 +42,7 @@ import (
|
|||||||
"k8s.io/client-go/tools/record"
|
"k8s.io/client-go/tools/record"
|
||||||
"k8s.io/client-go/tools/record/util"
|
"k8s.io/client-go/tools/record/util"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -24,12 +24,12 @@ import (
|
|||||||
eventsv1 "k8s.io/api/events/v1"
|
eventsv1 "k8s.io/api/events/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/client-go/tools/record/util"
|
"k8s.io/client-go/tools/record/util"
|
||||||
"k8s.io/client-go/tools/reference"
|
"k8s.io/client-go/tools/reference"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
type recorderImpl struct {
|
type recorderImpl struct {
|
||||||
|
@ -24,8 +24,8 @@ import (
|
|||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
|
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
|
||||||
|
testingclock "k8s.io/utils/clock/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeLock struct {
|
type fakeLock struct {
|
||||||
@ -91,7 +91,7 @@ func TestLeaderElectionHealthChecker(t *testing.T) {
|
|||||||
HolderIdentity: "healthTest",
|
HolderIdentity: "healthTest",
|
||||||
},
|
},
|
||||||
observedTime: current,
|
observedTime: current,
|
||||||
clock: clock.NewFakeClock(current.Add(time.Hour)),
|
clock: testingclock.NewFakeClock(current.Add(time.Hour)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -108,7 +108,7 @@ func TestLeaderElectionHealthChecker(t *testing.T) {
|
|||||||
HolderIdentity: "otherServer",
|
HolderIdentity: "otherServer",
|
||||||
},
|
},
|
||||||
observedTime: current,
|
observedTime: current,
|
||||||
clock: clock.NewFakeClock(current.Add(time.Hour)),
|
clock: testingclock.NewFakeClock(current.Add(time.Hour)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -125,7 +125,7 @@ func TestLeaderElectionHealthChecker(t *testing.T) {
|
|||||||
HolderIdentity: "healthTest",
|
HolderIdentity: "healthTest",
|
||||||
},
|
},
|
||||||
observedTime: current,
|
observedTime: current,
|
||||||
clock: clock.NewFakeClock(current),
|
clock: testingclock.NewFakeClock(current),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -142,7 +142,7 @@ func TestLeaderElectionHealthChecker(t *testing.T) {
|
|||||||
HolderIdentity: "healthTest",
|
HolderIdentity: "healthTest",
|
||||||
},
|
},
|
||||||
observedTime: current,
|
observedTime: current,
|
||||||
clock: clock.NewFakeClock(current.Add(time.Minute).Add(time.Second)),
|
clock: testingclock.NewFakeClock(current.Add(time.Minute).Add(time.Second)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -61,10 +61,10 @@ import (
|
|||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/runtime"
|
"k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
|
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
@ -30,12 +30,12 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/clock"
|
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/client-go/kubernetes/fake"
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
fakeclient "k8s.io/client-go/testing"
|
fakeclient "k8s.io/client-go/testing"
|
||||||
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
|
rl "k8s.io/client-go/tools/leaderelection/resourcelock"
|
||||||
"k8s.io/client-go/tools/record"
|
"k8s.io/client-go/tools/record"
|
||||||
|
"k8s.io/utils/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createLockObject(t *testing.T, objectType, namespace, name string, record *rl.LeaderElectionRecord) (obj runtime.Object) {
|
func createLockObject(t *testing.T, objectType, namespace, name string, record *rl.LeaderElectionRecord) (obj runtime.Object) {
|
||||||
|
Loading…
Reference in New Issue
Block a user