util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "os"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/capabilities"
  19. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  20. "k8s.io/kubernetes/pkg/securitycontext"
  21. )
  22. // Check whether we have the capabilities to run the specified pod.
  23. func canRunPod(pod *api.Pod) error {
  24. if !capabilities.Get().AllowPrivileged {
  25. for _, container := range pod.Spec.Containers {
  26. if securitycontext.HasPrivilegedRequest(&container) {
  27. return fmt.Errorf("pod with UID %q specified privileged container, but is disallowed", pod.UID)
  28. }
  29. }
  30. for _, container := range pod.Spec.InitContainers {
  31. if securitycontext.HasPrivilegedRequest(&container) {
  32. return fmt.Errorf("pod with UID %q specified privileged init container, but is disallowed", pod.UID)
  33. }
  34. }
  35. }
  36. if pod.Spec.SecurityContext == nil {
  37. return nil
  38. }
  39. if pod.Spec.SecurityContext.HostNetwork {
  40. allowed, err := allowHostNetwork(pod)
  41. if err != nil {
  42. return err
  43. }
  44. if !allowed {
  45. return fmt.Errorf("pod with UID %q specified host networking, but is disallowed", pod.UID)
  46. }
  47. }
  48. if pod.Spec.SecurityContext.HostPID {
  49. allowed, err := allowHostPID(pod)
  50. if err != nil {
  51. return err
  52. }
  53. if !allowed {
  54. return fmt.Errorf("pod with UID %q specified host PID, but is disallowed", pod.UID)
  55. }
  56. }
  57. if pod.Spec.SecurityContext.HostIPC {
  58. allowed, err := allowHostIPC(pod)
  59. if err != nil {
  60. return err
  61. }
  62. if !allowed {
  63. return fmt.Errorf("pod with UID %q specified host ipc, but is disallowed", pod.UID)
  64. }
  65. }
  66. return nil
  67. }
  68. // Determined whether the specified pod is allowed to use host networking
  69. func allowHostNetwork(pod *api.Pod) (bool, error) {
  70. podSource, err := kubetypes.GetPodSource(pod)
  71. if err != nil {
  72. return false, err
  73. }
  74. for _, source := range capabilities.Get().PrivilegedSources.HostNetworkSources {
  75. if source == podSource {
  76. return true, nil
  77. }
  78. }
  79. return false, nil
  80. }
  81. // Determined whether the specified pod is allowed to use host networking
  82. func allowHostPID(pod *api.Pod) (bool, error) {
  83. podSource, err := kubetypes.GetPodSource(pod)
  84. if err != nil {
  85. return false, err
  86. }
  87. for _, source := range capabilities.Get().PrivilegedSources.HostPIDSources {
  88. if source == podSource {
  89. return true, nil
  90. }
  91. }
  92. return false, nil
  93. }
  94. // Determined whether the specified pod is allowed to use host ipc
  95. func allowHostIPC(pod *api.Pod) (bool, error) {
  96. podSource, err := kubetypes.GetPodSource(pod)
  97. if err != nil {
  98. return false, err
  99. }
  100. for _, source := range capabilities.Get().PrivilegedSources.HostIPCSources {
  101. if source == podSource {
  102. return true, nil
  103. }
  104. }
  105. return false, nil
  106. }
  107. // dirExists returns true if the path exists and represents a directory.
  108. func dirExists(path string) bool {
  109. s, err := os.Stat(path)
  110. if err != nil {
  111. return false
  112. }
  113. return s.IsDir()
  114. }
  115. // empty is a placeholder type used to implement a set
  116. type empty struct{}