Skip to content

Commit

Permalink
Support using sync.Map in common Config
Browse files Browse the repository at this point in the history
  • Loading branch information
kabicin committed Nov 19, 2024
1 parent eb57e85 commit c092702
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
35 changes: 29 additions & 6 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,46 @@ func LoadFromConfigMap(oc *sync.Map, cm *corev1.ConfigMap) {
oc.Store(k, v)
}
}
func CheckValidValue(oc *sync.Map, key string, OperatorName string) error {
// value := oc[key]

// Loads the value stored in operator config's key or falls back to the operator default value
func LoadConfigValueOrFallback(oc *sync.Map, key string) (string, error) {
value, ok := oc.Load(key)
if !ok {
return errors.New("operator config could not get value for key: " + key)
fallbackValue, fallbackErr := GetFallbackConfigValue(key)
if fallbackErr != nil {
return "", fallbackErr
}
return fallbackValue, nil
}
return value.(string), nil
}

// Gets the value associated with the operator default config if the key exists, otherwise return an error
func GetFallbackConfigValue(key string) (string, error) {
fallbackConfig := DefaultOpConfig()
fallbackValue, fallbackOk := fallbackConfig.Load(key)
if !fallbackOk {
return "", errors.New("could not get value for key " + key + " in the default operator config")
}
return fallbackValue.(string), nil
}

func CheckValidValue(oc *sync.Map, key string, OperatorName string) error {
value, err := LoadConfigValueOrFallback(oc, key)
if err != nil {
return err
}

intValue, err := strconv.Atoi(value.(string))
intValue, err := strconv.Atoi(value)
if err != nil {
SetConfigMapDefaultValue(oc, key)
return errors.New(key + " in ConfigMap: " + OperatorName + " has an invalid syntax, error: " + err.Error())
} else if key == OpConfigReconcileIntervalSeconds && intValue <= 0 {
SetConfigMapDefaultValue(oc, key)
return errors.New(key + " in ConfigMap: " + OperatorName + " is set to " + value.(string) + ". It must be greater than 0.")
return errors.New(key + " in ConfigMap: " + OperatorName + " is set to " + value + ". It must be greater than 0.")
} else if key == OpConfigReconcileIntervalPercentage && intValue < 0 {
SetConfigMapDefaultValue(oc, key)
return errors.New(key + " in ConfigMap: " + OperatorName + " is set to " + value.(string) + ". It must be greater than or equal to 0.")
return errors.New(key + " in ConfigMap: " + OperatorName + " is set to " + value + ". It must be greater than or equal to 0.")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/runtimecomponent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
return reconcile.Result{}, err
}

if err = common.Config.CheckValidValue(common.OpConfigReconcileIntervalSeconds, OperatorName); err != nil {
if err = common.CheckValidValue(common.Config, common.OpConfigReconcileIntervalSeconds, OperatorName); err != nil {
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}

if err = common.Config.CheckValidValue(common.OpConfigReconcileIntervalPercentage, OperatorName); err != nil {
if err = common.CheckValidValue(common.Config, common.OpConfigReconcileIntervalPercentage, OperatorName); err != nil {
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}

Expand Down
40 changes: 35 additions & 5 deletions utils/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ func getBaseReconcileInterval(newCondition common.StatusCondition, s common.Base
var newCount *int32
newCondition.SetUnchangedConditionCount(newCount)

baseIntervalInt, _ := strconv.Atoi(common.Config[common.OpConfigReconcileIntervalSeconds])
reconcileIntervalSeconds, err := common.LoadConfigValueOrFallback(common.Config, common.OpConfigReconcileIntervalSeconds)
if err != nil {
return time.Second
}
baseIntervalInt, _ := strconv.Atoi(reconcileIntervalSeconds)
baseInterval := int32(baseIntervalInt)
s.SetReconcileInterval(&baseInterval)

Expand All @@ -209,11 +213,18 @@ func getBaseReconcileInterval(newCondition common.StatusCondition, s common.Base

func updateReconcileInterval(maxSeconds int, oldCondition common.StatusCondition, newCondition common.StatusCondition, s common.BaseComponentStatus) time.Duration {
oldReconcileInterval := float64(*s.GetReconcileInterval())

defaultReconcileInterval := 5 * time.Second
var newCount int32
if count := oldCondition.GetUnchangedConditionCount(); count == nil {
newCount = 0
oldReconcileInterval, _ = strconv.ParseFloat(common.Config[common.OpConfigReconcileIntervalSeconds], 64)
oldReconcileIntervalSeconds, err := common.LoadConfigValueOrFallback(common.Config, common.OpConfigReconcileIntervalSeconds)
if err != nil {
return defaultReconcileInterval
}
oldReconcileInterval, err := strconv.ParseFloat(oldReconcileIntervalSeconds, 64)
if err != nil {
return defaultReconcileInterval
}
baseInterval := int32(oldReconcileInterval)
s.SetReconcileInterval(&baseInterval)
} else {
Expand All @@ -226,10 +237,25 @@ func updateReconcileInterval(maxSeconds int, oldCondition common.StatusCondition

// For every repeated 2 reconciliation errors, increase reconcile period
if newCount >= 2 && newCount%2 == 0 {
intervalIncreasePercentage, _ := strconv.ParseFloat(common.Config[common.OpConfigReconcileIntervalPercentage], 64)
reconcileIntervalPercentage, err := common.LoadConfigValueOrFallback(common.Config, common.OpConfigReconcileIntervalPercentage)
if err != nil {
return defaultReconcileInterval
}
intervalIncreasePercentage, err := strconv.ParseFloat(reconcileIntervalPercentage, 64)
if err != nil {
return defaultReconcileInterval
}
exp := float64(newCount / 2)
increase := math.Pow(1+(intervalIncreasePercentage/100), exp)
baseInterval, _ := strconv.ParseFloat(common.Config[common.OpConfigReconcileIntervalSeconds], 64)

reconcileIntervalSeconds, err := common.LoadConfigValueOrFallback(common.Config, common.OpConfigReconcileIntervalSeconds)
if err != nil {
return defaultReconcileInterval
}
baseInterval, err := strconv.ParseFloat(reconcileIntervalSeconds, 64)
if err != nil {
return defaultReconcileInterval
}
newInterval := int32(baseInterval * increase)

// Only increase to the maximum interval
Expand Down Expand Up @@ -516,6 +542,8 @@ func (r *ReconcilerBase) GenerateCMIssuer(namespace string, prefix string, CACom
if err != nil {
return err
}
} else {
return errors.New("could not load value " + common.OpConfigCMCADuration + " from the operator config map")
}
caCert.Spec.Duration = &metav1.Duration{Duration: duration}
return nil
Expand Down Expand Up @@ -696,6 +724,8 @@ func (r *ReconcilerBase) GenerateSvcCertSecret(ba common.BaseComponent, prefix s
if err != nil {
return err
}
} else {
return errors.New("could not load value " + common.OpConfigCMCertDuration + " from the operator config map")
}
svcCert.Spec.Duration = &metav1.Duration{Duration: duration}

Expand Down

0 comments on commit c092702

Please sign in to comment.