helpers.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. Copyright 2015 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 container
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "hash/fnv"
  18. "strings"
  19. "k8s.io/klog"
  20. v1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/apimachinery/pkg/util/sets"
  24. "k8s.io/client-go/tools/record"
  25. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  26. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  27. "k8s.io/kubernetes/pkg/kubelet/util/format"
  28. hashutil "k8s.io/kubernetes/pkg/util/hash"
  29. "k8s.io/kubernetes/third_party/forked/golang/expansion"
  30. utilsnet "k8s.io/utils/net"
  31. )
  32. // HandlerRunner runs a lifecycle handler for a container.
  33. type HandlerRunner interface {
  34. Run(containerID ContainerID, pod *v1.Pod, container *v1.Container, handler *v1.Handler) (string, error)
  35. }
  36. // RuntimeHelper wraps kubelet to make container runtime
  37. // able to get necessary informations like the RunContainerOptions, DNS settings, Host IP.
  38. type RuntimeHelper interface {
  39. GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string, podIPs []string) (contOpts *RunContainerOptions, cleanupAction func(), err error)
  40. GetPodDNS(pod *v1.Pod) (dnsConfig *runtimeapi.DNSConfig, err error)
  41. // GetPodCgroupParent returns the CgroupName identifier, and its literal cgroupfs form on the host
  42. // of a pod.
  43. GetPodCgroupParent(pod *v1.Pod) string
  44. GetPodDir(podUID types.UID) string
  45. GeneratePodHostNameAndDomain(pod *v1.Pod) (hostname string, hostDomain string, err error)
  46. // GetExtraSupplementalGroupsForPod returns a list of the extra
  47. // supplemental groups for the Pod. These extra supplemental groups come
  48. // from annotations on persistent volumes that the pod depends on.
  49. GetExtraSupplementalGroupsForPod(pod *v1.Pod) []int64
  50. }
  51. // ShouldContainerBeRestarted checks whether a container needs to be restarted.
  52. // TODO(yifan): Think about how to refactor this.
  53. func ShouldContainerBeRestarted(container *v1.Container, pod *v1.Pod, podStatus *PodStatus) bool {
  54. // Get latest container status.
  55. status := podStatus.FindContainerStatusByName(container.Name)
  56. // If the container was never started before, we should start it.
  57. // NOTE(random-liu): If all historical containers were GC'd, we'll also return true here.
  58. if status == nil {
  59. return true
  60. }
  61. // Check whether container is running
  62. if status.State == ContainerStateRunning {
  63. return false
  64. }
  65. // Always restart container in the unknown, or in the created state.
  66. if status.State == ContainerStateUnknown || status.State == ContainerStateCreated {
  67. return true
  68. }
  69. // Check RestartPolicy for dead container
  70. if pod.Spec.RestartPolicy == v1.RestartPolicyNever {
  71. klog.V(4).Infof("Already ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
  72. return false
  73. }
  74. if pod.Spec.RestartPolicy == v1.RestartPolicyOnFailure {
  75. // Check the exit code.
  76. if status.ExitCode == 0 {
  77. klog.V(4).Infof("Already successfully ran container %q of pod %q, do nothing", container.Name, format.Pod(pod))
  78. return false
  79. }
  80. }
  81. return true
  82. }
  83. // HashContainer returns the hash of the container. It is used to compare
  84. // the running container with its desired spec.
  85. // Note: remember to update hashValues in container_hash_test.go as well.
  86. func HashContainer(container *v1.Container) uint64 {
  87. hash := fnv.New32a()
  88. // Omit nil or empty field when calculating hash value
  89. // Please see https://github.com/kubernetes/kubernetes/issues/53644
  90. containerJson, _ := json.Marshal(container)
  91. hashutil.DeepHashObject(hash, containerJson)
  92. return uint64(hash.Sum32())
  93. }
  94. // EnvVarsToMap constructs a map of environment name to value from a slice
  95. // of env vars.
  96. func EnvVarsToMap(envs []EnvVar) map[string]string {
  97. result := map[string]string{}
  98. for _, env := range envs {
  99. result[env.Name] = env.Value
  100. }
  101. return result
  102. }
  103. // V1EnvVarsToMap constructs a map of environment name to value from a slice
  104. // of env vars.
  105. func V1EnvVarsToMap(envs []v1.EnvVar) map[string]string {
  106. result := map[string]string{}
  107. for _, env := range envs {
  108. result[env.Name] = env.Value
  109. }
  110. return result
  111. }
  112. // ExpandContainerCommandOnlyStatic substitutes only static environment variable values from the
  113. // container environment definitions. This does *not* include valueFrom substitutions.
  114. // TODO: callers should use ExpandContainerCommandAndArgs with a fully resolved list of environment.
  115. func ExpandContainerCommandOnlyStatic(containerCommand []string, envs []v1.EnvVar) (command []string) {
  116. mapping := expansion.MappingFuncFor(V1EnvVarsToMap(envs))
  117. if len(containerCommand) != 0 {
  118. for _, cmd := range containerCommand {
  119. command = append(command, expansion.Expand(cmd, mapping))
  120. }
  121. }
  122. return command
  123. }
  124. func ExpandContainerVolumeMounts(mount v1.VolumeMount, envs []EnvVar) (string, error) {
  125. envmap := EnvVarsToMap(envs)
  126. missingKeys := sets.NewString()
  127. expanded := expansion.Expand(mount.SubPathExpr, func(key string) string {
  128. value, ok := envmap[key]
  129. if !ok || len(value) == 0 {
  130. missingKeys.Insert(key)
  131. }
  132. return value
  133. })
  134. if len(missingKeys) > 0 {
  135. return "", fmt.Errorf("missing value for %s", strings.Join(missingKeys.List(), ", "))
  136. }
  137. return expanded, nil
  138. }
  139. func ExpandContainerCommandAndArgs(container *v1.Container, envs []EnvVar) (command []string, args []string) {
  140. mapping := expansion.MappingFuncFor(EnvVarsToMap(envs))
  141. if len(container.Command) != 0 {
  142. for _, cmd := range container.Command {
  143. command = append(command, expansion.Expand(cmd, mapping))
  144. }
  145. }
  146. if len(container.Args) != 0 {
  147. for _, arg := range container.Args {
  148. args = append(args, expansion.Expand(arg, mapping))
  149. }
  150. }
  151. return command, args
  152. }
  153. // Create an event recorder to record object's event except implicitly required container's, like infra container.
  154. func FilterEventRecorder(recorder record.EventRecorder) record.EventRecorder {
  155. return &innerEventRecorder{
  156. recorder: recorder,
  157. }
  158. }
  159. type innerEventRecorder struct {
  160. recorder record.EventRecorder
  161. }
  162. func (irecorder *innerEventRecorder) shouldRecordEvent(object runtime.Object) (*v1.ObjectReference, bool) {
  163. if object == nil {
  164. return nil, false
  165. }
  166. if ref, ok := object.(*v1.ObjectReference); ok {
  167. if !strings.HasPrefix(ref.FieldPath, ImplicitContainerPrefix) {
  168. return ref, true
  169. }
  170. }
  171. return nil, false
  172. }
  173. func (irecorder *innerEventRecorder) Event(object runtime.Object, eventtype, reason, message string) {
  174. if ref, ok := irecorder.shouldRecordEvent(object); ok {
  175. irecorder.recorder.Event(ref, eventtype, reason, message)
  176. }
  177. }
  178. func (irecorder *innerEventRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
  179. if ref, ok := irecorder.shouldRecordEvent(object); ok {
  180. irecorder.recorder.Eventf(ref, eventtype, reason, messageFmt, args...)
  181. }
  182. }
  183. func (irecorder *innerEventRecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{}) {
  184. if ref, ok := irecorder.shouldRecordEvent(object); ok {
  185. irecorder.recorder.AnnotatedEventf(ref, annotations, eventtype, reason, messageFmt, args...)
  186. }
  187. }
  188. // Pod must not be nil.
  189. func IsHostNetworkPod(pod *v1.Pod) bool {
  190. return pod.Spec.HostNetwork
  191. }
  192. // TODO(random-liu): Convert PodStatus to running Pod, should be deprecated soon
  193. func ConvertPodStatusToRunningPod(runtimeName string, podStatus *PodStatus) Pod {
  194. runningPod := Pod{
  195. ID: podStatus.ID,
  196. Name: podStatus.Name,
  197. Namespace: podStatus.Namespace,
  198. }
  199. for _, containerStatus := range podStatus.ContainerStatuses {
  200. if containerStatus.State != ContainerStateRunning {
  201. continue
  202. }
  203. container := &Container{
  204. ID: containerStatus.ID,
  205. Name: containerStatus.Name,
  206. Image: containerStatus.Image,
  207. ImageID: containerStatus.ImageID,
  208. Hash: containerStatus.Hash,
  209. State: containerStatus.State,
  210. }
  211. runningPod.Containers = append(runningPod.Containers, container)
  212. }
  213. // Populate sandboxes in kubecontainer.Pod
  214. for _, sandbox := range podStatus.SandboxStatuses {
  215. runningPod.Sandboxes = append(runningPod.Sandboxes, &Container{
  216. ID: ContainerID{Type: runtimeName, ID: sandbox.Id},
  217. State: SandboxToContainerState(sandbox.State),
  218. })
  219. }
  220. return runningPod
  221. }
  222. // SandboxToContainerState converts runtimeapi.PodSandboxState to
  223. // kubecontainer.ContainerState.
  224. // This is only needed because we need to return sandboxes as if they were
  225. // kubecontainer.Containers to avoid substantial changes to PLEG.
  226. // TODO: Remove this once it becomes obsolete.
  227. func SandboxToContainerState(state runtimeapi.PodSandboxState) ContainerState {
  228. switch state {
  229. case runtimeapi.PodSandboxState_SANDBOX_READY:
  230. return ContainerStateRunning
  231. case runtimeapi.PodSandboxState_SANDBOX_NOTREADY:
  232. return ContainerStateExited
  233. }
  234. return ContainerStateUnknown
  235. }
  236. // FormatPod returns a string representing a pod in a human readable format,
  237. // with pod UID as part of the string.
  238. func FormatPod(pod *Pod) string {
  239. // Use underscore as the delimiter because it is not allowed in pod name
  240. // (DNS subdomain format), while allowed in the container name format.
  241. return fmt.Sprintf("%s_%s(%s)", pod.Name, pod.Namespace, pod.ID)
  242. }
  243. // GetContainerSpec gets the container spec by containerName.
  244. func GetContainerSpec(pod *v1.Pod, containerName string) *v1.Container {
  245. var containerSpec *v1.Container
  246. podutil.VisitContainers(&pod.Spec, func(c *v1.Container) bool {
  247. if containerName == c.Name {
  248. containerSpec = c
  249. return false
  250. }
  251. return true
  252. })
  253. return containerSpec
  254. }
  255. // HasPrivilegedContainer returns true if any of the containers in the pod are privileged.
  256. func HasPrivilegedContainer(pod *v1.Pod) bool {
  257. var hasPrivileged bool
  258. podutil.VisitContainers(&pod.Spec, func(c *v1.Container) bool {
  259. if c.SecurityContext != nil && c.SecurityContext.Privileged != nil && *c.SecurityContext.Privileged {
  260. hasPrivileged = true
  261. return false
  262. }
  263. return true
  264. })
  265. return hasPrivileged
  266. }
  267. // MakePortMappings creates internal port mapping from api port mapping.
  268. func MakePortMappings(container *v1.Container) (ports []PortMapping) {
  269. names := make(map[string]struct{})
  270. for _, p := range container.Ports {
  271. pm := PortMapping{
  272. HostPort: int(p.HostPort),
  273. ContainerPort: int(p.ContainerPort),
  274. Protocol: p.Protocol,
  275. HostIP: p.HostIP,
  276. }
  277. // We need to determine the address family this entry applies to. We do this to ensure
  278. // duplicate containerPort / protocol rules work across different address families.
  279. // https://github.com/kubernetes/kubernetes/issues/82373
  280. family := "any"
  281. if p.HostIP != "" {
  282. if utilsnet.IsIPv6String(p.HostIP) {
  283. family = "v6"
  284. } else {
  285. family = "v4"
  286. }
  287. }
  288. // We need to create some default port name if it's not specified, since
  289. // this is necessary for the dockershim CNI driver.
  290. // https://github.com/kubernetes/kubernetes/pull/82374#issuecomment-529496888
  291. if p.Name == "" {
  292. pm.Name = fmt.Sprintf("%s-%s-%s:%d", container.Name, family, p.Protocol, p.ContainerPort)
  293. } else {
  294. pm.Name = fmt.Sprintf("%s-%s", container.Name, p.Name)
  295. }
  296. // Protect against a port name being used more than once in a container.
  297. if _, ok := names[pm.Name]; ok {
  298. klog.Warningf("Port name conflicted, %q is defined more than once", pm.Name)
  299. continue
  300. }
  301. ports = append(ports, pm)
  302. names[pm.Name] = struct{}{}
  303. }
  304. return
  305. }