-
Notifications
You must be signed in to change notification settings - Fork 375
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
231 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,61 @@ | ||
// Copyright 2019 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. | ||
// It only contains the version of the component the antctl server be installed | ||
// in, either the agent or the controller. | ||
// This struct is not the response struct of the version command. The version command | ||
// definition has the AddonTransform field which will populate the antctl version | ||
// to the final response. | ||
type ComponentAgentInfoResponse struct { | ||
ControllerConn string `json:"controllerConn,omitempty" yaml:"controllerConn,omitempty"` | ||
OVSDBConn string `json:"ovsdbConn,omitempty" yaml:"ovsdbConn,omitempty"` | ||
OFConn string `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 agnet-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,64 @@ | ||
// Copyright 2019 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" | ||
|
||
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 string | ||
ovsdbConn string | ||
ofConn string | ||
subent string | ||
expectedOutput string | ||
expectedStatusCode int | ||
}{ | ||
"AgentInfo": { | ||
controllerConn: "UP", | ||
ovsdbConn: "UP", | ||
ofConn: "UP", | ||
subent: "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.subent) | ||
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.