Skip to content

Commit 325d07e

Browse files
authored
Merge pull request #688 from l1b0k/release-1.9
backport fix for 1.9
2 parents 94795f0 + 38eeb31 commit 325d07e

File tree

7 files changed

+99
-8
lines changed

7 files changed

+99
-8
lines changed

pkg/aliyun/credential/aliyun_client_mgr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (c *ClientMgr) refreshToken() (bool, error) {
181181
if err != nil {
182182
return false, err
183183
}
184-
c.ecs.SetEndpointRules(c.ecs.EndpointMap, "regional", "public")
184+
c.ecs.SetEndpointRules(c.ecs.EndpointMap, "regional", "vpc")
185185

186186
if c.ecsDomainOverride != "" {
187187
c.ecs.Domain = c.ecsDomainOverride
@@ -191,7 +191,7 @@ func (c *ClientMgr) refreshToken() (bool, error) {
191191
if err != nil {
192192
return false, err
193193
}
194-
c.vpc.SetEndpointRules(c.vpc.EndpointMap, "regional", "public")
194+
c.vpc.SetEndpointRules(c.vpc.EndpointMap, "regional", "vpc")
195195

196196
if c.vpcDomainOverride != "" {
197197
c.vpc.Domain = c.vpcDomainOverride
@@ -201,7 +201,7 @@ func (c *ClientMgr) refreshToken() (bool, error) {
201201
if err != nil {
202202
return false, err
203203
}
204-
c.eflo.SetEndpointRules(c.eflo.EndpointMap, "regional", "public")
204+
c.eflo.SetEndpointRules(c.eflo.EndpointMap, "regional", "vpc")
205205

206206
if c.efloDomainOverride != "" {
207207
c.eflo.Domain = c.efloDomainOverride

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{}
@@ -178,7 +181,7 @@ func (l *Local) Run(ctx context.Context, podResources []daemon.PodResources, wg
178181

179182
go l.notify(ctx)
180183

181-
go wait.JitterUntil(l.sync, 1*time.Minute, 1.0, true, ctx.Done())
184+
go wait.JitterUntil(l.sync, defaultSyncPeriod, 1.0, true, ctx.Done())
182185

183186
return nil
184187
}
@@ -371,6 +374,7 @@ func (l *Local) sync() {
371374

372375
syncIPLocked(l.ipv4, ipv4)
373376
syncIPLocked(l.ipv6, ipv6)
377+
report()
374378

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

1077+
var invalidIPCache = cache.NewLRUExpireCache(100)
1078+
10431079
func parseResourceID(id string) (string, string, error) {
10441080
parts := strings.SplitN(id, ".", 2)
10451081
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

pkg/factory/aliyun/aliyun.go

Lines changed: 13 additions & 0 deletions
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

plugin/terway/cni.go

Lines changed: 14 additions & 1 deletion
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"
@@ -295,7 +298,17 @@ func parseSetupConf(args *skel.CmdArgs, alloc *rpc.NetConf, conf *types.CNIConf,
295298
if alloc.GetENIInfo() != nil {
296299
mac := alloc.GetENIInfo().GetMAC()
297300
if mac != "" {
298-
deviceID, err = link.GetDeviceNumber(mac)
301+
err = retry.OnError(wait.Backoff{
302+
Steps: 10,
303+
Duration: 1 * time.Second,
304+
Factor: 1.0,
305+
Jitter: 0,
306+
}, func(err error) bool {
307+
return errors.Is(err, link.ErrNotFound)
308+
}, func() error {
309+
deviceID, err = link.GetDeviceNumber(mac)
310+
return err
311+
})
299312
if err != nil {
300313
return nil, err
301314
}

policy/policyinit.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if [ "$DATASTORE_TYPE" = "kubernetes" ]; then
99
exit 1
1010
fi
1111
return_code="$(curl -k -o /dev/null -I -L -s -w "%{http_code}" https://"${KUBERNETES_SERVICE_HOST}":"${KUBERNETES_SERVICE_PORT:-443}")"
12-
if [ "$return_code" -ne 403 ]&&[ "$return_code" -ne 200 ]&&[ "$return_code" -ne 201 ];then
12+
if [ "$return_code" -ne 401 ]&&[ "$return_code" -ne 403 ]&&[ "$return_code" -ne 200 ]&&[ "$return_code" -ne 201 ];then
1313
echo "can not access kubernetes service, exiting"
1414
exit 1
1515
fi
@@ -173,4 +173,4 @@ fi
173173
else
174174
# shellcheck disable=SC2016
175175
exec socat TCP-LISTEN:9099,bind=127.0.0.1,fork,reuseaddr system:'sleep 2;kill -9 $SOCAT_PID 2>/dev/null'
176-
fi
176+
fi

0 commit comments

Comments
 (0)