Merge pull request #104413 from liggitt/openapi-ipvalidation

Bump k8s.io/kube-openapi
This commit is contained in:
Kubernetes Prow Robot
2021-08-17 11:42:26 -07:00
committed by GitHub
56 changed files with 313 additions and 138 deletions

View File

@@ -548,7 +548,7 @@ func mustEnforceDefault(t *types.Type, omitEmpty bool) (interface{}, error) {
}
func (g openAPITypeWriter) generateDefault(comments []string, t *types.Type, omitEmpty bool) error {
t = resolveAliasType(t)
t = resolveAliasAndEmbeddedType(t)
def, err := defaultFromComments(comments)
if err != nil {
return err
@@ -674,13 +674,18 @@ func (g openAPITypeWriter) generateReferenceProperty(t *types.Type) {
g.Do("Ref: ref(\"$.$\"),\n", t.Name.String())
}
func resolveAliasType(t *types.Type) *types.Type {
func resolveAliasAndEmbeddedType(t *types.Type) *types.Type {
var prev *types.Type
for prev != t {
prev = t
if t.Kind == types.Alias {
t = t.Underlying
}
if t.Kind == types.Struct {
if len(t.Members) == 1 && t.Members[0].Embedded {
t = t.Members[0].Type
}
}
}
return t
}

View File

@@ -449,10 +449,7 @@ func (c *convert) VisitPrimitive(p *proto.Primitive) {
}
func (c *convert) VisitArbitrary(a *proto.Arbitrary) {
*c.top() = untypedDef.Atom
if c.preserveUnknownFields {
*c.top() = deducedDef.Atom
}
*c.top() = deducedDef.Atom
}
func (c *convert) VisitReference(proto.Reference) {

View File

@@ -23,6 +23,8 @@ import (
"strings"
"github.com/asaskevich/govalidator"
netutils "k8s.io/utils/net"
)
const (
@@ -154,13 +156,13 @@ func init() {
Default.Add("hostname", &hn, IsHostname)
ip4 := IPv4("")
Default.Add("ipv4", &ip4, govalidator.IsIPv4)
Default.Add("ipv4", &ip4, isIPv4)
ip6 := IPv6("")
Default.Add("ipv6", &ip6, govalidator.IsIPv6)
cidr := CIDR("")
Default.Add("cidr", &cidr, govalidator.IsCIDR)
Default.Add("cidr", &cidr, isCIDR)
mac := MAC("")
Default.Add("mac", &mac, govalidator.IsMAC)
@@ -205,6 +207,18 @@ func init() {
Default.Add("password", &pw, func(_ string) bool { return true })
}
// isIPv4 checks if the string is an IPv4 address, tolerating leading 0's for compatibility with go < 1.17.
func isIPv4(s string) bool {
ip := netutils.ParseIPSloppy(s)
return ip != nil && strings.Contains(s, ".")
}
// isCIDR checks if the string is valid CIDR notation, tolerating leading 0's for compatibility with go < 1.17.
func isCIDR(s string) bool {
_, _, err := netutils.ParseCIDRSloppy(s)
return err == nil
}
// Base64 represents a base64 encoded string, using URLEncoding alphabet
//
// swagger:strfmt byte

49
vendor/k8s.io/utils/clock/clock.go generated vendored
View File

@@ -30,13 +30,36 @@ type PassiveClock interface {
// needs to do arbitrary things based on time.
type Clock interface {
PassiveClock
// After returns the channel of a new Timer.
// This method does not allow to free/GC the backing timer before it fires. Use
// NewTimer instead.
After(d time.Duration) <-chan time.Time
// NewTimer returns a new Timer.
NewTimer(d time.Duration) Timer
// Sleep sleeps for the provided duration d.
// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
Sleep(d time.Duration)
// Tick returns the channel of a new Ticker.
// This method does not allow to free/GC the backing ticker. Use
// NewTicker from WithTicker instead.
Tick(d time.Duration) <-chan time.Time
}
var _ = Clock(RealClock{})
// WithTicker allows for injecting fake or real clocks into code that
// needs to do arbitrary things based on time.
type WithTicker interface {
Clock
// NewTicker returns a new Ticker.
NewTicker(time.Duration) Ticker
}
// Ticker defines the Ticker interface.
type Ticker interface {
C() <-chan time.Time
Stop()
}
var _ = WithTicker(RealClock{})
// RealClock really calls time.Now()
type RealClock struct{}
@@ -52,6 +75,8 @@ func (RealClock) Since(ts time.Time) time.Duration {
}
// After is the same as time.After(d).
// This method does not allow to free/GC the backing timer before it fires. Use
// NewTimer instead.
func (RealClock) After(d time.Duration) <-chan time.Time {
return time.After(d)
}
@@ -64,11 +89,21 @@ func (RealClock) NewTimer(d time.Duration) Timer {
}
// Tick is the same as time.Tick(d)
// This method does not allow to free/GC the backing ticker. Use
// NewTicker instead.
func (RealClock) Tick(d time.Duration) <-chan time.Time {
return time.Tick(d)
}
// NewTicker returns a new Ticker.
func (RealClock) NewTicker(d time.Duration) Ticker {
return &realTicker{
ticker: time.NewTicker(d),
}
}
// Sleep is the same as time.Sleep(d)
// Consider making the sleep interruptible by using 'select' on a context channel and a timer channel.
func (RealClock) Sleep(d time.Duration) {
time.Sleep(d)
}
@@ -102,3 +137,15 @@ func (r *realTimer) Stop() bool {
func (r *realTimer) Reset(d time.Duration) bool {
return r.timer.Reset(d)
}
type realTicker struct {
ticker *time.Ticker
}
func (r *realTicker) C() <-chan time.Time {
return r.ticker.C
}
func (r *realTicker) Stop() {
r.ticker.Stop()
}

View File

@@ -25,7 +25,7 @@ import (
var (
_ = clock.PassiveClock(&FakePassiveClock{})
_ = clock.Clock(&FakeClock{})
_ = clock.WithTicker(&FakeClock{})
_ = clock.Clock(&IntervalClock{})
)
@@ -135,6 +135,24 @@ func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
return ch
}
// NewTicker returns a new Ticker.
func (f *FakeClock) NewTicker(d time.Duration) clock.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 the clock by Duration and notifies anyone that's called After,
// Tick, or NewTimer.
func (f *FakeClock) Step(d time.Duration) {
@@ -196,7 +214,9 @@ func (f *FakeClock) Sleep(d time.Duration) {
f.Step(d)
}
// IntervalClock implements clock.Clock, but each invocation of Now steps the clock forward the specified duration
// IntervalClock implements clock.PassiveClock, but each invocation of Now steps the clock forward the specified duration.
// IntervalClock technically implements the other methods of clock.Clock, but each implementation is just a panic.
// See SimpleIntervalClock for an alternative that only has the methods of PassiveClock.
type IntervalClock struct {
Time time.Time
Duration time.Duration
@@ -231,6 +251,12 @@ func (*IntervalClock) Tick(d time.Duration) <-chan time.Time {
panic("IntervalClock doesn't implement Tick")
}
// NewTicker has no implementation yet and is omitted.
// TODO: make interval clock use FakeClock so this can be implemented.
//func (*IntervalClock) NewTicker(d time.Duration) clock.Ticker {
// panic("IntervalClock doesn't implement NewTicker")
//}
// Sleep is unimplemented, will panic.
func (*IntervalClock) Sleep(d time.Duration) {
panic("IntervalClock doesn't implement Sleep")
@@ -292,3 +318,14 @@ func (f *fakeTimer) Reset(d time.Duration) bool {
return active
}
type fakeTicker struct {
c <-chan time.Time
}
func (t *fakeTicker) C() <-chan time.Time {
return t.c
}
func (t *fakeTicker) Stop() {
}

View File

@@ -0,0 +1,44 @@
/*
Copyright 2021 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 testing
import (
"time"
"k8s.io/utils/clock"
)
var (
_ = clock.PassiveClock(&SimpleIntervalClock{})
)
// SimpleIntervalClock implements clock.PassiveClock, but each invocation of Now steps the clock forward the specified duration
type SimpleIntervalClock struct {
Time time.Time
Duration time.Duration
}
// Now returns i's time.
func (i *SimpleIntervalClock) Now() time.Time {
i.Time = i.Time.Add(i.Duration)
return i.Time
}
// Since returns time since the time in i.
func (i *SimpleIntervalClock) Since(ts time.Time) time.Duration {
return i.Time.Sub(ts)
}

View File

@@ -46,6 +46,24 @@ func AllPtrFieldsNil(obj interface{}) bool {
return true
}
// Int returns a pointer to an int
func Int(i int) *int {
return &i
}
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 IntPtrDerefOr = IntDeref // for back-compat
// Int32 returns a pointer to an int32.
func Int32(i int32) *int32 {
return &i