зеркало из https://github.com/Azure/ARO-RP.git
go mod vendor
This commit is contained in:
Родитель
5e58754a7a
Коммит
48a46997ad
|
@ -1,445 +0,0 @@
|
|||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package clock
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PassiveClock allows for injecting fake or real clocks into code
|
||||
// that needs to read the current time but does not support scheduling
|
||||
// activity in the future.
|
||||
type PassiveClock interface {
|
||||
Now() time.Time
|
||||
Since(time.Time) time.Duration
|
||||
}
|
||||
|
||||
// Clock allows for injecting fake or real clocks into code that
|
||||
// needs to do arbitrary things based on time.
|
||||
type Clock interface {
|
||||
PassiveClock
|
||||
After(time.Duration) <-chan time.Time
|
||||
AfterFunc(time.Duration, func()) Timer
|
||||
NewTimer(time.Duration) Timer
|
||||
Sleep(time.Duration)
|
||||
NewTicker(time.Duration) Ticker
|
||||
}
|
||||
|
||||
// RealClock really calls time.Now()
|
||||
type RealClock struct{}
|
||||
|
||||
// Now returns the current time.
|
||||
func (RealClock) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// Since returns time since the specified timestamp.
|
||||
func (RealClock) Since(ts time.Time) time.Duration {
|
||||
return time.Since(ts)
|
||||
}
|
||||
|
||||
// After is the same as time.After(d).
|
||||
func (RealClock) After(d time.Duration) <-chan time.Time {
|
||||
return time.After(d)
|
||||
}
|
||||
|
||||
// AfterFunc is the same as time.AfterFunc(d, f).
|
||||
func (RealClock) AfterFunc(d time.Duration, f func()) Timer {
|
||||
return &realTimer{
|
||||
timer: time.AfterFunc(d, f),
|
||||
}
|
||||
}
|
||||
|
||||
// NewTimer returns a new Timer.
|
||||
func (RealClock) NewTimer(d time.Duration) Timer {
|
||||
return &realTimer{
|
||||
timer: time.NewTimer(d),
|
||||
}
|
||||
}
|
||||
|
||||
// NewTicker returns a new Ticker.
|
||||
func (RealClock) NewTicker(d time.Duration) Ticker {
|
||||
return &realTicker{
|
||||
ticker: time.NewTicker(d),
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep pauses the RealClock for duration d.
|
||||
func (RealClock) Sleep(d time.Duration) {
|
||||
time.Sleep(d)
|
||||
}
|
||||
|
||||
// FakePassiveClock implements PassiveClock, but returns an arbitrary time.
|
||||
type FakePassiveClock struct {
|
||||
lock sync.RWMutex
|
||||
time time.Time
|
||||
}
|
||||
|
||||
// FakeClock implements Clock, but returns an arbitrary time.
|
||||
type FakeClock struct {
|
||||
FakePassiveClock
|
||||
|
||||
// waiters are waiting for the fake time to pass their specified time
|
||||
waiters []fakeClockWaiter
|
||||
}
|
||||
|
||||
type fakeClockWaiter struct {
|
||||
targetTime time.Time
|
||||
stepInterval time.Duration
|
||||
skipIfBlocked bool
|
||||
destChan chan time.Time
|
||||
afterFunc func()
|
||||
}
|
||||
|
||||
// NewFakePassiveClock returns a new FakePassiveClock.
|
||||
func NewFakePassiveClock(t time.Time) *FakePassiveClock {
|
||||
return &FakePassiveClock{
|
||||
time: t,
|
||||
}
|
||||
}
|
||||
|
||||
// NewFakeClock returns a new FakeClock
|
||||
func NewFakeClock(t time.Time) *FakeClock {
|
||||
return &FakeClock{
|
||||
FakePassiveClock: *NewFakePassiveClock(t),
|
||||
}
|
||||
}
|
||||
|
||||
// Now returns f's time.
|
||||
func (f *FakePassiveClock) Now() time.Time {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
return f.time
|
||||
}
|
||||
|
||||
// Since returns time since the time in f.
|
||||
func (f *FakePassiveClock) Since(ts time.Time) time.Duration {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
return f.time.Sub(ts)
|
||||
}
|
||||
|
||||
// SetTime sets the time on the FakePassiveClock.
|
||||
func (f *FakePassiveClock) SetTime(t time.Time) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.time = t
|
||||
}
|
||||
|
||||
// After is the Fake version of time.After(d).
|
||||
func (f *FakeClock) After(d time.Duration) <-chan time.Time {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
stopTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // Don't block!
|
||||
f.waiters = append(f.waiters, fakeClockWaiter{
|
||||
targetTime: stopTime,
|
||||
destChan: ch,
|
||||
})
|
||||
return ch
|
||||
}
|
||||
|
||||
// AfterFunc is the Fake version of time.AfterFunc(d, callback).
|
||||
func (f *FakeClock) AfterFunc(d time.Duration, cb func()) Timer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
stopTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // Don't block!
|
||||
|
||||
timer := &fakeTimer{
|
||||
fakeClock: f,
|
||||
waiter: fakeClockWaiter{
|
||||
targetTime: stopTime,
|
||||
destChan: ch,
|
||||
afterFunc: cb,
|
||||
},
|
||||
}
|
||||
f.waiters = append(f.waiters, timer.waiter)
|
||||
return timer
|
||||
}
|
||||
|
||||
// NewTimer is the Fake version of time.NewTimer(d).
|
||||
func (f *FakeClock) NewTimer(d time.Duration) Timer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
stopTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // Don't block!
|
||||
timer := &fakeTimer{
|
||||
fakeClock: f,
|
||||
waiter: fakeClockWaiter{
|
||||
targetTime: stopTime,
|
||||
destChan: ch,
|
||||
},
|
||||
}
|
||||
f.waiters = append(f.waiters, timer.waiter)
|
||||
return timer
|
||||
}
|
||||
|
||||
// NewTicker returns a new Ticker.
|
||||
func (f *FakeClock) NewTicker(d time.Duration) Ticker {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
tickTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // hold one tick
|
||||
f.waiters = append(f.waiters, fakeClockWaiter{
|
||||
targetTime: tickTime,
|
||||
stepInterval: d,
|
||||
skipIfBlocked: true,
|
||||
destChan: ch,
|
||||
})
|
||||
|
||||
return &fakeTicker{
|
||||
c: ch,
|
||||
}
|
||||
}
|
||||
|
||||
// Step moves clock by Duration, notifies anyone that's called After, Tick, or NewTimer
|
||||
func (f *FakeClock) Step(d time.Duration) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.setTimeLocked(f.time.Add(d))
|
||||
}
|
||||
|
||||
// SetTime sets the time on a FakeClock.
|
||||
func (f *FakeClock) SetTime(t time.Time) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.setTimeLocked(t)
|
||||
}
|
||||
|
||||
// Actually changes the time and checks any waiters. f must be write-locked.
|
||||
func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
f.time = t
|
||||
newWaiters := make([]fakeClockWaiter, 0, len(f.waiters))
|
||||
for i := range f.waiters {
|
||||
w := &f.waiters[i]
|
||||
if !w.targetTime.After(t) {
|
||||
|
||||
if w.skipIfBlocked {
|
||||
select {
|
||||
case w.destChan <- t:
|
||||
default:
|
||||
}
|
||||
} else {
|
||||
w.destChan <- t
|
||||
}
|
||||
|
||||
if w.afterFunc != nil {
|
||||
w.afterFunc()
|
||||
}
|
||||
|
||||
if w.stepInterval > 0 {
|
||||
for !w.targetTime.After(t) {
|
||||
w.targetTime = w.targetTime.Add(w.stepInterval)
|
||||
}
|
||||
newWaiters = append(newWaiters, *w)
|
||||
}
|
||||
|
||||
} else {
|
||||
newWaiters = append(newWaiters, f.waiters[i])
|
||||
}
|
||||
}
|
||||
f.waiters = newWaiters
|
||||
}
|
||||
|
||||
// HasWaiters returns true if After or AfterFunc has been called on f but not yet satisfied
|
||||
// (so you can write race-free tests).
|
||||
func (f *FakeClock) HasWaiters() bool {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
return len(f.waiters) > 0
|
||||
}
|
||||
|
||||
// Sleep pauses the FakeClock for duration d.
|
||||
func (f *FakeClock) Sleep(d time.Duration) {
|
||||
f.Step(d)
|
||||
}
|
||||
|
||||
// IntervalClock implements Clock, but each invocation of Now steps the clock forward the specified duration
|
||||
type IntervalClock struct {
|
||||
Time time.Time
|
||||
Duration time.Duration
|
||||
}
|
||||
|
||||
// Now returns i's time.
|
||||
func (i *IntervalClock) Now() time.Time {
|
||||
i.Time = i.Time.Add(i.Duration)
|
||||
return i.Time
|
||||
}
|
||||
|
||||
// Since returns time since the time in i.
|
||||
func (i *IntervalClock) Since(ts time.Time) time.Duration {
|
||||
return i.Time.Sub(ts)
|
||||
}
|
||||
|
||||
// After is currently unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) After(d time.Duration) <-chan time.Time {
|
||||
panic("IntervalClock doesn't implement After")
|
||||
}
|
||||
|
||||
// AfterFunc is currently unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) AfterFunc(d time.Duration, cb func()) Timer {
|
||||
panic("IntervalClock doesn't implement AfterFunc")
|
||||
}
|
||||
|
||||
// NewTimer is currently unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) NewTimer(d time.Duration) Timer {
|
||||
panic("IntervalClock doesn't implement NewTimer")
|
||||
}
|
||||
|
||||
// NewTicker is currently unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) NewTicker(d time.Duration) Ticker {
|
||||
panic("IntervalClock doesn't implement NewTicker")
|
||||
}
|
||||
|
||||
// Sleep is currently unimplemented; will panic.
|
||||
func (*IntervalClock) Sleep(d time.Duration) {
|
||||
panic("IntervalClock doesn't implement Sleep")
|
||||
}
|
||||
|
||||
// Timer allows for injecting fake or real timers into code that
|
||||
// needs to do arbitrary things based on time.
|
||||
type Timer interface {
|
||||
C() <-chan time.Time
|
||||
Stop() bool
|
||||
Reset(d time.Duration) bool
|
||||
}
|
||||
|
||||
// realTimer is backed by an actual time.Timer.
|
||||
type realTimer struct {
|
||||
timer *time.Timer
|
||||
}
|
||||
|
||||
// C returns the underlying timer's channel.
|
||||
func (r *realTimer) C() <-chan time.Time {
|
||||
return r.timer.C
|
||||
}
|
||||
|
||||
// Stop calls Stop() on the underlying timer.
|
||||
func (r *realTimer) Stop() bool {
|
||||
return r.timer.Stop()
|
||||
}
|
||||
|
||||
// Reset calls Reset() on the underlying timer.
|
||||
func (r *realTimer) Reset(d time.Duration) bool {
|
||||
return r.timer.Reset(d)
|
||||
}
|
||||
|
||||
// fakeTimer implements Timer based on a FakeClock.
|
||||
type fakeTimer struct {
|
||||
fakeClock *FakeClock
|
||||
waiter fakeClockWaiter
|
||||
}
|
||||
|
||||
// C returns the channel that notifies when this timer has fired.
|
||||
func (f *fakeTimer) C() <-chan time.Time {
|
||||
return f.waiter.destChan
|
||||
}
|
||||
|
||||
// Stop conditionally stops the timer. If the timer has neither fired
|
||||
// nor been stopped then this call stops the timer and returns true,
|
||||
// otherwise this call returns false. This is like time.Timer::Stop.
|
||||
func (f *fakeTimer) Stop() bool {
|
||||
f.fakeClock.lock.Lock()
|
||||
defer f.fakeClock.lock.Unlock()
|
||||
// The timer has already fired or been stopped, unless it is found
|
||||
// among the clock's waiters.
|
||||
stopped := false
|
||||
oldWaiters := f.fakeClock.waiters
|
||||
newWaiters := make([]fakeClockWaiter, 0, len(oldWaiters))
|
||||
seekChan := f.waiter.destChan
|
||||
for i := range oldWaiters {
|
||||
// Identify the timer's fakeClockWaiter by the identity of the
|
||||
// destination channel, nothing else is necessarily unique and
|
||||
// constant since the timer's creation.
|
||||
if oldWaiters[i].destChan == seekChan {
|
||||
stopped = true
|
||||
} else {
|
||||
newWaiters = append(newWaiters, oldWaiters[i])
|
||||
}
|
||||
}
|
||||
|
||||
f.fakeClock.waiters = newWaiters
|
||||
|
||||
return stopped
|
||||
}
|
||||
|
||||
// Reset conditionally updates the firing time of the timer. If the
|
||||
// timer has neither fired nor been stopped then this call resets the
|
||||
// timer to the fake clock's "now" + d and returns true, otherwise
|
||||
// it creates a new waiter, adds it to the clock, and returns true.
|
||||
//
|
||||
// It is not possible to return false, because a fake timer can be reset
|
||||
// from any state (waiting to fire, already fired, and stopped).
|
||||
//
|
||||
// See the GoDoc for time.Timer::Reset for more context on why
|
||||
// the return value of Reset() is not useful.
|
||||
func (f *fakeTimer) Reset(d time.Duration) bool {
|
||||
f.fakeClock.lock.Lock()
|
||||
defer f.fakeClock.lock.Unlock()
|
||||
waiters := f.fakeClock.waiters
|
||||
seekChan := f.waiter.destChan
|
||||
for i := range waiters {
|
||||
if waiters[i].destChan == seekChan {
|
||||
waiters[i].targetTime = f.fakeClock.time.Add(d)
|
||||
return true
|
||||
}
|
||||
}
|
||||
// No existing waiter, timer has already fired or been reset.
|
||||
// We should still enable Reset() to succeed by creating a
|
||||
// new waiter and adding it to the clock's waiters.
|
||||
newWaiter := fakeClockWaiter{
|
||||
targetTime: f.fakeClock.time.Add(d),
|
||||
destChan: seekChan,
|
||||
}
|
||||
f.fakeClock.waiters = append(f.fakeClock.waiters, newWaiter)
|
||||
return true
|
||||
}
|
||||
|
||||
// Ticker defines the Ticker interface
|
||||
type Ticker interface {
|
||||
C() <-chan time.Time
|
||||
Stop()
|
||||
}
|
||||
|
||||
type realTicker struct {
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func (t *realTicker) C() <-chan time.Time {
|
||||
return t.ticker.C
|
||||
}
|
||||
|
||||
func (t *realTicker) Stop() {
|
||||
t.ticker.Stop()
|
||||
}
|
||||
|
||||
type fakeTicker struct {
|
||||
c <-chan time.Time
|
||||
}
|
||||
|
||||
func (t *fakeTicker) C() <-chan time.Time {
|
||||
return t.c
|
||||
}
|
||||
|
||||
func (t *fakeTicker) Stop() {
|
||||
}
|
|
@ -14,12 +14,15 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Deprecated: Use functions in k8s.io/utils/ptr instead: ptr.To to obtain
|
||||
// a pointer, ptr.Deref to dereference a pointer, ptr.Equal to compare
|
||||
// dereferenced pointers.
|
||||
package pointer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"k8s.io/utils/ptr"
|
||||
)
|
||||
|
||||
// AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when,
|
||||
|
@ -28,383 +31,219 @@ import (
|
|||
//
|
||||
// This function is only valid for structs and pointers to structs. Any other
|
||||
// type will cause a panic. Passing a typed nil pointer will return true.
|
||||
func AllPtrFieldsNil(obj interface{}) bool {
|
||||
v := reflect.ValueOf(obj)
|
||||
if !v.IsValid() {
|
||||
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
|
||||
}
|
||||
if v.Kind() == reflect.Ptr {
|
||||
if v.IsNil() {
|
||||
return true
|
||||
}
|
||||
v = v.Elem()
|
||||
}
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
//
|
||||
// Deprecated: Use ptr.AllPtrFieldsNil instead.
|
||||
var AllPtrFieldsNil = ptr.AllPtrFieldsNil
|
||||
|
||||
// Int returns a pointer to an int
|
||||
func Int(i int) *int {
|
||||
return &i
|
||||
}
|
||||
// Int returns a pointer to an int.
|
||||
var Int = ptr.To[int]
|
||||
|
||||
// IntPtr is a function variable referring to Int.
|
||||
//
|
||||
// Deprecated: Use Int instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var IntPtr = Int // for back-compat
|
||||
|
||||
// IntDeref dereferences the int ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func IntDeref(ptr *int, def int) int {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var IntDeref = ptr.Deref[int]
|
||||
|
||||
// IntPtrDerefOr is a function variable referring to IntDeref.
|
||||
//
|
||||
// Deprecated: Use IntDeref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var IntPtrDerefOr = IntDeref // for back-compat
|
||||
|
||||
// Int32 returns a pointer to an int32.
|
||||
func Int32(i int32) *int32 {
|
||||
return &i
|
||||
}
|
||||
var Int32 = ptr.To[int32]
|
||||
|
||||
// Int32Ptr is a function variable referring to Int32.
|
||||
//
|
||||
// Deprecated: Use Int32 instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var Int32Ptr = Int32 // for back-compat
|
||||
|
||||
// Int32Deref dereferences the int32 ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func Int32Deref(ptr *int32, def int32) int32 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var Int32Deref = ptr.Deref[int32]
|
||||
|
||||
// Int32PtrDerefOr is a function variable referring to Int32Deref.
|
||||
//
|
||||
// Deprecated: Use Int32Deref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var Int32PtrDerefOr = Int32Deref // for back-compat
|
||||
|
||||
// Int32Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Int32Equal(a, b *int32) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var Int32Equal = ptr.Equal[int32]
|
||||
|
||||
// Uint returns a pointer to an uint
|
||||
func Uint(i uint) *uint {
|
||||
return &i
|
||||
}
|
||||
var Uint = ptr.To[uint]
|
||||
|
||||
// UintPtr is a function variable referring to Uint.
|
||||
//
|
||||
// Deprecated: Use Uint instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var UintPtr = Uint // for back-compat
|
||||
|
||||
// UintDeref dereferences the uint ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func UintDeref(ptr *uint, def uint) uint {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var UintDeref = ptr.Deref[uint]
|
||||
|
||||
// UintPtrDerefOr is a function variable referring to UintDeref.
|
||||
//
|
||||
// Deprecated: Use UintDeref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var UintPtrDerefOr = UintDeref // for back-compat
|
||||
|
||||
// Uint32 returns a pointer to an uint32.
|
||||
func Uint32(i uint32) *uint32 {
|
||||
return &i
|
||||
}
|
||||
var Uint32 = ptr.To[uint32]
|
||||
|
||||
// Uint32Ptr is a function variable referring to Uint32.
|
||||
//
|
||||
// Deprecated: Use Uint32 instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var Uint32Ptr = Uint32 // for back-compat
|
||||
|
||||
// Uint32Deref dereferences the uint32 ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func Uint32Deref(ptr *uint32, def uint32) uint32 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var Uint32Deref = ptr.Deref[uint32]
|
||||
|
||||
// Uint32PtrDerefOr is a function variable referring to Uint32Deref.
|
||||
//
|
||||
// Deprecated: Use Uint32Deref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var Uint32PtrDerefOr = Uint32Deref // for back-compat
|
||||
|
||||
// Uint32Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Uint32Equal(a, b *uint32) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var Uint32Equal = ptr.Equal[uint32]
|
||||
|
||||
// Int64 returns a pointer to an int64.
|
||||
func Int64(i int64) *int64 {
|
||||
return &i
|
||||
}
|
||||
var Int64 = ptr.To[int64]
|
||||
|
||||
// Int64Ptr is a function variable referring to Int64.
|
||||
//
|
||||
// Deprecated: Use Int64 instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var Int64Ptr = Int64 // for back-compat
|
||||
|
||||
// Int64Deref dereferences the int64 ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func Int64Deref(ptr *int64, def int64) int64 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var Int64Deref = ptr.Deref[int64]
|
||||
|
||||
// Int64PtrDerefOr is a function variable referring to Int64Deref.
|
||||
//
|
||||
// Deprecated: Use Int64Deref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var Int64PtrDerefOr = Int64Deref // for back-compat
|
||||
|
||||
// Int64Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Int64Equal(a, b *int64) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var Int64Equal = ptr.Equal[int64]
|
||||
|
||||
// Uint64 returns a pointer to an uint64.
|
||||
func Uint64(i uint64) *uint64 {
|
||||
return &i
|
||||
}
|
||||
var Uint64 = ptr.To[uint64]
|
||||
|
||||
// Uint64Ptr is a function variable referring to Uint64.
|
||||
//
|
||||
// Deprecated: Use Uint64 instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var Uint64Ptr = Uint64 // for back-compat
|
||||
|
||||
// Uint64Deref dereferences the uint64 ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func Uint64Deref(ptr *uint64, def uint64) uint64 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var Uint64Deref = ptr.Deref[uint64]
|
||||
|
||||
// Uint64PtrDerefOr is a function variable referring to Uint64Deref.
|
||||
//
|
||||
// Deprecated: Use Uint64Deref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var Uint64PtrDerefOr = Uint64Deref // for back-compat
|
||||
|
||||
// Uint64Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Uint64Equal(a, b *uint64) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var Uint64Equal = ptr.Equal[uint64]
|
||||
|
||||
// Bool returns a pointer to a bool.
|
||||
func Bool(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
var Bool = ptr.To[bool]
|
||||
|
||||
// BoolPtr is a function variable referring to Bool.
|
||||
//
|
||||
// Deprecated: Use Bool instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var BoolPtr = Bool // for back-compat
|
||||
|
||||
// BoolDeref dereferences the bool ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func BoolDeref(ptr *bool, def bool) bool {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var BoolDeref = ptr.Deref[bool]
|
||||
|
||||
// BoolPtrDerefOr is a function variable referring to BoolDeref.
|
||||
//
|
||||
// Deprecated: Use BoolDeref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var BoolPtrDerefOr = BoolDeref // for back-compat
|
||||
|
||||
// BoolEqual returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func BoolEqual(a, b *bool) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var BoolEqual = ptr.Equal[bool]
|
||||
|
||||
// String returns a pointer to a string.
|
||||
func String(s string) *string {
|
||||
return &s
|
||||
}
|
||||
var String = ptr.To[string]
|
||||
|
||||
// StringPtr is a function variable referring to String.
|
||||
//
|
||||
// Deprecated: Use String instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var StringPtr = String // for back-compat
|
||||
|
||||
// StringDeref dereferences the string ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func StringDeref(ptr *string, def string) string {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var StringDeref = ptr.Deref[string]
|
||||
|
||||
// StringPtrDerefOr is a function variable referring to StringDeref.
|
||||
//
|
||||
// Deprecated: Use StringDeref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var StringPtrDerefOr = StringDeref // for back-compat
|
||||
|
||||
// StringEqual returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func StringEqual(a, b *string) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var StringEqual = ptr.Equal[string]
|
||||
|
||||
// Float32 returns a pointer to a float32.
|
||||
func Float32(i float32) *float32 {
|
||||
return &i
|
||||
}
|
||||
var Float32 = ptr.To[float32]
|
||||
|
||||
// Float32Ptr is a function variable referring to Float32.
|
||||
//
|
||||
// Deprecated: Use Float32 instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var Float32Ptr = Float32
|
||||
|
||||
// Float32Deref dereferences the float32 ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func Float32Deref(ptr *float32, def float32) float32 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var Float32Deref = ptr.Deref[float32]
|
||||
|
||||
// Float32PtrDerefOr is a function variable referring to Float32Deref.
|
||||
//
|
||||
// Deprecated: Use Float32Deref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var Float32PtrDerefOr = Float32Deref // for back-compat
|
||||
|
||||
// Float32Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Float32Equal(a, b *float32) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var Float32Equal = ptr.Equal[float32]
|
||||
|
||||
// Float64 returns a pointer to a float64.
|
||||
func Float64(i float64) *float64 {
|
||||
return &i
|
||||
}
|
||||
var Float64 = ptr.To[float64]
|
||||
|
||||
// Float64Ptr is a function variable referring to Float64.
|
||||
//
|
||||
// Deprecated: Use Float64 instead.
|
||||
// Deprecated: Use ptr.To instead.
|
||||
var Float64Ptr = Float64
|
||||
|
||||
// Float64Deref dereferences the float64 ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func Float64Deref(ptr *float64, def float64) float64 {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var Float64Deref = ptr.Deref[float64]
|
||||
|
||||
// Float64PtrDerefOr is a function variable referring to Float64Deref.
|
||||
//
|
||||
// Deprecated: Use Float64Deref instead.
|
||||
// Deprecated: Use ptr.Deref instead.
|
||||
var Float64PtrDerefOr = Float64Deref // for back-compat
|
||||
|
||||
// Float64Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Float64Equal(a, b *float64) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var Float64Equal = ptr.Equal[float64]
|
||||
|
||||
// Duration returns a pointer to a time.Duration.
|
||||
func Duration(d time.Duration) *time.Duration {
|
||||
return &d
|
||||
}
|
||||
var Duration = ptr.To[time.Duration]
|
||||
|
||||
// DurationDeref dereferences the time.Duration ptr and returns it if not nil, or else
|
||||
// returns def.
|
||||
func DurationDeref(ptr *time.Duration, def time.Duration) time.Duration {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
var DurationDeref = ptr.Deref[time.Duration]
|
||||
|
||||
// DurationEqual returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func DurationEqual(a, b *time.Duration) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
||||
var DurationEqual = ptr.Equal[time.Duration]
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
approvers:
|
||||
- apelisse
|
||||
- stewart-yu
|
||||
- thockin
|
||||
reviewers:
|
||||
- apelisse
|
||||
- stewart-yu
|
||||
- thockin
|
|
@ -0,0 +1,3 @@
|
|||
# Pointer
|
||||
|
||||
This package provides some functions for pointer-based operations.
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
Copyright 2023 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package ptr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when,
|
||||
// for example, an API struct is handled by plugins which need to distinguish
|
||||
// "no plugin accepted this spec" from "this spec is empty".
|
||||
//
|
||||
// This function is only valid for structs and pointers to structs. Any other
|
||||
// type will cause a panic. Passing a typed nil pointer will return true.
|
||||
func AllPtrFieldsNil(obj interface{}) bool {
|
||||
v := reflect.ValueOf(obj)
|
||||
if !v.IsValid() {
|
||||
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
|
||||
}
|
||||
if v.Kind() == reflect.Ptr {
|
||||
if v.IsNil() {
|
||||
return true
|
||||
}
|
||||
v = v.Elem()
|
||||
}
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// To returns a pointer to the given value.
|
||||
func To[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
|
||||
// Deref dereferences ptr and returns the value it points to if no nil, or else
|
||||
// returns def.
|
||||
func Deref[T any](ptr *T, def T) T {
|
||||
if ptr != nil {
|
||||
return *ptr
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// Equal returns true if both arguments are nil or both arguments
|
||||
// dereference to the same value.
|
||||
func Equal[T comparable](a, b *T) bool {
|
||||
if (a == nil) != (b == nil) {
|
||||
return false
|
||||
}
|
||||
if a == nil {
|
||||
return true
|
||||
}
|
||||
return *a == *b
|
||||
}
|
|
@ -65,6 +65,11 @@ func durationToMilliseconds(timeDuration time.Duration) int64 {
|
|||
}
|
||||
|
||||
type traceItem interface {
|
||||
// rLock must be called before invoking time or writeItem.
|
||||
rLock()
|
||||
// rUnlock must be called after processing the item is complete.
|
||||
rUnlock()
|
||||
|
||||
// time returns when the trace was recorded as completed.
|
||||
time() time.Time
|
||||
// writeItem outputs the traceItem to the buffer. If stepThreshold is non-nil, only output the
|
||||
|
@ -79,6 +84,10 @@ type traceStep struct {
|
|||
fields []Field
|
||||
}
|
||||
|
||||
// rLock doesn't need to do anything because traceStep instances are immutable.
|
||||
func (s traceStep) rLock() {}
|
||||
func (s traceStep) rUnlock() {}
|
||||
|
||||
func (s traceStep) time() time.Time {
|
||||
return s.stepTime
|
||||
}
|
||||
|
@ -106,6 +115,14 @@ type Trace struct {
|
|||
traceItems []traceItem
|
||||
}
|
||||
|
||||
func (t *Trace) rLock() {
|
||||
t.lock.RLock()
|
||||
}
|
||||
|
||||
func (t *Trace) rUnlock() {
|
||||
t.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (t *Trace) time() time.Time {
|
||||
if t.endTime != nil {
|
||||
return *t.endTime
|
||||
|
@ -231,8 +248,10 @@ func (t *Trace) logTrace() {
|
|||
func (t *Trace) writeTraceSteps(b *bytes.Buffer, formatter string, stepThreshold *time.Duration) {
|
||||
lastStepTime := t.startTime
|
||||
for _, stepOrTrace := range t.traceItems {
|
||||
stepOrTrace.rLock()
|
||||
stepOrTrace.writeItem(b, formatter, lastStepTime, stepThreshold)
|
||||
lastStepTime = stepOrTrace.time()
|
||||
stepOrTrace.rUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1682,7 +1682,6 @@ k8s.io/apimachinery/pkg/runtime/serializer/versioning
|
|||
k8s.io/apimachinery/pkg/selection
|
||||
k8s.io/apimachinery/pkg/types
|
||||
k8s.io/apimachinery/pkg/util/cache
|
||||
k8s.io/apimachinery/pkg/util/clock
|
||||
k8s.io/apimachinery/pkg/util/diff
|
||||
k8s.io/apimachinery/pkg/util/duration
|
||||
k8s.io/apimachinery/pkg/util/errors
|
||||
|
@ -2087,7 +2086,7 @@ k8s.io/kubernetes/pkg/apis/rbac/v1
|
|||
k8s.io/kubernetes/pkg/features
|
||||
k8s.io/kubernetes/pkg/kubelet/events
|
||||
k8s.io/kubernetes/pkg/util/parsers
|
||||
# k8s.io/utils v0.0.0-20230313181309-38a27ef9d749
|
||||
# k8s.io/utils v0.0.0-20230726121419-3b25d923346b
|
||||
## explicit; go 1.18
|
||||
k8s.io/utils/buffer
|
||||
k8s.io/utils/clock
|
||||
|
@ -2097,6 +2096,7 @@ k8s.io/utils/integer
|
|||
k8s.io/utils/internal/third_party/forked/golang/net
|
||||
k8s.io/utils/net
|
||||
k8s.io/utils/pointer
|
||||
k8s.io/utils/ptr
|
||||
k8s.io/utils/strings/slices
|
||||
k8s.io/utils/trace
|
||||
# sigs.k8s.io/cluster-api-provider-azure v1.2.1 => github.com/openshift/cluster-api-provider-azure v0.1.0-alpha.3.0.20210626224711-5d94c794092f
|
||||
|
|
Загрузка…
Ссылка в новой задаче