Skip to content

Commit

Permalink
Init first sync error time with transation start time
Browse files Browse the repository at this point in the history
  • Loading branch information
kl52752 committed Jun 8, 2022
1 parent cdf339e commit 511cb82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
37 changes: 37 additions & 0 deletions pkg/l4lb/l4netlbcontroller_test.go
Expand Up @@ -671,6 +671,43 @@ func TestProcessServiceDeletionFailed(t *testing.T) {
}
}

func TestServiceStatusForErrorSync(t *testing.T) {
lc := newL4NetLBServiceController()
(lc.ctx.Cloud.Compute().(*cloud.MockGCE)).MockForwardingRules.InsertHook = mock.InsertForwardingRulesInternalErrHook

svc := test.NewL4NetLBRBSService(8080)
addNetLBService(lc, svc)

syncResult := lc.syncInternal(svc)
if syncResult.Error == nil {
t.Errorf("Expected error in sync controller")
}
if syncResult.MetricsState.InSuccess == true {
t.Fatalf("Metric status InSuccess for service %s/%s mismatch, expected: true, received: false", svc.Namespace, svc.Name)
}
if syncResult.MetricsState.FirstSyncErrorTime == nil {
t.Fatalf("Metric status FirstSyncErrorTime for service %s/%s mismatch, expected: not nil, received: nil", svc.Namespace, svc.Name)
}
}

func TestServiceStatusForSuccessSync(t *testing.T) {
lc := newL4NetLBServiceController()

svc := test.NewL4NetLBRBSService(8080)
addNetLBService(lc, svc)

syncResult := lc.syncInternal(svc)
if syncResult.Error != nil {
t.Errorf("Unexpected error in sync controller")
}
if syncResult.MetricsState.InSuccess != true {
t.Fatalf("Metric status InSuccess for service %s/%s mismatch, expected: false, received: true", svc.Namespace, svc.Name)
}
if syncResult.MetricsState.FirstSyncErrorTime != nil {
t.Fatalf("Metric status FirstSyncErrorTime for service %s/%s mismatch, expected: nil, received: %v", svc.Namespace, svc.Name, syncResult.MetricsState.FirstSyncErrorTime)
}
}

func TestProcessServiceUpdate(t *testing.T) {
flags.F.MaxIgSize = 1000

Expand Down
18 changes: 12 additions & 6 deletions pkg/loadbalancers/l4netlb.go
Expand Up @@ -64,6 +64,16 @@ type L4NetLBSyncResult struct {
StartTime time.Time
}

func NewL4SynResult(syncType string) *L4NetLBSyncResult {
result := &L4NetLBSyncResult{
Annotations: make(map[string]string),
StartTime: time.Now(),
SyncType: syncType,
}
result.MetricsState = metrics.InitL4NetLBServiceState(&result.StartTime)
return result
}

// SetMetricsForSuccessfulService should be call after successful sync.
func (r *L4NetLBSyncResult) SetMetricsForSuccessfulService() {
r.MetricsState.FirstSyncErrorTime = nil
Expand Down Expand Up @@ -101,11 +111,7 @@ func (l4netlb *L4NetLB) createKey(name string) (*meta.Key, error) {
// It returns a LoadBalancerStatus with the updated ForwardingRule IP address.
// This function does not link instances to Backend Service.
func (l4netlb *L4NetLB) EnsureFrontend(nodeNames []string, svc *corev1.Service) *L4NetLBSyncResult {
result := &L4NetLBSyncResult{
Annotations: make(map[string]string),
StartTime: time.Now(),
SyncType: SyncTypeCreate,
}
result := NewL4SynResult(SyncTypeCreate)

// If service already has an IP assigned, treat it as an update instead of a new Loadbalancer.
if len(svc.Status.LoadBalancer.Ingress) > 0 {
Expand Down Expand Up @@ -161,7 +167,7 @@ func (l4netlb *L4NetLB) EnsureFrontend(nodeNames []string, svc *corev1.Service)
// EnsureLoadBalancerDeleted performs a cleanup of all GCE resources for the given loadbalancer service.
// It is health check, firewall rules and backend service
func (l4netlb *L4NetLB) EnsureLoadBalancerDeleted(svc *corev1.Service) *L4NetLBSyncResult {
result := &L4NetLBSyncResult{SyncType: SyncTypeDelete, StartTime: time.Now()}
result := NewL4SynResult(SyncTypeDelete)

frName := l4netlb.GetFRName()
key, err := l4netlb.createKey(frName)
Expand Down
4 changes: 4 additions & 0 deletions pkg/metrics/types.go
Expand Up @@ -87,6 +87,10 @@ type L4NetLBServiceState struct {
FirstSyncErrorTime *time.Time
}

func InitL4NetLBServiceState(syncTime *time.Time) L4NetLBServiceState {
return L4NetLBServiceState{ /*IsManagedIP =*/ false /*IsPremiumTier =*/, false /*InSuccess =*/, false /*IsUserError =*/, false, syncTime}
}

// IngressMetricsCollector is an interface to update/delete ingress states in the cache
// that is used for computing ingress usage metrics.
type IngressMetricsCollector interface {
Expand Down

0 comments on commit 511cb82

Please sign in to comment.