result.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright The containerd Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cni
  14. import (
  15. "fmt"
  16. "net"
  17. "github.com/containernetworking/cni/pkg/types"
  18. types100 "github.com/containernetworking/cni/pkg/types/100"
  19. )
  20. type IPConfig struct {
  21. IP net.IP
  22. Gateway net.IP
  23. }
  24. // Result contains the network information returned by CNI.Setup
  25. //
  26. // a) Interfaces list. Depending on the plugin, this can include the sandbox
  27. //
  28. // (eg, container or hypervisor) interface name and/or the host interface
  29. // name, the hardware addresses of each interface, and details about the
  30. // sandbox (if any) the interface is in.
  31. //
  32. // b) IP configuration assigned to each interface. The IPv4 and/or IPv6 addresses,
  33. //
  34. // gateways, and routes assigned to sandbox and/or host interfaces.
  35. //
  36. // c) DNS information. Dictionary that includes DNS information for nameservers,
  37. //
  38. // domain, search domains and options.
  39. type Result struct {
  40. Interfaces map[string]*Config
  41. DNS []types.DNS
  42. Routes []*types.Route
  43. raw []*types100.Result
  44. }
  45. // Raw returns the raw CNI results of multiple networks.
  46. func (r *Result) Raw() []*types100.Result {
  47. return r.raw
  48. }
  49. type Config struct {
  50. IPConfigs []*IPConfig
  51. Mac string
  52. Sandbox string
  53. }
  54. // createResult creates a Result from the given slice of types100.Result, adding
  55. // structured data containing the interface configuration for each of the
  56. // interfaces created in the namespace. It returns an error if validation of
  57. // results fails, or if a network could not be found.
  58. func (c *libcni) createResult(results []*types100.Result) (*Result, error) {
  59. c.RLock()
  60. defer c.RUnlock()
  61. r := &Result{
  62. Interfaces: make(map[string]*Config),
  63. raw: results,
  64. }
  65. // Plugins may not need to return Interfaces in result if
  66. // if there are no multiple interfaces created. In that case
  67. // all configs should be applied against default interface
  68. r.Interfaces[defaultInterface(c.prefix)] = &Config{}
  69. // Walk through all the results
  70. for _, result := range results {
  71. // Walk through all the interface in each result
  72. for _, intf := range result.Interfaces {
  73. r.Interfaces[intf.Name] = &Config{
  74. Mac: intf.Mac,
  75. Sandbox: intf.Sandbox,
  76. }
  77. }
  78. // Walk through all the IPs in the result and attach it to corresponding
  79. // interfaces
  80. for _, ipConf := range result.IPs {
  81. if err := validateInterfaceConfig(ipConf, len(result.Interfaces)); err != nil {
  82. return nil, fmt.Errorf("invalid interface config: %v: %w", err, ErrInvalidResult)
  83. }
  84. name := c.getInterfaceName(result.Interfaces, ipConf)
  85. r.Interfaces[name].IPConfigs = append(r.Interfaces[name].IPConfigs,
  86. &IPConfig{IP: ipConf.Address.IP, Gateway: ipConf.Gateway})
  87. }
  88. r.DNS = append(r.DNS, result.DNS)
  89. r.Routes = append(r.Routes, result.Routes...)
  90. }
  91. if _, ok := r.Interfaces[defaultInterface(c.prefix)]; !ok {
  92. return nil, fmt.Errorf("default network not found for: %s: %w", defaultInterface(c.prefix), ErrNotFound)
  93. }
  94. return r, nil
  95. }
  96. // getInterfaceName returns the interface name if the plugins
  97. // return the result with associated interfaces. If interface
  98. // is not present then default interface name is used
  99. func (c *libcni) getInterfaceName(interfaces []*types100.Interface,
  100. ipConf *types100.IPConfig) string {
  101. if ipConf.Interface != nil {
  102. return interfaces[*ipConf.Interface].Name
  103. }
  104. return defaultInterface(c.prefix)
  105. }