-
Notifications
You must be signed in to change notification settings - Fork 370
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
Add antrea agent command `antctl get memberlist` to get state of memberlist cluster of antrea agent. Fixes #4601 Signed-off-by: Kumar Atish <[email protected]>
- Loading branch information
Showing
20 changed files
with
476 additions
and
56 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
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,76 @@ | ||
// 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" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"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, _ := aq.GetNodeInformer().Lister().List(labels.Everything()) | ||
aliveNodes := aq.GetMemberlistCluster().AliveNodes() | ||
for _, node := range allNodes { | ||
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" | ||
"sort" | ||
"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/informers" | ||
"k8s.io/client-go/kubernetes/fake" | ||
|
||
memberlisttest "antrea.io/antrea/pkg/agent/memberlist/testing" | ||
queriertest "antrea.io/antrea/pkg/agent/querier/testing" | ||
) | ||
|
||
var ( | ||
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", | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
func TestMemberlistQuery(t *testing.T) { | ||
clientset := fake.NewSimpleClientset() | ||
informerFactory := informers.NewSharedInformerFactory(clientset, 0) | ||
nodeInformer := informerFactory.Core().V1().Nodes() | ||
|
||
stopCh := make(chan struct{}) | ||
defer close(stopCh) | ||
|
||
informerFactory.Start(stopCh) | ||
informerFactory.WaitForCacheSync(stopCh) | ||
informerFactory.Core().V1().Nodes().Informer().GetIndexer().Add(&node1) | ||
informerFactory.Core().V1().Nodes().Informer().GetIndexer().Add(&node2) | ||
|
||
ctrl := gomock.NewController(t) | ||
q := queriertest.NewMockAgentQuerier(ctrl) | ||
memberlistInterface := memberlisttest.NewMockInterface(ctrl) | ||
q.EXPECT().GetNodeInformer().Return(nodeInformer) | ||
q.EXPECT().GetMemberlistCluster().Return(memberlistInterface) | ||
memberlistInterface.EXPECT().AliveNodes().Return(sets.NewString("node1")) | ||
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) | ||
|
||
expectedResponse := []Response{ | ||
{ | ||
NodeName: "node1", | ||
IP: "172.16.0.11", | ||
Status: "Alive", | ||
}, | ||
{ | ||
NodeName: "node2", | ||
IP: "172.16.0.12", | ||
Status: "Dead", | ||
}, | ||
} | ||
var received []Response | ||
err = json.Unmarshal(recorder.Body.Bytes(), &received) | ||
require.NoError(t, err) | ||
sort.Slice(received, func(i, j int) bool { | ||
return received[i].NodeName < received[j].NodeName | ||
}) | ||
assert.Equal(t, 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.