Skip to content

Commit

Permalink
Followup antctl code refactor
Browse files Browse the repository at this point in the history
This solution refactors the transform functions
for antctl networkpolicy to indicate request and
response. Also adds some additional unit tests.

Signed-off-by: Qiyue Yao <[email protected]>
  • Loading branch information
qiyueyao committed Mar 6, 2024
1 parent 9f4adb5 commit f275d81
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
3 changes: 1 addition & 2 deletions pkg/antctl/antctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"antrea.io/antrea/pkg/agent/apiserver/handlers/podinterface"
"antrea.io/antrea/pkg/agent/apiserver/handlers/serviceexternalip"
fallbackversion "antrea.io/antrea/pkg/antctl/fallback/version"
"antrea.io/antrea/pkg/antctl/parameter"
"antrea.io/antrea/pkg/antctl/raw/featuregates"
"antrea.io/antrea/pkg/antctl/raw/multicluster"
"antrea.io/antrea/pkg/antctl/raw/proxy"
Expand Down Expand Up @@ -537,7 +536,7 @@ $ antctl get podmulticaststats pod -n namespace`,
shorthand: "D",
},
},
parameterTransform: parameter.NewNetworkPolicyEvaluation,
parameterTransform: networkpolicy.NewNetworkPolicyEvaluation,
restMethod: restPost,
},
addonTransform: networkpolicy.EvaluationTransform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package parameter
package networkpolicy

import (
"fmt"
Expand All @@ -37,6 +37,8 @@ func parsePeer(str string) (string, string) {
return ns, pod
}

// NewNetworkPolicyEvaluation creates a new NetworkPolicyEvaluation resource
// request from the command-line arguments provided to antctl.
func NewNetworkPolicyEvaluation(args map[string]string) (runtime.Object, error) {
var ns1, pod1, ns2, pod2 string
if val, ok := args["source"]; ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package parameter
package networkpolicy

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Antrea Authors
// Copyright 2024 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -163,6 +163,8 @@ func (r Response) SortRows() bool {
return false
}

// EvaluationResponse stores the response from NetworkPolicyEvaluation command,
// and implements TableOutput.
type EvaluationResponse struct {
*cpv1beta.NetworkPolicyEvaluation
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,65 +62,108 @@ func TestListTransform(t *testing.T) {
TierPriority: pointer.Int32(200),
Priority: pointer.Float64(8),
}
var npD = cpv1beta.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "d",
UID: "aab",
CreationTimestamp: metav1.Time{Time: metav1.Now().Add(3)},
},
SourceRef: &cpv1beta.NetworkPolicyReference{
Name: "d",
},
}
var npE = cpv1beta.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "e",
UID: "bbb",
CreationTimestamp: metav1.Time{Time: metav1.Now().Add(4)},
},
SourceRef: &cpv1beta.NetworkPolicyReference{
Name: "e",
},
}

var npList = &cpv1beta.NetworkPolicyList{
var npList1 = &cpv1beta.NetworkPolicyList{
Items: []cpv1beta.NetworkPolicy{npA, npC, npB},
}
var npList2 = &cpv1beta.NetworkPolicyList{
Items: []cpv1beta.NetworkPolicy{npA, npE, npD, npC},
}

tests := []struct {
name string
opts map[string]string
expectedResponse []Response
npList *cpv1beta.NetworkPolicyList
expectedResponse interface{}
expectedError string
}{
{
name: "sort by name",
opts: map[string]string{
"sort-by": ".sourceRef.name",
},
npList: npList1,
expectedResponse: []Response{{&npA}, {&npB}, {&npC}},
},
{
name: "sort by uid",
opts: map[string]string{
"sort-by": ".metadata.uid",
},
npList: npList1,
expectedResponse: []Response{{&npB}, {&npC}, {&npA}},
},
{
name: "sort by creationTimestamp",
opts: map[string]string{
"sort-by": ".metadata.creationTimestamp",
},
npList: npList1,
expectedResponse: []Response{{&npA}, {&npB}, {&npC}},
},
{
name: "sort by effectivePriority",
opts: map[string]string{
"sort-by": "effectivePriority",
},
npList: npList1,
expectedResponse: []Response{{&npC}, {&npA}, {&npB}},
},
{
name: "sort by name default",
opts: map[string]string{
"sort-by": "",
},
npList: npList1,
expectedResponse: []Response{{&npA}, {&npB}, {&npC}},
},
{
name: "sort by effectivePriority including K8s np",
opts: map[string]string{
"sort-by": "effectivePriority",
},
npList: npList2,
expectedResponse: []Response{{&npC}, {&npD}, {&npE}, {&npA}},
},
{
name: "invalid case",
opts: map[string]string{
"sort-by": "effective",
},
npList: npList1,
expectedResponse: []Response{{&npA}, {&npB}, {&npC}},
expectedError: "couldn't find any field with path \"{.effective}\" in the list of objects",
},
{
name: "empty case",
npList: &cpv1beta.NetworkPolicyList{Items: []cpv1beta.NetworkPolicy{}},
expectedResponse: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := listTransform(npList, tt.opts)
result, err := listTransform(tt.npList, tt.opts)
if tt.expectedError == "" {
require.NoError(t, err)
assert.Equal(t, tt.expectedResponse, result)
Expand Down

0 comments on commit f275d81

Please sign in to comment.