common.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package controlapi
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/docker/docker/pkg/plugingetter"
  6. "github.com/docker/libnetwork/ipamapi"
  7. "github.com/docker/swarmkit/api"
  8. "github.com/docker/swarmkit/manager/allocator/networkallocator"
  9. "github.com/docker/swarmkit/manager/state/store"
  10. "google.golang.org/grpc"
  11. "google.golang.org/grpc/codes"
  12. )
  13. var isValidName = regexp.MustCompile(`^[a-zA-Z0-9](?:[-_]*[A-Za-z0-9]+)*$`)
  14. func buildFilters(by func(string) store.By, values []string) store.By {
  15. filters := make([]store.By, 0, len(values))
  16. for _, v := range values {
  17. filters = append(filters, by(v))
  18. }
  19. return store.Or(filters...)
  20. }
  21. func filterContains(match string, candidates []string) bool {
  22. if len(candidates) == 0 {
  23. return true
  24. }
  25. for _, c := range candidates {
  26. if c == match {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. func filterContainsPrefix(match string, candidates []string) bool {
  33. if len(candidates) == 0 {
  34. return true
  35. }
  36. for _, c := range candidates {
  37. if strings.HasPrefix(match, c) {
  38. return true
  39. }
  40. }
  41. return false
  42. }
  43. func filterMatchLabels(match map[string]string, candidates map[string]string) bool {
  44. if len(candidates) == 0 {
  45. return true
  46. }
  47. for k, v := range candidates {
  48. c, ok := match[k]
  49. if !ok {
  50. return false
  51. }
  52. if v != "" && v != c {
  53. return false
  54. }
  55. }
  56. return true
  57. }
  58. func validateAnnotations(m api.Annotations) error {
  59. if m.Name == "" {
  60. return grpc.Errorf(codes.InvalidArgument, "meta: name must be provided")
  61. }
  62. if !isValidName.MatchString(m.Name) {
  63. // if the name doesn't match the regex
  64. return grpc.Errorf(codes.InvalidArgument, "name must be valid as a DNS name component")
  65. }
  66. if len(m.Name) > 63 {
  67. // DNS labels are limited to 63 characters
  68. return grpc.Errorf(codes.InvalidArgument, "name must be 63 characters or fewer")
  69. }
  70. return nil
  71. }
  72. func validateDriver(driver *api.Driver, pg plugingetter.PluginGetter, pluginType string) error {
  73. if driver == nil {
  74. // It is ok to not specify the driver. We will choose
  75. // a default driver.
  76. return nil
  77. }
  78. if driver.Name == "" {
  79. return grpc.Errorf(codes.InvalidArgument, "driver name: if driver is specified name is required")
  80. }
  81. if strings.ToLower(driver.Name) == networkallocator.DefaultDriver || strings.ToLower(driver.Name) == ipamapi.DefaultIPAM {
  82. return nil
  83. }
  84. p, err := pg.Get(driver.Name, pluginType, plugingetter.Lookup)
  85. if err != nil {
  86. return grpc.Errorf(codes.InvalidArgument, "error during lookup of plugin %s", driver.Name)
  87. }
  88. if p.IsV1() {
  89. return grpc.Errorf(codes.InvalidArgument, "legacy plugin %s of type %s is not supported in swarm mode", driver.Name, pluginType)
  90. }
  91. return nil
  92. }