From 04a86f9d5881213dafd1cb66674a045229d216fa Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 13 Jan 2023 14:49:43 +0100 Subject: [PATCH 1/2] gofmt files Signed-off-by: Sebastiaan van Stijn --- constants.go | 1 + ipvs.go | 2 +- ipvs_test.go | 7 ------- netlink.go | 33 +++++++++++++++------------------ 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/constants.go b/constants.go index c0dced1..4f17245 100644 --- a/constants.go +++ b/constants.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package ipvs diff --git a/ipvs.go b/ipvs.go index 61b6f0a..a1aeaf5 100644 --- a/ipvs.go +++ b/ipvs.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package ipvs @@ -181,7 +182,6 @@ func (i *Handle) GetDestinations(s *Service) ([]*Destination, error) { // GetService gets details of a specific IPVS services, useful in updating statisics etc., func (i *Handle) GetService(s *Service) (*Service, error) { - res, err := i.doGetServicesCmd(s) if err != nil { return nil, err diff --git a/ipvs_test.go b/ipvs_test.go index f06760e..19728a0 100644 --- a/ipvs_test.go +++ b/ipvs_test.go @@ -47,7 +47,6 @@ var ( ) func lookupFwMethod(fwMethod uint32) string { - switch fwMethod { case ConnectionFlagMasq: return fwdMethodStrings[0] @@ -79,7 +78,6 @@ func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, check switch checkPresent { case true: // The test expects the service to be present if !dstFound { - t.Fatalf("Did not find the service %s in ipvs output", d.Address.String()) } case false: // The test expects that the service should not be present @@ -87,11 +85,9 @@ func checkDestination(t *testing.T, i *Handle, s *Service, d *Destination, check t.Fatalf("Did not find the destination %s fwdMethod %s in ipvs output", d.Address.String(), lookupFwMethod(d.ConnectionFlags)) } } - } func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) { - svcArray, err := i.GetServices() if err != nil { t.Fatalf("Failed to get service; %v", err) @@ -100,7 +96,6 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) { var svcFound bool for _, svc := range svcArray { - if svc.Protocol == s.Protocol && svc.Address.String() == s.Address.String() && svc.Port == s.Port { svcFound = true break @@ -110,7 +105,6 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) { switch checkPresent { case true: // The test expects the service to be present if !svcFound { - t.Fatalf("Did not find the service %s in ipvs output", s.Address.String()) } case false: // The test expects that the service should not be present @@ -118,7 +112,6 @@ func checkService(t *testing.T, i *Handle, s *Service, checkPresent bool) { t.Fatalf("Did not expect the service %s in ipvs output", s.Address.String()) } } - } func TestGetFamily(t *testing.T) { diff --git a/netlink.go b/netlink.go index 534c6f5..d5a53d9 100644 --- a/netlink.go +++ b/netlink.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package ipvs @@ -124,8 +125,8 @@ func (i *Handle) doCmdwithResponse(s *Service, d *Destination, cmd uint8) ([][]b req.Seq = atomic.AddUint32(&i.seq, 1) if s == nil { - req.Flags |= syscall.NLM_F_DUMP //Flag to dump all messages - req.AddData(nl.NewRtAttr(ipvsCmdAttrService, nil)) //Add a dummy attribute + req.Flags |= syscall.NLM_F_DUMP // Flag to dump all messages + req.AddData(nl.NewRtAttr(ipvsCmdAttrService, nil)) // Add a dummy attribute } else { req.AddData(fillService(s)) } @@ -134,7 +135,6 @@ func (i *Handle) doCmdwithResponse(s *Service, d *Destination, cmd uint8) ([][]b if cmd == ipvsCmdGetDest { req.Flags |= syscall.NLM_F_DUMP } - } else { req.AddData(fillDestination(d)) } @@ -259,7 +259,6 @@ done: } func parseIP(ip []byte, family uint16) (net.IP, error) { - var resIP net.IP switch family { @@ -276,7 +275,6 @@ func parseIP(ip []byte, family uint16) (net.IP, error) { // parseStats func assembleStats(msg []byte) (SvcStats, error) { - var s SvcStats attrs, err := nl.ParseRouteAttr(msg) @@ -314,7 +312,6 @@ func assembleStats(msg []byte) (SvcStats, error) { // assembleService assembles a services back from a hain of netlink attributes func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) { - var s Service var addressBytes []byte @@ -366,10 +363,9 @@ func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) { // parseService given a ipvs netlink response this function will respond with a valid service entry, an error otherwise func (i *Handle) parseService(msg []byte) (*Service, error) { - var s *Service - //Remove General header for this message and parse the NetLink message + // Remove General header for this message and parse the NetLink message hdr := deserializeGenlMsg(msg) NetLinkAttrs, err := nl.ParseRouteAttr(msg[hdr.Len():]) if err != nil { @@ -379,13 +375,13 @@ func (i *Handle) parseService(msg []byte) (*Service, error) { return nil, fmt.Errorf("error no valid netlink message found while parsing service record") } - //Now Parse and get IPVS related attributes messages packed in this message. + // Now Parse and get IPVS related attributes messages packed in this message. ipvsAttrs, err := nl.ParseRouteAttr(NetLinkAttrs[0].Value) if err != nil { return nil, err } - //Assemble all the IPVS related attribute messages and create a service record + // Assemble all the IPVS related attribute messages and create a service record s, err = assembleService(ipvsAttrs) if err != nil { return nil, err @@ -422,7 +418,6 @@ func (i *Handle) doCmdWithoutAttr(cmd uint8) ([][]byte, error) { } func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) { - var d Destination var addressBytes []byte @@ -486,9 +481,12 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) // getIPFamily parses the IP family based on raw data from netlink. // For AF_INET, netlink will set the first 4 bytes with trailing zeros -// 10.0.0.1 -> [10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] +// +// 10.0.0.1 -> [10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] +// // For AF_INET6, the full 16 byte array is used: -// 2001:db8:3c4d:15::1a00 -> [32 1 13 184 60 77 0 21 0 0 0 0 0 0 26 0] +// +// 2001:db8:3c4d:15::1a00 -> [32 1 13 184 60 77 0 21 0 0 0 0 0 0 26 0] func getIPFamily(address []byte) (uint16, error) { if len(address) == 4 { return syscall.AF_INET, nil @@ -519,7 +517,7 @@ func isZeros(b []byte) bool { func (i *Handle) parseDestination(msg []byte) (*Destination, error) { var dst *Destination - //Remove General header for this message + // Remove General header for this message hdr := deserializeGenlMsg(msg) NetLinkAttrs, err := nl.ParseRouteAttr(msg[hdr.Len():]) if err != nil { @@ -529,13 +527,13 @@ func (i *Handle) parseDestination(msg []byte) (*Destination, error) { return nil, fmt.Errorf("error no valid netlink message found while parsing destination record") } - //Now Parse and get IPVS related attributes messages packed in this message. + // Now Parse and get IPVS related attributes messages packed in this message. ipvsAttrs, err := nl.ParseRouteAttr(NetLinkAttrs[0].Value) if err != nil { return nil, err } - //Assemble netlink attributes and create a Destination record + // Assemble netlink attributes and create a Destination record dst, err = assembleDestination(ipvsAttrs) if err != nil { return nil, err @@ -546,7 +544,6 @@ func (i *Handle) parseDestination(msg []byte) (*Destination, error) { // doGetDestinationsCmd a wrapper function to be used by GetDestinations and GetDestination(d) apis func (i *Handle) doGetDestinationsCmd(s *Service, d *Destination) ([]*Destination, error) { - var res []*Destination msgs, err := i.doCmdwithResponse(s, d, ipvsCmdGetDest) @@ -568,7 +565,7 @@ func (i *Handle) doGetDestinationsCmd(s *Service, d *Destination) ([]*Destinatio func (i *Handle) parseConfig(msg []byte) (*Config, error) { var c Config - //Remove General header for this message + // Remove General header for this message hdr := deserializeGenlMsg(msg) attrs, err := nl.ParseRouteAttr(msg[hdr.Len():]) if err != nil { From 2f31a04506c6ca085bb33a0b0409d41a39808a67 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 12 Jan 2023 14:55:16 +0100 Subject: [PATCH 2/2] rename linux-only files to _linux, and remove build-tag comments Signed-off-by: Sebastiaan van Stijn --- constants.go => constants_linux.go | 3 --- ipvs.go => ipvs_linux.go | 3 --- ipvs_test.go => ipvs_linux_test.go | 3 --- netlink.go => netlink_linux.go | 3 --- netlink_test.go => netlink_linux_test.go | 3 --- 5 files changed, 15 deletions(-) rename constants.go => constants_linux.go (99%) rename ipvs.go => ipvs_linux.go (99%) rename ipvs_test.go => ipvs_linux_test.go (99%) rename netlink.go => netlink_linux.go (99%) rename netlink_test.go => netlink_linux_test.go (97%) diff --git a/constants.go b/constants_linux.go similarity index 99% rename from constants.go rename to constants_linux.go index 4f17245..0e0561d 100644 --- a/constants.go +++ b/constants_linux.go @@ -1,6 +1,3 @@ -//go:build linux -// +build linux - package ipvs const ( diff --git a/ipvs.go b/ipvs_linux.go similarity index 99% rename from ipvs.go rename to ipvs_linux.go index a1aeaf5..686e746 100644 --- a/ipvs.go +++ b/ipvs_linux.go @@ -1,6 +1,3 @@ -//go:build linux -// +build linux - package ipvs import ( diff --git a/ipvs_test.go b/ipvs_linux_test.go similarity index 99% rename from ipvs_test.go rename to ipvs_linux_test.go index 19728a0..3f9ef99 100644 --- a/ipvs_test.go +++ b/ipvs_linux_test.go @@ -1,6 +1,3 @@ -//go:build linux -// +build linux - package ipvs import ( diff --git a/netlink.go b/netlink_linux.go similarity index 99% rename from netlink.go rename to netlink_linux.go index d5a53d9..711d805 100644 --- a/netlink.go +++ b/netlink_linux.go @@ -1,6 +1,3 @@ -//go:build linux -// +build linux - package ipvs import ( diff --git a/netlink_test.go b/netlink_linux_test.go similarity index 97% rename from netlink_test.go rename to netlink_linux_test.go index 728dde4..0dc3fc3 100644 --- a/netlink_test.go +++ b/netlink_linux_test.go @@ -1,6 +1,3 @@ -//go:build linux -// +build linux - package ipvs import (