-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add antctl query for policy conjunction ID
The antctl query will be available in Antrea agents. It returns the OpenFlow conjunction ID(s) assigned for the queried policy's rules on that specific Node. Note that if a policy does not apply to any Pods on that Node, this antctl command will return a http NotFound since the Antrea agent will not have information of that policy. Signed-off-by: Dyanngg <dingyang@vmware.com>
- Loading branch information
Showing
15 changed files
with
391 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
pkg/agent/apiserver/handlers/policyconjunctions/handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package policyconjunctions | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
|
||
agentquerier "antrea.io/antrea/pkg/agent/querier" | ||
cpv1beta "antrea.io/antrea/pkg/apis/controlplane/v1beta2" | ||
"antrea.io/antrea/pkg/querier" | ||
) | ||
|
||
// From user shorthand input to cpv1beta1.NetworkPolicyType | ||
var mapToNetworkPolicyType = map[string]cpv1beta.NetworkPolicyType{ | ||
"K8SNP": cpv1beta.K8sNetworkPolicy, | ||
"ACNP": cpv1beta.AntreaClusterNetworkPolicy, | ||
"ANNP": cpv1beta.AntreaNetworkPolicy, | ||
"ANP": cpv1beta.AdminNetworkPolicy, | ||
} | ||
|
||
var clusterScopedResources = sets.New[string]("ACNP", "ANP") | ||
|
||
// HandleFunc creates a http.HandlerFunc which uses an AgentNetworkPolicyInfoQuerier | ||
// to query network policy rules in current agent. | ||
func HandleFunc(aq agentquerier.AgentQuerier) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
query := r.URL.Query() | ||
uid := query.Get("uid") | ||
npFilter := &querier.NetworkPolicyQueryFilter{Name: uid} | ||
if uid == "" { | ||
if query.Get("source") == "" { | ||
http.Error(w, "policy uid or name must be provided", http.StatusBadRequest) | ||
return | ||
} | ||
policyType := strings.ToUpper(query.Get("type")) | ||
cpType, ok := mapToNetworkPolicyType[policyType] | ||
if !ok { | ||
http.Error(w, "valid policy type must be provided with policy name", http.StatusBadRequest) | ||
return | ||
} | ||
if !clusterScopedResources.Has(policyType) && query.Get("namespace") == "" { | ||
http.Error(w, "policy Namespace must be provided for policy type "+policyType, http.StatusBadRequest) | ||
return | ||
} | ||
npFilter = &querier.NetworkPolicyQueryFilter{ | ||
SourceName: query.Get("source"), | ||
Namespace: query.Get("namespace"), | ||
SourceType: cpType, | ||
} | ||
} | ||
npq := aq.GetNetworkPolicyInfoQuerier() | ||
policies := npq.GetNetworkPolicies(npFilter) | ||
if len(policies) == 0 { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
uid = string(policies[0].SourceRef.UID) | ||
realizedRules := npq.GetRealizedRulesByPolicy(uid) | ||
if err := json.NewEncoder(w).Encode(realizedRules); err != nil { | ||
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
pkg/agent/apiserver/handlers/policyconjunctions/handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// 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. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package policyconjunctions | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
aqtest "antrea.io/antrea/pkg/agent/querier/testing" | ||
cpv1beta "antrea.io/antrea/pkg/apis/controlplane/v1beta2" | ||
qtest "antrea.io/antrea/pkg/querier/testing" | ||
) | ||
|
||
func TestBadRequests(t *testing.T) { | ||
badRequests := map[string]string{ | ||
"Policy name only": "?source=allow-http", | ||
"No policy type": "?source=allow-http&namespace=ns1", | ||
"No namespace for ANNP": "?source=allow-http&type=ANNP", | ||
"No namespace for K8s NP": "?source=allow-http&type=K8sNP", | ||
} | ||
handler := HandleFunc(nil) | ||
for k, r := range badRequests { | ||
req, err := http.NewRequest(http.MethodGet, r, nil) | ||
assert.Nil(t, err) | ||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, req) | ||
assert.Equal(t, http.StatusBadRequest, recorder.Code, k) | ||
} | ||
} | ||
|
||
func TestPolicyConjunctionsQuery(t *testing.T) { | ||
c := gomock.NewController(t) | ||
tests := []struct { | ||
name string | ||
query string | ||
policiesReturned []cpv1beta.NetworkPolicy | ||
expectedStatus int | ||
}{ | ||
{ | ||
name: "policy found", | ||
query: "?uid=uid1", | ||
policiesReturned: []cpv1beta.NetworkPolicy{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "uid1", | ||
}, | ||
SourceRef: &cpv1beta.NetworkPolicyReference{ | ||
Type: cpv1beta.AntreaClusterNetworkPolicy, | ||
Name: "test-acnp", | ||
UID: "uid1", | ||
}, | ||
}, | ||
}, | ||
expectedStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "policy not found", | ||
query: "?uid=uid2", | ||
policiesReturned: []cpv1beta.NetworkPolicy{}, | ||
expectedStatus: http.StatusNotFound, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
npQuerier := qtest.NewMockAgentNetworkPolicyInfoQuerier(c) | ||
npQuerier.EXPECT().GetNetworkPolicies(gomock.Any()).Return(tt.policiesReturned).Times(1) | ||
if len(tt.policiesReturned) == 1 { | ||
npQuerier.EXPECT().GetRealizedRulesByPolicy(string(tt.policiesReturned[0].SourceRef.UID)).Times(1) | ||
} | ||
aq := aqtest.NewMockAgentQuerier(c) | ||
aq.EXPECT().GetNetworkPolicyInfoQuerier().Return(npQuerier).Times(1) | ||
|
||
handler := HandleFunc(aq) | ||
req, err := http.NewRequest(http.MethodGet, tt.query, nil) | ||
assert.Nil(t, err) | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, req) | ||
assert.Equal(t, tt.expectedStatus, recorder.Code) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.