-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "antctl get bgppeers" agent command
Signed-off-by: Kumar Atish <kumar.atish@broadcom.com>
- Loading branch information
Showing
11 changed files
with
337 additions
and
1 deletion.
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
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,66 @@ | ||
// 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 bgppeer | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"reflect" | ||
"strconv" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"antrea.io/antrea/pkg/agent/apis" | ||
"antrea.io/antrea/pkg/agent/controller/bgp" | ||
"antrea.io/antrea/pkg/querier" | ||
) | ||
|
||
// HandleFunc returns the function which can handle queries issued by the bgppeers command. | ||
func HandleFunc(bq querier.AgentBGPPolicyInfoQuerier) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if bq == nil || reflect.ValueOf(bq).IsNil() { | ||
// The error message must match the "FOO is not enabled" pattern to pass antctl e2e tests. | ||
http.Error(w, "bgp is not enabled", http.StatusServiceUnavailable) | ||
return | ||
} | ||
|
||
peers, err := bq.GetBGPPeerStatus(r.Context()) | ||
if err != nil { | ||
if errors.Is(err, bgp.ErrBGPPolicyNotFound) { | ||
http.Error(w, "there is no effective bgp policy applied to the Node", http.StatusNotFound) | ||
return | ||
} else { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
var bgpPeersResp []apis.BGPPeerResponse | ||
for _, peer := range peers { | ||
bgpPeersResp = append(bgpPeersResp, apis.BGPPeerResponse{ | ||
Peer: net.JoinHostPort(peer.Address, strconv.Itoa(int(peer.Port))), | ||
ASN: peer.ASN, | ||
State: string(peer.SessionState), | ||
}) | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(bgpPeersResp); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
klog.ErrorS(err, "Error when encoding BGPPeersResp to json") | ||
} | ||
} | ||
} |
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,102 @@ | ||
// 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 bgppeer | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
|
||
"antrea.io/antrea/pkg/agent/apis" | ||
"antrea.io/antrea/pkg/agent/bgp" | ||
bgpcontroller "antrea.io/antrea/pkg/agent/controller/bgp" | ||
queriertest "antrea.io/antrea/pkg/querier/testing" | ||
) | ||
|
||
func TestBGPPeerQuery(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fakeBGPPeerStatus []bgp.PeerStatus | ||
expectedStatus int | ||
expectedResponse []apis.BGPPeerResponse | ||
fakeErr error | ||
}{ | ||
{ | ||
name: "bgpPolicyState exists", | ||
fakeBGPPeerStatus: []bgp.PeerStatus{ | ||
{ | ||
Address: "192.168.77.200", | ||
Port: 179, | ||
ASN: 65001, | ||
SessionState: bgp.SessionEstablished, | ||
}, | ||
{ | ||
Address: "192.168.77.201", | ||
Port: 179, | ||
ASN: 65002, | ||
SessionState: bgp.SessionActive, | ||
}, | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResponse: []apis.BGPPeerResponse{ | ||
{ | ||
Peer: "192.168.77.200:179", | ||
ASN: 65001, | ||
State: "Established", | ||
}, | ||
{ | ||
Peer: "192.168.77.201:179", | ||
ASN: 65002, | ||
State: "Active", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "bgpPolicyState does not exist", | ||
fakeBGPPeerStatus: nil, | ||
expectedStatus: http.StatusNotFound, | ||
fakeErr: bgpcontroller.ErrBGPPolicyNotFound, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
q := queriertest.NewMockAgentBGPPolicyInfoQuerier(ctrl) | ||
q.EXPECT().GetBGPPeerStatus(context.Background()).Return(tt.fakeBGPPeerStatus, tt.fakeErr) | ||
handler := HandleFunc(q) | ||
|
||
req, err := http.NewRequest(http.MethodGet, "", nil) | ||
require.NoError(t, err) | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, req) | ||
assert.Equal(t, tt.expectedStatus, recorder.Code) | ||
|
||
if tt.expectedStatus == http.StatusOK { | ||
var received []apis.BGPPeerResponse | ||
err = json.Unmarshal(recorder.Body.Bytes(), &received) | ||
require.NoError(t, err) | ||
assert.ElementsMatch(t, tt.expectedResponse, received) | ||
} | ||
}) | ||
} | ||
} |
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.