Skip to content

Commit

Permalink
metallb: adjust return consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Dec 19, 2024
1 parent 75a2d0c commit 52c0a0d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 121 deletions.
18 changes: 12 additions & 6 deletions pkg/metallb/addresspool.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewIPAddressPoolBuilder(
return nil
}

builder := IPAddressPoolBuilder{
builder := &IPAddressPoolBuilder{
apiClient: apiClient.Client,
Definition: &mlbtypes.IPAddressPool{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -56,21 +56,27 @@ func NewIPAddressPoolBuilder(
glog.V(100).Infof("The name of the IPAddressPool is empty")

builder.errorMsg = "IPAddressPool 'name' cannot be empty"

return builder
}

if nsname == "" {
glog.V(100).Infof("The namespace of the IPAddressPool is empty")

builder.errorMsg = "IPAddressPool 'nsname' cannot be empty"

return builder
}

if len(addrPool) < 1 {
glog.V(100).Infof("The addrPool of the IPAddressPool is empty list")

builder.errorMsg = "IPAddressPool 'addrPool' cannot be empty list"

return builder
}

return &builder
return builder
}

// Get returns IPAddressPool object if found.
Expand Down Expand Up @@ -133,7 +139,7 @@ func PullAddressPool(apiClient *clients.Settings, name, nsname string) (*IPAddre
return nil, err
}

builder := IPAddressPoolBuilder{
builder := &IPAddressPoolBuilder{
apiClient: apiClient.Client,
Definition: &mlbtypes.IPAddressPool{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -161,7 +167,7 @@ func PullAddressPool(apiClient *clients.Settings, name, nsname string) (*IPAddre

builder.Definition = builder.Object

return &builder, nil
return builder, nil
}

// Create makes a IPAddressPool in the cluster and stores the created object in struct.
Expand Down Expand Up @@ -342,13 +348,13 @@ func (builder *IPAddressPoolBuilder) validate() (bool, error) {
if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)

builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD)
return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}

if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)

builder.errorMsg = fmt.Sprintf("%s builder cannot have nil apiClient", resourceCRD)
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}

if builder.errorMsg != "" {
Expand Down
16 changes: 10 additions & 6 deletions pkg/metallb/bfdprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewBFDBuilder(apiClient *clients.Settings, name, nsname string) *BFDBuilder
return nil
}

builder := BFDBuilder{
builder := &BFDBuilder{
apiClient: apiClient.Client,
Definition: &mlbtypes.BFDProfile{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -53,15 +53,19 @@ func NewBFDBuilder(apiClient *clients.Settings, name, nsname string) *BFDBuilder
glog.V(100).Infof("The name of the BFDProfile is empty")

builder.errorMsg = "BFDProfile 'name' cannot be empty"

return builder
}

if nsname == "" {
glog.V(100).Infof("The namespace of the BFDProfile is empty")

builder.errorMsg = "BFDProfile 'nsname' cannot be empty"

return builder
}

return &builder
return builder
}

// Get returns BFDProfile object if found.
Expand Down Expand Up @@ -124,7 +128,7 @@ func PullBFDProfile(apiClient *clients.Settings, name, nsname string) (*BFDBuild
return nil, err
}

builder := BFDBuilder{
builder := &BFDBuilder{
apiClient: apiClient.Client,
Definition: &mlbtypes.BFDProfile{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -152,7 +156,7 @@ func PullBFDProfile(apiClient *clients.Settings, name, nsname string) (*BFDBuild

builder.Definition = builder.Object

return &builder, nil
return builder, nil
}

// Create makes a BFDProfile in the cluster and stores the created object in struct.
Expand Down Expand Up @@ -392,13 +396,13 @@ func (builder *BFDBuilder) validate() (bool, error) {
if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)

builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD)
return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}

if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)

builder.errorMsg = fmt.Sprintf("%s builder cannot have nil apiClient", resourceCRD)
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}

if builder.errorMsg != "" {
Expand Down
30 changes: 10 additions & 20 deletions pkg/metallb/bgpadvertisement.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewBGPAdvertisementBuilder(apiClient *clients.Settings, name, nsname string
return nil
}

builder := BGPAdvertisementBuilder{
builder := &BGPAdvertisementBuilder{
apiClient: apiClient.Client,
Definition: &mlbtypes.BGPAdvertisement{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -53,15 +53,19 @@ func NewBGPAdvertisementBuilder(apiClient *clients.Settings, name, nsname string
glog.V(100).Infof("The name of the BGPAdvertisement is empty")

builder.errorMsg = "BGPAdvertisement 'name' cannot be empty"

return builder
}

if nsname == "" {
glog.V(100).Infof("The namespace of the BGPAdvertisement is empty")

builder.errorMsg = "BGPAdvertisement 'nsname' cannot be empty"

return builder
}

return &builder
return builder
}

// Exists checks whether the given BGPAdvertisement exists.
Expand Down Expand Up @@ -123,7 +127,7 @@ func PullBGPAdvertisement(apiClient *clients.Settings, name, nsname string) (*BG
return nil, err
}

builder := BGPAdvertisementBuilder{
builder := &BGPAdvertisementBuilder{
apiClient: apiClient.Client,
Definition: &mlbtypes.BGPAdvertisement{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -151,7 +155,7 @@ func PullBGPAdvertisement(apiClient *clients.Settings, name, nsname string) (*BG

builder.Definition = builder.Object

return &builder, nil
return builder, nil
}

// Create makes a BGPAdvertisement in the cluster and stores the created object in struct.
Expand Down Expand Up @@ -267,9 +271,7 @@ func (builder *BGPAdvertisementBuilder) WithAggregationLength4(aggregationLength
if aggregationLength < 0 || aggregationLength > 32 {
builder.errorMsg = fmt.Sprintf("AggregationLength %d is invalid, the value shoud be in range 0...32",
aggregationLength)
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -292,9 +294,7 @@ func (builder *BGPAdvertisementBuilder) WithAggregationLength6(aggregationLength
fmt.Printf("%d", aggregationLength)
builder.errorMsg = fmt.Sprintf("AggregationLength %d is invalid, the value shoud be in range 0...128",
aggregationLength)
}

if builder.errorMsg != "" {
return builder
}

Expand Down Expand Up @@ -330,9 +330,7 @@ func (builder *BGPAdvertisementBuilder) WithCommunities(communities []string) *B

if len(communities) < 1 {
builder.errorMsg = "error: community setting is empty list, the list should contain at least one element"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -353,9 +351,7 @@ func (builder *BGPAdvertisementBuilder) WithIPAddressPools(ipAddressPools []stri

if len(ipAddressPools) < 1 {
builder.errorMsg = "error: IPAddressPools setting is empty list, the list should contain at least one element"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -377,9 +373,7 @@ func (builder *BGPAdvertisementBuilder) WithIPAddressPoolsSelectors(

if len(poolSelector) < 1 {
builder.errorMsg = "error: IPAddressPoolSelectors setting is empty list, the list should contain at least one element"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -401,9 +395,7 @@ func (builder *BGPAdvertisementBuilder) WithNodeSelector(

if len(nodeSelectors) < 1 {
builder.errorMsg = "error: nodeSelectors setting is empty list, the list should contain at least one element"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -424,9 +416,7 @@ func (builder *BGPAdvertisementBuilder) WithPeers(peers []string) *BGPAdvertisem

if len(peers) < 1 {
builder.errorMsg = "error: peers setting is empty list, the list should contain at least one element"
}

if builder.errorMsg != "" {
return builder
}

Expand Down Expand Up @@ -482,13 +472,13 @@ func (builder *BGPAdvertisementBuilder) validate() (bool, error) {
if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)

builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD)
return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}

if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)

builder.errorMsg = fmt.Sprintf("%s builder cannot have nil apiClient", resourceCRD)
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}

if builder.errorMsg != "" {
Expand Down
Loading

0 comments on commit 52c0a0d

Please sign in to comment.