-
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
5 changed files
with
233 additions
and
0 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,58 @@ | ||
// 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" | ||
|
||
"k8s.io/klog" | ||
|
||
"github.com/vmware-tanzu/antrea/pkg/monitor" | ||
) | ||
|
||
var _ Factory = new(AgentInfo) | ||
|
||
// ComponentAgentInfoResponse describes the internal response struct of agent-info command. | ||
// The first three fields refer to connection between the agent and controller, OVSDB and | ||
// openflow. Subnet refers to the subnet range from the Pod CIDR allocated to the Node. | ||
type ComponentAgentInfoResponse struct { | ||
ControllerConn monitor.ConnStatus `json:"controllerConn,omitempty" yaml:"controllerConn,omitempty"` | ||
OVSDBConn monitor.ConnStatus `json:"ovsdbConn,omitempty" yaml:"ovsdbConn,omitempty"` | ||
OFConn monitor.ConnStatus `json:"ofConn,omitempty" yaml:"ofConn,omitempty"` | ||
Subnet string `json:"subnet,omitempty" yaml:"subnet,omitempty"` | ||
} | ||
|
||
// 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 ComponentAgentInfoResponse. | ||
func (v *AgentInfo) Handler(aq monitor.AgentQuerier, cq monitor.ControllerQuerier) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var m ComponentAgentInfoResponse | ||
if aq != nil { | ||
m.ControllerConn = aq.GetControllerConnection() | ||
m.OVSDBConn = aq.GetOVSDBConnection() | ||
m.OFConn = aq.GetOFConnection() | ||
m.Subnet = aq.GetNodeSubnet() | ||
} | ||
err := json.NewEncoder(w).Encode(m) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
klog.Errorf("Error when encoding ComponentAgentInfoResponse 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,65 @@ | ||
// 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" | ||
|
||
monitor "github.com/vmware-tanzu/antrea/pkg/monitor" | ||
mockmonitor "github.com/vmware-tanzu/antrea/pkg/monitor/testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAgentInfo(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
testcases := map[string]struct { | ||
controllerConn monitor.ConnStatus | ||
ovsdbConn monitor.ConnStatus | ||
ofConn monitor.ConnStatus | ||
subnet string | ||
expectedOutput string | ||
expectedStatusCode int | ||
}{ | ||
"AgentInfo": { | ||
controllerConn: monitor.ConnStatusUp, | ||
ovsdbConn: monitor.ConnStatusUp, | ||
ofConn: monitor.ConnStatusUp, | ||
subnet: "192.168.1.0/24", | ||
expectedOutput: "{\"controllerConn\":\"UP\",\"ovsdbConn\":\"UP\",\"ofConn\":\"UP\",\"subnet\":\"192.168.1.0/24\"}\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().GetControllerConnection().Return(tc.controllerConn) | ||
aq.EXPECT().GetOVSDBConnection().Return(tc.ovsdbConn) | ||
aq.EXPECT().GetOFConnection().Return(tc.ofConn) | ||
aq.EXPECT().GetNodeSubnet().Return(tc.subnet) | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.