config.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 {
  36. if err := json.Unmarshal(be, &bt); err != nil {
  37. return "", fmt.Errorf("error decoding Backend property of config: %v", err)
  38. }
  39. }
  40. return bt.Type, nil
  41. }
  42. func ParseConfig(s string) (*Config, error) {
  43. cfg := new(Config)
  44. err := json.Unmarshal([]byte(s), cfg)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if cfg.SubnetLen > 0 {
  49. if cfg.SubnetLen < cfg.Network.PrefixLen {
  50. return nil, errors.New("HostSubnet is larger network than Network")
  51. }
  52. } else {
  53. // try to give each host a /24 but if the whole network
  54. // is /24 or smaller, half the network
  55. if cfg.Network.PrefixLen < 24 {
  56. cfg.SubnetLen = 24
  57. } else {
  58. cfg.SubnetLen = cfg.Network.PrefixLen + 1
  59. }
  60. }
  61. subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen))
  62. if cfg.SubnetMin == ip.IP4(0) {
  63. // skip over the first subnet otherwise it causes problems. e.g.
  64. // if Network is 10.100.0.0/16, having an interface with 10.0.0.0
  65. // makes ping think it's a broadcast address (not sure why)
  66. cfg.SubnetMin = cfg.Network.IP + subnetSize
  67. } else if !cfg.Network.Contains(cfg.SubnetMin) {
  68. return nil, errors.New("SubnetMin is not in the range of the Network")
  69. }
  70. if cfg.SubnetMax == ip.IP4(0) {
  71. cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
  72. } else if !cfg.Network.Contains(cfg.SubnetMax) {
  73. return nil, errors.New("SubnetMax is not in the range of the Network")
  74. }
  75. bt, err := parseBackendType(cfg.Backend)
  76. if err != nil {
  77. return nil, err
  78. }
  79. cfg.BackendType = bt
  80. return cfg, nil
  81. }