@@ -18,11 +18,9 @@ package networkfence
1818
1919import (
2020 "context"
21- "encoding/json"
2221 "errors"
2322 "fmt"
2423 "net"
25- "strconv"
2624 "strings"
2725 "time"
2826
@@ -42,8 +40,6 @@ const (
4240 // TODO: Make this configurable.
4341 blockListCoolDownPeriod = 5 * time .Minute
4442 invalidCommandStr = "invalid command"
45- // we can always use mds rank 0, since all the clients have a session with rank-0.
46- mdsRank = 0
4743)
4844
4945// NetworkFence contains the CIDR blocks to be blocked.
@@ -53,11 +49,6 @@ type NetworkFence struct {
5349 cr * util.Credentials
5450}
5551
56- // activeClient represents the structure of an active client.
57- type activeClient struct {
58- Inst string `json:"inst"`
59- }
60-
6152// NewNetworkFence returns a networkFence struct object from the Network fence/unfence request.
6253func NewNetworkFence (
6354 ctx context.Context ,
@@ -88,51 +79,6 @@ func NewNetworkFence(
8879 return nwFence , nil
8980}
9081
91- // AddClientEviction blocks access for all the IPs in the CIDR block
92- // using client eviction, it also blocks the entire CIDR.
93- func (nf * NetworkFence ) AddClientEviction (ctx context.Context ) error {
94- evictedIPs := make (map [string ]bool )
95- // fetch active clients
96- activeClients , err := nf .listActiveClients (ctx )
97- if err != nil {
98- return err
99- }
100- // iterate through CIDR blocks and check if any active client matches
101- for _ , cidr := range nf .Cidr {
102- for _ , client := range activeClients {
103- var clientIP string
104- clientIP , err = client .fetchIP ()
105- if err != nil {
106- return fmt .Errorf ("error fetching client IP: %w" , err )
107- }
108- // check if the clientIP is in the CIDR block
109- if isIPInCIDR (ctx , clientIP , cidr ) {
110- var clientID int
111- clientID , err = client .fetchID ()
112- if err != nil {
113- return fmt .Errorf ("error fetching client ID: %w" , err )
114- }
115- // evict the client
116- err = nf .evictCephFSClient (ctx , clientID )
117- if err != nil {
118- return fmt .Errorf ("error evicting client %d: %w" , clientID , err )
119- }
120- log .DebugLog (ctx , "client %d has been evicted\n " , clientID )
121- // add the CIDR to the list of blocklisted IPs
122- evictedIPs [clientIP ] = true
123- }
124- }
125- }
126-
127- // add the range based blocklist for CIDR
128- err = nf .AddNetworkFence (ctx )
129- if err != nil {
130- return err
131- }
132-
133- return nil
134- }
135-
13682// RemoveNetworkFence unblocks access for all the IPs in the IP range mentioned via the CIDR block
13783// using a network fence.
13884// Unfencing one of the protocols(CephFS or RBD) suggests the node is expected to be recovered, so
@@ -220,91 +166,6 @@ func (nf *NetworkFence) addCephBlocklist(ctx context.Context, ip string, useRang
220166 return util .AddCephBlocklist (ctx , nf .Monitors , nf .cr , ip , useRange )
221167}
222168
223- func (nf * NetworkFence ) listActiveClients (ctx context.Context ) ([]activeClient , error ) {
224- arg := []string {
225- "--id" , nf .cr .ID ,
226- "--keyfile=" + nf .cr .KeyFile ,
227- "-m" , nf .Monitors ,
228- }
229- // FIXME: replace the ceph command with go-ceph API in future
230- cmd := []string {"tell" , fmt .Sprintf ("mds.%d" , mdsRank ), "client" , "ls" }
231- cmd = append (cmd , arg ... )
232- stdout , stdErr , err := util .ExecCommandWithTimeout (ctx , 2 * time .Minute , "ceph" , cmd ... )
233- if err != nil {
234- return nil , fmt .Errorf ("failed to list active clients: %w, stderr: %q" , err , stdErr )
235- }
236-
237- var activeClients []activeClient
238- if err := json .Unmarshal ([]byte (stdout ), & activeClients ); err != nil {
239- return nil , fmt .Errorf ("failed to unmarshal JSON: %w" , err )
240- }
241-
242- return activeClients , nil
243- }
244-
245- func (nf * NetworkFence ) evictCephFSClient (ctx context.Context , clientID int ) error {
246- arg := []string {
247- "--id" , nf .cr .ID ,
248- "--keyfile=" + nf .cr .KeyFile ,
249- "-m" , nf .Monitors ,
250- }
251- // FIXME: replace the ceph command with go-ceph API in future
252- cmd := []string {"tell" , fmt .Sprintf ("mds.%d" , mdsRank ), "client" , "evict" , fmt .Sprintf ("id=%d" , clientID )}
253- cmd = append (cmd , arg ... )
254- _ , stdErr , err := util .ExecCommandWithTimeout (ctx , 2 * time .Minute , "ceph" , cmd ... )
255- if err != nil {
256- return fmt .Errorf ("failed to evict client %d: %w, stderr: %q" , clientID , err , stdErr )
257- }
258- log .DebugLog (ctx , "client %s has been evicted from CephFS\n " , clientID )
259-
260- return nil
261- }
262-
263- func isIPInCIDR (ctx context.Context , ip , cidr string ) bool {
264- // Parse the CIDR block
265- _ , ipCidr , err := net .ParseCIDR (cidr )
266- if err != nil {
267- log .ErrorLog (ctx , "error parsing CIDR block %s: %w\n " , cidr , err )
268-
269- return false
270- }
271-
272- // Parse the IP address
273- ipAddress := net .ParseIP (ip )
274- if ipAddress == nil {
275- log .ErrorLog (ctx , "error parsing IP address %s\n " , ip )
276-
277- return false
278- }
279-
280- // Check if the IP address is within the CIDR block
281- return ipCidr .Contains (ipAddress )
282- }
283-
284- func (ac * activeClient ) fetchIP () (string , error ) {
285- // example: "inst": "client.4305 172.21.9.34:0/422650892",
286- // then returning value will be 172.21.9.34
287- return util .ParseClientIP (ac .Inst )
288- }
289-
290- func (ac * activeClient ) fetchID () (int , error ) {
291- // example: "inst": "client.4305 172.21.9.34:0/422650892",
292- // then returning value will be 4305
293- clientInfo := ac .Inst
294- parts := strings .Fields (clientInfo )
295- if len (parts ) >= 1 {
296- clientIDStr := strings .TrimPrefix (parts [0 ], "client." )
297- clientID , err := strconv .Atoi (clientIDStr )
298- if err != nil {
299- return 0 , fmt .Errorf ("failed to convert client ID to int: %w" , err )
300- }
301-
302- return clientID , nil
303- }
304-
305- return 0 , fmt .Errorf ("failed to extract client ID, incorrect format: %s" , clientInfo )
306- }
307-
308169// getIPRange returns a list of IPs from the IP range
309170// corresponding to a CIDR block.
310171func getIPRange (cidr string ) ([]string , error ) {
0 commit comments