cni_others.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // +build !windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package cni
  15. import (
  16. "fmt"
  17. "github.com/containernetworking/cni/libcni"
  18. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  19. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  20. "k8s.io/kubernetes/pkg/kubelet/dockershim/network"
  21. )
  22. func getLoNetwork(binDirs []string) *cniNetwork {
  23. loConfig, err := libcni.ConfListFromBytes([]byte(`{
  24. "cniVersion": "0.2.0",
  25. "name": "cni-loopback",
  26. "plugins":[{
  27. "type": "loopback"
  28. }]
  29. }`))
  30. if err != nil {
  31. // The hardcoded config above should always be valid and unit tests will
  32. // catch this
  33. panic(err)
  34. }
  35. loNetwork := &cniNetwork{
  36. name: "lo",
  37. NetworkConfig: loConfig,
  38. CNIConfig: &libcni.CNIConfig{Path: binDirs},
  39. }
  40. return loNetwork
  41. }
  42. func (plugin *cniNetworkPlugin) platformInit() error {
  43. var err error
  44. plugin.nsenterPath, err = plugin.execer.LookPath("nsenter")
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. // TODO: Use the addToNetwork function to obtain the IP of the Pod. That will assume idempotent ADD call to the plugin.
  51. // Also fix the runtime's call to Status function to be done only in the case that the IP is lost, no need to do periodic calls
  52. func (plugin *cniNetworkPlugin) GetPodNetworkStatus(namespace string, name string, id kubecontainer.ContainerID) (*network.PodNetworkStatus, error) {
  53. netnsPath, err := plugin.host.GetNetNS(id.ID)
  54. if err != nil {
  55. return nil, fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
  56. }
  57. if netnsPath == "" {
  58. return nil, fmt.Errorf("cannot find the network namespace, skipping pod network status for container %q", id)
  59. }
  60. ips, err := network.GetPodIPs(plugin.execer, plugin.nsenterPath, netnsPath, network.DefaultInterfaceName)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if len(ips) == 0 {
  65. return nil, fmt.Errorf("cannot find pod IPs in the network namespace, skipping pod network status for container %q", id)
  66. }
  67. return &network.PodNetworkStatus{
  68. IP: ips[0],
  69. IPs: ips,
  70. }, nil
  71. }
  72. // buildDNSCapabilities builds cniDNSConfig from runtimeapi.DNSConfig.
  73. func buildDNSCapabilities(dnsConfig *runtimeapi.DNSConfig) *cniDNSConfig {
  74. return nil
  75. }