config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 flannel authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package subnet
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "github.com/coreos/flannel/pkg/ip"
  20. )
  21. type Config struct {
  22. Network ip.IP4Net
  23. SubnetMin ip.IP4
  24. SubnetMax ip.IP4
  25. SubnetLen uint
  26. BackendType string `json:"-"`
  27. Backend json.RawMessage `json:",omitempty"`
  28. }
  29. func parseBackendType(be json.RawMessage) (string, error) {
  30. var bt struct {
  31. Type string
  32. }
  33. if len(be) == 0 {
  34. return "udp", nil
  35. } else if err := json.Unmarshal(be, &bt); err != nil {
  36. return "", fmt.Errorf("error decoding Backend property of config: %v", err)
  37. }
  38. return bt.Type, nil
  39. }
  40. func ParseConfig(s string) (*Config, error) {
  41. cfg := new(Config)
  42. err := json.Unmarshal([]byte(s), cfg)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if cfg.SubnetLen > 0 {
  47. // SubnetLen needs to allow for a tunnel and bridge device on each host.
  48. if cfg.SubnetLen > 30 {
  49. return nil, errors.New("SubnetLen must be less than /31")
  50. }
  51. // SubnetLen needs to fit _more_ than twice into the Network.
  52. // the first subnet isn't used, so splitting into two one only provide one usable host.
  53. if cfg.SubnetLen < cfg.Network.PrefixLen+2 {
  54. return nil, errors.New("Network must be able to accommodate at least four subnets")
  55. }
  56. } else {
  57. // If the network is smaller than a /28 then the network isn't big enough for flannel so return an error.
  58. // Default to giving each host at least a /24 (as long as the network is big enough to support at least four hosts)
  59. // Otherwise, if the network is too small to give each host a /24 just split the network into four.
  60. if cfg.Network.PrefixLen > 28 {
  61. // Each subnet needs at least four addresses (/30) and the network needs to accommodate at least four
  62. // since the first subnet isn't used, so splitting into two would only provide one usable host.
  63. // So the min useful PrefixLen is /28
  64. return nil, errors.New("Network is too small. Minimum useful network prefix is /28")
  65. } else if cfg.Network.PrefixLen <= 22 {
  66. // Network is big enough to give each host a /24
  67. cfg.SubnetLen = 24
  68. } else {
  69. // Use +2 to provide four hosts per subnet.
  70. cfg.SubnetLen = cfg.Network.PrefixLen + 2
  71. }
  72. }
  73. subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen))
  74. if cfg.SubnetMin == ip.IP4(0) {
  75. // skip over the first subnet otherwise it causes problems. e.g.
  76. // if Network is 10.100.0.0/16, having an interface with 10.0.0.0
  77. // conflicts with the broadcast address.
  78. cfg.SubnetMin = cfg.Network.IP + subnetSize
  79. } else if !cfg.Network.Contains(cfg.SubnetMin) {
  80. return nil, errors.New("SubnetMin is not in the range of the Network")
  81. }
  82. if cfg.SubnetMax == ip.IP4(0) {
  83. cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
  84. } else if !cfg.Network.Contains(cfg.SubnetMax) {
  85. return nil, errors.New("SubnetMax is not in the range of the Network")
  86. }
  87. // The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
  88. mask := ip.IP4(0xFFFFFFFF << (32 - cfg.SubnetLen))
  89. if cfg.SubnetMin != cfg.SubnetMin&mask {
  90. return nil, fmt.Errorf("SubnetMin is not on a SubnetLen boundary: %v", cfg.SubnetMin)
  91. }
  92. if cfg.SubnetMax != cfg.SubnetMax&mask {
  93. return nil, fmt.Errorf("SubnetMax is not on a SubnetLen boundary: %v", cfg.SubnetMax)
  94. }
  95. bt, err := parseBackendType(cfg.Backend)
  96. if err != nil {
  97. return nil, err
  98. }
  99. cfg.BackendType = bt
  100. return cfg, nil
  101. }