From 98d0d7eeb67d49773ebf610c627192f7940a7846 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Fri, 15 Nov 2024 08:55:51 -0500 Subject: [PATCH] Address perfsprint linter violations "fmt.Errorf can be replaced with errors.New" "fmt.Sprintf can be replaced with string concatenation" "fmt.Sprintf can be replaced with faster strconv.Itoa" Signed-off-by: Tom Pantelis --- pkg/apis/submariner.io/v1/endpoint.go | 4 ++-- pkg/cable/wireguard/driver.go | 4 ++-- pkg/globalnet/controllers/egress_pod_watcher.go | 4 +--- pkg/ipset/ipset.go | 3 ++- pkg/netlink/netlink.go | 4 ++-- pkg/routeagent_driver/handlers/ovn/utils.go | 2 +- pkg/routeagent_driver/handlers/ovn/vsctl/vsctl.go | 7 ++++--- pkg/versions/version.go | 9 ++++----- test/e2e/framework/dataplane.go | 4 ++-- test/external/dataplane/gn_connectivity.go | 4 ++-- 10 files changed, 22 insertions(+), 23 deletions(-) diff --git a/pkg/apis/submariner.io/v1/endpoint.go b/pkg/apis/submariner.io/v1/endpoint.go index db4d2ed8f..ff50c18f8 100644 --- a/pkg/apis/submariner.io/v1/endpoint.go +++ b/pkg/apis/submariner.io/v1/endpoint.go @@ -68,11 +68,11 @@ func parsePort(port string) (int32, error) { func (ep *EndpointSpec) GenerateName() (string, error) { if ep.ClusterID == "" { - return "", fmt.Errorf("ClusterID cannot be empty") + return "", errors.New("ClusterID cannot be empty") } if ep.CableName == "" { - return "", fmt.Errorf("CableName cannot be empty") + return "", errors.New("CableName cannot be empty") } return resource.EnsureValidName(fmt.Sprintf("%s-%s", ep.ClusterID, ep.CableName)), nil diff --git a/pkg/cable/wireguard/driver.go b/pkg/cable/wireguard/driver.go index d3f497ccf..9578a0a71 100644 --- a/pkg/cable/wireguard/driver.go +++ b/pkg/cable/wireguard/driver.go @@ -104,7 +104,7 @@ func NewDriver(localEndpoint *endpoint.Local, _ *types.SubmarinerCluster) (cable // Create the controller. if w.client, err = wgctrl.New(); err != nil { if os.IsNotExist(err) { - return nil, fmt.Errorf("wgctrl is not available on this system") + return nil, errors.New("wgctrl is not available on this system") } return nil, errors.Wrap(err, "failed to open wgctl client") @@ -312,7 +312,7 @@ func (w *wireguard) ConnectToEndpoint(endpointInfo *natdiscovery.NATEndpointInfo func keyFromSpec(ep *v1.EndpointSpec) (*wgtypes.Key, error) { s, found := ep.BackendConfig[PublicKey] if !found { - return nil, fmt.Errorf("endpoint is missing public key") + return nil, errors.New("endpoint is missing public key") } key, err := wgtypes.ParseKey(s) diff --git a/pkg/globalnet/controllers/egress_pod_watcher.go b/pkg/globalnet/controllers/egress_pod_watcher.go index dfd08516c..b98697f7c 100644 --- a/pkg/globalnet/controllers/egress_pod_watcher.go +++ b/pkg/globalnet/controllers/egress_pod_watcher.go @@ -19,8 +19,6 @@ limitations under the License. package controllers import ( - "fmt" - "github.com/pkg/errors" "github.com/submariner-io/admiral/pkg/log" "github.com/submariner-io/admiral/pkg/watcher" @@ -53,7 +51,7 @@ func startEgressPodWatcher(name, namespace string, namedSet packetfilter.NamedSe Scheme: config.Scheme, ResourceConfigs: []watcher.ResourceConfig{ { - Name: fmt.Sprintf("Pod watcher %s", name), + Name: "Pod watcher " + name, ResourceType: &corev1.Pod{}, Handler: watcher.EventHandlerFuncs{ OnCreateFunc: pw.onCreateOrUpdate, diff --git a/pkg/ipset/ipset.go b/pkg/ipset/ipset.go index 09f365381..8b8130481 100644 --- a/pkg/ipset/ipset.go +++ b/pkg/ipset/ipset.go @@ -50,6 +50,7 @@ limitations under the License. package ipset import ( + "errors" "fmt" "os/exec" "regexp" @@ -384,7 +385,7 @@ func (runner *runner) ListSets() ([]string, error) { // ListEntries lists all the entries from a named set. func (runner *runner) ListEntries(set string) ([]string, error) { if set == "" { - return nil, fmt.Errorf("set name can't be empty") + return nil, errors.New("set name can't be empty") } out, err := runner.runWithOutput([]string{"list", set}, "error listing set %q", set) diff --git a/pkg/netlink/netlink.go b/pkg/netlink/netlink.go index 69c774528..5b6c92ad1 100644 --- a/pkg/netlink/netlink.go +++ b/pkg/netlink/netlink.go @@ -274,7 +274,7 @@ func GetDefaultGatewayInterface() (*net.Interface, error) { for i := range routes { if routes[i].Dst == nil || routes[i].Dst.String() == allZeroAddress { if routes[i].LinkIndex == 0 { - return nil, fmt.Errorf("default gateway interface could not be determined") + return nil, errors.New("default gateway interface could not be determined") } iface, err := net.InterfaceByIndex(routes[i].LinkIndex) @@ -286,7 +286,7 @@ func GetDefaultGatewayInterface() (*net.Interface, error) { } } - return nil, fmt.Errorf("unable to find default route") + return nil, errors.New("unable to find default route") } func DeleteIfaceAndAssociatedRoutes(iface string, tableID int) error { diff --git a/pkg/routeagent_driver/handlers/ovn/utils.go b/pkg/routeagent_driver/handlers/ovn/utils.go index 3f6c25431..ac5075a5d 100644 --- a/pkg/routeagent_driver/handlers/ovn/utils.go +++ b/pkg/routeagent_driver/handlers/ovn/utils.go @@ -60,7 +60,7 @@ func jsonToIP(jsonData string) (string, error) { ipStr, found := data["ipv4"] if !found { - return "", fmt.Errorf("json data does not contain an 'ipv4' field") + return "", errors.New("json data does not contain an 'ipv4' field") } ip, _, err := net.ParseCIDR(ipStr) diff --git a/pkg/routeagent_driver/handlers/ovn/vsctl/vsctl.go b/pkg/routeagent_driver/handlers/ovn/vsctl/vsctl.go index c4163f823..b24839745 100644 --- a/pkg/routeagent_driver/handlers/ovn/vsctl/vsctl.go +++ b/pkg/routeagent_driver/handlers/ovn/vsctl/vsctl.go @@ -22,6 +22,7 @@ import ( "bytes" "fmt" "os/exec" + "strconv" "strings" "github.com/pkg/errors" @@ -75,8 +76,8 @@ func DelBridge(bridgeName string) error { func AddInternalPort(bridgeName, portName, macAddress string, mtu int) error { _, err := vsctlCmd("--may-exist", "add-port", bridgeName, portName, "--", - "set", "interface", portName, "type=internal", "mtu_request="+fmt.Sprintf("%d", mtu), - fmt.Sprintf("mac=%s", strings.ReplaceAll(macAddress, ":", "\\:"))) + "set", "interface", portName, "type=internal", "mtu_request="+strconv.Itoa(mtu), + "mac="+strings.ReplaceAll(macAddress, ":", "\\:")) return err } @@ -109,7 +110,7 @@ func AddOVNBridgeMapping(netName, bridgeName string) error { } if _, err = vsctlCmd("set", "Open_vSwitch", ".", - fmt.Sprintf("external_ids:ovn-bridge-mappings=%s", bridgeMappings)); err != nil { + "external_ids:ovn-bridge-mappings="+bridgeMappings); err != nil { return errors.Wrap(err, "failed to set ovn-bridge-mappings") } diff --git a/pkg/versions/version.go b/pkg/versions/version.go index 7f1c039cc..ce3330f4a 100644 --- a/pkg/versions/version.go +++ b/pkg/versions/version.go @@ -19,7 +19,6 @@ limitations under the License. package versions import ( - "fmt" "runtime" "github.com/submariner-io/admiral/pkg/log" @@ -32,10 +31,10 @@ var ( ) func Log(logger *log.Logger) { - logger.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) - logger.Info(fmt.Sprintf("Go Arch: %s", runtime.GOARCH)) - logger.Info(fmt.Sprintf("Git Commit Hash: %s", gitCommitHash)) - logger.Info(fmt.Sprintf("Git Commit Date: %s", gitCommitDate)) + logger.Info("Go Version: " + runtime.Version()) + logger.Info("Go Arch: " + runtime.GOARCH) + logger.Info("Git Commit Hash: " + gitCommitHash) + logger.Info("Git Commit Date: " + gitCommitDate) } // Submariner returns the version info of submariner. diff --git a/test/e2e/framework/dataplane.go b/test/e2e/framework/dataplane.go index a28927a79..b82dd02bf 100644 --- a/test/e2e/framework/dataplane.go +++ b/test/e2e/framework/dataplane.go @@ -219,7 +219,7 @@ func getGlobalIngressIP(p tcp.ConnectivityTestParams, service *v1.Service) strin } else if p.ToEndpointType == tcp.GlobalPodIP { podList := p.Framework.AwaitPodsByLabelSelector(p.ToCluster, labels.Set(service.Spec.Selector).AsSelector().String(), service.Namespace, 1) - ingressIPName := fmt.Sprintf("pod-%s", podList.Items[0].Name) + ingressIPName := "pod-" + podList.Items[0].Name return p.Framework.AwaitGlobalIngressIP(p.ToCluster, ingressIPName, service.Namespace) } @@ -228,7 +228,7 @@ func getGlobalIngressIP(p tcp.ConnectivityTestParams, service *v1.Service) strin } func newGlobalEgressIPObj(namespace string, selector *metav1.LabelSelector) (*unstructured.Unstructured, error) { - geipName := fmt.Sprintf("test-e2e-egressip-%s", namespace) + geipName := "test-e2e-egressip-" + namespace egressIPSpec := &submarinerv1.GlobalEgressIP{ ObjectMeta: metav1.ObjectMeta{ Name: geipName, diff --git a/test/external/dataplane/gn_connectivity.go b/test/external/dataplane/gn_connectivity.go index 41e27aaae..83cdd62ad 100644 --- a/test/external/dataplane/gn_connectivity.go +++ b/test/external/dataplane/gn_connectivity.go @@ -412,7 +412,7 @@ func testGlobalNetExternalConnectivity(p testParams, g globalnetTestParams) { } func newGlobalEgressIPObj(namespace string, selector *metav1.LabelSelector) (*unstructured.Unstructured, error) { - geipName := fmt.Sprintf("test-e2e-egressip-%s", namespace) + geipName := "test-e2e-egressip-" + namespace egressIPSpec := &submarinerv1.GlobalEgressIP{ ObjectMeta: metav1.ObjectMeta{ Name: geipName, @@ -461,7 +461,7 @@ func getGlobalIngressIP(p testParams, service *v1.Service) string { case tcp.GlobalPodIP: podList := p.Framework.AwaitPodsByLabelSelector(p.Cluster, labels.Set(service.Spec.Selector).AsSelector().String(), service.Namespace, 1) - ingressIPName := fmt.Sprintf("pod-%s", podList.Items[0].Name) + ingressIPName := "pod-" + podList.Items[0].Name return p.Framework.AwaitGlobalIngressIP(p.Cluster, ingressIPName, service.Namespace) }