Skip to content

Commit

Permalink
Merge pull request #792 from l1b0k/feat/eflo
Browse files Browse the repository at this point in the history
refactor(eflo): enhance error handling and improve code readability
  • Loading branch information
BSWANG authored Mar 7, 2025
2 parents 917a644 + 6430ff9 commit 7fa229c
Show file tree
Hide file tree
Showing 16 changed files with 1,018 additions and 110 deletions.
70 changes: 58 additions & 12 deletions pkg/aliyun/client/eflo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func (a *OpenAPI) CreateElasticNetworkInterface(zoneID, nodeID, vSwitchID, secur
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
return "", "", err
}
return resp.Content.NodeId, resp.Content.ElasticNetworkInterfaceId, nil
Expand All @@ -50,9 +55,17 @@ func (a *OpenAPI) DeleteElasticNetworkInterface(ctx context.Context, eniID strin
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
l.Error(err, "failed")
return err
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
// 1011 IpName/leni not exist
if !apiErr.IsEfloCode(err, apiErr.ErrEfloResourceNotFound) {
l.Error(err, "failed")
return err
}
}

l.WithValues(LogFieldRequestID, resp.RequestId).Info("succeed")
Expand All @@ -76,7 +89,12 @@ func (a *OpenAPI) AssignLeniPrivateIPAddress(ctx context.Context, eniID, prefer
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed")
return "", err
}
Expand All @@ -102,9 +120,17 @@ func (a *OpenAPI) UnassignLeniPrivateIPAddress(ctx context.Context, eniID, ipNam
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
l.Error(err, "failed")
return err
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
// 1011 IpName/leni not exist
if !apiErr.IsEfloCode(err, apiErr.ErrEfloResourceNotFound) {
l.Error(err, "failed")
return err
}
}
l.WithValues(LogFieldRequestID, resp.RequestId).Info("success")

Expand All @@ -123,7 +149,12 @@ func (a *OpenAPI) GetElasticNetworkInterface(eniID string) (*eflo.Content, error
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
return nil, err
}

Expand All @@ -149,7 +180,12 @@ func (a *OpenAPI) ListLeniPrivateIPAddresses(ctx context.Context, eniID, ipName,
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed")
return nil, err
}
Expand Down Expand Up @@ -179,7 +215,12 @@ func (a *OpenAPI) ListElasticNetworkInterfaces(ctx context.Context, zoneID, node
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed")
return nil, err
}
Expand All @@ -204,7 +245,12 @@ func (a *OpenAPI) GetNodeInfoForPod(ctx context.Context, nodeID string) (*eflo.C
l.WithValues(LogFieldRequestID, resp.RequestId).Info("success")

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed")
return nil, err
}
Expand Down
36 changes: 28 additions & 8 deletions pkg/aliyun/client/eflo_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,22 @@ func (a *OpenAPI) CreateElasticNetworkInterfaceV2(ctx context.Context, opts ...C
}

if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed")
return nil, err
}

l.WithValues(LogFieldRequestID, resp.RequestId).Info("leni created", "leni", resp.Content.ElasticNetworkInterfaceId)

return &NetworkInterface{
NetworkInterfaceID: resp.Content.ElasticNetworkInterfaceId,
NetworkInterfaceID: resp.Content.ElasticNetworkInterfaceId,
Type: ENITypeSecondary,
NetworkInterfaceTrafficMode: ENITrafficModeStandard,
}, err
}

Expand All @@ -78,19 +85,25 @@ func (a *OpenAPI) DescribeLeniNetworkInterface(ctx context.Context, opts ...Desc

resp, err := a.ClientSet.EFLO().ListElasticNetworkInterfaces(req)
if err != nil {
l.Error(err, "failed to list leni")
return nil, err
}
if resp.Code != 0 {
err = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
l.Error(err, "failed")
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed to list leni")
return nil, err
}
enis := make([]*NetworkInterface, 0)
for _, data := range resp.Content.Data {

l.Info("ListElasticNetworkInterfaces", "data", data)

if data.Type == "DEFAULT" { // CUSTOM for our own card
if data.Type != "CUSTOM" { // CUSTOM for our own card
continue
}

Expand Down Expand Up @@ -120,6 +133,9 @@ func (a *OpenAPI) DescribeLeniNetworkInterface(ctx context.Context, opts ...Desc
eni.Status = ENIStatusAvailable
case LENIStatusAvailable:
eni.Status = ENIStatusInUse

case LENIStatusCreateFailed, LENIStatusDeleting, LENIStatusDeleteFailed:
eni.Status = ENIStatusDeleting
}

privateIPs = append(privateIPs, IPSet{
Expand Down Expand Up @@ -186,11 +202,15 @@ func (a *OpenAPI) AssignLeniPrivateIPAddress2(ctx context.Context, opts ...Assig
}

if resp.Code != 0 {
innerErr = fmt.Errorf("%s requestID %s", resp.Message, resp.RequestId)
err = &apiErr.EFLOCode{
Code: resp.Code,
Message: resp.Message,
RequestID: resp.RequestId,
Content: resp.Content,
}
l.Error(err, "failed")
return true, innerErr
return true, err
}

return true, nil
})
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/aliyun/client/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const (
ErrThrottling = "Throttling"

ErrOperationConflict = "Operation.Conflict"

ErrEfloPrivateIPQuotaExecuted = 1013
ErrEfloResourceNotFound = 1011
)

// define well known err
Expand Down Expand Up @@ -149,3 +152,22 @@ func ErrorIs(err error, fns ...CheckErr) bool {
}
return false
}

type EFLOCode struct {
Code int
Message string
RequestID string
Content any
}

func (e *EFLOCode) Error() string {
return fmt.Sprintf("errCode: %d, msg: %s, requestID: %s", e.Code, e.Message, e.RequestID)
}

func IsEfloCode(err error, code int) bool {
var efloErr *EFLOCode
if errors.As(err, &efloErr) {
return efloErr.Code == code
}
return false
}
14 changes: 14 additions & 0 deletions pkg/aliyun/client/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"errors"
"fmt"
"testing"

apiErr "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
Expand Down Expand Up @@ -125,3 +126,16 @@ func TestErrorIsReturnsFalseWhenNoCheckErrMatches(t *testing.T) {
checkFunc2 := WarpFn("yetAnotherErr")
assert.False(t, ErrorIs(err, checkFunc1, checkFunc2))
}

func TestEflo(t *testing.T) {
err := &EFLOCode{
Code: 403,
Content: "{\"Code\": \"err\"}",
Message: "message",
RequestID: "requestId",
}

assert.True(t, IsEfloCode(err, 403))
assert.True(t, IsEfloCode(fmt.Errorf("warp %w", err), 403))
assert.False(t, IsEfloCode(fmt.Errorf("warp %w", err), 00))
}
7 changes: 6 additions & 1 deletion pkg/aliyun/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,12 @@ func (o *DescribeNetworkInterfaceOptions) EFLO() *eflo.ListElasticNetworkInterfa
req.Type = *o.InstanceType
}
if o.Status != nil {
req.Status = *o.Status
switch *o.Status {
case ENIStatusInUse:
req.Status = LENIStatusAvailable
default:
req.Status = *o.Status
}
}

if o.Backoff == nil {
Expand Down
14 changes: 11 additions & 3 deletions pkg/aliyun/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ const (
)

const (
LENIStatusAvailable string = "Available"
LENIStatusUnattached string = "Unattached"
LENIStatusAvailable string = "Available"
LENIStatusUnattached string = "Unattached"
LENIStatusExecuting string = "Executing"
LENIStatusCreateFailed string = "Create Failed"
LENIStatusDeleteFailed string = "Delete Failed"
LENIStatusDeleting string = "Deleting"

LENIIPStatusAvailable string = "Available"
)
Expand Down Expand Up @@ -94,7 +98,7 @@ type IPSet struct {
}

func FromCreateResp(in *ecs.CreateNetworkInterfaceResponse) *NetworkInterface {
return &NetworkInterface{
r := &NetworkInterface{
Status: in.Status,
MacAddress: in.MacAddress,
NetworkInterfaceID: in.NetworkInterfaceId,
Expand All @@ -117,6 +121,10 @@ func FromCreateResp(in *ecs.CreateNetworkInterfaceResponse) *NetworkInterface {
Type: in.Type,
ResourceGroupID: in.ResourceGroupId,
}
if r.Type == "" {
r.Type = ENITypeSecondary
}
return r
}

func FromDescribeResp(in *ecs.NetworkInterfaceSet) *NetworkInterface {
Expand Down
14 changes: 0 additions & 14 deletions pkg/apis/crds/network.alibabacloud.com_nodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@ spec:
type: string
zoneID:
type: string
required:
- instanceID
- instanceType
- regionID
- zoneID
type: object
pool:
properties:
Expand Down Expand Up @@ -265,16 +260,7 @@ spec:
type: string
required:
- id
- ipv4
- ipv4CIDR
- ipv6CIDR
- macAddress
- networkInterfaceTrafficMode
- networkInterfaceType
- primaryIPAddress
- securityGroupIDs
- status
- vSwitchID
type: object
type: object
nextSyncOpenAPITime:
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/crds/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func getCRD(name string) apiextensionsv1.CustomResourceDefinition {
switch name {
case CRDPodENI:
crdBytes = crdsPodENI
version = "v0.4.0"
version = "v0.4.1"
case CRDPodNetworking:
crdBytes = crdsPodNetworking
version = "v0.1.0"
case CRDNode:
crdBytes = crdsNode
version = "v0.3.0"
version = "v0.3.1"
case CRDNodeRuntime:
crdBytes = crdsNodeRuntime
version = "v0.1.0"
Expand Down
Loading

0 comments on commit 7fa229c

Please sign in to comment.