Skip to content

Commit

Permalink
Add support the number of networkpolicies per node
Browse files Browse the repository at this point in the history
- Change name of networkpolicy rule metric variable
- Add support the number of networkpolicies per node

Signed-off-by: Yuki Tsuboi <ytsuboi@vmware.com>
  • Loading branch information
Yuki Tsuboi committed Jun 13, 2020
1 parent bed6067 commit 05562c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pkg/agent/controller/networkpolicy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/klog"

"github.com/vmware-tanzu/antrea/pkg/agent/metrics"
"github.com/vmware-tanzu/antrea/pkg/apis/networking/v1beta1"
)

Expand Down Expand Up @@ -563,6 +564,7 @@ func (c *ruleCache) AddNetworkPolicy(policy *v1beta1.NetworkPolicy) error {

func (c *ruleCache) addNetworkPolicyLocked(policy *v1beta1.NetworkPolicy) error {
c.policyMap[string(policy.UID)] = &types.NamespacedName{policy.Namespace, policy.Name}
metrics.NetworkPolicyCount.Inc()
return c.UpdateNetworkPolicy(policy)
}

Expand Down Expand Up @@ -613,6 +615,7 @@ func (c *ruleCache) deleteNetworkPolicyLocked(uid string) error {
c.rules.Delete(r)
c.dirtyRuleHandler(ruleID)
}
metrics.NetworkPolicyCount.Dec()
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/agent/controller/networkpolicy/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ func (r *reconciler) add(rule *CompletedRule) error {

// Count up antrea_agent_ingress_networkpolicy_rule_count or antrea_agent_egress_networkpolicy_rule_count
if rule.Direction == v1beta1.DirectionIn {
metrics.IngressNetworkPolicyCount.Inc()
metrics.IngressNetworkPolicyRuleCount.Inc()
} else if rule.Direction == v1beta1.DirectionOut {
metrics.EgressNetworkPolicyCount.Inc()
metrics.EgressNetworkPolicyRuleCount.Inc()
}

return nil
Expand Down Expand Up @@ -440,9 +440,9 @@ func (r *reconciler) Forget(ruleID string) error {

// Decrement antrea_agent_ingress_networkpolicy_rule_count or antrea_agent_egress_networkpolicy_rule_count
if lastRealized.Direction == v1beta1.DirectionIn {
metrics.IngressNetworkPolicyCount.Dec()
metrics.IngressNetworkPolicyRuleCount.Dec()
} else if lastRealized.Direction == v1beta1.DirectionOut {
metrics.EgressNetworkPolicyCount.Dec()
metrics.EgressNetworkPolicyRuleCount.Dec()
}

r.lastRealizeds.Delete(ruleID)
Expand Down
20 changes: 16 additions & 4 deletions pkg/agent/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
)

var (
EgressNetworkPolicyCount = metrics.NewGauge(
EgressNetworkPolicyRuleCount = metrics.NewGauge(
&metrics.GaugeOpts{
Name: "antrea_agent_egress_networkpolicy_rule_count",
Help: "Number of egress networkpolicy rules on local node which are managed by the Antrea Agent.",
StabilityLevel: metrics.STABLE,
},
)

IngressNetworkPolicyCount = metrics.NewGauge(
IngressNetworkPolicyRuleCount = metrics.NewGauge(
&metrics.GaugeOpts{
Name: "antrea_agent_ingress_networkpolicy_rule_count",
Help: "Number of ingress networkpolicy rules on local node which are managed by the Antrea Agent.",
Expand All @@ -47,6 +47,14 @@ var (
},
)

NetworkPolicyCount = metrics.NewGauge(
&metrics.GaugeOpts{
Name: "antrea_agent_networkpolicy_count",
Help: "Number of networkpolicies on local node which are managed by the Antrea Agent.",
StabilityLevel: metrics.STABLE,
},
)

OVSTotalFlowCount = metrics.NewGauge(&metrics.GaugeOpts{
Name: "antrea_agent_ovs_total_flow_count",
Help: "Total flow count of all OVS flow tables.",
Expand Down Expand Up @@ -86,14 +94,18 @@ func InitializePrometheusMetrics() {
// and will not measure anything unless the collector is first registered.
gaugeHost.Set(1)

if err := legacyregistry.Register(EgressNetworkPolicyCount); err != nil {
if err := legacyregistry.Register(EgressNetworkPolicyRuleCount); err != nil {
klog.Error("Failed to register antrea_agent_egress_networkpolicy_rule_count with Prometheus")
}

if err := legacyregistry.Register(IngressNetworkPolicyCount); err != nil {
if err := legacyregistry.Register(IngressNetworkPolicyRuleCount); err != nil {
klog.Error("Failed to register antrea_agent_ingress_networkpolicy_rule_count with Prometheus")
}

if err := legacyregistry.Register(NetworkPolicyCount); err != nil {
klog.Error("Failed to register antrea_agent_networkpolicy_count with Prometheus")
}

if err := legacyregistry.Register(OVSTotalFlowCount); err != nil {
klog.Error("Failed to register antrea_agent_ovs_total_flow_count with Prometheus")
}
Expand Down
1 change: 1 addition & 0 deletions test/e2e/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var antreaAgentMetrics = []string{
"antrea_agent_egress_networkpolicy_rule_count",
"antrea_agent_ingress_networkpolicy_rule_count",
"antrea_agent_local_pod_count",
"antrea_agent_networkpolicy_count",
"antrea_agent_ovs_total_flow_count",
"antrea_agent_ovs_flow_count",
"antrea_agent_runtime_info",
Expand Down

0 comments on commit 05562c8

Please sign in to comment.