Skip to content

Commit

Permalink
Merge pull request #1933 from sawsa307/define-sync-results
Browse files Browse the repository at this point in the history
Define sync results
  • Loading branch information
k8s-ci-robot authored Feb 16, 2023
2 parents 98aa2cf + 8749d93 commit c723cd1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
16 changes: 16 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,14 @@ 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) {
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] = syncResult.Result
}
58 changes: 58 additions & 0 deletions pkg/neg/types/sync_results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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

import "errors"

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
}

func NewNegSyncResult(err error, result string) *NegSyncResult {
return &NegSyncResult{
Error: err,
Result: result,
}
}

0 comments on commit c723cd1

Please sign in to comment.