...
1
16
17 package extendedresourcetoleration
18
19 import (
20 "context"
21 "fmt"
22 "io"
23
24 "k8s.io/apimachinery/pkg/api/errors"
25 "k8s.io/apimachinery/pkg/util/sets"
26 "k8s.io/apiserver/pkg/admission"
27 "k8s.io/kubernetes/pkg/apis/core"
28 "k8s.io/kubernetes/pkg/apis/core/helper"
29 )
30
31
32 const PluginName = "ExtendedResourceToleration"
33
34
35 func Register(plugins *admission.Plugins) {
36 plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
37 return newExtendedResourceToleration(), nil
38 })
39 }
40
41
42 func newExtendedResourceToleration() *plugin {
43 return &plugin{
44 Handler: admission.NewHandler(admission.Create, admission.Update),
45 }
46 }
47
48
49 var _ admission.MutationInterface = &plugin{}
50
51 type plugin struct {
52 *admission.Handler
53 }
54
55
56
57
58
59
60 func (p *plugin) Admit(ctx context.Context, attributes admission.Attributes, o admission.ObjectInterfaces) error {
61
62 if len(attributes.GetSubresource()) != 0 || attributes.GetResource().GroupResource() != core.Resource("pods") {
63 return nil
64 }
65
66 pod, ok := attributes.GetObject().(*core.Pod)
67 if !ok {
68 return errors.NewBadRequest(fmt.Sprintf("expected *core.Pod but got %T", attributes.GetObject()))
69 }
70
71 resources := sets.String{}
72 for _, container := range pod.Spec.Containers {
73 for resourceName := range container.Resources.Requests {
74 if helper.IsExtendedResourceName(resourceName) {
75 resources.Insert(string(resourceName))
76 }
77 }
78 }
79 for _, container := range pod.Spec.InitContainers {
80 for resourceName := range container.Resources.Requests {
81 if helper.IsExtendedResourceName(resourceName) {
82 resources.Insert(string(resourceName))
83 }
84 }
85 }
86
87
88
89 for _, resource := range resources.List() {
90 helper.AddOrUpdateTolerationInPod(pod, &core.Toleration{
91 Key: resource,
92 Operator: core.TolerationOpExists,
93 Effect: core.TaintEffectNoSchedule,
94 })
95 }
96
97 return nil
98 }
99
View as plain text