config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 CNI 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 hostlocal
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net"
  19. "github.com/containernetworking/cni/pkg/types"
  20. )
  21. // IPAMConfig represents the IP related network configuration.
  22. type IPAMConfig struct {
  23. Name string
  24. Type string `json:"type"`
  25. RangeStart net.IP `json:"rangeStart"`
  26. RangeEnd net.IP `json:"rangeEnd"`
  27. Subnet types.IPNet `json:"subnet"`
  28. Gateway net.IP `json:"gateway"`
  29. Routes []types.Route `json:"routes"`
  30. Args *IPAMArgs `json:"-"`
  31. }
  32. type IPAMArgs struct {
  33. types.CommonArgs
  34. IP net.IP `json:"ip,omitempty"`
  35. }
  36. type Net struct {
  37. Name string `json:"name"`
  38. IPAM *IPAMConfig `json:"ipam"`
  39. }
  40. // NewIPAMConfig creates a NetworkConfig from the given network name.
  41. func LoadIPAMConfig(bytes []byte, args string) (*IPAMConfig, error) {
  42. n := Net{}
  43. if err := json.Unmarshal(bytes, &n); err != nil {
  44. return nil, err
  45. }
  46. if args != "" {
  47. n.IPAM.Args = &IPAMArgs{}
  48. err := types.LoadArgs(args, n.IPAM.Args)
  49. if err != nil {
  50. return nil, err
  51. }
  52. }
  53. if n.IPAM == nil {
  54. return nil, fmt.Errorf("IPAM config missing 'ipam' key")
  55. }
  56. // Copy net name into IPAM so not to drag Net struct around
  57. n.IPAM.Name = n.Name
  58. return n.IPAM, nil
  59. }