kubelet_network.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2016 The Kubernetes 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 kubelet
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  18. "k8s.io/klog"
  19. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  20. )
  21. const (
  22. // KubeMarkMasqChain is the mark-for-masquerade chain
  23. // TODO: clean up this logic in kube-proxy
  24. KubeMarkMasqChain utiliptables.Chain = "KUBE-MARK-MASQ"
  25. // KubeMarkDropChain is the mark-for-drop chain
  26. KubeMarkDropChain utiliptables.Chain = "KUBE-MARK-DROP"
  27. // KubePostroutingChain is kubernetes postrouting rules
  28. KubePostroutingChain utiliptables.Chain = "KUBE-POSTROUTING"
  29. // KubeFirewallChain is kubernetes firewall rules
  30. KubeFirewallChain utiliptables.Chain = "KUBE-FIREWALL"
  31. )
  32. // providerRequiresNetworkingConfiguration returns whether the cloud provider
  33. // requires special networking configuration.
  34. func (kl *Kubelet) providerRequiresNetworkingConfiguration() bool {
  35. // TODO: We should have a mechanism to say whether native cloud provider
  36. // is used or whether we are using overlay networking. We should return
  37. // true for cloud providers if they implement Routes() interface and
  38. // we are not using overlay networking.
  39. if kl.cloud == nil || kl.cloud.ProviderName() != "gce" {
  40. return false
  41. }
  42. _, supported := kl.cloud.Routes()
  43. return supported
  44. }
  45. // updatePodCIDR updates the pod CIDR in the runtime state if it is different
  46. // from the current CIDR. Return true if pod CIDR is actually changed.
  47. func (kl *Kubelet) updatePodCIDR(cidr string) (bool, error) {
  48. kl.updatePodCIDRMux.Lock()
  49. defer kl.updatePodCIDRMux.Unlock()
  50. podCIDR := kl.runtimeState.podCIDR()
  51. if podCIDR == cidr {
  52. return false, nil
  53. }
  54. // kubelet -> generic runtime -> runtime shim -> network plugin
  55. // docker/non-cri implementations have a passthrough UpdatePodCIDR
  56. if err := kl.getRuntime().UpdatePodCIDR(cidr); err != nil {
  57. // If updatePodCIDR would fail, theoretically pod CIDR could not change.
  58. // But it is better to be on the safe side to still return true here.
  59. return true, fmt.Errorf("failed to update pod CIDR: %v", err)
  60. }
  61. klog.Infof("Setting Pod CIDR: %v -> %v", podCIDR, cidr)
  62. kl.runtimeState.setPodCIDR(cidr)
  63. return true, nil
  64. }
  65. // GetPodDNS returns DNS settings for the pod.
  66. // This function is defined in kubecontainer.RuntimeHelper interface so we
  67. // have to implement it.
  68. func (kl *Kubelet) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error) {
  69. return kl.dnsConfigurer.GetPodDNS(pod)
  70. }