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

SDN-4179: Metrics: Add metric for multicast enabled netnamespaces #603

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/network/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,8 @@ func GetNodeInternalIP(node *corev1.Node) string {
}
return nodeIP
}

func NetnsIsMulticastEnabled(netns *osdnv1.NetNamespace) bool {
enabled, ok := netns.Annotations[osdnv1.MulticastEnabledAnnotation]
return enabled == "true" && ok
}
5 changes: 5 additions & 0 deletions pkg/network/master/metrics/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ func RecordEgressFirewallRuleCount(count float64) {
func RecordEgressIPCount(count float64) {
metricEgressIPCount.Set(count)
}

// RecordMulticastEnabledNamespaceCount records the number of namespaces with multicast enabled.
func RecordMulticastEnabledNamespaceCount(count float64) {
metricMulticastEnabledNamespaceCount.Set(count)
}
9 changes: 9 additions & 0 deletions pkg/network/master/metrics/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ var metricEgressFirewallCount = prometheus.NewGauge(prometheus.GaugeOpts{
Help: "The number of egress firewall policies",
})

// represents the count of multicast enabled namespaces
var metricMulticastEnabledNamespaceCount = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: metricSDNNamespace,
Subsystem: metricSDNSubsystemController,
Name: "num_multicast_enabled_namespaces",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not suggesting a change here because youre following existing naming convention, but for your own info here is the prometheus naming guide: https://prometheus.io/docs/practices/naming/

Help: "The number of multicast enabled namespaces",
})

var registry = prometheus.NewRegistry()

func Register() {
registry.MustRegister(metricEgressIPCount)
registry.MustRegister(metricEgressFirewallRuleCount)
registry.MustRegister(metricEgressFirewallCount)
registry.MustRegister(metricMulticastEnabledNamespaceCount)
}
19 changes: 19 additions & 0 deletions pkg/network/master/vnids.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
osdnclient "github.com/openshift/client-go/network/clientset/versioned"
osdnapihelpers "github.com/openshift/library-go/pkg/network/networkapihelpers"
"github.com/openshift/sdn/pkg/network/common"
metrics "github.com/openshift/sdn/pkg/network/master/metrics"
pnetid "github.com/openshift/sdn/pkg/network/master/netid"
)

type masterVNIDMap struct {
// Synchronizes assign, revoke and update VNID
lock sync.Mutex
ids map[string]uint32
mcEnabled map[string]bool
netIDManager *pnetid.Allocator

allowRenumbering bool
Expand All @@ -37,6 +39,7 @@ func newMasterVNIDMap(allowRenumbering bool) *masterVNIDMap {
return &masterVNIDMap{
netIDManager: pnetid.NewInMemory(netIDRange),
ids: make(map[string]uint32),
mcEnabled: make(map[string]bool),
allowRenumbering: allowRenumbering,
}
}
Expand Down Expand Up @@ -215,6 +218,7 @@ func (vmap *masterVNIDMap) assignVNID(osdnClient osdnclient.Interface, nsName st
func (vmap *masterVNIDMap) revokeVNID(osdnClient osdnclient.Interface, nsName string) error {
vmap.lock.Lock()
defer vmap.lock.Unlock()
delete(vmap.mcEnabled, nsName)

// Delete NetNamespace object
if err := osdnClient.NetworkV1().NetNamespaces().Delete(context.TODO(), nsName, metav1.DeleteOptions{}); err != nil {
Expand All @@ -236,6 +240,8 @@ func (vmap *masterVNIDMap) updateVNID(osdnClient osdnclient.Interface, origNetns
// Informer cache should not be mutated, so get a copy of the object
netns := origNetns.DeepCopy()

vmap.mcEnabled[netns.Name] = common.NetnsIsMulticastEnabled(netns)

action, args, err := osdnapihelpers.GetChangePodNetworkAnnotation(netns)
if err == osdnapihelpers.ErrorPodNetworkAnnotationNotFound {
// Nothing to update
Expand Down Expand Up @@ -311,6 +317,7 @@ func (master *OsdnMaster) handleDeleteNamespace(obj interface{}) {
if err := master.vnids.revokeVNID(master.osdnClient, ns.Name); err != nil {
klog.Errorf("Error revoking netid: %v", err)
}
master.recordMetrics()
}

func (master *OsdnMaster) watchNetNamespaces() {
Expand All @@ -325,4 +332,16 @@ func (master *OsdnMaster) handleAddOrUpdateNetNamespace(obj, _ interface{}, even
if err := master.vnids.updateVNID(master.osdnClient, netns); err != nil {
klog.Errorf("Error updating netid: %v", err)
}
master.recordMetrics()
}

// recordMetrics records prometheus metrics
func (master *OsdnMaster) recordMetrics() {
mcEnabledNamespaceCount := 0.0
for _, enabled := range master.vnids.mcEnabled {
if enabled {
mcEnabledNamespaceCount += 1
}
}
metrics.RecordMulticastEnabledNamespaceCount(float64(mcEnabledNamespaceCount))
}
9 changes: 2 additions & 7 deletions pkg/network/node/vnids.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,14 @@ func (vmap *nodeVNIDMap) unsetVNID(name string) (id uint32, err error) {
return id, nil
}

func netnsIsMulticastEnabled(netns *osdnv1.NetNamespace) bool {
enabled, ok := netns.Annotations[osdnv1.MulticastEnabledAnnotation]
return enabled == "true" && ok
}

func (vmap *nodeVNIDMap) populateVNIDs() error {
nets, err := common.ListAllNetNamespaces(context.TODO(), vmap.osdnClient)
if err != nil {
return err
}

for _, net := range nets {
vmap.setVNID(net.Name, net.NetID, netnsIsMulticastEnabled(net))
vmap.setVNID(net.Name, net.NetID, common.NetnsIsMulticastEnabled(net))
}
return nil
}
Expand Down Expand Up @@ -227,7 +222,7 @@ func (vmap *nodeVNIDMap) handleAddOrUpdateNetNamespace(obj, _ interface{}, event
// Skip this event if nothing has changed
oldNetID, err := vmap.getVNID(netns.NetName)
oldMCEnabled := vmap.mcEnabled[netns.NetName]
mcEnabled := netnsIsMulticastEnabled(netns)
mcEnabled := common.NetnsIsMulticastEnabled(netns)
if err == nil && oldNetID == netns.NetID && oldMCEnabled == mcEnabled {
return
}
Expand Down