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: legacy ip record is not handled #640

Merged
merged 1 commit into from
Jun 6, 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
32 changes: 27 additions & 5 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,8 @@ func newNetworkService(ctx context.Context, configFilePath, daemonMode string) (
return nil, err
}

attachedENIID := lo.SliceToMap(attached, func(item *daemon.ENI) (string, struct{}) {
return item.ID, struct{}{}
attachedENIID := lo.SliceToMap(attached, func(item *daemon.ENI) (string, *daemon.ENI) {
return item.ID, item
})
podResources := getPodResources(objList)
serviceLog.Info(fmt.Sprintf("loaded pod res, %v", podResources))
Expand Down Expand Up @@ -1400,13 +1400,35 @@ func getPodIPs(netConfs []*rpc.NetConf) []string {
return ips
}

func filterENINotFound(podResources []daemon.PodResources, attachedENIID map[string]struct{}) []daemon.PodResources {
func filterENINotFound(podResources []daemon.PodResources, attachedENIID map[string]*daemon.ENI) []daemon.PodResources {
for i := range podResources {
for j := 0; j < len(podResources[i].Resources); j++ {
if podResources[i].Resources[j].Type == daemon.ResourceTypeENI ||
podResources[i].Resources[j].Type == daemon.ResourceTypeENIIP {
if _, ok := attachedENIID[podResources[i].Resources[j].ENIID]; !ok {
podResources[i].Resources = append(podResources[i].Resources[:j], podResources[i].Resources[j+1:]...)

eniID := podResources[i].Resources[j].ENIID
if eniID == "" {
list := strings.SplitN(podResources[i].Resources[j].ID, ".", 2)
if len(list) == 0 {
continue
}
mac := list[0]

found := false
for _, eni := range attachedENIID {
if eni.MAC == mac {
// found
found = true
break
}
}
if !found {
podResources[i].Resources = append(podResources[i].Resources[:j], podResources[i].Resources[j+1:]...)
}
} else {
if _, ok := attachedENIID[eniID]; !ok {
podResources[i].Resources = append(podResources[i].Resources[:j], podResources[i].Resources[j+1:]...)
}
}
}
}
Expand Down
38 changes: 35 additions & 3 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,33 @@ func TestFilterENINotFound(t *testing.T) {
{Type: daemon.ResourceTypeEIP, ENIID: "eip3", ID: "resource"},
},
},
{
Resources: []daemon.ResourceItem{
{Type: daemon.ResourceTypeENIIP, ENIID: "", ID: "00:00:00:00:00:04.192.0.0.4"},
},
},
{
Resources: []daemon.ResourceItem{
{Type: daemon.ResourceTypeENI, ENIID: "", ID: "00:00:00:00:00:05"},
},
},
}

attachedENIID := map[string]struct{}{
"eni1": struct{}{},
"eni3": struct{}{},
attachedENIID := map[string]*daemon.ENI{
"eni1": {
ID: "eni1",
},
"eni3": {
ID: "eni3",
},
"eni4": {
ID: "eni4",
MAC: "00:00:00:00:00:04",
},
"eni5": {
ID: "eni5",
MAC: "00:00:00:00:00:05",
},
}

expected := []daemon.PodResources{
Expand All @@ -299,6 +321,16 @@ func TestFilterENINotFound(t *testing.T) {
{Type: daemon.ResourceTypeEIP, ENIID: "eip3", ID: "resource"},
},
},
{
Resources: []daemon.ResourceItem{
{Type: daemon.ResourceTypeENIIP, ENIID: "", ID: "00:00:00:00:00:04.192.0.0.4"},
},
},
{
Resources: []daemon.ResourceItem{
{Type: daemon.ResourceTypeENI, ENIID: "", ID: "00:00:00:00:00:05"},
},
},
}

filtered := filterENINotFound(podResources, attachedENIID)
Expand Down
126 changes: 96 additions & 30 deletions pkg/eni/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,43 +227,101 @@ func (l *Local) load(podResources []daemon.PodResources) error {
continue
}

if res.ENIID != l.eni.ID {
continue
}

logf.Log.Info("existed pod", "pod", podID, "ipv4", res.IPv4, "ipv6", res.IPv6, "eni", l.eni.ID)

// belong to this eni
if res.IPv4 != "" {
ip, err := netip.ParseAddr(res.IPv4)
if err != nil {
return &types.Error{
Code: types.ErrInvalidDataType,
Msg: err.Error(),
R: err,
if res.ENIID == "" {
logf.Log.Info("legacy record", "id", res.ID, "type", res.Type)
if res.ID != "" {
// 1. eni only the res id is the mac address
// 2. eniip the res id is the mac.ip
// this case is ipv4 only

switch res.Type {
case daemon.ResourceTypeENIIP:
mac, ipStr, err := parseResourceID(res.ID)
if err != nil {
return err
}
if mac != l.eni.MAC {
continue
}
logf.Log.Info("existed pod", "pod", podID, "ipv4", ipStr, "eni", l.eni.ID)

ip, err := netip.ParseAddr(ipStr)
if err != nil {
return &types.Error{
Code: types.ErrInvalidDataType,
Msg: err.Error(),
R: err,
}
}
v, ok := l.ipv4[ip]
if !ok {
continue
}
v.Allocate(podID)
metric.ResourcePoolIdle.WithLabelValues(metric.ResourcePoolTypeLocal, string(types.IPStackIPv4)).Dec()
case daemon.PodNetworkTypeVPCENI:
if res.ID != l.eni.MAC {
continue
}
primaryIP := l.eni.PrimaryIP.IPv4.String()
logf.Log.Info("existed pod", "pod", podID, "ipv4", primaryIP, "eni", l.eni.ID)

ip, err := netip.ParseAddr(primaryIP)
if err != nil {
return &types.Error{
Code: types.ErrInvalidDataType,
Msg: err.Error(),
R: err,
}
}
v, ok := l.ipv4[ip]
if !ok {
continue
}
v.Allocate(podID)
metric.ResourcePoolIdle.WithLabelValues(metric.ResourcePoolTypeLocal, string(types.IPStackIPv4)).Dec()
}
}
v, ok := l.ipv4[ip]
if !ok {
} else {
if res.ENIID != l.eni.ID {
continue
}
v.Allocate(podID)
metric.ResourcePoolIdle.WithLabelValues(metric.ResourcePoolTypeLocal, string(types.IPStackIPv4)).Dec()
}
if res.IPv6 != "" {
ip, err := netip.ParseAddr(res.IPv6)
if err != nil {
return &types.Error{
Code: types.ErrInvalidDataType,
Msg: err.Error(),
R: err,

logf.Log.Info("existed pod", "pod", podID, "ipv4", res.IPv4, "ipv6", res.IPv6, "eni", l.eni.ID)

// belong to this eni
if res.IPv4 != "" {
ip, err := netip.ParseAddr(res.IPv4)
if err != nil {
return &types.Error{
Code: types.ErrInvalidDataType,
Msg: err.Error(),
R: err,
}
}
v, ok := l.ipv4[ip]
if !ok {
continue
}
v.Allocate(podID)
metric.ResourcePoolIdle.WithLabelValues(metric.ResourcePoolTypeLocal, string(types.IPStackIPv4)).Dec()
}
v, ok := l.ipv6[ip]
if !ok {
continue
if res.IPv6 != "" {
ip, err := netip.ParseAddr(res.IPv6)
if err != nil {
return &types.Error{
Code: types.ErrInvalidDataType,
Msg: err.Error(),
R: err,
}
}
v, ok := l.ipv6[ip]
if !ok {
continue
}
v.Allocate(podID)
}
v.Allocate(podID)

metric.ResourcePoolIdle.WithLabelValues(metric.ResourcePoolTypeLocal, string(types.IPStackIPv6)).Dec()
}
}
Expand Down Expand Up @@ -980,3 +1038,11 @@ func syncIPLocked(lo Set, remote []netip.Addr) {
}
}
}

func parseResourceID(id string) (string, string, error) {
parts := strings.SplitN(id, ".", 2)
if len(parts) < 2 {
return "", "", fmt.Errorf("invalid resource id: %s", id)
}
return parts[0], parts[1], nil
}
32 changes: 32 additions & 0 deletions pkg/eni/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eni

import (
"context"
"fmt"
"net/netip"
"sync"
"testing"
Expand Down Expand Up @@ -263,3 +264,34 @@ func TestLocal_Allocate_ERDMA(t *testing.T) {
assert.Equal(t, 1, len(resp))
assert.Equal(t, ResourceTypeMismatch, resp[0].Condition)
}

func Test_parseResourceID(t *testing.T) {
type args struct {
id string
}
tests := []struct {
name string
args args
want string
want1 string
wantErr assert.ErrorAssertionFunc
}{
{
name: "test1",
args: args{id: "00:00:00:00:00:00.192.0.2.1"},
want: "00:00:00:00:00:00",
want1: "192.0.2.1",
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := parseResourceID(tt.args.id)
if !tt.wantErr(t, err, fmt.Sprintf("parseResourceID(%v)", tt.args.id)) {
return
}
assert.Equalf(t, tt.want, got, "parseResourceID(%v)", tt.args.id)
assert.Equalf(t, tt.want1, got1, "parseResourceID(%v)", tt.args.id)
})
}
}
Loading