Skip to content

Commit

Permalink
fix: policy count logic and log typo
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Gregory <42728408+huntergregory@users.noreply.github.com>
  • Loading branch information
huntergregory committed Jan 8, 2025
1 parent 86a0da3 commit 2913732
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions npm/metrics/ai-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func SendHeartbeatWithNumPolicies() {
numPolicies, err := GetNumPolicies()
numPoliciesString := "unknown"
if err != nil {
klog.Warningf("warn: NPM hearbeat. Couldn't get number of policies for telemetry log: %s", err.Error())
klog.Warningf("warn: NPM heartbeat. Couldn't get number of policies for telemetry log: %s", err.Error())
} else {
numPoliciesString = strconv.Itoa(numPolicies)
}

cidrNetPols := GetCidrNetPols()
namedPortNetPols := GetNamedPortNetPols()
message := fmt.Sprintf("info: NPM hearbeat. Total policies: %s, CIDR policies: %d, NamedPort policies: %d", numPoliciesString, cidrNetPols, namedPortNetPols)
message := fmt.Sprintf("info: NPM heartbeat. Total policies: %s, CIDR policies: %d, NamedPort policies: %d", numPoliciesString, cidrNetPols, namedPortNetPols)
SendLog(util.NpmID, message, DonotPrint)
}
20 changes: 8 additions & 12 deletions npm/pkg/controlplane/controllers/v2/networkPolicyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,9 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
}

oldNetPolSpec, policyExisted := c.rawNpSpecMap[netpolKey]
hadCIDR := false
hadNamedPort := false
var operationKind metrics.OperationKind
if policyExisted {
operationKind = metrics.UpdateOp
hadCIDR = translation.HasCIDRBlock(oldNetPolSpec)
hadNamedPort = translation.HasNamedPort(oldNetPolSpec)
} else {
operationKind = metrics.CreateOp
}
Expand All @@ -325,24 +321,24 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
}

if policyExisted {
if hadCIDR && !translation.HasCIDRBlock(&netPolObj.Spec) {
if translation.HasCIDRBlock(oldNetPolSpec) {
metrics.DecCidrNetPols()
}

if hadNamedPort && !translation.HasNamedPort(&netPolObj.Spec) {
if translation.HasNamedPort(oldNetPolSpec) {
metrics.DecNamedPortNetPols()
}
} else {
// inc metric for NumPolicies only if it a new network policy
metrics.IncNumPolicies()
}

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

if translation.HasNamedPort(&netPolObj.Spec) {
metrics.IncNamedPortNetPols()
}
if translation.HasNamedPort(&netPolObj.Spec) {
metrics.IncNamedPortNetPols()
}

c.rawNpSpecMap[netpolKey] = &netPolObj.Spec
Expand Down
4 changes: 2 additions & 2 deletions npm/pkg/controlplane/translation/translatePolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,15 +708,15 @@ func HasCIDRBlock(netPolSpec *networkingv1.NetworkPolicySpec) bool {
func HasNamedPort(netPolObj *networkingv1.NetworkPolicySpec) bool {
for _, ingress := range netPolObj.Ingress {
for _, port := range ingress.Ports {
if t, err := portType(port); err != nil && t == namedPortType {
if t, err := portType(port); err == nil && t == namedPortType {
return true
}
}
}

for _, egress := range netPolObj.Egress {
for _, port := range egress.Ports {
if t, err := portType(port); err != nil && t == namedPortType {
if t, err := portType(port); err == nil && t == namedPortType {
return true
}
}
Expand Down

0 comments on commit 2913732

Please sign in to comment.