Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

catalog: simplify function isValidTrafficTarget() #2246

Merged
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
5 changes: 1 addition & 4 deletions pkg/catalog/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,7 @@ func (mc *MeshCatalog) listPoliciesFromTrafficTargets(sa service.K8sServiceAccou
}

func isValidTrafficTarget(t *target.TrafficTarget) bool {
if t.Spec.Rules == nil || len(t.Spec.Rules) == 0 {
return false
}
return true
return t.Spec.Rules != nil && len(t.Spec.Rules) > 0
draychev marked this conversation as resolved.
Show resolved Hide resolved
}

// buildPolicyName creates a name for a policy associated with the given service
Expand Down
56 changes: 33 additions & 23 deletions pkg/catalog/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ import (
func TestIsValidTrafficTarget(t *testing.T) {
assert := assert.New(t)

getTrafficTarget := func(rules []target.TrafficTargetRule) *target.TrafficTarget {
return &target.TrafficTarget{
TypeMeta: v1.TypeMeta{
APIVersion: "access.smi-spec.io/v1alpha2",
Kind: "TrafficTarget",
},
ObjectMeta: v1.ObjectMeta{
Name: "target",
Namespace: "default",
},
Spec: target.TrafficTargetSpec{
Destination: target.IdentityBindingSubject{
Kind: "Name",
Name: "dest-id",
Namespace: "default",
},
Sources: []target.IdentityBindingSubject{{
Kind: "Name",
Name: "source-id",
Namespace: "default",
}},
Rules: rules,
},
}
}

testCases := []struct {
name string
input *target.TrafficTarget
Expand All @@ -38,29 +64,13 @@ func TestIsValidTrafficTarget(t *testing.T) {
expected: true,
},
{
name: "is not valid",
input: &target.TrafficTarget{
TypeMeta: v1.TypeMeta{
APIVersion: "access.smi-spec.io/v1alpha2",
Kind: "TrafficTarget",
},
ObjectMeta: v1.ObjectMeta{
Name: "target",
Namespace: "default",
},
Spec: target.TrafficTargetSpec{
Destination: target.IdentityBindingSubject{
Kind: "Name",
Name: "dest-id",
Namespace: "default",
},
Sources: []target.IdentityBindingSubject{{
Kind: "Name",
Name: "source-id",
Namespace: "default",
}},
},
},
name: "is not valid because TrafficTarget.Spec.Rules is nil",
input: getTrafficTarget(nil),
expected: false,
},
{
name: "is not valid because TrafficTarget.Spec.Rules is not nil but is empty",
input: getTrafficTarget([]target.TrafficTargetRule{}),
expected: false,
},
}
Expand Down