admission.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2017 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 defaulttolerationseconds
  14. import (
  15. "flag"
  16. "fmt"
  17. "io"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. "k8s.io/apiserver/pkg/admission"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  22. )
  23. // PluginName indicates name of admission plugin.
  24. const PluginName = "DefaultTolerationSeconds"
  25. var (
  26. defaultNotReadyTolerationSeconds = flag.Int64("default-not-ready-toleration-seconds", 300,
  27. "Indicates the tolerationSeconds of the toleration for notReady:NoExecute"+
  28. " that is added by default to every pod that does not already have such a toleration.")
  29. defaultUnreachableTolerationSeconds = flag.Int64("default-unreachable-toleration-seconds", 300,
  30. "Indicates the tolerationSeconds of the toleration for unreachable:NoExecute"+
  31. " that is added by default to every pod that does not already have such a toleration.")
  32. notReadyToleration = api.Toleration{
  33. Key: schedulerapi.TaintNodeNotReady,
  34. Operator: api.TolerationOpExists,
  35. Effect: api.TaintEffectNoExecute,
  36. TolerationSeconds: defaultNotReadyTolerationSeconds,
  37. }
  38. unreachableToleration = api.Toleration{
  39. Key: schedulerapi.TaintNodeUnreachable,
  40. Operator: api.TolerationOpExists,
  41. Effect: api.TaintEffectNoExecute,
  42. TolerationSeconds: defaultUnreachableTolerationSeconds,
  43. }
  44. )
  45. // Register registers a plugin
  46. func Register(plugins *admission.Plugins) {
  47. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  48. return NewDefaultTolerationSeconds(), nil
  49. })
  50. }
  51. // Plugin contains the client used by the admission controller
  52. // It will add default tolerations for every pod
  53. // that tolerate taints `notReady:NoExecute` and `unreachable:NoExecute`,
  54. // with tolerationSeconds of 300s.
  55. // If the pod already specifies a toleration for taint `notReady:NoExecute`
  56. // or `unreachable:NoExecute`, the plugin won't touch it.
  57. type Plugin struct {
  58. *admission.Handler
  59. }
  60. var _ admission.MutationInterface = &Plugin{}
  61. // NewDefaultTolerationSeconds creates a new instance of the DefaultTolerationSeconds admission controller
  62. func NewDefaultTolerationSeconds() *Plugin {
  63. return &Plugin{
  64. Handler: admission.NewHandler(admission.Create, admission.Update),
  65. }
  66. }
  67. // Admit makes an admission decision based on the request attributes
  68. func (p *Plugin) Admit(attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
  69. if attributes.GetResource().GroupResource() != api.Resource("pods") {
  70. return nil
  71. }
  72. if len(attributes.GetSubresource()) > 0 {
  73. // only run the checks below on pods proper and not subresources
  74. return nil
  75. }
  76. pod, ok := attributes.GetObject().(*api.Pod)
  77. if !ok {
  78. return errors.NewBadRequest(fmt.Sprintf("expected *api.Pod but got %T", attributes.GetObject()))
  79. }
  80. tolerations := pod.Spec.Tolerations
  81. toleratesNodeNotReady := false
  82. toleratesNodeUnreachable := false
  83. for _, toleration := range tolerations {
  84. if (toleration.Key == schedulerapi.TaintNodeNotReady || len(toleration.Key) == 0) &&
  85. (toleration.Effect == api.TaintEffectNoExecute || len(toleration.Effect) == 0) {
  86. toleratesNodeNotReady = true
  87. }
  88. if (toleration.Key == schedulerapi.TaintNodeUnreachable || len(toleration.Key) == 0) &&
  89. (toleration.Effect == api.TaintEffectNoExecute || len(toleration.Effect) == 0) {
  90. toleratesNodeUnreachable = true
  91. }
  92. }
  93. if !toleratesNodeNotReady {
  94. pod.Spec.Tolerations = append(pod.Spec.Tolerations, notReadyToleration)
  95. }
  96. if !toleratesNodeUnreachable {
  97. pod.Spec.Tolerations = append(pod.Spec.Tolerations, unreachableToleration)
  98. }
  99. return nil
  100. }