@@ -34,6 +34,12 @@ import (
3434 "sigs.k8s.io/controller-runtime/pkg/webhook"
3535)
3636
37+ const (
38+ fqdnRegex = `^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z]{2,})+$`
39+ sriovResourceNameRegex = `^([A-Za-z0-9][A-Za-z0-9_.]*)?[A-Za-z0-9]$`
40+ rdmaResourceNameRegex = `^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$`
41+ )
42+
3743// log is for logging in this package.
3844var nicClusterPolicyLog = logf .Log .WithName ("nicclusterpolicy-resource" )
3945
@@ -198,12 +204,9 @@ func (dp *DevicePluginSpec) validateSriovNetworkDevicePlugin(fldPath *field.Path
198204 resourceJSONLoader := gojsonschema .NewStringLoader (string (resourceJSONString ))
199205 var selectorResult * gojsonschema.Result
200206 var selectorErr error
201- resourceName := resource ["resourceName" ].(string )
202- if ! isValidSriovNetworkDevicePluginResourceName (resourceName ) {
203- allErrs = append (allErrs , field .Invalid (fldPath .Child ("Config" ), dp .Config ,
204- "Invalid Resource name, it must consist of alphanumeric characters, '_' or '.', " +
205- "and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', " +
206- "or '123_abc', regex used for validation is '([A-Za-z0-9][A-Za-z0-9_.]*)?[A-Za-z0-9]')" ))
207+ var ok bool
208+ ok , allErrs = validateResourceNamePrefix (resource , allErrs , fldPath , dp )
209+ if ! ok {
207210 return allErrs
208211 }
209212 deviceType := resource ["deviceType" ]
@@ -229,6 +232,28 @@ func (dp *DevicePluginSpec) validateSriovNetworkDevicePlugin(fldPath *field.Path
229232 return allErrs
230233}
231234
235+ func validateResourceNamePrefix (resource map [string ]interface {},
236+ allErrs field.ErrorList , fldPath * field.Path , dp * DevicePluginSpec ) (bool , field.ErrorList ) {
237+ resourceName := resource ["resourceName" ].(string )
238+ if ! isValidSriovNetworkDevicePluginResourceName (resourceName ) {
239+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("Config" ), dp .Config ,
240+ "Invalid Resource name, it must consist of alphanumeric characters, '_' or '.', " +
241+ "and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', " +
242+ "or '123_abc', regex used for validation is " + sriovResourceNameRegex ))
243+ return false , allErrs
244+ }
245+ resourcePrefix , ok := resource ["resourcePrefix" ]
246+ if ok {
247+ if ! isValidFQDN (resourcePrefix .(string )) {
248+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("Config" ), dp .Config ,
249+ "Invalid Resource prefix, it must be a valid FQDN" +
250+ "regex used for validation is " + fqdnRegex ))
251+ return false , allErrs
252+ }
253+ }
254+ return true , allErrs
255+ }
256+
232257func (dp * DevicePluginSpec ) validateRdmaSharedDevicePlugin (fldPath * field.Path ) field.ErrorList {
233258 var allErrs field.ErrorList
234259 var rdmaSharedDevicePluginConfigJSON map [string ]interface {}
@@ -263,8 +288,16 @@ func (dp *DevicePluginSpec) validateRdmaSharedDevicePlugin(fldPath *field.Path)
263288 allErrs = append (allErrs , field .Invalid (fldPath .Child ("Config" ),
264289 dp .Config , "Invalid Resource name, it must consist of alphanumeric characters, " +
265290 "'-', '_' or '.', and must start and end with an alphanumeric character " +
266- "(e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0" +
267- "-9_.]*)?[A-Za-z0-9]')" ))
291+ "(e.g. 'MyName', or 'my.name', or '123-abc') regex used for validation is " + rdmaResourceNameRegex ))
292+ }
293+ resourcePrefix , ok := config ["resourcePrefix" ]
294+ if ok {
295+ if ! isValidFQDN (resourcePrefix .(string )) {
296+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("Config" ), dp .Config ,
297+ "Invalid Resource prefix, it must be a valid FQDN " +
298+ "regex used for validation is " + fqdnRegex ))
299+ return allErrs
300+ }
268301 }
269302 }
270303 } else {
@@ -336,17 +369,20 @@ func isValidOFEDVersion(version string) bool {
336369}
337370
338371func isValidSriovNetworkDevicePluginResourceName (resourceName string ) bool {
339- resourceNamePattern := `^([A-Za-z0-9][A-Za-z0-9_.]*)?[A-Za-z0-9]$`
340- resourceNameRegex := regexp .MustCompile (resourceNamePattern )
372+ resourceNameRegex := regexp .MustCompile (sriovResourceNameRegex )
341373 return resourceNameRegex .MatchString (resourceName )
342374}
343375
344376func isValidRdmaSharedDevicePluginResourceName (resourceName string ) bool {
345- resourceNamePattern := `^([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]$`
346- resourceNameRegex := regexp .MustCompile (resourceNamePattern )
377+ resourceNameRegex := regexp .MustCompile (rdmaResourceNameRegex )
347378 return resourceNameRegex .MatchString (resourceName )
348379}
349380
381+ func isValidFQDN (input string ) bool {
382+ regex := regexp .MustCompile (fqdnRegex )
383+ return regex .MatchString (input )
384+ }
385+
350386// +kubebuilder:object:generate=false
351387type schemaValidator struct {
352388 schemas map [string ]* gojsonschema.Schema
0 commit comments