Skip to content

Commit 2913732

Browse files
committed
fix: policy count logic and log typo
Signed-off-by: Hunter Gregory <[email protected]>
1 parent 86a0da3 commit 2913732

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

npm/metrics/ai-utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ func SendHeartbeatWithNumPolicies() {
103103
numPolicies, err := GetNumPolicies()
104104
numPoliciesString := "unknown"
105105
if err != nil {
106-
klog.Warningf("warn: NPM hearbeat. Couldn't get number of policies for telemetry log: %s", err.Error())
106+
klog.Warningf("warn: NPM heartbeat. Couldn't get number of policies for telemetry log: %s", err.Error())
107107
} else {
108108
numPoliciesString = strconv.Itoa(numPolicies)
109109
}
110110

111111
cidrNetPols := GetCidrNetPols()
112112
namedPortNetPols := GetNamedPortNetPols()
113-
message := fmt.Sprintf("info: NPM hearbeat. Total policies: %s, CIDR policies: %d, NamedPort policies: %d", numPoliciesString, cidrNetPols, namedPortNetPols)
113+
message := fmt.Sprintf("info: NPM heartbeat. Total policies: %s, CIDR policies: %d, NamedPort policies: %d", numPoliciesString, cidrNetPols, namedPortNetPols)
114114
SendLog(util.NpmID, message, DonotPrint)
115115
}

npm/pkg/controlplane/controllers/v2/networkPolicyController.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,9 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
302302
}
303303

304304
oldNetPolSpec, policyExisted := c.rawNpSpecMap[netpolKey]
305-
hadCIDR := false
306-
hadNamedPort := false
307305
var operationKind metrics.OperationKind
308306
if policyExisted {
309307
operationKind = metrics.UpdateOp
310-
hadCIDR = translation.HasCIDRBlock(oldNetPolSpec)
311-
hadNamedPort = translation.HasNamedPort(oldNetPolSpec)
312308
} else {
313309
operationKind = metrics.CreateOp
314310
}
@@ -325,24 +321,24 @@ func (c *NetworkPolicyController) syncAddAndUpdateNetPol(netPolObj *networkingv1
325321
}
326322

327323
if policyExisted {
328-
if hadCIDR && !translation.HasCIDRBlock(&netPolObj.Spec) {
324+
if translation.HasCIDRBlock(oldNetPolSpec) {
329325
metrics.DecCidrNetPols()
330326
}
331327

332-
if hadNamedPort && !translation.HasNamedPort(&netPolObj.Spec) {
328+
if translation.HasNamedPort(oldNetPolSpec) {
333329
metrics.DecNamedPortNetPols()
334330
}
335331
} else {
336332
// inc metric for NumPolicies only if it a new network policy
337333
metrics.IncNumPolicies()
334+
}
338335

339-
if translation.HasCIDRBlock(&netPolObj.Spec) {
340-
metrics.IncCidrNetPols()
341-
}
336+
if translation.HasCIDRBlock(&netPolObj.Spec) {
337+
metrics.IncCidrNetPols()
338+
}
342339

343-
if translation.HasNamedPort(&netPolObj.Spec) {
344-
metrics.IncNamedPortNetPols()
345-
}
340+
if translation.HasNamedPort(&netPolObj.Spec) {
341+
metrics.IncNamedPortNetPols()
346342
}
347343

348344
c.rawNpSpecMap[netpolKey] = &netPolObj.Spec

npm/pkg/controlplane/translation/translatePolicy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,15 @@ func HasCIDRBlock(netPolSpec *networkingv1.NetworkPolicySpec) bool {
708708
func HasNamedPort(netPolObj *networkingv1.NetworkPolicySpec) bool {
709709
for _, ingress := range netPolObj.Ingress {
710710
for _, port := range ingress.Ports {
711-
if t, err := portType(port); err != nil && t == namedPortType {
711+
if t, err := portType(port); err == nil && t == namedPortType {
712712
return true
713713
}
714714
}
715715
}
716716

717717
for _, egress := range netPolObj.Egress {
718718
for _, port := range egress.Ports {
719-
if t, err := portType(port); err != nil && t == namedPortType {
719+
if t, err := portType(port); err == nil && t == namedPortType {
720720
return true
721721
}
722722
}

0 commit comments

Comments
 (0)