Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log: [NPM] heartbeat includes number of cidr and endport netpols #3319

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
13 changes: 8 additions & 5 deletions npm/metrics/ai-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@ func SendLog(operationID int, msg string, printLog bool) {
}

func SendHeartbeatWithNumPolicies() {
var message string
numPolicies, err := GetNumPolicies()
if err == nil {
message = fmt.Sprintf("info: NPM heartbeat. Current num policies: %d", numPolicies)
numPoliciesString := "unknown"
if err != nil {
klog.Warningf("warn: NPM heartbeat. Couldn't get number of policies for telemetry log: %s", err.Error())
} else {
message = fmt.Sprintf("warn: NPM hearbeat. Couldn't get number of policies for telemetry log: %v", err)
klog.Warning(message)
numPoliciesString = strconv.Itoa(numPolicies)
}

cidrNetPols := GetCidrNetPols()
endPortNetPols := GetEndPortNetPols()
message := fmt.Sprintf("info: NPM heartbeat. Total policies: %s, CIDR policies: %d, EndPort policies: %d", numPoliciesString, cidrNetPols, endPortNetPols)
SendLog(util.NpmID, message, DonotPrint)
}
66 changes: 66 additions & 0 deletions npm/metrics/counts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package metrics

import "sync"

var nonPrometheusCounts *counts

// counts is a struct holding non-Prometheus counts.
type counts struct {
sync.Mutex
cidrNetPols int
endPortNetPols int
}

func IncCidrNetPols() {
if nonPrometheusCounts == nil {
return
}
nonPrometheusCounts.Lock()
defer nonPrometheusCounts.Unlock()
nonPrometheusCounts.cidrNetPols++
}

func DecCidrNetPols() {
if nonPrometheusCounts == nil {
return
}
nonPrometheusCounts.Lock()
defer nonPrometheusCounts.Unlock()
nonPrometheusCounts.cidrNetPols--
}

func GetCidrNetPols() int {
if nonPrometheusCounts == nil {
return 0
}
nonPrometheusCounts.Lock()
defer nonPrometheusCounts.Unlock()
return nonPrometheusCounts.cidrNetPols
}

func IncEndPortNetPols() {
if nonPrometheusCounts == nil {
return
}
nonPrometheusCounts.Lock()
defer nonPrometheusCounts.Unlock()
nonPrometheusCounts.endPortNetPols++
}

func DecEndPortNetPols() {
if nonPrometheusCounts == nil {
return
}
nonPrometheusCounts.Lock()
defer nonPrometheusCounts.Unlock()
nonPrometheusCounts.endPortNetPols--
}

func GetEndPortNetPols() int {
if nonPrometheusCounts == nil {
return 0
}
nonPrometheusCounts.Lock()
defer nonPrometheusCounts.Unlock()
return nonPrometheusCounts.endPortNetPols
}
3 changes: 3 additions & 0 deletions npm/metrics/prometheus-metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func InitializeAll() {
return
}

// initialize global variable (see counts.go)
nonPrometheusCounts = &counts{}

initializeDaemonMetrics()
initializeControllerMetrics()

Expand Down
30 changes: 27 additions & 3 deletions npm/pkg/controlplane/controllers/v2/networkPolicyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
return metrics.NoOp, nil
}

_, policyExisted := c.rawNpSpecMap[netpolKey]
oldNetPolSpec, policyExisted := c.rawNpSpecMap[netpolKey]
var operationKind metrics.OperationKind
if policyExisted {
operationKind = metrics.UpdateOp
Expand All @@ -320,18 +320,34 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
return operationKind, fmt.Errorf("[syncAddAndUpdateNetPol] Error: failed to update translated NPMNetworkPolicy into Dataplane due to %w", err)
}

if !policyExisted {
if policyExisted {
if translation.HasCIDRBlock(oldNetPolSpec) {
metrics.DecCidrNetPols()
}

if translation.HasEndPort(oldNetPolSpec) {
metrics.DecEndPortNetPols()
}
} else {
// inc metric for NumPolicies only if it a new network policy
metrics.IncNumPolicies()
}

if translation.HasCIDRBlock(&netPolObj.Spec) {
metrics.IncCidrNetPols()
}

if translation.HasEndPort(&netPolObj.Spec) {
metrics.IncEndPortNetPols()
}

c.rawNpSpecMap[netpolKey] = &netPolObj.Spec
return operationKind, nil
}

// DeleteNetworkPolicy handles deleting network policy based on netPolKey.
func (c *NetworkPolicyController) cleanUpNetworkPolicy(netPolKey string) error {
_, cachedNetPolObjExists := c.rawNpSpecMap[netPolKey]
cachedNetPolSpec, cachedNetPolObjExists := c.rawNpSpecMap[netPolKey]
// if there is no applied network policy with the netPolKey, do not need to clean up process.
if !cachedNetPolObjExists {
return nil
Expand All @@ -342,6 +358,14 @@ func (c *NetworkPolicyController) cleanUpNetworkPolicy(netPolKey string) error {
return fmt.Errorf("[cleanUpNetworkPolicy] Error: failed to remove policy due to %w", err)
}

if translation.HasCIDRBlock(cachedNetPolSpec) {
metrics.DecCidrNetPols()
}

if translation.HasEndPort(cachedNetPolSpec) {
metrics.DecEndPortNetPols()
}

// Success to clean up ipset and iptables operations in kernel and delete the cached network policy from RawNpMap
delete(c.rawNpSpecMap, netPolKey)
metrics.DecNumPolicies()
Expand Down
Loading
Loading