docker_validator.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "context"
  16. "regexp"
  17. "github.com/docker/docker/api/types"
  18. "github.com/docker/docker/client"
  19. "github.com/pkg/errors"
  20. )
  21. var _ Validator = &DockerValidator{}
  22. // DockerValidator validates docker configuration.
  23. type DockerValidator struct {
  24. Reporter Reporter
  25. }
  26. // Name is part of the system.Validator interface.
  27. func (d *DockerValidator) Name() string {
  28. return "docker"
  29. }
  30. const (
  31. dockerConfigPrefix = "DOCKER_"
  32. latestValidatedDockerVersion = "18.09"
  33. )
  34. // Validate is part of the system.Validator interface.
  35. // TODO(random-liu): Add more validating items.
  36. func (d *DockerValidator) Validate(spec SysSpec) (error, error) {
  37. if spec.RuntimeSpec.DockerSpec == nil {
  38. // If DockerSpec is not specified, assume current runtime is not
  39. // docker, skip the docker configuration validation.
  40. return nil, nil
  41. }
  42. c, err := client.NewClient(dockerEndpoint, "", nil, nil)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "failed to create docker client")
  45. }
  46. info, err := c.Info(context.Background())
  47. if err != nil {
  48. return nil, errors.Wrap(err, "failed to get docker info")
  49. }
  50. return d.validateDockerInfo(spec.RuntimeSpec.DockerSpec, info)
  51. }
  52. func (d *DockerValidator) validateDockerInfo(spec *DockerSpec, info types.Info) (error, error) {
  53. // Validate docker version.
  54. matched := false
  55. for _, v := range spec.Version {
  56. r := regexp.MustCompile(v)
  57. if r.MatchString(info.ServerVersion) {
  58. d.Reporter.Report(dockerConfigPrefix+"VERSION", info.ServerVersion, good)
  59. matched = true
  60. }
  61. }
  62. if !matched {
  63. // If it's of the new Docker version scheme but didn't match above, it
  64. // must be a newer version than the most recently validated one.
  65. ver := `\d{2}\.\d+\.\d+(?:-[a-z]{2})?`
  66. r := regexp.MustCompile(ver)
  67. if r.MatchString(info.ServerVersion) {
  68. d.Reporter.Report(dockerConfigPrefix+"VERSION", info.ServerVersion, good)
  69. w := errors.Errorf(
  70. "this Docker version is not on the list of validated versions: %s. Latest validated version: %s",
  71. info.ServerVersion,
  72. latestValidatedDockerVersion,
  73. )
  74. return w, nil
  75. }
  76. d.Reporter.Report(dockerConfigPrefix+"VERSION", info.ServerVersion, bad)
  77. return nil, errors.Errorf("unsupported docker version: %s", info.ServerVersion)
  78. }
  79. // Validate graph driver.
  80. item := dockerConfigPrefix + "GRAPH_DRIVER"
  81. for _, gd := range spec.GraphDriver {
  82. if info.Driver == gd {
  83. d.Reporter.Report(item, info.Driver, good)
  84. return nil, nil
  85. }
  86. }
  87. d.Reporter.Report(item, info.Driver, bad)
  88. return nil, errors.Errorf("unsupported graph driver: %s", info.Driver)
  89. }