|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package priorityholdback |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "k8s.io/utils/ptr" |
| 24 | +) |
| 25 | + |
| 26 | +// strategy constants define the supported gating strategies. |
| 27 | +const ( |
| 28 | + // strategyStepwiseSpread divides the [minCeiling, maxCeiling] range into equal steps based on |
| 29 | + // the count of active priorities, ignoring their absolute numerical difference. |
| 30 | + strategyStepwiseSpread = "stepwise-spread" |
| 31 | + |
| 32 | + // strategyLinearProportional scales ceilings linearly based on numerical priority values |
| 33 | + // relative to the observed active range. |
| 34 | + strategyLinearProportional = "linear-proportional" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + // defaultMaxCeiling allows the highest priority to use full capacity. |
| 39 | + defaultMaxCeiling float64 = 1.0 |
| 40 | +) |
| 41 | + |
| 42 | +// apiConfig represents the external configuration schema for the priority holdback policy. |
| 43 | +// It is designed to be deserialized from JSON via the plugin's raw parameters. |
| 44 | +type apiConfig struct { |
| 45 | + // Strategy selects the gating algorithm used to compute per-priority admission ceilings. |
| 46 | + // |
| 47 | + // Required. Valid values: "stepwise-spread", "linear-proportional". |
| 48 | + Strategy *string `json:"strategy"` |
| 49 | + |
| 50 | + // MinCeiling is the admission ceiling assigned to the lowest-priority traffic. |
| 51 | + // Determines how aggressively the lowest priority is gated as saturation rises. |
| 52 | + // |
| 53 | + // Required. Must be in [0.0, 1.0) and strictly less than MaxCeiling. |
| 54 | + MinCeiling *float64 `json:"minCeiling"` |
| 55 | + |
| 56 | + // MaxCeiling is the admission ceiling assigned to the highest-priority traffic. |
| 57 | + // A value of 1.0 means the highest priority is only gated at full saturation. |
| 58 | + // |
| 59 | + // Defaults to 1.0 if unset. Must be in (0.0, 1.0] and strictly greater than MinCeiling. |
| 60 | + MaxCeiling *float64 `json:"maxCeiling,omitempty"` |
| 61 | +} |
| 62 | + |
| 63 | +// config is the internal, fully-validated configuration used by the policy. |
| 64 | +type config struct { |
| 65 | + strategy string |
| 66 | + minCeiling float64 |
| 67 | + maxCeiling float64 |
| 68 | +} |
| 69 | + |
| 70 | +// buildConfig applies the configuration lifecycle (defaulting and validation) and translates the |
| 71 | +// external schema into the internal domain model. |
| 72 | +// The provided apiConfig is copied to prevent mutation side-effects. |
| 73 | +func buildConfig(apiCfg *apiConfig) (*config, error) { |
| 74 | + var safeCfg apiConfig |
| 75 | + if apiCfg != nil { |
| 76 | + safeCfg = *apiCfg |
| 77 | + } |
| 78 | + |
| 79 | + if err := checkRequired(&safeCfg); err != nil { |
| 80 | + return nil, fmt.Errorf("invalid priority holdback policy configuration: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + applyDefaults(&safeCfg) |
| 84 | + |
| 85 | + if err := validateConfig(&safeCfg); err != nil { |
| 86 | + return nil, fmt.Errorf("invalid priority holdback policy configuration: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + return &config{ |
| 90 | + strategy: *safeCfg.Strategy, |
| 91 | + minCeiling: *safeCfg.MinCeiling, |
| 92 | + maxCeiling: *safeCfg.MaxCeiling, |
| 93 | + }, nil |
| 94 | +} |
| 95 | + |
| 96 | +// checkRequired verifies that mandatory fields are present before defaulting. |
| 97 | +func checkRequired(cfg *apiConfig) error { |
| 98 | + var errs []error |
| 99 | + |
| 100 | + if cfg.Strategy == nil { |
| 101 | + errs = append(errs, fmt.Errorf("strategy is required")) |
| 102 | + } |
| 103 | + if cfg.MinCeiling == nil { |
| 104 | + errs = append(errs, fmt.Errorf("minCeiling is required")) |
| 105 | + } |
| 106 | + |
| 107 | + return errors.Join(errs...) |
| 108 | +} |
| 109 | + |
| 110 | +// applyDefaults populates unset optional fields with their standard defaults. |
| 111 | +func applyDefaults(cfg *apiConfig) { |
| 112 | + if cfg.MaxCeiling == nil { |
| 113 | + cfg.MaxCeiling = ptr.To(defaultMaxCeiling) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// validateConfig checks the constraints of the fully defaulted configuration. |
| 118 | +// It aggregates all validation failures rather than failing on the first error. |
| 119 | +func validateConfig(cfg *apiConfig) error { |
| 120 | + var errs []error |
| 121 | + |
| 122 | + if cfg.Strategy != nil { |
| 123 | + switch *cfg.Strategy { |
| 124 | + case strategyStepwiseSpread, strategyLinearProportional: |
| 125 | + default: |
| 126 | + errs = append(errs, fmt.Errorf("unsupported strategy %q, must be one of: %q, %q", |
| 127 | + *cfg.Strategy, strategyStepwiseSpread, strategyLinearProportional)) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if cfg.MinCeiling != nil && (*cfg.MinCeiling < 0.0 || *cfg.MinCeiling >= 1.0) { |
| 132 | + errs = append(errs, fmt.Errorf("minCeiling must be in [0.0, 1.0), got %f", *cfg.MinCeiling)) |
| 133 | + } |
| 134 | + |
| 135 | + if cfg.MaxCeiling != nil && (*cfg.MaxCeiling <= 0.0 || *cfg.MaxCeiling > 1.0) { |
| 136 | + errs = append(errs, fmt.Errorf("maxCeiling must be in (0.0, 1.0], got %f", *cfg.MaxCeiling)) |
| 137 | + } |
| 138 | + |
| 139 | + if cfg.MinCeiling != nil && cfg.MaxCeiling != nil && *cfg.MinCeiling >= *cfg.MaxCeiling { |
| 140 | + errs = append(errs, fmt.Errorf("minCeiling (%f) must be strictly less than maxCeiling (%f)", |
| 141 | + *cfg.MinCeiling, *cfg.MaxCeiling)) |
| 142 | + } |
| 143 | + |
| 144 | + return errors.Join(errs...) |
| 145 | +} |
0 commit comments