runtime.go 2.6 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/api"
  17. "k8s.io/kubernetes/pkg/kubelet/container"
  18. "k8s.io/kubernetes/pkg/kubelet/dockertools"
  19. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  20. )
  21. const (
  22. UnsupportedReason = "SysctlUnsupported"
  23. )
  24. type runtimeAdmitHandler struct {
  25. result lifecycle.PodAdmitResult
  26. }
  27. var _ lifecycle.PodAdmitHandler = &runtimeAdmitHandler{}
  28. // NewRuntimeAdmitHandler returns a sysctlRuntimeAdmitHandler which checks whether
  29. // the given runtime support sysctls.
  30. func NewRuntimeAdmitHandler(runtime container.Runtime) (*runtimeAdmitHandler, error) {
  31. if runtime.Type() == dockertools.DockerType {
  32. v, err := runtime.APIVersion()
  33. if err != nil {
  34. return nil, fmt.Errorf("failed to get runtime version: %v", err)
  35. }
  36. // only Docker >= 1.12 supports sysctls
  37. c, err := v.Compare(dockertools.DockerV112APIVersion)
  38. if err != nil {
  39. return nil, fmt.Errorf("failed to compare Docker version for sysctl support: %v", err)
  40. }
  41. if c >= 0 {
  42. return &runtimeAdmitHandler{
  43. result: lifecycle.PodAdmitResult{
  44. Admit: true,
  45. },
  46. }, nil
  47. }
  48. return &runtimeAdmitHandler{
  49. result: lifecycle.PodAdmitResult{
  50. Admit: false,
  51. Reason: UnsupportedReason,
  52. Message: "Docker before 1.12 does not support sysctls",
  53. },
  54. }, nil
  55. }
  56. // for other runtimes like rkt sysctls are not supported
  57. return &runtimeAdmitHandler{
  58. result: lifecycle.PodAdmitResult{
  59. Admit: false,
  60. Reason: UnsupportedReason,
  61. Message: fmt.Sprintf("runtime %v does not support sysctls", runtime.Type()),
  62. },
  63. }, nil
  64. }
  65. // Admit checks whether the runtime supports sysctls.
  66. func (w *runtimeAdmitHandler) Admit(attrs *lifecycle.PodAdmitAttributes) lifecycle.PodAdmitResult {
  67. sysctls, unsafeSysctls, err := api.SysctlsFromPodAnnotations(attrs.Pod.Annotations)
  68. if err != nil {
  69. return lifecycle.PodAdmitResult{
  70. Admit: false,
  71. Reason: AnnotationInvalidReason,
  72. Message: fmt.Sprintf("invalid sysctl annotation: %v", err),
  73. }
  74. }
  75. if len(sysctls)+len(unsafeSysctls) > 0 {
  76. return w.result
  77. }
  78. return lifecycle.PodAdmitResult{
  79. Admit: true,
  80. }
  81. }