-
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.
- Loading branch information
Showing
6 changed files
with
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2020 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 handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/apis/clusterinformation/v1beta1" | ||
"github.com/vmware-tanzu/antrea/pkg/monitor" | ||
) | ||
|
||
var _ Factory = new(AgentInfo) | ||
|
||
// AntreaAgentInfoResponse is the struct for the response of agent-info command. | ||
// It includes all fields except meta info from v1beta1.AntreaAgentInfo struct. | ||
type AntreaAgentInfoResponse struct { | ||
Version string `json:"version,omitempty"` // Antrea binary version | ||
PodRef corev1.ObjectReference `json:"podRef,omitempty"` // The Pod that Antrea Agent is running in | ||
NodeRef corev1.ObjectReference `json:"nodeRef,omitempty"` // The Node that Antrea Agent is running in | ||
NodeSubnet []string `json:"nodeSubnet,omitempty"` // Node subnet | ||
OVSInfo v1beta1.OVSInfo `json:"ovsInfo,omitempty"` // OVS Information | ||
NetworkPolicyControllerInfo v1beta1.NetworkPolicyControllerInfo `json:"networkPolicyControllerInfo,omitempty"` // Antrea Agent NetworkPolicy information | ||
LocalPodNum int32 `json:"localPodNum,omitempty"` // The number of Pods which the agent is in charge of | ||
AgentConditions []v1beta1.AgentCondition `json:"agentConditions,omitempty"` // Agent condition contains types like AgentHealthy | ||
} | ||
|
||
// AgentInfo is the implementation of the Factory interface for the agent-info command. | ||
type AgentInfo struct{} | ||
|
||
// Handler returns the function which can handle queries issued by agent-info commands, | ||
// the handler function populate component's agent-info to the response. | ||
func (v *AgentInfo) Handler(aq monitor.AgentQuerier, cq monitor.ControllerQuerier) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var info *AntreaAgentInfoResponse | ||
if aq != nil { | ||
allInfo := aq.GetAgentInfo() | ||
info = &AntreaAgentInfoResponse{ | ||
Version: allInfo.Version, | ||
PodRef: allInfo.PodRef, | ||
NodeRef: allInfo.NodeRef, | ||
OVSInfo: allInfo.OVSInfo, | ||
NetworkPolicyControllerInfo: allInfo.NetworkPolicyControllerInfo, | ||
LocalPodNum: allInfo.LocalPodNum, | ||
AgentConditions: allInfo.AgentConditions, | ||
} | ||
} | ||
err := json.NewEncoder(w).Encode(info) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
klog.Errorf("Error when encoding AntreaAgentInfo to json: %v", err) | ||
} | ||
} | ||
} |
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,87 @@ | ||
// Copyright 2020 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 handlers | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/apis/clusterinformation/v1beta1" | ||
mockmonitor "github.com/vmware-tanzu/antrea/pkg/monitor/testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var antreaAgentInfo0 v1beta1.AntreaAgentInfo = v1beta1.AntreaAgentInfo{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node0-k8", | ||
}, | ||
Version: "1.0", | ||
PodRef: corev1.ObjectReference{ | ||
Kind: "Pod", | ||
Namespace: "kube-system", | ||
Name: "antrea-agent-flx99", | ||
}, | ||
NodeRef: corev1.ObjectReference{ | ||
Kind: "Node", | ||
Name: "node0-k8", | ||
}, | ||
NodeSubnet: []string{ | ||
"192.168.0.0/24", | ||
}, | ||
OVSInfo: v1beta1.OVSInfo{}, | ||
NetworkPolicyControllerInfo: v1beta1.NetworkPolicyControllerInfo{}, | ||
LocalPodNum: 1, | ||
AgentConditions: []v1beta1.AgentCondition{ | ||
{ | ||
Type: v1beta1.AgentHealthy, | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
} | ||
|
||
func TestAgentInfo(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
testcases := map[string]struct { | ||
testNode corev1.ObjectReference | ||
agentInfo *v1beta1.AntreaAgentInfo | ||
expectedOutput string | ||
expectedStatusCode int | ||
}{ | ||
"AgentInfo": { | ||
agentInfo: &antreaAgentInfo0, | ||
expectedOutput: "{\"version\":\"1.0\",\"podRef\":{\"kind\":\"Pod\",\"namespace\":\"kube-system\",\"name\":\"antrea-agent-flx99\"},\"nodeRef\":{\"kind\":\"Node\",\"name\":\"node0-k8\"},\"ovsInfo\":{},\"networkPolicyControllerInfo\":{},\"localPodNum\":1,\"agentConditions\":[{\"type\":\"AgentHealthy\",\"status\":\"True\",\"lastHeartbeatTime\":null}]}\n", | ||
expectedStatusCode: http.StatusOK, | ||
}, | ||
} | ||
for k, tc := range testcases { | ||
t.Run(k, func(t *testing.T) { | ||
req, err := http.NewRequest("GET", "/", nil) | ||
assert.Nil(t, err) | ||
recorder := httptest.NewRecorder() | ||
aq := mockmonitor.NewMockAgentQuerier(ctrl) | ||
aq.EXPECT().GetAgentInfo().Return(tc.agentInfo) | ||
new(AgentInfo).Handler(aq, nil).ServeHTTP(recorder, req) | ||
assert.Equal(t, tc.expectedStatusCode, recorder.Code, k) | ||
assert.Equal(t, tc.expectedOutput, recorder.Body.String(), k) | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.