Skip to content

Commit 874f9fc

Browse files
Rakshith-Rmergify[bot]
authored andcommitted
util: add a blocklist cooldown period of 5 mins
This commit adds a blocklist cool period of 5 mins making sure cephcsi will wait for atleast 5 mins before auto unfencing a node. Signed-off-by: Rakshith R <[email protected]>
1 parent d974d07 commit 874f9fc

File tree

2 files changed

+149
-5
lines changed

2 files changed

+149
-5
lines changed

internal/csi-addons/networkfence/fencing.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ import (
3737

3838
const (
3939
ISO8601TimeLayout = "2006-01-02T15:04:05.000000-0700"
40-
invalidCommandStr = "invalid command"
40+
// BlockListCoolDownPeriod defines the time duration
41+
// after which a blocklist entry can be removed.
42+
// TODO: Make this configurable.
43+
blockListCoolDownPeriod = 5 * time.Minute
44+
invalidCommandStr = "invalid command"
4145
// we can always use mds rank 0, since all the clients have a session with rank-0.
4246
mdsRank = 0
4347
)
@@ -592,7 +596,17 @@ func containsMatchingBlockListEntry(
592596
entry.Until, err)
593597
}
594598

595-
if until.Sub(time.Now()) <= util.AutoBlocklistTime {
599+
timeLeftUntilExpire := time.Until(until)
600+
// Check if the blocklist entry is eligible for auto-unfencing if
601+
// 1. the time left until expiry is less than or equal to AutoBlocklistTime,
602+
// 2. the blocklist was done at least blockListCoolDownPeriod seconds (5 Minutes) ago.
603+
if timeLeftUntilExpire <= util.AutoBlocklistTime {
604+
if timeLeftUntilExpire > (util.AutoBlocklistTime - blockListCoolDownPeriod) {
605+
// still in cool-down period
606+
return false, fmt.Errorf("blocklist entry %q is still in cool-down period for %s",
607+
entry.Addr, (timeLeftUntilExpire - (util.AutoBlocklistTime - blockListCoolDownPeriod)))
608+
}
609+
596610
return true, nil
597611
}
598612
}

internal/csi-addons/networkfence/fencing_test.go

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,153 @@ func Test_containsMatchingBlockListEntry(t *testing.T) {
274274
wantErr bool
275275
}{
276276
{
277-
name: "matching entry found",
277+
name: "matching entry found blocked outside cool down period 1",
278278
args: args{
279279
blocklist: &[]osdAdmin.Blocklist{
280280
{
281281
Addr: "192.0.1.0:0/32",
282-
Until: time.Now().Format(ISO8601TimeLayout),
282+
Until: time.Now().Add(1 * time.Hour).Format(ISO8601TimeLayout),
283283
},
284284
{
285285
Addr: "192.0.2.0:0/32",
286-
Until: time.Now().Format(ISO8601TimeLayout),
286+
Until: time.Now().Add(1 * time.Hour).Format(ISO8601TimeLayout),
287+
},
288+
},
289+
addr: "192.0.1.0",
290+
},
291+
want: true,
292+
wantErr: false,
293+
},
294+
{
295+
name: "matching entry found blocked outside cool down period 2",
296+
args: args{
297+
blocklist: &[]osdAdmin.Blocklist{
298+
{
299+
Addr: "192.0.1.0:0/32",
300+
Until: time.Now().Add(util.AutoBlocklistTime - blockListCoolDownPeriod).Format(ISO8601TimeLayout),
301+
},
302+
{
303+
Addr: "192.0.2.0:0/32",
304+
Until: time.Now().Add(util.AutoBlocklistTime - blockListCoolDownPeriod).Format(ISO8601TimeLayout),
287305
},
288306
},
289307
addr: "192.0.1.0",
290308
},
291309
want: true,
292310
wantErr: false,
293311
},
312+
{
313+
name: "matching entry found blocked in cool down period 1",
314+
args: args{
315+
blocklist: &[]osdAdmin.Blocklist{
316+
{
317+
Addr: "192.0.1.0:0/32",
318+
Until: time.Now().Add(util.AutoBlocklistTime).Format(ISO8601TimeLayout),
319+
},
320+
{
321+
Addr: "192.0.2.0:0/32",
322+
Until: time.Now().Add(util.AutoBlocklistTime).Format(ISO8601TimeLayout),
323+
},
324+
},
325+
addr: "192.0.1.0",
326+
},
327+
want: false,
328+
wantErr: true,
329+
},
330+
{
331+
name: "matching entry found blocked in cool down period 2",
332+
args: args{
333+
blocklist: &[]osdAdmin.Blocklist{
334+
{
335+
Addr: "192.0.1.0:0/32",
336+
Until: time.Now().Add(util.AutoBlocklistTime - 2*time.Minute).
337+
Format(ISO8601TimeLayout),
338+
},
339+
{
340+
Addr: "192.0.2.0:0/32",
341+
Until: time.Now().Add(util.AutoBlocklistTime - 2*time.Minute).
342+
Format(ISO8601TimeLayout),
343+
},
344+
},
345+
addr: "192.0.1.0",
346+
},
347+
want: false,
348+
wantErr: true,
349+
},
350+
{
351+
name: "matching IPv6 entry found blocked outside cool down period 1",
352+
args: args{
353+
blocklist: &[]osdAdmin.Blocklist{
354+
{
355+
Addr: "2001:db8::1:0/128",
356+
Until: time.Now().Add(1 * time.Hour).Format(ISO8601TimeLayout),
357+
},
358+
{
359+
Addr: "2001:db8::2:0/128",
360+
Until: time.Now().Add(1 * time.Hour).Format(ISO8601TimeLayout),
361+
},
362+
},
363+
addr: "2001:db8::1",
364+
},
365+
want: true,
366+
wantErr: false,
367+
},
368+
{
369+
name: "matching IPv6 entry found blocked outside cool down period 2",
370+
args: args{
371+
blocklist: &[]osdAdmin.Blocklist{
372+
{
373+
Addr: "2001:db8::1:0/128",
374+
Until: time.Now().Add(util.AutoBlocklistTime - blockListCoolDownPeriod).Format(ISO8601TimeLayout),
375+
},
376+
{
377+
Addr: "2001:db8::2:0/128",
378+
Until: time.Now().Add(util.AutoBlocklistTime - blockListCoolDownPeriod).Format(ISO8601TimeLayout),
379+
},
380+
},
381+
addr: "2001:db8::1",
382+
},
383+
want: true,
384+
wantErr: false,
385+
},
386+
{
387+
name: "matching IPv6 entry found blocked in cool down period 1",
388+
args: args{
389+
blocklist: &[]osdAdmin.Blocklist{
390+
{
391+
Addr: "2001:db8::1:0/128",
392+
Until: time.Now().Add(util.AutoBlocklistTime).Format(ISO8601TimeLayout),
393+
},
394+
{
395+
Addr: "2001:db8::2:0/128",
396+
Until: time.Now().Add(util.AutoBlocklistTime).Format(ISO8601TimeLayout),
397+
},
398+
},
399+
addr: "2001:db8::1",
400+
},
401+
want: false,
402+
wantErr: true,
403+
},
404+
{
405+
name: "matching IPv6 entry found blocked in cool down period 2",
406+
args: args{
407+
blocklist: &[]osdAdmin.Blocklist{
408+
{
409+
Addr: "2001:db8::1:0/128",
410+
Until: time.Now().Add(util.AutoBlocklistTime - 2*time.Minute).
411+
Format(ISO8601TimeLayout),
412+
},
413+
{
414+
Addr: "2001:db8::2:0/128",
415+
Until: time.Now().Add(util.AutoBlocklistTime - 2*time.Minute).
416+
Format(ISO8601TimeLayout),
417+
},
418+
},
419+
addr: "2001:db8::1",
420+
},
421+
want: false,
422+
wantErr: true,
423+
},
294424
{
295425
name: "address does not match",
296426
args: args{

0 commit comments

Comments
 (0)