From 05562c8b92588bac1861e7d868c56943a6252932 Mon Sep 17 00:00:00 2001 From: Yuki Tsuboi Date: Sat, 13 Jun 2020 13:11:08 +0900 Subject: [PATCH] Add support the number of networkpolicies per node - Change name of networkpolicy rule metric variable - Add support the number of networkpolicies per node Signed-off-by: Yuki Tsuboi --- pkg/agent/controller/networkpolicy/cache.go | 3 +++ .../controller/networkpolicy/reconciler.go | 8 ++++---- pkg/agent/metrics/prometheus.go | 20 +++++++++++++++---- test/e2e/prometheus_test.go | 1 + 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/agent/controller/networkpolicy/cache.go b/pkg/agent/controller/networkpolicy/cache.go index 4fc1613c09c..aa5af4f3fdf 100644 --- a/pkg/agent/controller/networkpolicy/cache.go +++ b/pkg/agent/controller/networkpolicy/cache.go @@ -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" ) @@ -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) } @@ -613,6 +615,7 @@ func (c *ruleCache) deleteNetworkPolicyLocked(uid string) error { c.rules.Delete(r) c.dirtyRuleHandler(ruleID) } + metrics.NetworkPolicyCount.Dec() return nil } diff --git a/pkg/agent/controller/networkpolicy/reconciler.go b/pkg/agent/controller/networkpolicy/reconciler.go index f2e456aea5d..3fec150eb65 100644 --- a/pkg/agent/controller/networkpolicy/reconciler.go +++ b/pkg/agent/controller/networkpolicy/reconciler.go @@ -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 @@ -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) diff --git a/pkg/agent/metrics/prometheus.go b/pkg/agent/metrics/prometheus.go index 3305c6917d3..f8bf801d492 100644 --- a/pkg/agent/metrics/prometheus.go +++ b/pkg/agent/metrics/prometheus.go @@ -23,7 +23,7 @@ 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.", @@ -31,7 +31,7 @@ var ( }, ) - 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.", @@ -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.", @@ -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") } diff --git a/test/e2e/prometheus_test.go b/test/e2e/prometheus_test.go index 80466e29489..66ff0173fb5 100644 --- a/test/e2e/prometheus_test.go +++ b/test/e2e/prometheus_test.go @@ -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",