Skip to content

Commit e7bc1ea

Browse files
committed
Exposed a new API to get number of CPU COres
1 parent 22f945d commit e7bc1ea

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

cns/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
GetIPAddressUtilizationPath = "/network/ip/utilization"
1919
GetUnhealthyIPAddressesPath = "/network/ipaddresses/unhealthy"
2020
GetHealthReportPath = "/network/health"
21+
NumberOfCPUCoresPath = "/network/hostcpucores"
2122
V1Prefix = "/v0.1"
2223
V2Prefix = "/v0.2"
2324
)
@@ -139,6 +140,12 @@ type Response struct {
139140
Message string
140141
}
141142

143+
// getNumberOfCPUCores describes reponse that returns the host local IP Address.
144+
type NumOfCPUCoresResponse struct {
145+
Response Response
146+
NumOfCPUCores int
147+
}
148+
142149
// OptionMap describes generic options that can be passed to CNS.
143150
type OptionMap map[string]interface{}
144151

cns/restserver/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
UnknownContainerID = 18
2222
UnsupportedOrchestratorType = 19
2323
DockerContainerNotSpecified = 20
24+
UnsupportedVerb = 21
2425
UnexpectedError = 99
2526
)
2627

cns/restserver/restserver.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net"
1010
"net/http"
11+
"runtime"
1112
"sync"
1213
"time"
1314

@@ -157,6 +158,7 @@ func (service *HTTPRestService) Start(config *common.ServiceConfig) error {
157158
listener.AddHandler(cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork)
158159
listener.AddHandler(cns.CreateHnsNetworkPath, service.createHnsNetwork)
159160
listener.AddHandler(cns.DeleteHnsNetworkPath, service.deleteHnsNetwork)
161+
listener.AddHandler(cns.NumberOfCPUCoresPath, service.getNumberOfCPUCores)
160162

161163
// handlers for v0.2
162164
listener.AddHandler(cns.V2Prefix+cns.SetEnvironmentPath, service.setEnvironment)
@@ -177,6 +179,7 @@ func (service *HTTPRestService) Start(config *common.ServiceConfig) error {
177179
listener.AddHandler(cns.V2Prefix+cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork)
178180
listener.AddHandler(cns.V2Prefix+cns.CreateHnsNetworkPath, service.createHnsNetwork)
179181
listener.AddHandler(cns.V2Prefix+cns.DeleteHnsNetworkPath, service.deleteHnsNetwork)
182+
listener.AddHandler(cns.V2Prefix+cns.NumberOfCPUCoresPath, service.getNumberOfCPUCores)
180183

181184
log.Printf("[Azure CNS] Listening.")
182185
return nil
@@ -1573,3 +1576,32 @@ func (service *HTTPRestService) getNetPluginDetails() *networkcontainers.NetPlug
15731576
configPath, _ := service.GetOption(acn.OptNetPluginConfigFile).(string)
15741577
return networkcontainers.NewNetPluginConfiguration(pluginBinPath, configPath)
15751578
}
1579+
1580+
// Retrieves the number of logic processors on a node. It will be primarily
1581+
// used to enforce per VM delegated NIC limit by DNC.
1582+
func (service *HTTPRestService) getNumberOfCPUCores(w http.ResponseWriter, r *http.Request) {
1583+
log.Printf("[Azure CNS] getNumberOfCPUCores")
1584+
log.Request(service.Name, "getNumberOfCPUCores", nil)
1585+
1586+
var num = 0
1587+
var returnCode = 0
1588+
var errmsg string
1589+
1590+
switch r.Method {
1591+
case "GET":
1592+
num = runtime.NumCPU()
1593+
default:
1594+
errmsg = "[Azure-CNS] getNumberOfCPUCores API expects a GET."
1595+
returnCode = UnsupportedVerb
1596+
}
1597+
1598+
resp := cns.Response{ReturnCode: returnCode, Message: errmsg}
1599+
numOfCPUCoresResp := cns.NumOfCPUCoresResponse{
1600+
Response: resp,
1601+
NumOfCPUCores: num,
1602+
}
1603+
1604+
err := service.Listener.Encode(w, &numOfCPUCoresResp)
1605+
1606+
log.Response(service.Name, numOfCPUCoresResp, resp.ReturnCode, ReturnCodeToString(resp.ReturnCode), err)
1607+
}

cns/restserver/restserver_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,23 @@ func TestGetInterfaceForNetworkContainer(t *testing.T) {
689689
t.Fatal(err)
690690
}
691691
}
692+
693+
func TestGetNumOfCPUCores(t *testing.T) {
694+
fmt.Println("Test: getNumberOfCPUCores")
695+
696+
req, err := http.NewRequest(http.MethodGet, cns.NumberOfCPUCoresPath, nil)
697+
if err != nil {
698+
t.Fatal(err)
699+
}
700+
701+
w := httptest.NewRecorder()
702+
mux.ServeHTTP(w, req)
703+
var numOfCoresResponse cns.NumOfCPUCoresResponse
704+
705+
err = decodeResponse(w, &numOfCoresResponse)
706+
if err != nil || numOfCoresResponse.Response.ReturnCode != 0 {
707+
t.Errorf("getNumberOfCPUCores failed with response %+v", numOfCoresResponse)
708+
} else {
709+
fmt.Printf("getNumberOfCPUCores Responded with %+v\n", numOfCoresResponse)
710+
}
711+
}

0 commit comments

Comments
 (0)