github.com/verrazzano/verrazzano@v1.7.1/application-operator/controllers/controller_utils.go (about) 1 // Copyright (c) 2020, 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package controllers 5 6 import ( 7 "context" 8 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1" 9 "k8s.io/client-go/util/workqueue" 10 ctrl "sigs.k8s.io/controller-runtime" 11 clipkg "sigs.k8s.io/controller-runtime/pkg/client" 12 "time" 13 14 "strconv" 15 "strings" 16 ) 17 18 // ConvertAPIVersionToGroupAndVersion splits APIVersion into API and version parts. 19 // An APIVersion takes the form api/version (e.g. networking.k8s.io/v1) 20 // If the input does not contain a / the group is defaulted to the empty string. 21 // apiVersion - The combined api and version to split 22 func ConvertAPIVersionToGroupAndVersion(apiVersion string) (string, string) { 23 parts := strings.SplitN(apiVersion, "/", 2) 24 if len(parts) < 2 { 25 // Use empty group for core types. 26 return "", parts[0] 27 } 28 return parts[0], parts[1] 29 } 30 31 // EnsureLastGenerationInStatus ensures that the status has the last generation saved 32 func EnsureLastGenerationInStatus(client clipkg.Client, wl *vzapi.VerrazzanoWebLogicWorkload) (ctrl.Result, error) { 33 if len(wl.Status.LastGeneration) > 0 { 34 return ctrl.Result{}, nil 35 } 36 37 // Update the status generation and always requeue 38 wl.Status.LastGeneration = strconv.Itoa(int(wl.Generation)) 39 err := client.Status().Update(context.TODO(), wl) 40 return ctrl.Result{Requeue: true, RequeueAfter: 1}, err 41 } 42 43 // NewDefaultRateLimiter returns a RateLimiter with default base backoff and max backoff 44 func NewDefaultRateLimiter() workqueue.RateLimiter { 45 // Default base delay for controller runtime requeue 46 const BaseDelay = 5 47 48 // Default maximum delay for controller runtime requeue 49 const MaxDelay = 60 50 51 return workqueue.NewItemExponentialFailureRateLimiter( 52 secsToDuration(BaseDelay), 53 secsToDuration(MaxDelay)) 54 } 55 56 func secsToDuration(secs int) time.Duration { 57 return time.Duration(float64(secs) * float64(time.Second)) 58 }