Skip to content

Commit

Permalink
Collect sync results
Browse files Browse the repository at this point in the history
Define NegSyncResult to store the result of sync and the error
encountered. Update the status of syncer based on the sync result.
  • Loading branch information
sawsa307 committed Feb 5, 2023
1 parent 5ccefff commit 845a00e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/neg/metrics/neg_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import (
"time"

"k8s.io/apimachinery/pkg/util/wait"
negtypes "k8s.io/ingress-gce/pkg/neg/types"
"k8s.io/klog/v2"
)

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

type SyncerMetrics struct {
// syncerStatusMap tracks the status of each syncer
syncerStatusMap map[negtypes.NegSyncerKey]string
// mu avoid race conditions and ensure correctness of metrics
mu sync.Mutex
// duration between metrics exports
Expand All @@ -39,6 +43,7 @@ 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"),
}
Expand Down Expand Up @@ -66,3 +71,16 @@ func (sm *SyncerMetrics) Run(stopCh <-chan struct{}) {
// export exports syncer metrics.
func (sm *SyncerMetrics) export() {
}

// UpdateSyncer update the status of corresponding syncer based on the syncResult.
func (sm *SyncerMetrics) UpdateSyncer(key negtypes.NegSyncerKey, syncResult *negtypes.NegSyncResult) {
syncerStatus := negtypes.GetSyncerStatus(syncResult.Result)

sm.mu.Lock()
defer sm.mu.Unlock()
if sm.syncerStatusMap == nil {
sm.syncerStatusMap = make(map[negtypes.NegSyncerKey]string)
sm.logger.V(3).Info("Syncer Metrics failed to initialize correctly, reinitializing syncerStatusMap: %v", sm.syncerStatusMap)
}
sm.syncerStatusMap[key] = syncerStatus
}
47 changes: 47 additions & 0 deletions pkg/neg/types/sync_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@ limitations under the License.

package types

import (
"errors"
"fmt"
)

var (
ResultEPCountsDiffer = "EPCountsDiffer"
ErrEPCountsDiffer = errors.New("endpoint counts from endpointData and endpointPodMap differ")

ResultEPMissingNodeName = "EPMissingNodeName"
ErrEPMissingNodeName = errors.New("endpoint has empty nodeName field")

ResultNodeNotFound = "NodeNotFound"
ErrNodeNotFound = errors.New("failed to retrieve associated zone of node")

ResultEPMissingZone = "EPMissingZone"
ErrEPMissingZone = errors.New("endpoint has empty zone field")

ResultEPSEndpointCountZero = "EPSEndpointCountZero"
ErrEPSEndpointCountZero = errors.New("endpoint count from endpointData cannot be zero")

ResultEPCalculationCountZero = "EPCalculationCountZero"
ErrEPCalculationCountZero = errors.New("endpoint count from endpointPodMap cannot be zero")

// these results have their own errors
ResultInvalidEPAttach = "InvalidEPAttach"
ResultInvalidEPDetach = "InvalidEPDetach"
ResultNegNotFound = "NegNotFound"
ResultCurrentEPNotFound = "CurrentEPNotFound"
ResultEPSNotFound = "EPSNotFound"
ResultOtherError = "OtherError"
ResultInProgress = "InProgress"
ResultSuccess = "Success"
)

type NegSyncResult struct {
Error error
Result string
Expand All @@ -24,3 +59,15 @@ func NewNegSyncResult(err error, result string) *NegSyncResult {
Result: result,
}
}

func GetAllResult() []string {
return []string{ResultEPCountsDiffer, ResultEPMissingNodeName,
ResultNodeNotFound, ResultEPMissingZone, ResultEPSEndpointCountZero,
ResultEPCalculationCountZero, ResultInvalidEPAttach, ResultInvalidEPDetach,
ResultNegNotFound, ResultCurrentEPNotFound, ResultEPSNotFound,
ResultOtherError, ResultInProgress, ResultSuccess}
}

func GetSyncerStatus(result string) string {
return fmt.Sprintf("Syncer%s", result)
}

0 comments on commit 845a00e

Please sign in to comment.