validators.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2016 The Kubernetes 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 system
  14. import (
  15. "fmt"
  16. errorsutil "k8s.io/apimachinery/pkg/util/errors"
  17. )
  18. // Validator is the interface for all validators.
  19. type Validator interface {
  20. // Name is the name of the validator.
  21. Name() string
  22. // Validate is the validate function.
  23. Validate(SysSpec) (error, error)
  24. }
  25. // Reporter is the interface for the reporters for the validators.
  26. type Reporter interface {
  27. // Report reports the results of the system verification
  28. Report(string, string, ValidationResultType) error
  29. }
  30. // Validate uses validators to validate the system and returns a warning or error.
  31. func Validate(spec SysSpec, validators []Validator) (error, error) {
  32. var errs []error
  33. var warns []error
  34. for _, v := range validators {
  35. fmt.Printf("Validating %s...\n", v.Name())
  36. warn, err := v.Validate(spec)
  37. errs = append(errs, err)
  38. warns = append(warns, warn)
  39. }
  40. return errorsutil.NewAggregate(warns), errorsutil.NewAggregate(errs)
  41. }
  42. // ValidateSpec uses all default validators to validate the system and writes to stdout.
  43. func ValidateSpec(spec SysSpec, runtime string) (error, error) {
  44. // OS-level validators.
  45. var osValidators = []Validator{
  46. &OSValidator{Reporter: DefaultReporter},
  47. &KernelValidator{Reporter: DefaultReporter},
  48. &CgroupsValidator{Reporter: DefaultReporter},
  49. &packageValidator{reporter: DefaultReporter},
  50. }
  51. // Docker-specific validators.
  52. var dockerValidators = []Validator{
  53. &DockerValidator{Reporter: DefaultReporter},
  54. }
  55. validators := osValidators
  56. switch runtime {
  57. case "docker":
  58. validators = append(validators, dockerValidators...)
  59. }
  60. return Validate(spec, validators)
  61. }