...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/jitter/jitter.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/jitter

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package jitter
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/reconciliationinterval"
    21  	dclmetadata "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/metadata"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/util/wait"
    28  )
    29  
    30  // GenerateWatchJitteredTimeoutPeriod returns a wait duration to reenqueue the request between
    31  // 1/2 * MeanReconcileReenqueuePeriod and 3/2 * MeanReconcileReenqueuePeriod (not inclusive of
    32  // upper bound). The mean duration to reenqueue is MeanReconcileReenqueuePeriod.
    33  func GenerateWatchJitteredTimeoutPeriod() time.Duration {
    34  	return wait.Jitter(k8s.MeanReconcileReenqueuePeriod/2, k8s.JitterFactor)
    35  }
    36  
    37  // GenerateJitteredReenqueuePeriod returns a wait duration to reenqueue the request based
    38  // on configured reconcile interval in TF servicemapping, DCL metadata, IAM resource config.
    39  // The wait duration can be overridden with the reconcile interval configured as the object's annotation.
    40  func GenerateJitteredReenqueuePeriod(gvk schema.GroupVersionKind,
    41  	smLoader *servicemappingloader.ServiceMappingLoader,
    42  	serviceMetadataLoader dclmetadata.ServiceMetadataLoader, obj metav1.Object) (time.Duration, error) {
    43  	if val, ok := k8s.GetAnnotation(k8s.ReconcileIntervalInSecondsAnnotation, obj); ok {
    44  		reconcileInterval, err := reconciliationinterval.MeanReconcileReenqueuePeriodFromAnnotation(val)
    45  		if err != nil {
    46  			return 0, err
    47  		}
    48  		return wait.Jitter(reconcileInterval/2, k8s.JitterFactor), nil
    49  	}
    50  	return wait.Jitter(reconciliationinterval.MeanReconcileReenqueuePeriod(gvk, smLoader, serviceMetadataLoader)/2, k8s.JitterFactor), nil
    51  }
    52  

View as plain text