Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix erdma interface allocate normal ip resource #617

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/eni/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (l *Local) Allocate(ctx context.Context, cni *daemon.CNI, request ResourceR
if request.ResourceType() != ResourceTypeLocalIP {
return nil, []Trace{{Condition: ResourceTypeMismatch}}
}
if request.(*LocalIPRequest).LocalIPType == LocalIPTypeERDMA && l.eniType != "erdma" {
if (request.(*LocalIPRequest).LocalIPType == LocalIPTypeERDMA) != (l.eniType == "erdma") {
return nil, []Trace{{Condition: ResourceTypeMismatch}}
}

Expand Down Expand Up @@ -737,7 +737,8 @@ func (l *Local) Dispose(n int) int {

// 1. check if can dispose the eni
if n >= max(len(l.ipv4), len(l.ipv6)) {
if strings.ToLower(l.eniType) != "trunk" && !l.eni.Trunk && len(l.ipv4.InUse()) == 0 && len(l.ipv6.InUse()) == 0 {
eniType := strings.ToLower(l.eniType)
if eniType != "trunk" && eniType != "erdma" && !l.eni.Trunk && len(l.ipv4.InUse()) == 0 && len(l.ipv6.InUse()) == 0 {
log.Info("dispose eni")
l.status = statusDeleting
return max(len(l.ipv4), len(l.ipv6))
Expand Down
73 changes: 61 additions & 12 deletions pkg/eni/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/AliyunContainerService/terway/types/daemon"
)

func NewLocalTest(eni *daemon.ENI, factory factory.Factory, poolConfig *types.PoolConfig) *Local {
func NewLocalTest(eni *daemon.ENI, factory factory.Factory, poolConfig *types.PoolConfig, eniType string) *Local {
l := &Local{
eni: eni,
batchSize: poolConfig.BatchSize,
Expand All @@ -25,6 +25,7 @@ func NewLocalTest(eni *daemon.ENI, factory factory.Factory, poolConfig *types.Po
enableIPv4: poolConfig.EnableIPv4,
enableIPv6: poolConfig.EnableIPv6,
factory: factory,
eniType: eniType,

rateLimitEni: rate.NewLimiter(100, 100),
rateLimitv4: rate.NewLimiter(100, 100),
Expand All @@ -35,7 +36,7 @@ func NewLocalTest(eni *daemon.ENI, factory factory.Factory, poolConfig *types.Po
}

func TestLocal_Release_ValidIPv4(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "")
request := &LocalIPResource{
ENI: daemon.ENI{ID: "eni-1"},
IP: types.IPSet2{IPv4: netip.MustParseAddr("192.0.2.1")},
Expand All @@ -49,7 +50,7 @@ func TestLocal_Release_ValidIPv4(t *testing.T) {
}

func TestLocal_Release_ValidIPv6(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "")
request := &LocalIPResource{
ENI: daemon.ENI{ID: "eni-1"},
IP: types.IPSet2{IPv6: netip.MustParseAddr("fd00:46dd:e::1")},
Expand All @@ -63,7 +64,7 @@ func TestLocal_Release_ValidIPv6(t *testing.T) {
}

func TestLocal_Release_NilENI(t *testing.T) {
local := NewLocalTest(nil, nil, &types.PoolConfig{})
local := NewLocalTest(nil, nil, &types.PoolConfig{}, "")
request := &LocalIPResource{
ENI: daemon.ENI{ID: "eni-1"},
IP: types.IPSet2{IPv4: netip.MustParseAddr("192.0.2.1")},
Expand All @@ -74,7 +75,7 @@ func TestLocal_Release_NilENI(t *testing.T) {
}

func TestLocal_Release_DifferentENIID(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "")
request := &LocalIPResource{
ENI: daemon.ENI{ID: "eni-2"},
IP: types.IPSet2{IPv4: netip.MustParseAddr("192.0.2.1")},
Expand All @@ -85,7 +86,7 @@ func TestLocal_Release_DifferentENIID(t *testing.T) {
}

func TestLocal_Release_ValidIPv4_ReleaseIPv6(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "")
request := &LocalIPResource{
ENI: daemon.ENI{ID: "eni-1"},
IP: types.IPSet2{IPv4: netip.MustParseAddr("192.0.2.1"), IPv6: netip.MustParseAddr("fd00:46dd:e::1")},
Expand All @@ -110,7 +111,7 @@ func TestLocal_Release_ValidIPv4_ReleaseIPv6(t *testing.T) {
func TestLocal_AllocWorker_EnableIPv4(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{
EnableIPv4: true,
})
}, "")
cni := &daemon.CNI{PodID: "pod-1"}

respCh := make(chan *AllocResp)
Expand All @@ -135,7 +136,7 @@ func TestLocal_AllocWorker_EnableIPv4(t *testing.T) {
func TestLocal_AllocWorker_EnableIPv6(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{
EnableIPv6: true,
})
}, "")
cni := &daemon.CNI{PodID: "pod-1"}

respCh := make(chan *AllocResp)
Expand All @@ -160,7 +161,7 @@ func TestLocal_AllocWorker_EnableIPv6(t *testing.T) {
func TestLocal_AllocWorker_ParentCancelContext(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{
EnableIPv4: true,
})
}, "")
cni := &daemon.CNI{PodID: "pod-1"}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -174,7 +175,7 @@ func TestLocal_AllocWorker_ParentCancelContext(t *testing.T) {
}

func TestLocal_Dispose(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "")
local.status = statusInUse
local.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.1"), false))
local.ipv4[netip.MustParseAddr("192.0.2.1")].Allocate("pod-1")
Expand All @@ -190,7 +191,7 @@ func TestLocal_Dispose(t *testing.T) {
}

func TestLocal_DisposeWholeENI(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "")
local.status = statusInUse
local.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.1"), true))
local.ipv6.Add(NewValidIP(netip.MustParseAddr("fd00:46dd:e::1"), false))
Expand All @@ -202,7 +203,7 @@ func TestLocal_DisposeWholeENI(t *testing.T) {
}

func TestLocal_Allocate_NoCache(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{MaxIPPerENI: 2, EnableIPv4: true})
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{MaxIPPerENI: 2, EnableIPv4: true}, "")

request := &LocalIPRequest{NoCache: true}
cni := &daemon.CNI{PodID: "pod-1"}
Expand All @@ -214,3 +215,51 @@ func TestLocal_Allocate_NoCache(t *testing.T) {

assert.Equal(t, 1, len(resp))
}

func TestLocal_DisposeWholeERDMA(t *testing.T) {
local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{}, "erdma")
local.status = statusInUse
local.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.1"), false))

n := local.Dispose(1)

assert.Equal(t, 1, n)
assert.NotEqual(t, statusDeleting, local.status)
}

func TestLocal_Allocate_ERDMA(t *testing.T) {
localErdma := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{MaxIPPerENI: 2, EnableIPv4: true}, "erdma")

request := &LocalIPRequest{NoCache: true}
cni := &daemon.CNI{PodID: "pod-1"}

localErdma.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.1"), false))
localErdma.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.2"), false))

_, resp := localErdma.Allocate(context.Background(), cni, request)

assert.Equal(t, 1, len(resp))
assert.Equal(t, ResourceTypeMismatch, resp[0].Condition)

request = &LocalIPRequest{NoCache: true, LocalIPType: LocalIPTypeERDMA}

_, resp = localErdma.Allocate(context.Background(), cni, request)
assert.Equal(t, 1, len(resp))
assert.NotEqual(t, ResourceTypeMismatch, resp[0].Condition)

local := NewLocalTest(&daemon.ENI{ID: "eni-1"}, nil, &types.PoolConfig{MaxIPPerENI: 2, EnableIPv4: true}, "")
local.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.1"), false))
local.ipv4.Add(NewValidIP(netip.MustParseAddr("192.0.2.2"), false))

request = &LocalIPRequest{NoCache: true}
_, resp = local.Allocate(context.Background(), cni, request)

assert.Equal(t, 1, len(resp))
assert.NotEqual(t, ResourceTypeMismatch, resp[0].Condition)

request = &LocalIPRequest{NoCache: true, LocalIPType: LocalIPTypeERDMA}
_, resp = local.Allocate(context.Background(), cni, request)

assert.Equal(t, 1, len(resp))
assert.Equal(t, ResourceTypeMismatch, resp[0].Condition)
}
Loading