Skip to content

Commit

Permalink
add error handler
Browse files Browse the repository at this point in the history
Signed-off-by: bo.jiang <bo.jiang@daocloud.io>
  • Loading branch information
ErikJiang committed Feb 19, 2025
1 parent aad2b9c commit a4ae118
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 69 deletions.
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<!--
Add one of the following kinds:
/kind enhancement
/kind feature
/kind bug
/kind api-change
Expand Down
3 changes: 3 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ changelog:
- title: 🚀 Features
labels:
- kind/feature
- title: 🧰 Enhancements
labels:
- kind/enhancement
- title: 🐛 Bug Fixes
labels:
- kind/bug
Expand Down
36 changes: 0 additions & 36 deletions pkg/ecserrors/errors.go

This file was deleted.

18 changes: 18 additions & 0 deletions pkg/errors/ecserrs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errors

import (
"net/http"
)

const (
ErrECSNotFound = "Ecs.0114"
)

// ECSErrorHandler handles ECS-specific errors.
type ECSErrorHandler struct {
BaseErrorHandler
}

func (e ECSErrorHandler) IsNotFound(err error) bool {
return e.StatusCode(err) == http.StatusNotFound && e.ErrorCode(err) == ErrECSNotFound
}
23 changes: 23 additions & 0 deletions pkg/errors/elberrs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

import (
"net/http"
)

const (
ErrAPINotExist = "APIGW.0101"
ErrELBDataConflict = "ELB.8907"
)

// ELBErrorHandler handles ELB-specific errors.
type ELBErrorHandler struct {
BaseErrorHandler
}

func (e ELBErrorHandler) IsNotFound(err error) bool {
return e.StatusCode(err) == http.StatusNotFound && e.ErrorCode(err) == ErrAPINotExist
}

func (e ELBErrorHandler) IsExists(err error) bool {
return e.StatusCode(err) == http.StatusConflict && e.ErrorCode(err) == ErrELBDataConflict
}
29 changes: 29 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package errors

import (
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/sdkerr"
)

// ErrorHandler defines the interface for handling errors.
type ErrorHandler interface {
StatusCode(err error) int
ErrorCode(err error) string
IsNotFound(err error) bool
}

// BaseErrorHandler provides common error handling methods.
type BaseErrorHandler struct{}

func (b BaseErrorHandler) StatusCode(err error) int {
if t, ok := err.(*sdkerr.ServiceResponseError); ok {
return t.StatusCode
}
return -1
}

func (b BaseErrorHandler) ErrorCode(err error) string {
if t, ok := err.(*sdkerr.ServiceResponseError); ok {
return t.ErrorCode
}
return ""
}
19 changes: 19 additions & 0 deletions pkg/errors/vpcerrs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package errors

import (
"net/http"
)

const (
ErrVPCNotFound = "VPC.0202"
)

// VPCErrorHandler handles VPC-specific errors.
type VPCErrorHandler struct {
BaseErrorHandler
}

func (v VPCErrorHandler) IsNotFound(err error) bool {
statusCode, errorCode := v.StatusCode(err), v.ErrorCode(err)
return (statusCode == http.StatusNotFound || statusCode == http.StatusInternalServerError) && errorCode == ErrVPCNotFound
}
3 changes: 1 addition & 2 deletions pkg/services/ecs/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"k8s.io/utils/ptr"

infrav1 "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/api/v1alpha1"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/ecserrors"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/scope"
)

Expand Down Expand Up @@ -397,7 +396,7 @@ func (s *Service) InstanceIfExists(id *string) (*infrav1.Instance, error) {

out, err := s.ShowInstance(*id)
switch {
case ecserrors.IsNotFound(err):
case s.errHandler.IsNotFound(err):
return nil, ErrInstanceNotFoundByID
case err != nil:
return nil, ErrShowInstance
Expand Down
5 changes: 5 additions & 0 deletions pkg/services/ecs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
ecsiface "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2"
"github.com/pkg/errors"

errs "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/errors"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/scope"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/services/elb"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/services/network"
Expand All @@ -33,6 +34,7 @@ type Service struct {
ECSClient *ecsiface.EcsClient
netService *network.Service
elbService *elb.Service
errHandler errs.ECSErrorHandler
}

// NewService returns a new service given the ECS api client.
Expand All @@ -52,10 +54,13 @@ func NewService(clusterScope scope.ECSScope) (*Service, error) {
return nil, errors.Wrap(err, "failed to create ELB service")
}

errhandler := errs.ECSErrorHandler{BaseErrorHandler: errs.BaseErrorHandler{}}

return &Service{
scope: clusterScope,
ECSClient: ecsClient,
netService: netSvc,
elbService: elbSvc,
errHandler: errhandler,
}, nil
}
17 changes: 6 additions & 11 deletions pkg/services/elb/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package elb
import (
"fmt"
"net/http"
"strings"

infrav1alpha1 "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/api/v1alpha1"
eipmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2/model"
Expand Down Expand Up @@ -59,13 +58,13 @@ func (s *Service) createListener(lbId string, port int32) (string, error) {
}
response, err := s.elbClient.CreateListener(request)
if err != nil {
// listener is already exists
if strings.Contains(err.Error(), "ELB.8907") {
if s.errHandler.IsExists(err) {
return "", nil
} else {
return "", err
}
return "", err
}
fmt.Println("create listener success")
klog.Info("create listener success")
return response.Listener.Id, nil
}

Expand Down Expand Up @@ -97,7 +96,7 @@ func (s *Service) createPool(listenerId string) (string, error) {
if err != nil {
return "", err
}
fmt.Println("create pool success")
klog.Info("create pool success")
return response.Pool.Id, nil
}

Expand Down Expand Up @@ -277,7 +276,7 @@ func (s *Service) getLoadBalancerByName(name string) (*elbmodel.LoadBalancer, er

response, err := s.elbClient.ListLoadBalancers(request)
if err != nil {
if isNotFoundError(err) {
if s.errHandler.IsNotFound(err) {
return nil, nil
}
return nil, errors.Wrapf(err, "failed to list load balancers with name %s", name)
Expand All @@ -290,10 +289,6 @@ func (s *Service) getLoadBalancerByName(name string) (*elbmodel.LoadBalancer, er
return &(*response.Loadbalancers)[0], nil
}

func isNotFoundError(err error) bool {
return strings.Contains(err.Error(), "APIGW.0101")
}

func (s *Service) getAvailabilityZones() ([]string, error) {
availabilityZones := make([]string, 0)
request := &elbmodel.ListAvailabilityZonesRequest{}
Expand Down
16 changes: 10 additions & 6 deletions pkg/services/elb/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package elb
import (
"k8s.io/klog/v2"

errs "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/errors"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/scope"
eip "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2"
eipregion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2/region"
Expand All @@ -11,9 +12,10 @@ import (
)

type Service struct {
scope *scope.ClusterScope
elbClient *elb.ElbClient
eipClient *eip.EipClient
scope *scope.ClusterScope
elbClient *elb.ElbClient
eipClient *eip.EipClient
errHandler errs.ELBErrorHandler
}

func NewService(scope *scope.ClusterScope) (*Service, error) {
Expand Down Expand Up @@ -50,9 +52,11 @@ func NewService(scope *scope.ClusterScope) (*Service, error) {
}
eipCli := eip.NewEipClient(eipHCHttpCli)

errhandler := errs.ELBErrorHandler{BaseErrorHandler: errs.BaseErrorHandler{}}
return &Service{
elbClient: elbCli,
eipClient: eipCli,
scope: scope,
elbClient: elbCli,
eipClient: eipCli,
scope: scope,
errHandler: errhandler,
}, nil
}
20 changes: 12 additions & 8 deletions pkg/services/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
natReg "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/nat/v2/region"
"k8s.io/klog/v2"

errs "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/errors"
"github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/pkg/scope"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
eip "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2"
Expand All @@ -13,10 +14,11 @@ import (
)

type Service struct {
scope *scope.ClusterScope
vpcClient *vpc.VpcClient
eipClient *eip.EipClient
natClient *nat.NatClient
scope *scope.ClusterScope
vpcClient *vpc.VpcClient
eipClient *eip.EipClient
natClient *nat.NatClient
errHandler errs.VPCErrorHandler
}

func NewService(scope *scope.ClusterScope) (*Service, error) {
Expand Down Expand Up @@ -62,10 +64,12 @@ func NewService(scope *scope.ClusterScope) (*Service, error) {
}
natCli := nat.NewNatClient(natHCHttpCli)

errhandler := errs.VPCErrorHandler{BaseErrorHandler: errs.BaseErrorHandler{}}
return &Service{
scope: scope,
vpcClient: vpcCli,
eipClient: eipCli,
natClient: natCli,
scope: scope,
vpcClient: vpcCli,
eipClient: eipCli,
natClient: natCli,
errHandler: errhandler,
}, nil
}
4 changes: 1 addition & 3 deletions pkg/services/network/subnet.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package network

import (
"strings"

infrav1alpha1 "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/api/v1alpha1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v2/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -78,7 +76,7 @@ func (s *Service) deleteSubnets() error {
}
response, err := s.vpcClient.ListSubnets(request)
if err != nil {
if strings.Contains(err.Error(), "VPC.0202") {
if s.errHandler.IsNotFound(err) {
klog.Infof("VPC not found")
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/services/network/vpc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package network

import (
"strings"

infrav1alpha1 "github.com/HuaweiCloudDeveloper/cluster-api-provider-huawei/api/v1alpha1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v2/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -67,7 +65,7 @@ func (s *Service) deleteVPC() error {
}
response, err := s.vpcClient.DeleteVpc(deleteRequest)
if err != nil {
if strings.Contains(err.Error(), "404") {
if s.errHandler.StatusCode(err) == 404 {
klog.Info("VPC already deleted", "vpcID", s.scope.VPC().Id)
return nil
}
Expand Down

0 comments on commit a4ae118

Please sign in to comment.