config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if cfg.SubnetLen < cfg.Network.PrefixLen {
  48. return nil, errors.New("HostSubnet is larger network than Network")
  49. }
  50. } else {
  51. // try to give each host a /24 but if the whole network
  52. // is /24 or smaller, half the network
  53. if cfg.Network.PrefixLen < 24 {
  54. cfg.SubnetLen = 24
  55. } else {
  56. cfg.SubnetLen = cfg.Network.PrefixLen + 1
  57. }
  58. }
  59. subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen))
  60. if cfg.SubnetMin == ip.IP4(0) {
  61. // skip over the first subnet otherwise it causes problems. e.g.
  62. // if Network is 10.100.0.0/16, having an interface with 10.0.0.0
  63. // conflicts with the broadcast address.
  64. cfg.SubnetMin = cfg.Network.IP + subnetSize
  65. } else if !cfg.Network.Contains(cfg.SubnetMin) {
  66. return nil, errors.New("SubnetMin is not in the range of the Network")
  67. }
  68. if cfg.SubnetMax == ip.IP4(0) {
  69. cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
  70. } else if !cfg.Network.Contains(cfg.SubnetMax) {
  71. return nil, errors.New("SubnetMax is not in the range of the Network")
  72. }
  73. // The SubnetMin and SubnetMax need to be aligned to a SubnetLen boundary
  74. mask := ip.IP4(0xFFFFFFFF << (32 - cfg.SubnetLen))
  75. if cfg.SubnetMin != cfg.SubnetMin&mask {
  76. return nil, fmt.Errorf("SubnetMin is not on a SubnetLen boundary: %v", cfg.SubnetMin)
  77. }
  78. if cfg.SubnetMax != cfg.SubnetMax&mask {
  79. return nil, fmt.Errorf("SubnetMax is not on a SubnetLen boundary: %v", cfg.SubnetMax)
  80. }
  81. bt, err := parseBackendType(cfg.Backend)
  82. if err != nil {
  83. return nil, err
  84. }
  85. cfg.BackendType = bt
  86. return cfg, nil
  87. }