Skip to content

Commit 52209c9

Browse files
authored
Merge pull request #687 from l1b0k/release-1.8
backport fix for 1.8
2 parents e6468c1 + 00abe40 commit 52209c9

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

Diff for: pkg/eni/local.go

+37-1
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 {

Diff for: pkg/eni/local_test.go

+29
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+
}

Diff for: pkg/eni/types.go

+1-1
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

Diff for: pkg/factory/aliyun/aliyun.go

+13
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ func (a *Aliyun) CreateNetworkInterface(ipv4, ipv6 int, eniType string) (*daemon
191191
return r, nil, nil, err
192192
}
193193

194+
// wait mac
195+
err = wait.PollUntilContextTimeout(ctx, metadataPollInterval, metadataWaitTimeout, true, func(ctx context.Context) (bool, error) {
196+
macs, err := metadata.GetENIsMAC()
197+
if err != nil {
198+
klog.Errorf("metadata: error get mac: %v", err)
199+
return false, nil
200+
}
201+
return sets.NewString(macs...).Has(r.MAC), nil
202+
})
203+
if err != nil {
204+
return r, nil, nil, err
205+
}
206+
194207
prefix, err := metadata.GetVSwitchCIDR(eni.MacAddress)
195208
if err != nil {
196209
return r, nil, nil, err

Diff for: plugin/terway/cni.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net"
78
"runtime"
89
"time"
910

1011
"google.golang.org/grpc/backoff"
1112
"google.golang.org/grpc/credentials/insecure"
13+
"k8s.io/apimachinery/pkg/util/wait"
14+
"k8s.io/client-go/util/retry"
1215

1316
"github.com/AliyunContainerService/terway/pkg/link"
1417
"github.com/AliyunContainerService/terway/plugin/datapath"
@@ -298,7 +301,17 @@ func parseSetupConf(args *skel.CmdArgs, alloc *rpc.NetConf, conf *types.CNIConf,
298301
if alloc.GetENIInfo() != nil {
299302
mac := alloc.GetENIInfo().GetMAC()
300303
if mac != "" {
301-
deviceID, err = link.GetDeviceNumber(mac)
304+
err = retry.OnError(wait.Backoff{
305+
Steps: 10,
306+
Duration: 1 * time.Second,
307+
Factor: 1.0,
308+
Jitter: 0,
309+
}, func(err error) bool {
310+
return errors.Is(err, link.ErrNotFound)
311+
}, func() error {
312+
deviceID, err = link.GetDeviceNumber(mac)
313+
return err
314+
})
302315
if err != nil {
303316
return nil, err
304317
}

0 commit comments

Comments
 (0)