Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error log not printed nearby in util package #4574

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/util/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
func ArpResolve(nic, dstIP string, timeout time.Duration, maxRetry int, done chan struct{}) (net.HardwareAddr, int, error) {
target, err := netip.ParseAddr(dstIP)
if err != nil {
klog.Error(err)
return nil, 0, fmt.Errorf("failed to parse target address %s: %w", dstIP, err)
}

Expand Down Expand Up @@ -47,6 +48,7 @@ func ArpResolve(nic, dstIP string, timeout time.Duration, maxRetry int, done cha
}
}
if err != nil {
klog.Error(err)
return nil, count, fmt.Errorf("failed to get interface %s: %w", nic, err)
}

Expand All @@ -65,6 +67,7 @@ func ArpResolve(nic, dstIP string, timeout time.Duration, maxRetry int, done cha
}
}
if err != nil {
klog.Error(err)
return nil, count, fmt.Errorf("failed to set up ARP client: %w", err)
}

Expand Down Expand Up @@ -115,6 +118,7 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr

tpa, err := netip.ParseAddr(ip)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("failed to parse IP address %s: %w", ip, err)
}
ip = tpa.String()
Expand Down Expand Up @@ -166,6 +170,7 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
}

if readErr = client.SetReadDeadline(deadline); readErr != nil {
klog.Error(readErr)
return
}

Expand All @@ -177,6 +182,7 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
return
}
}
klog.Error(err)
readErr = err
return
}
Expand Down Expand Up @@ -213,9 +219,11 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
}

if err = client.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
klog.Error(err)
return nil, err
}
if err = client.WriteTo(pkt, dstMac); err != nil {
klog.Error(err)
return nil, err
}
}
Expand All @@ -233,6 +241,7 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
// except that now the sender and target IP addresses are both
// set to the host's newly selected IPv4 address.
if err = AnnounceArpAddress(nic, ip, mac, announceNum, announceInterval); err != nil {
klog.Error(err)
return nil, err
}

Expand Down Expand Up @@ -270,9 +279,11 @@ func AnnounceArpAddress(nic, ip string, mac net.HardwareAddr, announceNum int, a
for i := 0; i < announceNum; i++ {
c := time.NewTimer(announceInterval)
if err = client.SetDeadline(time.Now().Add(announceInterval)); err != nil {
klog.Error(err)
return err
}
if err = client.WriteTo(pkt, dstMac); err != nil {
klog.Error(err)
return err
}
if i == announceNum-1 {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
func DialTCP(host string, timeout time.Duration, verbose bool) error {
u, err := url.Parse(host)
if err != nil {
klog.Error(err)
return fmt.Errorf("failed to parse host %q: %w", host, err)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/util/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ package util
import (
"fmt"

"k8s.io/klog/v2"

"github.com/vishvananda/netlink"
)

// SetLinkUp sets a link up
func SetLinkUp(name string) error {
link, err := netlink.LinkByName(name)
if err != nil {
klog.Error(err)
return fmt.Errorf("failed to get link %s: %w", name, err)
}
if err = netlink.LinkSetUp(link); err != nil {
klog.Error(err)
return fmt.Errorf("failed to set link %s up: %w", name, err)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func SubnetBroadcast(subnet string) string {
func FirstIP(subnet string) (string, error) {
_, cidr, err := net.ParseCIDR(subnet)
if err != nil {
klog.Error(err)
return "", fmt.Errorf("%s is not a valid cidr", subnet)
}
// Handle ptp network case specially
Expand All @@ -107,6 +108,7 @@ func FirstIP(subnet string) (string, error) {
func LastIP(subnet string) (string, error) {
_, cidr, err := net.ParseCIDR(subnet)
if err != nil {
klog.Error(err)
return "", fmt.Errorf("%s is not a valid cidr", subnet)
}

Expand Down Expand Up @@ -244,6 +246,7 @@ func IsValidIP(ip string) bool {
func CheckCidrs(cidr string) error {
for _, cidrBlock := range strings.Split(cidr, ",") {
if _, _, err := net.ParseCIDR(cidrBlock); err != nil {
klog.Error(err)
return errors.New("CIDRInvalid")
}
}
Expand All @@ -255,6 +258,7 @@ func GetGwByCidr(cidrStr string) (string, error) {
for _, cidr := range strings.Split(cidrStr, ",") {
gw, err := FirstIP(cidr)
if err != nil {
klog.Error(err)
return "", err
}
gws = append(gws, gw)
Expand All @@ -272,6 +276,7 @@ func AppendGwByCidr(gateway, cidrStr string) (string, error) {
}
gw, err := FirstIP(cidr)
if err != nil {
klog.Error(err)
return "", err
}
var gwArray [2]string
Expand Down Expand Up @@ -536,6 +541,7 @@ func CIDRGlobalUnicast(cidr string) error {
func CheckSystemCIDR(cidrs []string) error {
for i, cidr := range cidrs {
if err := CIDRGlobalUnicast(cidr); err != nil {
klog.Error(err)
return err
}
for j, nextCidr := range cidrs {
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ func bool2PsParam(v bool) string {
func Powershell(cmd string) (string, error) {
shell, err := ps.New(&backend.Local{})
if err != nil {
klog.Error(err)
return "", err
}
defer shell.Exit()

stdout, _, err := shell.Execute(cmd)
if err != nil {
klog.Error(err)
return stdout, err
}
return stdout, nil
Expand Down Expand Up @@ -222,6 +224,7 @@ func GetInterfaceByIP(ip string) (*NetIPInterface, error) {

interfaces, err := GetNetIPInterface(ipAddr.InterfaceIndex)
if err != nil {
klog.Error(err)
return nil, err
}
for _, iface := range interfaces {
Expand Down
7 changes: 7 additions & 0 deletions pkg/util/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/klog/v2"
)

func GenerateStrategicMergePatchPayload(original, modified runtime.Object) ([]byte, error) {
originalJSON, err := json.Marshal(original)
if err != nil {
klog.Error(err)
return nil, err
}

modifiedJSON, err := json.Marshal(modified)
if err != nil {
klog.Error(err)
return nil, err
}

data, err := createStrategicMergePatch(originalJSON, modifiedJSON, modified)
if err != nil {
klog.Error(err)
return nil, err
}
return data, nil
Expand All @@ -32,16 +36,19 @@ func createStrategicMergePatch(originalJSON, modifiedJSON []byte, dataStruct int
func GenerateMergePatchPayload(original, modified runtime.Object) ([]byte, error) {
originalJSON, err := json.Marshal(original)
if err != nil {
klog.Error(err)
return nil, err
}

modifiedJSON, err := json.Marshal(modified)
if err != nil {
klog.Error(err)
return nil, err
}

data, err := createMergePatch(originalJSON, modifiedJSON, modified)
if err != nil {
klog.Error(err)
return nil, err
}
return data, nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error {
return fmt.Errorf("gateway %s is not in cidr %s", subnet.Spec.Gateway, subnet.Spec.CIDRBlock)
}
if err := ValidateNetworkBroadcast(subnet.Spec.CIDRBlock, subnet.Spec.Gateway); err != nil {
klog.Error(err)
return fmt.Errorf("validate gateway %s for cidr %s failed: %w", subnet.Spec.Gateway, subnet.Spec.CIDRBlock, err)
}
}
Expand Down Expand Up @@ -101,6 +102,7 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error {
return err
}
if _, _, err := net.ParseCIDR(cidr); err != nil {
klog.Error(err)
return fmt.Errorf("%s in allowSubnets is not a valid address", cidr)
}
}
Expand Down Expand Up @@ -169,6 +171,7 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error {

if len(subnet.Spec.NatOutgoingPolicyRules) != 0 {
if err := validateNatOutgoingPolicyRules(subnet); err != nil {
klog.Error(err)
return err
}
}
Expand Down Expand Up @@ -197,11 +200,13 @@ func validateNatOutgoingPolicyRules(subnet kubeovnv1.Subnet) error {

if rule.Match.SrcIPs != "" {
if srcProtocol, err = validateNatOutGoingPolicyRuleIPs(rule.Match.SrcIPs); err != nil {
klog.Error(err)
return fmt.Errorf("validate nat policy rules src ips %s failed with err %w", rule.Match.SrcIPs, err)
}
}
if rule.Match.DstIPs != "" {
if dstProtocol, err = validateNatOutGoingPolicyRuleIPs(rule.Match.DstIPs); err != nil {
klog.Error(err)
return fmt.Errorf("validate nat policy rules dst ips %s failed with err %w", rule.Match.DstIPs, err)
}
}
Expand Down Expand Up @@ -258,6 +263,7 @@ func ValidatePodNetwork(annotations map[string]string) error {
for _, ip := range strings.Split(ipAddress, ",") {
if strings.Contains(ip, "/") {
if _, _, err := net.ParseCIDR(ip); err != nil {
klog.Error(err)
errors = append(errors, fmt.Errorf("%s is not a valid %s", ip, IPAddressAnnotation))
continue
}
Expand All @@ -270,6 +276,7 @@ func ValidatePodNetwork(annotations map[string]string) error {

if cidrStr := annotations[CidrAnnotation]; cidrStr != "" {
if err := CheckCidrs(cidrStr); err != nil {
klog.Error(err)
errors = append(errors, fmt.Errorf("invalid cidr %s", cidrStr))
continue
}
Expand All @@ -285,6 +292,7 @@ func ValidatePodNetwork(annotations map[string]string) error {
mac := annotations[MacAddressAnnotation]
if mac != "" {
if _, err := net.ParseMAC(mac); err != nil {
klog.Error(err)
errors = append(errors, fmt.Errorf("%s is not a valid %s", mac, MacAddressAnnotation))
}
}
Expand Down Expand Up @@ -324,13 +332,15 @@ func ValidatePodNetwork(annotations map[string]string) error {
ingress := annotations[IngressRateAnnotation]
if ingress != "" {
if _, err := strconv.Atoi(ingress); err != nil {
klog.Error(err)
errors = append(errors, fmt.Errorf("%s is not a valid %s", ingress, IngressRateAnnotation))
}
}

egress := annotations[EgressRateAnnotation]
if egress != "" {
if _, err := strconv.Atoi(egress); err != nil {
klog.Error(err)
errors = append(errors, fmt.Errorf("%s is not a valid %s", egress, EgressRateAnnotation))
}
}
Expand Down Expand Up @@ -389,6 +399,7 @@ func ValidateVpc(vpc *kubeovnv1.Vpc) error {

if strings.Contains(item.CIDR, "/") {
if _, _, err := net.ParseCIDR(item.CIDR); err != nil {
klog.Error(err)
return fmt.Errorf("invalid cidr %s: %w", item.CIDR, err)
}
} else if ip := net.ParseIP(item.CIDR); ip == nil {
Expand Down Expand Up @@ -419,6 +430,7 @@ func ValidateVpc(vpc *kubeovnv1.Vpc) error {

for _, item := range vpc.Spec.VpcPeerings {
if err := CheckCidrs(item.LocalConnectIP); err != nil {
klog.Error(err)
return fmt.Errorf("invalid cidr %s", item.LocalConnectIP)
}
}
Expand Down
Loading