Skip to content

Commit bfb8bb4

Browse files
authored
feat: add IPConfig state logs/metrics to Request/Release handlers
Signed-off-by: Evan BaKer <[email protected]>
1 parent 3ed0bcd commit bfb8bb4

File tree

4 files changed

+53
-60
lines changed

4 files changed

+53
-60
lines changed

cns/restserver/internalapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
563563
// If the NC was created successfully, log NC snapshot.
564564
if returnCode == 0 {
565565
logNCSnapshot(*req)
566-
publishIPStateMetrics(service.buildIPState())
566+
service.publishIPStateMetrics()
567567
} else {
568568
logger.Errorf(returnMessage)
569569
}

cns/restserver/ipam.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func (service *HTTPRestService) updatePodInfoWithInterfaces(ctx context.Context,
197197
// RequestIPConfigHandler requests an IPConfig from the CNS state
198198
func (service *HTTPRestService) RequestIPConfigHandler(w http.ResponseWriter, r *http.Request) {
199199
opName := "requestIPConfigHandler"
200+
defer service.publishIPStateMetrics()
200201
var ipconfigRequest cns.IPConfigRequest
201202
err := common.Decode(w, r, &ipconfigRequest)
202203
logger.Request(opName, ipconfigRequest, err)
@@ -272,6 +273,7 @@ func (service *HTTPRestService) RequestIPConfigHandler(w http.ResponseWriter, r
272273
// RequestIPConfigsHandler requests multiple IPConfigs from the CNS state
273274
func (service *HTTPRestService) RequestIPConfigsHandler(w http.ResponseWriter, r *http.Request) {
274275
opName := "requestIPConfigsHandler"
276+
defer service.publishIPStateMetrics()
275277
var ipconfigsRequest cns.IPConfigsRequest
276278
err := common.Decode(w, r, &ipconfigsRequest)
277279
logger.Request(opName, ipconfigsRequest, err)
@@ -415,6 +417,7 @@ func (service *HTTPRestService) ReleaseIPConfigHandlerHelper(ctx context.Context
415417
// ReleaseIPConfigHandler frees the IP assigned to a pod from CNS
416418
func (service *HTTPRestService) ReleaseIPConfigHandler(w http.ResponseWriter, r *http.Request) {
417419
opName := "releaseIPConfigHandler"
420+
defer service.publishIPStateMetrics()
418421
var ipconfigRequest cns.IPConfigRequest
419422
err := common.Decode(w, r, &ipconfigRequest)
420423
logger.Request(opName, ipconfigRequest, err)
@@ -469,6 +472,7 @@ func (service *HTTPRestService) ReleaseIPConfigHandler(w http.ResponseWriter, r
469472
// ReleaseIPConfigsHandler frees multiple IPConfigs from the CNS state
470473
func (service *HTTPRestService) ReleaseIPConfigsHandler(w http.ResponseWriter, r *http.Request) {
471474
opName := "releaseIPConfigsHandler"
475+
defer service.publishIPStateMetrics()
472476
var ipconfigsRequest cns.IPConfigsRequest
473477
err := common.Decode(w, r, &ipconfigsRequest)
474478
logger.Request("releaseIPConfigsHandler", ipconfigsRequest, err)
@@ -518,6 +522,7 @@ func (service *HTTPRestService) removeEndpointState(podInfo cns.PodInfo) error {
518522
// MarkIPAsPendingRelease will set the IPs which are in PendingProgramming or Available to PendingRelease state
519523
// It will try to update [totalIpsToRelease] number of ips.
520524
func (service *HTTPRestService) MarkIPAsPendingRelease(totalIpsToRelease int) (map[string]cns.IPConfigurationStatus, error) {
525+
defer service.publishIPStateMetrics()
521526
pendingReleasedIps := make(map[string]cns.IPConfigurationStatus)
522527
service.Lock()
523528
defer service.Unlock()
@@ -563,6 +568,7 @@ func (service *HTTPRestService) MarkIPAsPendingRelease(totalIpsToRelease int) (m
563568
// and return an error.
564569
// MarkNIPsPendingRelease is no-op if [n] is not a positive integer.
565570
func (service *HTTPRestService) MarkNIPsPendingRelease(n int) (map[string]cns.IPConfigurationStatus, error) {
571+
defer service.publishIPStateMetrics()
566572
service.Lock()
567573
defer service.Unlock()
568574
// try to release from PendingProgramming

cns/restserver/ipusage.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

cns/restserver/metrics.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package restserver
22

33
import (
4+
"maps"
45
"net/http"
56
"time"
67

78
"github.com/Azure/azure-container-networking/cns"
9+
"github.com/Azure/azure-container-networking/cns/logger"
810
"github.com/Azure/azure-container-networking/cns/types"
911
"github.com/prometheus/client_golang/prometheus"
1012
"sigs.k8s.io/controller-runtime/pkg/metrics"
@@ -142,7 +144,50 @@ func stateTransitionMiddleware(i *cns.IPConfigurationStatus, s types.IPState) {
142144
ipConfigStatusStateTransitionTime.WithLabelValues(string(i.GetState()), string(s)).Observe(time.Since(i.LastStateTransition).Seconds())
143145
}
144146

145-
func publishIPStateMetrics(state *ipState) {
147+
type ipState struct {
148+
// allocatedIPs are all the IPs given to CNS by DNC.
149+
allocatedIPs int64
150+
// assignedIPs are the IPs CNS gives to Pods.
151+
assignedIPs int64
152+
// availableIPs are the IPs in state "Available".
153+
availableIPs int64
154+
// programmingIPs are the IPs in state "PendingProgramming".
155+
programmingIPs int64
156+
// releasingIPs are the IPs in state "PendingReleasr".
157+
releasingIPs int64
158+
}
159+
160+
// publishIPStateMetrics logs and publishes the IP Config state metrics to Prometheus.
161+
func (service *HTTPRestService) publishIPStateMetrics() {
162+
// copy state
163+
service.RLock()
164+
defer service.RUnlock()
165+
166+
var state ipState
167+
for ipConfig := range maps.Values(service.PodIPConfigState) {
168+
state.allocatedIPs++
169+
if ipConfig.GetState() == types.Assigned {
170+
state.assignedIPs++
171+
}
172+
if ipConfig.GetState() == types.Available {
173+
state.availableIPs++
174+
}
175+
if ipConfig.GetState() == types.PendingProgramming {
176+
state.programmingIPs++
177+
}
178+
if ipConfig.GetState() == types.PendingRelease {
179+
state.releasingIPs++
180+
}
181+
}
182+
183+
logger.Printf("Allocated IPs: %d, Assigned IPs: %d, Available IPs: %d, PendingProgramming IPs: %d, PendingRelease IPs: %d",
184+
state.allocatedIPs,
185+
state.assignedIPs,
186+
state.availableIPs,
187+
state.programmingIPs,
188+
state.releasingIPs,
189+
)
190+
146191
labels := []string{}
147192
allocatedIPCount.WithLabelValues(labels...).Set(float64(state.allocatedIPs))
148193
assignedIPCount.WithLabelValues(labels...).Set(float64(state.assignedIPs))

0 commit comments

Comments
 (0)