@@ -5,9 +5,11 @@ package vip
55
66import (
77 "runtime"
8+ "syscall"
89
910 "github.com/j-keck/arping"
1011 "github.com/pingcap/tiproxy/lib/util/errors"
12+ "github.com/pingcap/tiproxy/pkg/util/cmd"
1113 "github.com/vishvananda/netlink"
1214)
1315
@@ -68,13 +70,26 @@ func (no *networkOperation) HasIP() (bool, error) {
6870}
6971
7072func (no * networkOperation ) AddIP () error {
71- return netlink .AddrAdd (no .link , no .address )
73+ err := netlink .AddrAdd (no .link , no .address )
74+ // If TiProxy is deployed by TiUP, the user that runs TiProxy only has the sudo permission.
75+ if err != nil && errors .Is (err , syscall .EPERM ) {
76+ err = cmd .ExecCmd ("sudo" , "ip" , "addr" , "add" , no .address .String (), "dev" , no .link .Attrs ().Name )
77+ }
78+ return errors .WithStack (err )
7279}
7380
7481func (no * networkOperation ) DeleteIP () error {
75- return netlink .AddrDel (no .link , no .address )
82+ err := netlink .AddrDel (no .link , no .address )
83+ if err != nil && errors .Is (err , syscall .EPERM ) {
84+ err = cmd .ExecCmd ("sudo" , "ip" , "addr" , "del" , no .address .String (), "dev" , no .link .Attrs ().Name )
85+ }
86+ return errors .WithStack (err )
7687}
7788
7889func (no * networkOperation ) SendARP () error {
79- return arping .GratuitousArpOverIfaceByName (no .address .IP , no .link .Attrs ().Name )
90+ err := arping .GratuitousArpOverIfaceByName (no .address .IP , no .link .Attrs ().Name )
91+ if err != nil && errors .Is (err , syscall .EPERM ) {
92+ err = cmd .ExecCmd ("sudo" , "arping" , "-c" , "1" , "-U" , "-I" , no .link .Attrs ().Name , no .address .IP .String ())
93+ }
94+ return errors .WithStack (err )
8095}
0 commit comments