Update k8s.io/utils to latest version

Update all usages of FakeExec to pointer to avoid copying the mutex
This commit is contained in:
Artem Minyaylov
2023-02-03 14:51:25 -08:00
parent 0e3818e027
commit f573e14942
73 changed files with 421 additions and 358 deletions

View File

@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"sync"
"k8s.io/utils/exec"
)
@@ -32,11 +33,12 @@ type FakeExec struct {
// ExactOrder enforces that commands are called in the order they are scripted,
// and with the exact same arguments
ExactOrder bool
// DisableScripts removes the requirement that a slice of FakeCommandAction be
// populated before calling Command(). This makes the fakeexec (and subsequent
// calls to Run() or CombinedOutput() always return success and there is no
// ability to set their output.
// DisableScripts removes the requirement that CommandScripts be populated
// before calling Command(). This makes Command() and subsequent calls to
// Run() or CombinedOutput() always return success and empty output.
DisableScripts bool
mu sync.Mutex
}
var _ exec.Interface = &FakeExec{}
@@ -44,18 +46,15 @@ var _ exec.Interface = &FakeExec{}
// FakeCommandAction is the function to be executed
type FakeCommandAction func(cmd string, args ...string) exec.Cmd
// Command is to track the commands that are executed
// Command returns the next unexecuted command in CommandScripts.
// This function is safe for concurrent access as long as the underlying
// FakeExec struct is not modified during execution.
func (fake *FakeExec) Command(cmd string, args ...string) exec.Cmd {
if fake.DisableScripts {
fakeCmd := &FakeCmd{DisableScripts: true}
return InitFakeCmd(fakeCmd, cmd, args...)
}
if fake.CommandCalls > len(fake.CommandScript)-1 {
panic(fmt.Sprintf("ran out of Command() actions. Could not handle command [%d]: %s args: %v", fake.CommandCalls, cmd, args))
}
i := fake.CommandCalls
fake.CommandCalls++
fakeCmd := fake.CommandScript[i](cmd, args...)
fakeCmd := fake.nextCommand(cmd, args)
if fake.ExactOrder {
argv := append([]string{cmd}, args...)
fc := fakeCmd.(*FakeCmd)
@@ -74,6 +73,18 @@ func (fake *FakeExec) Command(cmd string, args ...string) exec.Cmd {
return fakeCmd
}
func (fake *FakeExec) nextCommand(cmd string, args []string) exec.Cmd {
fake.mu.Lock()
defer fake.mu.Unlock()
if fake.CommandCalls > len(fake.CommandScript)-1 {
panic(fmt.Sprintf("ran out of Command() actions. Could not handle command [%d]: %s args: %v", fake.CommandCalls, cmd, args))
}
i := fake.CommandCalls
fake.CommandCalls++
return fake.CommandScript[i](cmd, args...)
}
// CommandContext wraps arguments into exec.Cmd
func (fake *FakeExec) CommandContext(ctx context.Context, cmd string, args ...string) exec.Cmd {
return fake.Command(cmd, args...)

181
vendor/k8s.io/utils/net/ipfamily.go generated vendored Normal file
View File

@@ -0,0 +1,181 @@
/*
Copyright 2018 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 net
import (
"fmt"
"net"
)
// IPFamily refers to a specific family if not empty, i.e. "4" or "6".
type IPFamily string
// Constants for valid IPFamilys:
const (
IPFamilyUnknown IPFamily = ""
IPv4 IPFamily = "4"
IPv6 IPFamily = "6"
)
// IsDualStackIPs returns true if:
// - all elements of ips are valid
// - at least one IP from each family (v4 and v6) is present
func IsDualStackIPs(ips []net.IP) (bool, error) {
v4Found := false
v6Found := false
for i, ip := range ips {
switch IPFamilyOf(ip) {
case IPv4:
v4Found = true
case IPv6:
v6Found = true
default:
return false, fmt.Errorf("invalid IP[%d]: %v", i, ip)
}
}
return (v4Found && v6Found), nil
}
// IsDualStackIPStrings returns true if:
// - all elements of ips can be parsed as IPs
// - at least one IP from each family (v4 and v6) is present
func IsDualStackIPStrings(ips []string) (bool, error) {
parsedIPs := make([]net.IP, 0, len(ips))
for i, ip := range ips {
parsedIP := ParseIPSloppy(ip)
if parsedIP == nil {
return false, fmt.Errorf("invalid IP[%d]: %v", i, ip)
}
parsedIPs = append(parsedIPs, parsedIP)
}
return IsDualStackIPs(parsedIPs)
}
// IsDualStackCIDRs returns true if:
// - all elements of cidrs are non-nil
// - at least one CIDR from each family (v4 and v6) is present
func IsDualStackCIDRs(cidrs []*net.IPNet) (bool, error) {
v4Found := false
v6Found := false
for i, cidr := range cidrs {
switch IPFamilyOfCIDR(cidr) {
case IPv4:
v4Found = true
case IPv6:
v6Found = true
default:
return false, fmt.Errorf("invalid CIDR[%d]: %v", i, cidr)
}
}
return (v4Found && v6Found), nil
}
// IsDualStackCIDRStrings returns if
// - all elements of cidrs can be parsed as CIDRs
// - at least one CIDR from each family (v4 and v6) is present
func IsDualStackCIDRStrings(cidrs []string) (bool, error) {
parsedCIDRs, err := ParseCIDRs(cidrs)
if err != nil {
return false, err
}
return IsDualStackCIDRs(parsedCIDRs)
}
// IPFamilyOf returns the IP family of ip, or IPFamilyUnknown if it is invalid.
func IPFamilyOf(ip net.IP) IPFamily {
switch {
case ip.To4() != nil:
return IPv4
case ip.To16() != nil:
return IPv6
default:
return IPFamilyUnknown
}
}
// IPFamilyOfString returns the IP family of ip, or IPFamilyUnknown if ip cannot
// be parsed as an IP.
func IPFamilyOfString(ip string) IPFamily {
return IPFamilyOf(ParseIPSloppy(ip))
}
// IPFamilyOfCIDR returns the IP family of cidr.
func IPFamilyOfCIDR(cidr *net.IPNet) IPFamily {
if cidr == nil {
return IPFamilyUnknown
}
return IPFamilyOf(cidr.IP)
}
// IPFamilyOfCIDRString returns the IP family of cidr.
func IPFamilyOfCIDRString(cidr string) IPFamily {
ip, _, _ := ParseCIDRSloppy(cidr)
return IPFamilyOf(ip)
}
// IsIPv6 returns true if netIP is IPv6 (and false if it is IPv4, nil, or invalid).
func IsIPv6(netIP net.IP) bool {
return IPFamilyOf(netIP) == IPv6
}
// IsIPv6String returns true if ip contains a single IPv6 address and nothing else. It
// returns false if ip is an empty string, an IPv4 address, or anything else that is not a
// single IPv6 address.
func IsIPv6String(ip string) bool {
return IPFamilyOfString(ip) == IPv6
}
// IsIPv6CIDR returns true if a cidr is a valid IPv6 CIDR. It returns false if cidr is
// nil or an IPv4 CIDR. Its behavior is not defined if cidr is invalid.
func IsIPv6CIDR(cidr *net.IPNet) bool {
return IPFamilyOfCIDR(cidr) == IPv6
}
// IsIPv6CIDRString returns true if cidr contains a single IPv6 CIDR and nothing else. It
// returns false if cidr is an empty string, an IPv4 CIDR, or anything else that is not a
// single valid IPv6 CIDR.
func IsIPv6CIDRString(cidr string) bool {
return IPFamilyOfCIDRString(cidr) == IPv6
}
// IsIPv4 returns true if netIP is IPv4 (and false if it is IPv6, nil, or invalid).
func IsIPv4(netIP net.IP) bool {
return IPFamilyOf(netIP) == IPv4
}
// IsIPv4String returns true if ip contains a single IPv4 address and nothing else. It
// returns false if ip is an empty string, an IPv6 address, or anything else that is not a
// single IPv4 address.
func IsIPv4String(ip string) bool {
return IPFamilyOfString(ip) == IPv4
}
// IsIPv4CIDR returns true if cidr is a valid IPv4 CIDR. It returns false if cidr is nil
// or an IPv6 CIDR. Its behavior is not defined if cidr is invalid.
func IsIPv4CIDR(cidr *net.IPNet) bool {
return IPFamilyOfCIDR(cidr) == IPv4
}
// IsIPv4CIDRString returns true if cidr contains a single IPv4 CIDR and nothing else. It
// returns false if cidr is an empty string, an IPv6 CIDR, or anything else that is not a
// single valid IPv4 CIDR.
func IsIPv4CIDRString(cidr string) bool {
return IPFamilyOfCIDRString(cidr) == IPv4
}

126
vendor/k8s.io/utils/net/net.go generated vendored
View File

@@ -29,138 +29,16 @@ import (
// order is maintained
func ParseCIDRs(cidrsString []string) ([]*net.IPNet, error) {
cidrs := make([]*net.IPNet, 0, len(cidrsString))
for _, cidrString := range cidrsString {
for i, cidrString := range cidrsString {
_, cidr, err := ParseCIDRSloppy(cidrString)
if err != nil {
return nil, fmt.Errorf("failed to parse cidr value:%q with error:%v", cidrString, err)
return nil, fmt.Errorf("invalid CIDR[%d]: %v (%v)", i, cidr, err)
}
cidrs = append(cidrs, cidr)
}
return cidrs, nil
}
// IsDualStackIPs returns if a slice of ips is:
// - all are valid ips
// - at least one ip from each family (v4 or v6)
func IsDualStackIPs(ips []net.IP) (bool, error) {
v4Found := false
v6Found := false
for _, ip := range ips {
if ip == nil {
return false, fmt.Errorf("ip %v is invalid", ip)
}
if v4Found && v6Found {
continue
}
if IsIPv6(ip) {
v6Found = true
continue
}
v4Found = true
}
return (v4Found && v6Found), nil
}
// IsDualStackIPStrings returns if
// - all are valid ips
// - at least one ip from each family (v4 or v6)
func IsDualStackIPStrings(ips []string) (bool, error) {
parsedIPs := make([]net.IP, 0, len(ips))
for _, ip := range ips {
parsedIP := ParseIPSloppy(ip)
parsedIPs = append(parsedIPs, parsedIP)
}
return IsDualStackIPs(parsedIPs)
}
// IsDualStackCIDRs returns if
// - all are valid cidrs
// - at least one cidr from each family (v4 or v6)
func IsDualStackCIDRs(cidrs []*net.IPNet) (bool, error) {
v4Found := false
v6Found := false
for _, cidr := range cidrs {
if cidr == nil {
return false, fmt.Errorf("cidr %v is invalid", cidr)
}
if v4Found && v6Found {
continue
}
if IsIPv6(cidr.IP) {
v6Found = true
continue
}
v4Found = true
}
return v4Found && v6Found, nil
}
// IsDualStackCIDRStrings returns if
// - all are valid cidrs
// - at least one cidr from each family (v4 or v6)
func IsDualStackCIDRStrings(cidrs []string) (bool, error) {
parsedCIDRs, err := ParseCIDRs(cidrs)
if err != nil {
return false, err
}
return IsDualStackCIDRs(parsedCIDRs)
}
// IsIPv6 returns if netIP is IPv6.
func IsIPv6(netIP net.IP) bool {
return netIP != nil && netIP.To4() == nil
}
// IsIPv6String returns if ip is IPv6.
func IsIPv6String(ip string) bool {
netIP := ParseIPSloppy(ip)
return IsIPv6(netIP)
}
// IsIPv6CIDRString returns if cidr is IPv6.
// This assumes cidr is a valid CIDR.
func IsIPv6CIDRString(cidr string) bool {
ip, _, _ := ParseCIDRSloppy(cidr)
return IsIPv6(ip)
}
// IsIPv6CIDR returns if a cidr is ipv6
func IsIPv6CIDR(cidr *net.IPNet) bool {
ip := cidr.IP
return IsIPv6(ip)
}
// IsIPv4 returns if netIP is IPv4.
func IsIPv4(netIP net.IP) bool {
return netIP != nil && netIP.To4() != nil
}
// IsIPv4String returns if ip is IPv4.
func IsIPv4String(ip string) bool {
netIP := ParseIPSloppy(ip)
return IsIPv4(netIP)
}
// IsIPv4CIDR returns if a cidr is ipv4
func IsIPv4CIDR(cidr *net.IPNet) bool {
ip := cidr.IP
return IsIPv4(ip)
}
// IsIPv4CIDRString returns if cidr is IPv4.
// This assumes cidr is a valid CIDR.
func IsIPv4CIDRString(cidr string) bool {
ip, _, _ := ParseCIDRSloppy(cidr)
return IsIPv4(ip)
}
// ParsePort parses a string representing an IP port. If the string is not a
// valid port number, this returns an error.
func ParsePort(port string, allowZero bool) (int, error) {

18
vendor/k8s.io/utils/net/port.go generated vendored
View File

@@ -23,15 +23,6 @@ import (
"strings"
)
// IPFamily refers to a specific family if not empty, i.e. "4" or "6".
type IPFamily string
// Constants for valid IPFamilys:
const (
IPv4 IPFamily = "4"
IPv6 = "6"
)
// Protocol is a network protocol support by LocalPort.
type Protocol string
@@ -67,7 +58,7 @@ func NewLocalPort(desc, ip string, ipFamily IPFamily, port int, protocol Protoco
if protocol != TCP && protocol != UDP {
return nil, fmt.Errorf("Unsupported protocol %s", protocol)
}
if ipFamily != "" && ipFamily != "4" && ipFamily != "6" {
if ipFamily != IPFamilyUnknown && ipFamily != IPv4 && ipFamily != IPv6 {
return nil, fmt.Errorf("Invalid IP family %s", ipFamily)
}
if ip != "" {
@@ -75,9 +66,10 @@ func NewLocalPort(desc, ip string, ipFamily IPFamily, port int, protocol Protoco
if parsedIP == nil {
return nil, fmt.Errorf("invalid ip address %s", ip)
}
asIPv4 := parsedIP.To4()
if asIPv4 == nil && ipFamily == IPv4 || asIPv4 != nil && ipFamily == IPv6 {
return nil, fmt.Errorf("ip address and family mismatch %s, %s", ip, ipFamily)
if ipFamily != IPFamilyUnknown {
if IPFamily(parsedIP) != ipFamily {
return nil, fmt.Errorf("ip address and family mismatch %s, %s", ip, ipFamily)
}
}
}
return &LocalPort{Description: desc, IP: ip, IPFamily: ipFamily, Port: port, Protocol: protocol}, nil

2
vendor/modules.txt vendored
View File

@@ -2390,7 +2390,7 @@ k8s.io/sample-apiserver/pkg/registry/wardle/flunder
# k8s.io/system-validators v1.8.0
## explicit; go 1.16
k8s.io/system-validators/validators
# k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
# k8s.io/utils v0.0.0-20230202215443-34013725500c
## explicit; go 1.18
k8s.io/utils/buffer
k8s.io/utils/clock