runtime.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 sysctl
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/kubelet/container"
  17. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  18. )
  19. const (
  20. UnsupportedReason = "SysctlUnsupported"
  21. // CRI uses semver-compatible API version, while docker does not
  22. // (e.g., 1.24). Append the version with a ".0".
  23. dockerMinimumAPIVersion = "1.24.0"
  24. dockerTypeName = "docker"
  25. )
  26. // TODO: The admission logic in this file is runtime-dependent. It should be
  27. // changed to be generic and CRI-compatible.
  28. type runtimeAdmitHandler struct {
  29. result lifecycle.PodAdmitResult
  30. }
  31. var _ lifecycle.PodAdmitHandler = &runtimeAdmitHandler{}
  32. // NewRuntimeAdmitHandler returns a sysctlRuntimeAdmitHandler which checks whether
  33. // the given runtime support sysctls.
  34. func NewRuntimeAdmitHandler(runtime container.Runtime) (*runtimeAdmitHandler, error) {
  35. switch runtime.Type() {
  36. case dockerTypeName:
  37. v, err := runtime.APIVersion()
  38. if err != nil {
  39. return nil, fmt.Errorf("failed to get runtime version: %v", err)
  40. }
  41. // only Docker API version >= 1.24 supports sysctls
  42. c, err := v.Compare(dockerMinimumAPIVersion)
  43. if err != nil {
  44. return nil, fmt.Errorf("failed to compare Docker version for sysctl support: %v", err)
  45. }
  46. if c >= 0 {
  47. return &runtimeAdmitHandler{
  48. result: lifecycle.PodAdmitResult{
  49. Admit: true,
  50. },
  51. }, nil
  52. }
  53. return &runtimeAdmitHandler{
  54. result: lifecycle.PodAdmitResult{
  55. Admit: false,
  56. Reason: UnsupportedReason,
  57. Message: "Docker API version before 1.24 does not support sysctls",
  58. },
  59. }, nil
  60. default:
  61. // Return admit for other runtimes.
  62. return &runtimeAdmitHandler{
  63. result: lifecycle.PodAdmitResult{
  64. Admit: true,
  65. },
  66. }, nil
  67. }
  68. }
  69. // Admit checks whether the runtime supports sysctls.
  70. func (w *runtimeAdmitHandler) Admit(attrs *lifecycle.PodAdmitAttributes) lifecycle.PodAdmitResult {
  71. if attrs.Pod.Spec.SecurityContext != nil {
  72. if len(attrs.Pod.Spec.SecurityContext.Sysctls) > 0 {
  73. return w.result
  74. }
  75. }
  76. return lifecycle.PodAdmitResult{
  77. Admit: true,
  78. }
  79. }