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

Define endpoint state #1934

Merged
merged 2 commits into from
Feb 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ import (

type SyncerMetricsCollector interface {
UpdateSyncer(key negtypes.NegSyncerKey, result *negtypes.NegSyncResult)
SetSyncerEPMetrics(key negtypes.NegSyncerKey, epState *negtypes.SyncerEPStat)
}

type SyncerMetrics struct {
// syncerStatusMap tracks the status of each syncer
syncerStatusMap map[negtypes.NegSyncerKey]string
// syncerEndpointStateMap is a map between syncer and endpoint state counts
syncerEndpointStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap
// syncerEPSStateMap is a map between syncer and endpoint slice state counts
syncerEPSStateMap map[negtypes.NegSyncerKey]negtypes.StateCountMap
// mu avoid race conditions and ensure correctness of metrics
mu sync.Mutex
// duration between metrics exports
Expand All @@ -43,9 +48,11 @@ type SyncerMetrics struct {
// NewNEGMetricsCollector initializes SyncerMetrics and starts a go routine to compute and export metrics periodically.
func NewNegMetricsCollector(exportInterval time.Duration, logger klog.Logger) *SyncerMetrics {
return &SyncerMetrics{
syncerStatusMap: make(map[negtypes.NegSyncerKey]string),
metricsInterval: exportInterval,
logger: logger.WithName("NegMetricsCollector"),
syncerStatusMap: make(map[negtypes.NegSyncerKey]string),
syncerEndpointStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap),
syncerEPSStateMap: make(map[negtypes.NegSyncerKey]negtypes.StateCountMap),
metricsInterval: exportInterval,
logger: logger.WithName("NegMetricsCollector"),
}
}

Expand Down Expand Up @@ -82,3 +89,20 @@ func (sm *SyncerMetrics) UpdateSyncer(key negtypes.NegSyncerKey, syncResult *neg
}
sm.syncerStatusMap[key] = syncResult.Result
}

// SetSyncerEPMetrics update the endpoint count based on the endpointStat
func (sm *SyncerMetrics) SetSyncerEPMetrics(key negtypes.NegSyncerKey, endpointStat *negtypes.SyncerEPStat) {
sm.mu.Lock()
defer sm.mu.Unlock()
if sm.syncerEndpointStateMap == nil {
sm.syncerEndpointStateMap = make(map[negtypes.NegSyncerKey]negtypes.StateCountMap)
sm.logger.V(3).Info("Syncer Metrics failed to initialize correctly, reinitializing syncerEPStateMap: %v", sm.syncerEndpointStateMap)
}
sm.syncerEndpointStateMap[key] = endpointStat.EndpointStateCount

if sm.syncerEPSStateMap == nil {
sm.syncerEPSStateMap = make(map[negtypes.NegSyncerKey]negtypes.StateCountMap)
sm.logger.V(3).Info("Syncer Metrics failed to initialize correctly, reinitializing syncerEPSStateMap: %v", sm.syncerEPSStateMap)
}
sm.syncerEPSStateMap[key] = endpointStat.EndpointSliceStateCount
}
51 changes: 51 additions & 0 deletions pkg/neg/types/endpoint_stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

type State string

const (
swetharepakula marked this conversation as resolved.
Show resolved Hide resolved
EPMissingNodeName = State("EndpointMissingNodeName")
EPMissingPod = State("EndpointMissingPod")
EPMissingZone = State("EndpointMissingZone")
EPMissingField = State("EndpointMissingField")
EPDuplicate = State("EndpointDuplicate")
EPTotal = State("EndpointTotal")

EPSWithMissingNodeName = State("EndpointsliceWithMissingNodeNameEP")
EPSWithMissingPod = State("EndpointsliceWithMissingPodEP")
EPSWithMissingZone = State("EndpointsliceWithMissingZoneEP")
EPSWithMissingField = State("EndpointsliceWithMissingFieldEP")
EPSWithDuplicate = State("EndpointsliceWithDuplicateEP")
EPSTotal = State("EndpointsliceTotal")
)

// SyncerEPStat contains endpoint and endpointslice status related to a syncer
type SyncerEPStat struct {
EndpointStateCount StateCountMap
EndpointSliceStateCount StateCountMap
}

// StateCountMap collect the count of instances in different states
type StateCountMap map[State]int

func NewSyncerEPStat() *SyncerEPStat {
return &SyncerEPStat{
EndpointStateCount: make(map[State]int),
EndpointSliceStateCount: make(map[State]int),
}
}