validate.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright © The CDI Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/container-orchestrated-devices/container-device-interface/internal/validation/k8s"
  18. )
  19. // ValidateSpecAnnotations checks whether spec annotations are valid.
  20. func ValidateSpecAnnotations(name string, any interface{}) error {
  21. if any == nil {
  22. return nil
  23. }
  24. switch v := any.(type) {
  25. case map[string]interface{}:
  26. annotations := make(map[string]string)
  27. for k, v := range v {
  28. if s, ok := v.(string); ok {
  29. annotations[k] = s
  30. } else {
  31. return fmt.Errorf("invalid annotation %v.%v; %v is not a string", name, k, any)
  32. }
  33. }
  34. return validateSpecAnnotations(name, annotations)
  35. }
  36. return nil
  37. }
  38. // validateSpecAnnotations checks whether spec annotations are valid.
  39. func validateSpecAnnotations(name string, annotations map[string]string) error {
  40. path := "annotations"
  41. if name != "" {
  42. path = strings.Join([]string{name, path}, ".")
  43. }
  44. return k8s.ValidateAnnotations(annotations, path)
  45. }