admission.go 4.0 KB

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