kuberuntime_sandbox.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 kuberuntime
  14. import (
  15. "github.com/golang/glog"
  16. "k8s.io/kubernetes/pkg/api"
  17. runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
  18. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. )
  20. // generatePodSandboxConfig generates pod sandbox config from api.Pod.
  21. func (m *kubeGenericRuntimeManager) generatePodSandboxConfig(pod *api.Pod, podIP string, attempt uint32) (*runtimeApi.PodSandboxConfig, error) {
  22. // TODO: deprecating podsandbox resource requirements in favor of the pod level cgroup
  23. // Refer https://github.com/kubernetes/kubernetes/issues/29871
  24. podUID := string(pod.UID)
  25. podSandboxConfig := &runtimeApi.PodSandboxConfig{
  26. Metadata: &runtimeApi.PodSandboxMetadata{
  27. Name: &pod.Name,
  28. Namespace: &pod.Namespace,
  29. Uid: &podUID,
  30. Attempt: &attempt,
  31. },
  32. Labels: newPodLabels(pod),
  33. Annotations: newPodAnnotations(pod),
  34. }
  35. if !kubecontainer.IsHostNetworkPod(pod) {
  36. dnsServers, dnsSearches, err := m.runtimeHelper.GetClusterDNS(pod)
  37. if err != nil {
  38. return nil, err
  39. }
  40. podSandboxConfig.DnsOptions = &runtimeApi.DNSOption{
  41. Servers: dnsServers,
  42. Searches: dnsSearches,
  43. }
  44. // TODO: Add domain support in new runtime interface
  45. hostname, _, err := m.runtimeHelper.GeneratePodHostNameAndDomain(pod)
  46. if err != nil {
  47. return nil, err
  48. }
  49. podSandboxConfig.Hostname = &hostname
  50. }
  51. cgroupParent := ""
  52. portMappings := []*runtimeApi.PortMapping{}
  53. for _, c := range pod.Spec.Containers {
  54. opts, err := m.runtimeHelper.GenerateRunContainerOptions(pod, &c, podIP)
  55. if err != nil {
  56. return nil, err
  57. }
  58. for idx := range opts.PortMappings {
  59. port := opts.PortMappings[idx]
  60. hostPort := int32(port.HostPort)
  61. containerPort := int32(port.ContainerPort)
  62. protocol := toRuntimeProtocol(port.Protocol)
  63. portMappings = append(portMappings, &runtimeApi.PortMapping{
  64. HostIp: &port.HostIP,
  65. HostPort: &hostPort,
  66. ContainerPort: &containerPort,
  67. Protocol: &protocol,
  68. Name: &port.Name,
  69. })
  70. }
  71. // TODO: refactor kubelet to get cgroup parent for pod instead of containers
  72. cgroupParent = opts.CgroupParent
  73. }
  74. podSandboxConfig.Linux = generatePodSandboxLinuxConfig(pod, cgroupParent)
  75. if len(portMappings) > 0 {
  76. podSandboxConfig.PortMappings = portMappings
  77. }
  78. return podSandboxConfig, nil
  79. }
  80. // generatePodSandboxLinuxConfig generates LinuxPodSandboxConfig from api.Pod.
  81. func generatePodSandboxLinuxConfig(pod *api.Pod, cgroupParent string) *runtimeApi.LinuxPodSandboxConfig {
  82. if pod.Spec.SecurityContext == nil && cgroupParent == "" {
  83. return nil
  84. }
  85. linuxPodSandboxConfig := &runtimeApi.LinuxPodSandboxConfig{}
  86. if pod.Spec.SecurityContext != nil {
  87. securityContext := pod.Spec.SecurityContext
  88. linuxPodSandboxConfig.NamespaceOptions = &runtimeApi.NamespaceOption{
  89. HostNetwork: &securityContext.HostNetwork,
  90. HostIpc: &securityContext.HostIPC,
  91. HostPid: &securityContext.HostPID,
  92. }
  93. }
  94. if cgroupParent != "" {
  95. linuxPodSandboxConfig.CgroupParent = &cgroupParent
  96. }
  97. return linuxPodSandboxConfig
  98. }
  99. // getKubeletSandboxes lists all (or just the running) sandboxes managed by kubelet.
  100. func (m *kubeGenericRuntimeManager) getKubeletSandboxes(all bool) ([]*runtimeApi.PodSandbox, error) {
  101. var filter *runtimeApi.PodSandboxFilter
  102. if !all {
  103. readyState := runtimeApi.PodSandBoxState_READY
  104. filter = &runtimeApi.PodSandboxFilter{
  105. State: &readyState,
  106. }
  107. }
  108. resp, err := m.runtimeService.ListPodSandbox(filter)
  109. if err != nil {
  110. glog.Errorf("ListPodSandbox failed: %v", err)
  111. return nil, err
  112. }
  113. result := []*runtimeApi.PodSandbox{}
  114. for _, s := range resp {
  115. if !isManagedByKubelet(s.Labels) {
  116. glog.V(5).Infof("Sandbox %s is not managed by kubelet", kubecontainer.BuildPodFullName(
  117. s.Metadata.GetName(), s.Metadata.GetNamespace()))
  118. continue
  119. }
  120. result = append(result, s)
  121. }
  122. return result, nil
  123. }