Skip to content

Commit 0b3f31d

Browse files
authored
Added more details for podIpInfo in response to CNI GetIpConfigRequest (#658)
* Added more details for podIpInfo in response to CNI GetIpConfigRequest
1 parent dc76670 commit 0b3f31d

File tree

7 files changed

+104
-87
lines changed

7 files changed

+104
-87
lines changed

cns/NetworkContainerContract.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,25 @@ type GetNetworkContainerResponse struct {
189189
}
190190

191191
type GetIPConfigRequest struct {
192-
DesiredIPConfig IPSubnet
192+
DesiredIPAddress string
193193
OrchestratorContext json.RawMessage
194194
}
195195

196196
type GetIPConfigResponse struct {
197-
IPConfiguration IPConfiguration
198-
Response Response
197+
PodIpInfo PodIpInfo
198+
Response Response
199+
}
200+
201+
// DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container.
202+
type PodIpInfo struct {
203+
PodIPConfig IPSubnet
204+
NetworkContainerPrimaryIPConfig IPConfiguration
205+
HostPrimaryIPInfo HostIPInfo
206+
}
207+
208+
// DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container.
209+
type HostIPInfo struct {
210+
IPConfig IPSubnet
199211
}
200212

201213
// DeleteNetworkContainerRequest specifies the details about the request to delete a specifc network container.

cns/cnsclient/cnsclient_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
const (
2828
primaryIp = "10.0.0.5"
2929
gatewayIp = "10.0.0.1"
30+
subnetPrfixLength = 24
3031
dockerContainerType = cns.Docker
3132
)
3233

@@ -40,7 +41,7 @@ func addTestStateToRestServer(t *testing.T, secondaryIps []string) {
4041
ipConfig.GatewayIPAddress = gatewayIp
4142
var ipSubnet cns.IPSubnet
4243
ipSubnet.IPAddress = primaryIp
43-
ipSubnet.PrefixLength = 32
44+
ipSubnet.PrefixLength = subnetPrfixLength
4445
ipConfig.IPSubnet = ipSubnet
4546
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
4647

@@ -72,8 +73,8 @@ func getIPNetFromResponse(resp *cns.GetIPConfigResponse) (net.IPNet, error) {
7273
)
7374

7475
// set result ipconfig from CNS Response Body
75-
prefix := strconv.Itoa(int(resp.IPConfiguration.IPSubnet.PrefixLength))
76-
ip, ipnet, err := net.ParseCIDR(resp.IPConfiguration.IPSubnet.IPAddress + "/" + prefix)
76+
prefix := strconv.Itoa(int(resp.PodIpInfo.PodIPConfig.PrefixLength))
77+
ip, ipnet, err := net.ParseCIDR(resp.PodIpInfo.PodIPConfig.IPAddress + "/" + prefix)
7778
if err != nil {
7879
return resultIPnet, err
7980
}
@@ -155,7 +156,7 @@ func TestCNSClientRequestAndRelease(t *testing.T) {
155156
podNamespace := "testpodnamespace"
156157
desiredIpAddress := "10.0.0.5"
157158
ip := net.ParseIP(desiredIpAddress)
158-
_, ipnet, _ := net.ParseCIDR("10.0.0.5/32")
159+
_, ipnet, _ := net.ParseCIDR("10.0.0.5/24")
159160
desired := net.IPNet{
160161
IP: ip,
161162
Mask: ipnet.Mask,
@@ -185,13 +186,22 @@ func TestCNSClientRequestAndRelease(t *testing.T) {
185186
t.Fatalf("get IP from CNS failed with %+v", err)
186187
}
187188

188-
// validate gateway and dnsservers
189-
if reflect.DeepEqual(resp.IPConfiguration.DNSServers, dnsservers) != true {
190-
t.Fatalf("DnsServer is not added as expected ipConfig %+v, expected dnsServers: %+v", resp.IPConfiguration, dnsservers)
189+
podIPInfo := resp.PodIpInfo
190+
if reflect.DeepEqual(podIPInfo.NetworkContainerPrimaryIPConfig.IPSubnet.IPAddress, primaryIp) != true {
191+
t.Fatalf("PrimarIP is not added as expected ipConfig %+v, expected primaryIP: %+v", podIPInfo.NetworkContainerPrimaryIPConfig, primaryIp)
191192
}
192193

193-
if reflect.DeepEqual(resp.IPConfiguration.GatewayIPAddress, gatewayIp) != true {
194-
t.Fatalf("Gateway is not added as expected ipConfig %+v, expected GatewayIp: %+v", resp.IPConfiguration, gatewayIp)
194+
if podIPInfo.NetworkContainerPrimaryIPConfig.IPSubnet.PrefixLength != subnetPrfixLength {
195+
t.Fatalf("Primary IP Prefix length is not added as expected ipConfig %+v, expected: %+v", podIPInfo.NetworkContainerPrimaryIPConfig, subnetPrfixLength)
196+
}
197+
198+
// validate DnsServer and Gateway Ip as the same configured for Primary IP
199+
if reflect.DeepEqual(podIPInfo.NetworkContainerPrimaryIPConfig.DNSServers, dnsservers) != true {
200+
t.Fatalf("DnsServer is not added as expected ipConfig %+v, expected dnsServers: %+v", podIPInfo.NetworkContainerPrimaryIPConfig, dnsservers)
201+
}
202+
203+
if reflect.DeepEqual(podIPInfo.NetworkContainerPrimaryIPConfig.GatewayIPAddress, gatewayIp) != true {
204+
t.Fatalf("Gateway is not added as expected ipConfig %+v, expected GatewayIp: %+v", podIPInfo.NetworkContainerPrimaryIPConfig, gatewayIp)
195205
}
196206

197207
resultIPnet, err := getIPNetFromResponse(resp)

cns/restserver/internalapi.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,14 @@ func (service *HTTPRestService) ReconcileNCState(ncRequest *cns.CreateNetworkCon
170170
if podInfo, exists := podInfoByIp[secIpConfig.IPAddress]; exists {
171171
log.Logf("SecondaryIP %+v is allocated to Pod. %+v, ncId: %s", secIpConfig, podInfo, ncRequest.NetworkContainerid)
172172

173-
desiredIPConfig := cns.IPSubnet{
174-
IPAddress: secIpConfig.IPAddress,
175-
PrefixLength: 32, //todo: remove PrefixLenght in
176-
}
177-
178173
kubernetesPodInfo := cns.KubernetesPodInfo{
179174
PodName: podInfo.PodName,
180175
PodNamespace: podInfo.PodNamespace,
181176
}
182177
jsonContext, _ := json.Marshal(kubernetesPodInfo)
183178

184179
ipconfigRequest := cns.GetIPConfigRequest{
185-
DesiredIPConfig: desiredIPConfig,
180+
DesiredIPAddress: secIpConfig.IPAddress,
186181
OrchestratorContext: jsonContext,
187182
}
188183

cns/restserver/internalapi_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
const (
1818
primaryIp = "10.0.0.5"
1919
gatewayIp = "10.0.0.1"
20+
subnetPrfixLength = 24
2021
dockerContainerType = cns.Docker
2122
)
2223

@@ -256,7 +257,7 @@ func generateNetworkContainerRequest(secondaryIps map[string]cns.SecondaryIPConf
256257
ipConfig.GatewayIPAddress = gatewayIp
257258
var ipSubnet cns.IPSubnet
258259
ipSubnet.IPAddress = primaryIp
259-
ipSubnet.PrefixLength = 32
260+
ipSubnet.PrefixLength = subnetPrfixLength
260261
ipConfig.IPSubnet = ipSubnet
261262

262263
req := cns.CreateNetworkContainerRequest{

cns/restserver/ipam.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (service *HTTPRestService) requestIPConfigHandler(w http.ResponseWriter, r
1818
var (
1919
err error
2020
ipconfigRequest cns.GetIPConfigRequest
21-
ipconfiguration cns.IPConfiguration
21+
podIpInfo cns.PodIpInfo
2222
returnCode int
2323
returnMessage string
2424
)
@@ -32,7 +32,7 @@ func (service *HTTPRestService) requestIPConfigHandler(w http.ResponseWriter, r
3232
// retrieve ipconfig from nc
3333
_, returnCode, returnMessage = service.validateIpConfigRequest(ipconfigRequest)
3434
if returnCode == Success {
35-
if ipconfiguration, err = requestIPConfigHelper(service, ipconfigRequest); err != nil {
35+
if podIpInfo, err = requestIPConfigHelper(service, ipconfigRequest); err != nil {
3636
returnCode = FailedToAllocateIpConfig
3737
returnMessage = fmt.Sprintf("AllocateIPConfig failed: %v", err)
3838
}
@@ -46,7 +46,7 @@ func (service *HTTPRestService) requestIPConfigHandler(w http.ResponseWriter, r
4646
reserveResp := &cns.GetIPConfigResponse{
4747
Response: resp,
4848
}
49-
reserveResp.IPConfiguration = ipconfiguration
49+
reserveResp.PodIpInfo = podIpInfo
5050

5151
err = service.Listener.Encode(w, &reserveResp)
5252
logger.Response(service.Name, reserveResp, resp.ReturnCode, ReturnCodeToString(resp.ReturnCode), err)
@@ -158,10 +158,10 @@ func (service *HTTPRestService) releaseIPConfig(podInfo cns.KubernetesPodInfo) e
158158
return nil
159159
}
160160

161-
func (service *HTTPRestService) GetExistingIPConfig(podInfo cns.KubernetesPodInfo) (cns.IPConfiguration, bool, error) {
161+
func (service *HTTPRestService) GetExistingIPConfig(podInfo cns.KubernetesPodInfo) (cns.PodIpInfo, bool, error) {
162162
var (
163-
ipConfiguration cns.IPConfiguration
164-
isExist bool
163+
podIpInfo cns.PodIpInfo
164+
isExist bool
165165
)
166166

167167
service.RLock()
@@ -170,19 +170,19 @@ func (service *HTTPRestService) GetExistingIPConfig(podInfo cns.KubernetesPodInf
170170
ipID := service.PodIPIDByOrchestratorContext[podInfo.GetOrchestratorContextKey()]
171171
if ipID != "" {
172172
if ipState, isExist := service.PodIPConfigState[ipID]; isExist {
173-
err := service.populateIpConfigInfoUntransacted(ipState, &ipConfiguration)
174-
return ipConfiguration, isExist, err
173+
err := service.populateIpConfigInfoUntransacted(ipState, &podIpInfo)
174+
return podIpInfo, isExist, err
175175
}
176176

177177
logger.Errorf("Failed to get existing ipconfig. Pod to IPID exists, but IPID to IPConfig doesn't exist, CNS State potentially corrupt")
178-
return ipConfiguration, isExist, fmt.Errorf("Failed to get existing ipconfig. Pod to IPID exists, but IPID to IPConfig doesn't exist, CNS State potentially corrupt")
178+
return podIpInfo, isExist, fmt.Errorf("Failed to get existing ipconfig. Pod to IPID exists, but IPID to IPConfig doesn't exist, CNS State potentially corrupt")
179179
}
180180

181-
return ipConfiguration, isExist, nil
181+
return podIpInfo, isExist, nil
182182
}
183183

184-
func (service *HTTPRestService) AllocateDesiredIPConfig(podInfo cns.KubernetesPodInfo, desiredIPAddress string, orchestratorContext json.RawMessage) (cns.IPConfiguration, error) {
185-
var ipConfiguration cns.IPConfiguration
184+
func (service *HTTPRestService) AllocateDesiredIPConfig(podInfo cns.KubernetesPodInfo, desiredIPAddress string, orchestratorContext json.RawMessage) (cns.PodIpInfo, error) {
185+
var podIpInfo cns.PodIpInfo
186186
service.Lock()
187187
defer service.Unlock()
188188

@@ -197,62 +197,62 @@ func (service *HTTPRestService) AllocateDesiredIPConfig(podInfo cns.KubernetesPo
197197
} else {
198198
var pInfo cns.KubernetesPodInfo
199199
json.Unmarshal(ipState.OrchestratorContext, &pInfo)
200-
return ipConfiguration, fmt.Errorf("Desired IP is already allocated %+v to Pod: %+v, requested for pod %+v", ipState, pInfo, podInfo)
200+
return podIpInfo, fmt.Errorf("Desired IP is already allocated %+v to Pod: %+v, requested for pod %+v", ipState, pInfo, podInfo)
201201
}
202202
} else if ipState.State == cns.Available {
203203
service.setIPConfigAsAllocated(ipState, podInfo, orchestratorContext)
204204
found = true
205205
} else {
206-
return ipConfiguration, fmt.Errorf("Desired IP is not available %+v", ipState)
206+
return podIpInfo, fmt.Errorf("Desired IP is not available %+v", ipState)
207207
}
208208

209209
if found {
210-
err := service.populateIpConfigInfoUntransacted(ipState, &ipConfiguration)
211-
return ipConfiguration, err
210+
err := service.populateIpConfigInfoUntransacted(ipState, &podIpInfo)
211+
return podIpInfo, err
212212
}
213213
}
214214
}
215-
return ipConfiguration, fmt.Errorf("Requested IP not found in pool")
215+
return podIpInfo, fmt.Errorf("Requested IP not found in pool")
216216
}
217217

218-
func (service *HTTPRestService) AllocateAnyAvailableIPConfig(podInfo cns.KubernetesPodInfo, orchestratorContext json.RawMessage) (cns.IPConfiguration, error) {
219-
var ipConfiguration cns.IPConfiguration
218+
func (service *HTTPRestService) AllocateAnyAvailableIPConfig(podInfo cns.KubernetesPodInfo, orchestratorContext json.RawMessage) (cns.PodIpInfo, error) {
219+
var podIpInfo cns.PodIpInfo
220220

221221
service.Lock()
222222
defer service.Unlock()
223223

224224
for _, ipState := range service.PodIPConfigState {
225225
if ipState.State == cns.Available {
226-
err := service.populateIpConfigInfoUntransacted(ipState, &ipConfiguration)
226+
err := service.populateIpConfigInfoUntransacted(ipState, &podIpInfo)
227227
if err == nil {
228228
service.setIPConfigAsAllocated(ipState, podInfo, orchestratorContext)
229229
}
230-
return ipConfiguration, err
230+
return podIpInfo, err
231231
}
232232
}
233233

234-
return ipConfiguration, fmt.Errorf("No more free IP's available, trigger batch")
234+
return podIpInfo, fmt.Errorf("No more free IP's available, trigger batch")
235235
}
236236

237237
// If IPConfig is already allocated for pod, it returns that else it returns one of the available ipconfigs.
238-
func requestIPConfigHelper(service *HTTPRestService, req cns.GetIPConfigRequest) (cns.IPConfiguration, error) {
238+
func requestIPConfigHelper(service *HTTPRestService, req cns.GetIPConfigRequest) (cns.PodIpInfo, error) {
239239
var (
240-
podInfo cns.KubernetesPodInfo
241-
ipConfiguration cns.IPConfiguration
242-
isExist bool
243-
err error
240+
podInfo cns.KubernetesPodInfo
241+
podIpInfo cns.PodIpInfo
242+
isExist bool
243+
err error
244244
)
245245

246246
// check if ipconfig already allocated for this pod and return if exists or error
247247
// if error, ipstate is nil, if exists, ipstate is not nil and error is nil
248248
json.Unmarshal(req.OrchestratorContext, &podInfo)
249-
if ipConfiguration, isExist, err = service.GetExistingIPConfig(podInfo); err != nil || isExist {
250-
return ipConfiguration, err
249+
if podIpInfo, isExist, err = service.GetExistingIPConfig(podInfo); err != nil || isExist {
250+
return podIpInfo, err
251251
}
252252

253253
// return desired IPConfig
254-
if req.DesiredIPConfig.IPAddress != "" {
255-
return service.AllocateDesiredIPConfig(podInfo, req.DesiredIPConfig.IPAddress, req.OrchestratorContext)
254+
if req.DesiredIPAddress != "" {
255+
return service.AllocateDesiredIPConfig(podInfo, req.DesiredIPAddress, req.OrchestratorContext)
256256
}
257257

258258
// return any free IPConfig

0 commit comments

Comments
 (0)