|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + v1 "github.com/operator-framework/api/pkg/operators/v1" |
| 8 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 9 | + "github.com/operator-framework/api/pkg/validation/errors" |
| 10 | +) |
| 11 | + |
| 12 | +// CaseSensitiveAnnotationKeySet is a set of annotation keys that are case sensitive |
| 13 | +// and can be used for validation purposes. The key is always lowercase and the value |
| 14 | +// contains the expected case sensitive string. This may not be an exhaustive list. |
| 15 | +var CaseSensitiveAnnotationKeySet = map[string]string{ |
| 16 | + |
| 17 | + strings.ToLower(v1.OperatorGroupAnnotationKey): v1.OperatorGroupAnnotationKey, |
| 18 | + strings.ToLower(v1.OperatorGroupNamespaceAnnotationKey): v1.OperatorGroupNamespaceAnnotationKey, |
| 19 | + strings.ToLower(v1.OperatorGroupTargetsAnnotationKey): v1.OperatorGroupTargetsAnnotationKey, |
| 20 | + strings.ToLower(v1.OperatorGroupProvidedAPIsAnnotationKey): v1.OperatorGroupProvidedAPIsAnnotationKey, |
| 21 | + strings.ToLower(v1alpha1.SkipRangeAnnotationKey): v1alpha1.SkipRangeAnnotationKey, |
| 22 | +} |
| 23 | + |
| 24 | +/* |
| 25 | +ValidateAnnotationNames will check annotation keys to ensure they are using |
| 26 | +proper case. Uses CaseSensitiveAnnotationKeySet as a source for keys |
| 27 | +which are known to be case sensitive. This function can be used anywhere |
| 28 | +annotations need to be checked for case sensitivity. |
| 29 | +
|
| 30 | +Arguments |
| 31 | +
|
| 32 | +• annotations: annotations map usually obtained from ObjectMeta.GetAnnotations() |
| 33 | +
|
| 34 | +• value: is the field or file that caused an error or warning |
| 35 | +
|
| 36 | +Returns |
| 37 | +
|
| 38 | +• errs: Any errors that may have been detected with the annotation keys provided |
| 39 | +*/ |
| 40 | +func ValidateAnnotationNames(annotations map[string]string, value interface{}) (errs []errors.Error) { |
| 41 | + // for every annotation provided |
| 42 | + for annotationKey := range annotations { |
| 43 | + // check the case sensitive key set for a matching lowercase annotation |
| 44 | + if knownCaseSensitiveKey, ok := CaseSensitiveAnnotationKeySet[strings.ToLower(annotationKey)]; ok { |
| 45 | + // we have a case-insensitive match... now check to see if the case is really correct |
| 46 | + if annotationKey != knownCaseSensitiveKey { |
| 47 | + // annotation key supplied is invalid due to bad case. |
| 48 | + errs = append(errs, errors.ErrFailedValidation(fmt.Sprintf("provided annotation %s uses wrong case and should be %s instead", annotationKey, knownCaseSensitiveKey), value)) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return errs |
| 53 | +} |
0 commit comments