Skip to content

Commit b8b91be

Browse files
committed
daemon add the orphan ip warning
default 0s Warning ResourceInvalid node/cn-hangzhou.172.16.1.196 orphan ip found on ecs metadata, ip: 172.16.64.4, restart terway to resync data Signed-off-by: l1b0k <[email protected]>
1 parent 19ac6a0 commit b8b91be

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

pkg/eni/local.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"golang.org/x/time/rate"
1515
corev1 "k8s.io/api/core/v1"
16+
"k8s.io/apimachinery/pkg/util/cache"
1617
"k8s.io/apimachinery/pkg/util/sets"
1718
"k8s.io/apimachinery/pkg/util/wait"
1819
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -27,6 +28,8 @@ import (
2728
"github.com/AliyunContainerService/terway/pkg/metric"
2829
)
2930

31+
const defaultSyncPeriod = 1 * time.Minute
32+
3033
var _ NetworkInterface = &Local{}
3134
var _ Usage = &Local{}
3235
var _ ReportStatus = &Trunk{}
@@ -177,7 +180,7 @@ func (l *Local) Run(ctx context.Context, podResources []daemon.PodResources, wg
177180

178181
go l.notify(ctx)
179182

180-
go wait.JitterUntil(l.sync, 1*time.Minute, 1.0, true, ctx.Done())
183+
go wait.JitterUntil(l.sync, defaultSyncPeriod, 1.0, true, ctx.Done())
181184

182185
return nil
183186
}
@@ -370,6 +373,7 @@ func (l *Local) sync() {
370373

371374
syncIPLocked(l.ipv4, ipv4)
372375
syncIPLocked(l.ipv6, ipv6)
376+
report()
373377

374378
l.cond.Broadcast()
375379
}
@@ -1037,8 +1041,40 @@ func syncIPLocked(lo Set, remote []netip.Addr) {
10371041
}
10381042
}
10391043
}
1044+
orphanIP(lo, s)
1045+
}
1046+
1047+
func orphanIP(lo Set, remote sets.Set[netip.Addr]) {
1048+
for key := range remote {
1049+
if _, ok := lo[key]; !ok {
1050+
1051+
prev, ok := invalidIPCache.Get(key)
1052+
if !ok {
1053+
invalidIPCache.Add(key, 1, 5*defaultSyncPeriod)
1054+
} else {
1055+
invalidIPCache.Add(key, prev.(int)+1, 5*defaultSyncPeriod)
1056+
}
1057+
} else {
1058+
invalidIPCache.Remove(key)
1059+
}
1060+
}
1061+
}
1062+
1063+
func report() {
1064+
for _, key := range invalidIPCache.Keys() {
1065+
count, ok := invalidIPCache.Get(key)
1066+
if !ok {
1067+
continue
1068+
}
1069+
if count.(int) > 1 {
1070+
_ = tracing.RecordNodeEvent(corev1.EventTypeWarning, string(types.ErrResourceInvalid), fmt.Sprintf("orphan ip found on ecs metadata, ip: %s", key))
1071+
logf.Log.Info("orphan ip found on ecs metadata", "ip", key)
1072+
}
1073+
}
10401074
}
10411075

1076+
var invalidIPCache = cache.NewLRUExpireCache(100)
1077+
10421078
func parseResourceID(id string) (string, string, error) {
10431079
parts := strings.SplitN(id, ".", 2)
10441080
if len(parts) < 2 {

pkg/eni/local_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"github.com/stretchr/testify/assert"
1212
"golang.org/x/time/rate"
13+
"k8s.io/apimachinery/pkg/util/cache"
14+
"k8s.io/apimachinery/pkg/util/sets"
1315

1416
"github.com/AliyunContainerService/terway/pkg/factory"
1517
"github.com/AliyunContainerService/terway/types"
@@ -309,3 +311,30 @@ func Test_parseResourceID(t *testing.T) {
309311
})
310312
}
311313
}
314+
315+
func Test_orphanIP(t *testing.T) {
316+
invalidIPCache = cache.NewLRUExpireCache(100)
317+
318+
lo1 := map[netip.Addr]*IP{
319+
netip.MustParseAddr("127.0.0.1"): {
320+
ip: netip.MustParseAddr("127.0.0.1"),
321+
},
322+
}
323+
324+
remote1 := sets.Set[netip.Addr]{
325+
netip.MustParseAddr("127.0.0.1"): {},
326+
netip.MustParseAddr("127.0.0.2"): {},
327+
}
328+
329+
orphanIP(lo1, remote1)
330+
331+
v, _ := invalidIPCache.Get(netip.MustParseAddr("127.0.0.1"))
332+
assert.Equal(t, nil, v)
333+
334+
v, _ = invalidIPCache.Get(netip.MustParseAddr("127.0.0.2"))
335+
assert.Equal(t, 1, v)
336+
337+
orphanIP(lo1, remote1)
338+
v, _ = invalidIPCache.Get(netip.MustParseAddr("127.0.0.2"))
339+
assert.Equal(t, 2, v)
340+
}

pkg/eni/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (ip *IP) Allocatable() bool {
9494
return ip.Valid() && !ip.InUse()
9595
}
9696

97-
type Set map[any]*IP
97+
type Set map[netip.Addr]*IP
9898

9999
func (s Set) Idles() []*IP {
100100
var result []*IP

0 commit comments

Comments
 (0)