Implement parseFloat for annotations (#9195)

This commit is contained in:
Kir Shatrov 2022-10-20 22:57:25 +03:00 committed by GitHub
parent 5f2a79495a
commit 84614b99c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View file

@ -80,6 +80,18 @@ func (a ingAnnotations) parseInt(name string) (int, error) {
return 0, errors.ErrMissingAnnotations
}
func (a ingAnnotations) parseFloat32(name string) (float32, error) {
val, ok := a[name]
if ok {
i, err := strconv.ParseFloat(val, 32)
if err != nil {
return 0, errors.NewInvalidAnnotationContent(name, val)
}
return float32(i), nil
}
return 0, errors.ErrMissingAnnotations
}
func checkAnnotation(name string, ing *networking.Ingress) error {
if ing == nil || len(ing.GetAnnotations()) == 0 {
return errors.ErrMissingAnnotations
@ -122,6 +134,16 @@ func GetIntAnnotation(name string, ing *networking.Ingress) (int, error) {
return ingAnnotations(ing.GetAnnotations()).parseInt(v)
}
// GetFloatAnnotation extracts a float32 from an Ingress annotation
func GetFloatAnnotation(name string, ing *networking.Ingress) (float32, error) {
v := GetAnnotationWithPrefix(name)
err := checkAnnotation(v, ing)
if err != nil {
return 0, err
}
return ingAnnotations(ing.GetAnnotations()).parseFloat32(v)
}
// GetAnnotationWithPrefix returns the prefix of ingress annotations
func GetAnnotationWithPrefix(suffix string) string {
return fmt.Sprintf("%v/%v", AnnotationsPrefix, suffix)