Skip to content

Commit 50bafd7

Browse files
authored
Reduce antrea cni binary size (#6038)
It only needs a dial function but imports many unnecessary code from `pkg/agent/util`. Removing the import reduces the binary size by 10MB, from 18MB to 8.4MB. Signed-off-by: Quan Tian <[email protected]>
1 parent d28e282 commit 50bafd7

File tree

7 files changed

+58
-26
lines changed

7 files changed

+58
-26
lines changed

pkg/agent/secondarynetwork/podwatch/sriov.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
podresourcesv1alpha1 "k8s.io/kubelet/pkg/apis/podresources/v1alpha1"
3434

3535
cnipodcache "antrea.io/antrea/pkg/agent/secondarynetwork/cnipodcache"
36-
"antrea.io/antrea/pkg/agent/util"
3736
)
3837

3938
const (
@@ -68,7 +67,7 @@ func getPodContainerDeviceIDs(podName string, podNamespace string) ([]string, er
6867
path.Join(kubeletPodResourcesPath, kubeletSocket),
6968
grpc.WithTransportCredentials(grpcinsecure.NewCredentials()),
7069
grpc.WithContextDialer(func(ctx context.Context, addr string) (conn net.Conn, e error) {
71-
return util.DialLocalSocket(addr)
70+
return net.Dial("unix", addr)
7271
}),
7372
)
7473
if err != nil {

pkg/agent/util/net.go

-4
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ func listenUnix(address string) (net.Listener, error) {
135135
return net.Listen("unix", address)
136136
}
137137

138-
func dialUnix(address string) (net.Conn, error) {
139-
return net.Dial("unix", address)
140-
}
141-
142138
// GetIPNetDeviceFromIP returns local IPs/masks and associated device from IP, and ignores the interfaces which have
143139
// names in the ignoredInterfaces.
144140
func GetIPNetDeviceFromIP(localIPs *ip.DualStackIPs, ignoredInterfaces sets.Set[string]) (v4IPNet *net.IPNet, v6IPNet *net.IPNet, iface *net.Interface, err error) {

pkg/agent/util/net_linux.go

-5
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,6 @@ func ListenLocalSocket(address string) (net.Listener, error) {
230230
return listener, nil
231231
}
232232

233-
// DialLocalSocket connects to a Unix domain socket.
234-
func DialLocalSocket(address string) (net.Conn, error) {
235-
return dialUnix(address)
236-
}
237-
238233
// SetAdapterMACAddress set specified MAC address on interface.
239234
func SetAdapterMACAddress(adapterName string, macConfig *net.HardwareAddr) error {
240235
link, err := netlinkUtil.LinkByName(adapterName)

pkg/agent/util/net_windows.go

-10
Original file line numberDiff line numberDiff line change
@@ -654,16 +654,6 @@ func ListenLocalSocket(address string) (net.Listener, error) {
654654
return listenUnix(address)
655655
}
656656

657-
// DialLocalSocket connects to a Unix domain socket or a Windows named pipe.
658-
// - If the specified address starts with "\\.\pipe\", connect to a Windows named pipe path.
659-
// - Else connect to a Unix domain socket.
660-
func DialLocalSocket(address string) (net.Conn, error) {
661-
if strings.HasPrefix(address, namedPipePrefix) {
662-
return winio.DialPipe(address, nil)
663-
}
664-
return dialUnix(address)
665-
}
666-
667657
func HostInterfaceExists(ifaceName string) bool {
668658
_, err := getAdapterInAllCompartmentsByName(ifaceName)
669659
if err != nil {

pkg/cni/client.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package cni
1717
import (
1818
"context"
1919
"fmt"
20-
"net"
2120
"os"
2221

2322
"github.com/containernetworking/cni/pkg/skel"
@@ -27,7 +26,6 @@ import (
2726
grpcinsecure "google.golang.org/grpc/credentials/insecure"
2827
"google.golang.org/grpc/status"
2928

30-
"antrea.io/antrea/pkg/agent/util"
3129
cnipb "antrea.io/antrea/pkg/apis/cni/v1beta1"
3230
)
3331

@@ -91,9 +89,7 @@ func rpcClient(f func(client cnipb.CniClient) error) error {
9189
conn, err := grpc.Dial(
9290
AntreaCNISocketAddr,
9391
grpc.WithTransportCredentials(grpcinsecure.NewCredentials()),
94-
grpc.WithContextDialer(func(ctx context.Context, addr string) (conn net.Conn, e error) {
95-
return util.DialLocalSocket(addr)
96-
}),
92+
grpc.WithContextDialer(dial),
9793
)
9894
if err != nil {
9995
return err

pkg/cni/dial_linux.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2024 Antrea Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cni
16+
17+
import (
18+
"context"
19+
"net"
20+
)
21+
22+
func dial(_ context.Context, address string) (net.Conn, error) {
23+
return net.Dial("unix", address)
24+
}

pkg/cni/dial_windows.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 Antrea Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cni
16+
17+
import (
18+
"context"
19+
"net"
20+
"strings"
21+
22+
"github.com/Microsoft/go-winio"
23+
)
24+
25+
const namedPipePrefix = `\\.\pipe\`
26+
27+
func dial(_ context.Context, address string) (net.Conn, error) {
28+
if strings.HasPrefix(address, namedPipePrefix) {
29+
return winio.DialPipe(address, nil)
30+
}
31+
return net.Dial("unix", address)
32+
}

0 commit comments

Comments
 (0)