diff --git a/pkg/antctl/antctl.go b/pkg/antctl/antctl.go index ad597f21519..eef5a490d91 100644 --- a/pkg/antctl/antctl.go +++ b/pkg/antctl/antctl.go @@ -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" @@ -537,7 +536,7 @@ $ antctl get podmulticaststats pod -n namespace`, shorthand: "D", }, }, - parameterTransform: parameter.NewNetworkPolicyEvaluation, + parameterTransform: networkpolicy.NewNetworkPolicyEvaluation, restMethod: restPost, }, addonTransform: networkpolicy.EvaluationTransform, diff --git a/pkg/antctl/parameter/parameter.go b/pkg/antctl/transform/networkpolicy/request.go similarity index 92% rename from pkg/antctl/parameter/parameter.go rename to pkg/antctl/transform/networkpolicy/request.go index 88c9631a630..614a504f29d 100644 --- a/pkg/antctl/parameter/parameter.go +++ b/pkg/antctl/transform/networkpolicy/request.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package parameter +package networkpolicy import ( "fmt" @@ -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 { diff --git a/pkg/antctl/parameter/parameter_test.go b/pkg/antctl/transform/networkpolicy/request_test.go similarity index 99% rename from pkg/antctl/parameter/parameter_test.go rename to pkg/antctl/transform/networkpolicy/request_test.go index c13676e8358..6833db5f1c2 100644 --- a/pkg/antctl/parameter/parameter_test.go +++ b/pkg/antctl/transform/networkpolicy/request_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package parameter +package networkpolicy import ( "testing" diff --git a/pkg/antctl/transform/networkpolicy/transform.go b/pkg/antctl/transform/networkpolicy/response.go similarity index 97% rename from pkg/antctl/transform/networkpolicy/transform.go rename to pkg/antctl/transform/networkpolicy/response.go index f9f3a089e57..94b59381502 100644 --- a/pkg/antctl/transform/networkpolicy/transform.go +++ b/pkg/antctl/transform/networkpolicy/response.go @@ -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. @@ -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 } diff --git a/pkg/antctl/transform/networkpolicy/transform_test.go b/pkg/antctl/transform/networkpolicy/response_test.go similarity index 75% rename from pkg/antctl/transform/networkpolicy/transform_test.go rename to pkg/antctl/transform/networkpolicy/response_test.go index cc409bd8c6e..728185000da 100644 --- a/pkg/antctl/transform/networkpolicy/transform_test.go +++ b/pkg/antctl/transform/networkpolicy/response_test.go @@ -62,15 +62,39 @@ 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 }{ { @@ -78,6 +102,7 @@ func TestListTransform(t *testing.T) { opts: map[string]string{ "sort-by": ".sourceRef.name", }, + npList: npList1, expectedResponse: []Response{{&npA}, {&npB}, {&npC}}, }, { @@ -85,6 +110,7 @@ func TestListTransform(t *testing.T) { opts: map[string]string{ "sort-by": ".metadata.uid", }, + npList: npList1, expectedResponse: []Response{{&npB}, {&npC}, {&npA}}, }, { @@ -92,6 +118,7 @@ func TestListTransform(t *testing.T) { opts: map[string]string{ "sort-by": ".metadata.creationTimestamp", }, + npList: npList1, expectedResponse: []Response{{&npA}, {&npB}, {&npC}}, }, { @@ -99,6 +126,7 @@ func TestListTransform(t *testing.T) { opts: map[string]string{ "sort-by": "effectivePriority", }, + npList: npList1, expectedResponse: []Response{{&npC}, {&npA}, {&npB}}, }, { @@ -106,21 +134,36 @@ func TestListTransform(t *testing.T) { 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)