Skip to content

Commit

Permalink
networkpolicy: adjust return consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Jan 3, 2025
1 parent f01dfc5 commit 5651a79
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 29 deletions.
4 changes: 0 additions & 4 deletions pkg/networkpolicy/multinetegressrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ func (builder *EgressRuleBuilder) WithOptions(options ...EgressAdditionalOptions
func (builder *EgressRuleBuilder) WithPeerPodSelector(podSelector metav1.LabelSelector) *EgressRuleBuilder {
glog.V(100).Infof("Adding peer pod selector %v to EgressRule", podSelector)

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

builder.definition.To = append(builder.definition.To, v1beta1.MultiNetworkPolicyPeer{PodSelector: &podSelector})

return builder
Expand Down
6 changes: 6 additions & 0 deletions pkg/networkpolicy/multinetegressrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func TestEgressWithPeerPodSelector(t *testing.T) {
assert.Len(t, builder.definition.To, 1)
assert.Equal(t, builder.definition.To[0].PodSelector.MatchLabels["app"], "nginx")

builder = NewEgressRuleBuilder()

//nolint:goconst
builder.errorMsg = "error"

Expand Down Expand Up @@ -205,6 +207,8 @@ func TestEgressWithPeerPodSelectorAndCIDR(t *testing.T) {
assert.Equal(t, builder.definition.To[0].PodSelector.MatchLabels["app"], "nginx")
assert.Equal(t, builder.definition.To[0].IPBlock.CIDR, "192.168.0.1/24")

builder = NewEgressRuleBuilder()

// Test invalid CIDR
builder.WithPeerPodSelectorAndCIDR(metav1.LabelSelector{
MatchLabels: map[string]string{
Expand All @@ -214,6 +218,8 @@ func TestEgressWithPeerPodSelectorAndCIDR(t *testing.T) {

assert.Equal(t, builder.errorMsg, "Invalid CIDR argument 192.55.55.55")

builder = NewEgressRuleBuilder()

// Test with exception
builder.WithPeerPodSelectorAndCIDR(metav1.LabelSelector{
MatchLabels: map[string]string{
Expand Down
10 changes: 1 addition & 9 deletions pkg/networkpolicy/multinetingressrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ func (builder *IngressRuleBuilder) WithOptions(options ...IngressAdditionalOptio
func (builder *IngressRuleBuilder) WithPeerPodSelector(podSelector metav1.LabelSelector) *IngressRuleBuilder {
glog.V(100).Infof("Adding peer pod selector %v to Ingress Rule", podSelector)

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

builder.definition.From = append(
builder.definition.From, v1beta1.MultiNetworkPolicyPeer{
PodSelector: &podSelector,
Expand Down Expand Up @@ -203,10 +199,6 @@ func (builder *IngressRuleBuilder) WithPeerPodSelectorAndCIDR(
podSelector metav1.LabelSelector, cidr string, except ...[]string) *IngressRuleBuilder {
glog.V(100).Infof("Adding peer pod selector %v and CIDR %s to IngressRule", podSelector, cidr)

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

builder.WithPeerPodSelector(podSelector)
builder.WithCIDR(cidr, except...)

Expand Down Expand Up @@ -238,7 +230,7 @@ func (builder *IngressRuleBuilder) validate() (bool, error) {
if builder.definition == nil {
glog.V(100).Infof("The %s is undefined", objectName)

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

if builder.errorMsg != "" {
Expand Down
12 changes: 0 additions & 12 deletions pkg/networkpolicy/multinetingressrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,6 @@ func TestIngressWithPeerPodSelector(t *testing.T) {

assert.Len(t, builder.definition.From, 1)
assert.Equal(t, builder.definition.From[0].PodSelector.MatchLabels["app"], "nginx")

builder = NewIngressRuleBuilder()

builder.errorMsg = "error"

builder.WithPeerPodSelector(metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "nginx",
},
})

assert.Len(t, builder.definition.From, 0)
}

func TestIngressWithPeerNamespaceSelector(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions pkg/networkpolicy/multinetworkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ func (builder *MultiNetworkPolicyBuilder) WithPodSelector(podSelector metav1.Lab
"Creating MultiNetworkPolicy %s in %s namespace with the podSelector defined: %v",
builder.Definition.Name, builder.Definition.Namespace, podSelector)

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

builder.Definition.Spec.PodSelector = podSelector

return builder
Expand Down

0 comments on commit 5651a79

Please sign in to comment.