admission.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 antiaffinity
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. "k8s.io/apiserver/pkg/admission"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. )
  23. // PluginName is a string with the name of the plugin
  24. const PluginName = "LimitPodHardAntiAffinityTopology"
  25. // Register registers a plugin
  26. func Register(plugins *admission.Plugins) {
  27. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  28. return NewInterPodAntiAffinity(), nil
  29. })
  30. }
  31. // Plugin contains the client used by the admission controller
  32. type Plugin struct {
  33. *admission.Handler
  34. }
  35. var _ admission.ValidationInterface = &Plugin{}
  36. // NewInterPodAntiAffinity creates a new instance of the LimitPodHardAntiAffinityTopology admission controller
  37. func NewInterPodAntiAffinity() *Plugin {
  38. return &Plugin{
  39. Handler: admission.NewHandler(admission.Create, admission.Update),
  40. }
  41. }
  42. // Validate will deny any pod that defines AntiAffinity topology key other than v1.LabelHostname i.e. "kubernetes.io/hostname"
  43. // in requiredDuringSchedulingRequiredDuringExecution and requiredDuringSchedulingIgnoredDuringExecution.
  44. func (p *Plugin) Validate(ctx context.Context, attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
  45. // Ignore all calls to subresources or resources other than pods.
  46. if len(attributes.GetSubresource()) != 0 || attributes.GetResource().GroupResource() != api.Resource("pods") {
  47. return nil
  48. }
  49. pod, ok := attributes.GetObject().(*api.Pod)
  50. if !ok {
  51. return apierrors.NewBadRequest("Resource was marked with kind Pod but was unable to be converted")
  52. }
  53. affinity := pod.Spec.Affinity
  54. if affinity != nil && affinity.PodAntiAffinity != nil {
  55. var podAntiAffinityTerms []api.PodAffinityTerm
  56. if len(affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0 {
  57. podAntiAffinityTerms = affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution
  58. }
  59. // TODO: Uncomment this block when implement RequiredDuringSchedulingRequiredDuringExecution.
  60. //if len(affinity.PodAntiAffinity.RequiredDuringSchedulingRequiredDuringExecution) != 0 {
  61. // podAntiAffinityTerms = append(podAntiAffinityTerms, affinity.PodAntiAffinity.RequiredDuringSchedulingRequiredDuringExecution...)
  62. //}
  63. for _, v := range podAntiAffinityTerms {
  64. if v.TopologyKey != v1.LabelHostname {
  65. return apierrors.NewForbidden(attributes.GetResource().GroupResource(), pod.Name, fmt.Errorf("affinity.PodAntiAffinity.RequiredDuringScheduling has TopologyKey %v but only key %v is allowed", v.TopologyKey, v1.LabelHostname))
  66. }
  67. }
  68. }
  69. return nil
  70. }