-
Notifications
You must be signed in to change notification settings - Fork 346
/
handler.go
94 lines (82 loc) · 2.92 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2022 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 multicast
import (
"encoding/json"
"net/http"
"reflect"
"strconv"
"antrea.io/antrea/pkg/agent/multicast"
"antrea.io/antrea/pkg/antctl/transform/common"
"antrea.io/antrea/pkg/querier"
)
type Response struct {
PodName string `json:"name,omitempty" antctl:"name,Name of the Pod"`
PodNamespace string `json:"podNamespace,omitempty"`
Inbound string `json:"inbound,omitempty"`
Outbound string `json:"outbound,omitempty"`
}
func generateResponse(podName string, podNamespace string, trafficStats *multicast.PodTrafficStats) Response {
return Response{
PodName: podName,
PodNamespace: podNamespace,
Inbound: strconv.FormatUint(trafficStats.Inbound, 10),
Outbound: strconv.FormatUint(trafficStats.Outbound, 10),
}
}
// HandleFunc returns the function which can handle queries issued by 'antctl get podmulticaststats' command.
// It will return Pod multicast traffic statistics for the local Node.
func HandleFunc(mq querier.AgentMulticastInfoQuerier) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if mq == nil || reflect.ValueOf(mq).IsNil() {
http.Error(w, "Multicast is not enabled", http.StatusServiceUnavailable)
return
}
name := r.URL.Query().Get("name")
ns := r.URL.Query().Get("namespace")
responses := []Response{}
if name != "" && ns != "" {
podStats := mq.GetPodStats(name, ns)
if podStats == nil {
w.WriteHeader(http.StatusNotFound)
return
}
responses = append(responses, generateResponse(name, ns, podStats))
} else if ns == "" && name != "" {
http.Error(w, "name option should be used with namespace option", http.StatusServiceUnavailable)
return
} else {
allPodStats := mq.GetAllPodsStats()
for iface, trafficStats := range allPodStats {
if ns == "" || ns == iface.PodNamespace {
responses = append(responses, generateResponse(iface.PodName, iface.PodNamespace, trafficStats))
}
}
}
err := json.NewEncoder(w).Encode(responses)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}
var _ common.TableOutput = new(Response)
func (r Response) GetTableHeader() []string {
return []string{"NAMESPACE", "NAME", "INBOUND", "OUTBOUND"}
}
func (r Response) GetTableRow(_ int) []string {
return []string{r.PodNamespace, r.PodName, r.Inbound, r.Outbound}
}
func (r Response) SortRows() bool {
return true
}