|
| 1 | +/* |
| 2 | +Copyright 2021 IBM Corporation |
| 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 | +package v1alpha1 |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + kservev1alpha "github.com/kserve/kserve/pkg/apis/serving/v1alpha1" |
| 25 | + "github.com/kserve/kserve/pkg/constants" |
| 26 | + "github.com/kserve/modelmesh-serving/controllers/autoscaler" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 30 | +) |
| 31 | + |
| 32 | +// log is for logging in this package. |
| 33 | +var servingruntimeWebhookLog = logf.Log.WithName("servingruntime-webhook") |
| 34 | + |
| 35 | +//+kubebuilder:webhook:path=/validate-serving-modelmesh-io-v1alpha1-servingruntime,mutating=false,failurePolicy=fail,sideEffects=None,groups=serving.kserve.io,resources=servingruntimes;clusterservingruntimes,verbs=create;update,versions=v1alpha1,name=servingruntime.modelmesh-webhook-server.default,admissionReviewVersions=v1 |
| 36 | +type ServingRuntimeWebhook struct { |
| 37 | + Client client.Client |
| 38 | + decoder *admission.Decoder |
| 39 | +} |
| 40 | + |
| 41 | +func (s *ServingRuntimeWebhook) Handle(ctx context.Context, req admission.Request) admission.Response { |
| 42 | + var srAnnotations map[string]string |
| 43 | + |
| 44 | + if req.Kind.Kind == "ServingRuntime"{ |
| 45 | + servingRuntime := &kservev1alpha.ServingRuntime{} |
| 46 | + err := s.decoder.Decode(req, servingRuntime) |
| 47 | + if err != nil { |
| 48 | + return admission.Errored(http.StatusBadRequest, err) |
| 49 | + } |
| 50 | + srAnnotations = servingRuntime.ObjectMeta.Annotations |
| 51 | + }else{ |
| 52 | + clusterServingRuntime := &kservev1alpha.ClusterServingRuntime{} |
| 53 | + err := s.decoder.Decode(req, clusterServingRuntime) |
| 54 | + if err != nil { |
| 55 | + return admission.Errored(http.StatusBadRequest, err) |
| 56 | + } |
| 57 | + srAnnotations = clusterServingRuntime.ObjectMeta.Annotations |
| 58 | + } |
| 59 | + |
| 60 | + if err := validateServingRuntimeAutoscaler(srAnnotations); err != nil { |
| 61 | + return admission.Denied(err.Error()) |
| 62 | + } |
| 63 | + |
| 64 | + if err := validateAutoscalerTargetUtilizationPercentage(srAnnotations); err != nil { |
| 65 | + return admission.Denied(err.Error()) |
| 66 | + } |
| 67 | + |
| 68 | + if err := validateAutoScalingReplicas(srAnnotations); err != nil { |
| 69 | + return admission.Denied(err.Error()) |
| 70 | + } |
| 71 | + |
| 72 | + return admission.Allowed("Passed all validation checks for ServingRuntime") |
| 73 | +} |
| 74 | + |
| 75 | +// InjectDecoder injects the decoder. |
| 76 | +func (s *ServingRuntimeWebhook) InjectDecoder(d *admission.Decoder) error { |
| 77 | + s.decoder = d |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// Validation of servingruntime autoscaler class |
| 82 | +func validateServingRuntimeAutoscaler(annotations map[string]string) error { |
| 83 | + value, ok := annotations[constants.AutoscalerClass] |
| 84 | + class := constants.AutoscalerClassType(value) |
| 85 | + if ok { |
| 86 | + for _, item := range constants.AutoscalerAllowedClassList { |
| 87 | + if class == item { |
| 88 | + switch class { |
| 89 | + case constants.AutoscalerClassHPA: |
| 90 | + if metric, ok := annotations[constants.AutoscalerMetrics]; ok { |
| 91 | + return validateHPAMetrics(constants.AutoscalerMetricsType(metric)) |
| 92 | + } else { |
| 93 | + return nil |
| 94 | + } |
| 95 | + default: |
| 96 | + return fmt.Errorf("unknown autoscaler class [%s]", class) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return fmt.Errorf("[%s] is not a supported autoscaler class type.\n", value) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// Validate of autoscaler targetUtilizationPercentage |
| 107 | +func validateAutoscalerTargetUtilizationPercentage(annotations map[string]string) error { |
| 108 | + if value, ok := annotations[constants.TargetUtilizationPercentage]; ok { |
| 109 | + t, err := strconv.Atoi(value) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("The target utilization percentage should be a [1-100] integer.") |
| 112 | + } else { |
| 113 | + if t < 1 || t > 100 { |
| 114 | + return fmt.Errorf("The target utilization percentage should be a [1-100] integer.") |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +// Validate scaling options |
| 123 | +func validateAutoScalingReplicas(annotations map[string]string) error { |
| 124 | + autoscalerClassType := autoscaler.AutoscalerClassNone |
| 125 | + if value, ok := annotations[constants.AutoscalerClass]; ok { |
| 126 | + autoscalerClassType = value |
| 127 | + } |
| 128 | + |
| 129 | + switch autoscalerClassType { |
| 130 | + case string(constants.AutoscalerClassHPA): |
| 131 | + return validateScalingHPA(annotations) |
| 132 | + default: |
| 133 | + return nil |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func validateScalingHPA(annotations map[string]string) error { |
| 138 | + metric := constants.AutoScalerMetricsCPU |
| 139 | + if value, ok := annotations[constants.AutoscalerMetrics]; ok { |
| 140 | + metric = constants.AutoscalerMetricsType(value) |
| 141 | + } |
| 142 | + |
| 143 | + minReplicas := 1 |
| 144 | + if value, ok := annotations[constants.MinScaleAnnotationKey]; ok { |
| 145 | + if valueInt, err := strconv.Atoi(value); err != nil { |
| 146 | + return fmt.Errorf("The min replicas should be a integer.") |
| 147 | + } else if valueInt < 0 { |
| 148 | + return fmt.Errorf("The min replicas should be more than -1") |
| 149 | + } else { |
| 150 | + minReplicas = valueInt |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + maxReplicas := 1 |
| 155 | + if value, ok := annotations[constants.MaxScaleAnnotationKey]; ok { |
| 156 | + if valueInt, err := strconv.Atoi(value); err != nil { |
| 157 | + return fmt.Errorf("The max replicas should be a integer.") |
| 158 | + } else { |
| 159 | + maxReplicas = valueInt |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if minReplicas > maxReplicas { |
| 164 | + return fmt.Errorf("The max replicas should be bigger than min replicas.") |
| 165 | + } |
| 166 | + |
| 167 | + err := validateHPAMetrics(metric) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + if value, ok := annotations[constants.TargetUtilizationPercentage]; ok { |
| 173 | + t, err := strconv.Atoi(value) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("The target utilization percentage should be a [1-100] integer.") |
| 176 | + } else if metric == constants.AutoScalerMetricsMemory && t < 1 { |
| 177 | + return fmt.Errorf("The target memory should be greater than 1 MiB") |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +// Validate of autoscaler HPA metrics |
| 185 | +func validateHPAMetrics(metric constants.AutoscalerMetricsType) error { |
| 186 | + for _, item := range constants.AutoscalerAllowedMetricsList { |
| 187 | + if item == metric { |
| 188 | + return nil |
| 189 | + } |
| 190 | + } |
| 191 | + return fmt.Errorf("[%s] is not a supported metric.\n", metric) |
| 192 | + |
| 193 | +} |
0 commit comments