forked from antrea-io/antrea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command to get memberlist state
Fixes antrea-io#4601 Signed-off-by: Kumar Atish <atish.iaf@gmail.com>
- Loading branch information
Showing
15 changed files
with
271 additions
and
7 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
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,82 @@ | ||
// Copyright 2023 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 memberlist | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/klog/v2" | ||
|
||
"antrea.io/antrea/pkg/agent/querier" | ||
) | ||
|
||
// Response describes the response struct of memberlist command. | ||
type Response struct { | ||
NodeName string `json:"nodeName,omitempty"` | ||
IP string `json:"ip,omitempty"` | ||
Status string `json:"status,omitempty"` | ||
} | ||
|
||
func generateResponse(node v1.Node, aliveNodes sets.String) Response { | ||
status := "Dead" | ||
if aliveNodes.Has(node.Name) { | ||
status = "Alive" | ||
} | ||
return Response{ | ||
NodeName: node.Name, | ||
Status: status, | ||
IP: node.Status.Addresses[0].Address, | ||
} | ||
} | ||
|
||
// HandleFunc returns the function which can handle queries issued by the memberlist command. | ||
func HandleFunc(aq querier.AgentQuerier) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var memberlist []Response | ||
allNodes, err := aq.GetK8sClient().CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
klog.Errorf("Error when listing all Nodes: %v", err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
aliveNodes := aq.GetAliveNodes() | ||
for _, node := range allNodes.Items { | ||
memberlist = append(memberlist, generateResponse(node, aliveNodes)) | ||
} | ||
|
||
err = json.NewEncoder(w).Encode(memberlist) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
klog.Errorf("Error when encoding Memberlist to json: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func (r Response) GetTableHeader() []string { | ||
return []string{"NODE", "IP", "STATUS"} | ||
} | ||
|
||
func (r Response) GetTableRow(_ int) []string { | ||
return []string{r.NodeName, r.IP, r.Status} | ||
} | ||
|
||
func (r Response) SortRows() bool { | ||
return true | ||
} |
107 changes: 107 additions & 0 deletions
107
pkg/agent/apiserver/handlers/memberlist/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,107 @@ | ||
// Copyright 2023 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 memberlist | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/client-go/kubernetes/fake" | ||
|
||
queriertest "antrea.io/antrea/pkg/agent/querier/testing" | ||
) | ||
|
||
var ( | ||
expectedContent = []Response{ | ||
{ | ||
NodeName: "node1", | ||
IP: "172.16.0.11", | ||
Status: "Alive", | ||
}, | ||
{ | ||
NodeName: "node2", | ||
IP: "172.16.0.12", | ||
Status: "Alive", | ||
}, | ||
{ | ||
NodeName: "node3", | ||
IP: "172.16.0.13", | ||
Status: "Dead", | ||
}, | ||
} | ||
|
||
node1 = v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "node1"}, | ||
Status: v1.NodeStatus{ | ||
Addresses: []v1.NodeAddress{ | ||
{ | ||
Address: "172.16.0.11", | ||
}, | ||
}, | ||
}, | ||
} | ||
node2 = v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "node2"}, | ||
Status: v1.NodeStatus{ | ||
Addresses: []v1.NodeAddress{ | ||
{ | ||
Address: "172.16.0.12", | ||
}, | ||
}, | ||
}, | ||
} | ||
node3 = v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "node3"}, | ||
Status: v1.NodeStatus{ | ||
Addresses: []v1.NodeAddress{ | ||
{ | ||
Address: "172.16.0.13", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
aliveNodes = sets.NewString("node1", "node2") | ||
) | ||
|
||
func TestMemberlistQuery(t *testing.T) { | ||
k8sClient := fake.NewSimpleClientset(&node1, &node2, &node3) | ||
ctrl := gomock.NewController(t) | ||
q := queriertest.NewMockAgentQuerier(ctrl) | ||
q.EXPECT().GetK8sClient().Return(k8sClient) | ||
q.EXPECT().GetAliveNodes().Return(aliveNodes) | ||
|
||
handler := HandleFunc(q) | ||
|
||
req, err := http.NewRequest(http.MethodGet, "", nil) | ||
require.NoError(t, err) | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, req) | ||
assert.Equal(t, http.StatusOK, recorder.Code) | ||
|
||
var received []Response | ||
err = json.Unmarshal(recorder.Body.Bytes(), &received) | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedContent, 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.