forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admission.go
145 lines (128 loc) · 3.86 KB
/
admission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package runonceduration
import (
"errors"
"fmt"
"io"
"strconv"
"github.com/golang/glog"
"k8s.io/apiserver/pkg/admission"
"k8s.io/client-go/util/integer"
kapi "k8s.io/kubernetes/pkg/api"
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
configlatest "github.com/openshift/origin/pkg/cmd/server/api/latest"
projectcache "github.com/openshift/origin/pkg/project/cache"
"github.com/openshift/origin/pkg/quota/admission/runonceduration/api"
"github.com/openshift/origin/pkg/quota/admission/runonceduration/api/validation"
)
func Register(plugins *admission.Plugins) {
plugins.Register("RunOnceDuration",
func(config io.Reader) (admission.Interface, error) {
pluginConfig, err := readConfig(config)
if err != nil {
return nil, err
}
if pluginConfig == nil {
glog.Infof("Admission plugin %q is not configured so it will be disabled.", "RunOnceDuration")
return nil, nil
}
return NewRunOnceDuration(pluginConfig), nil
})
}
func readConfig(reader io.Reader) (*api.RunOnceDurationConfig, error) {
obj, err := configlatest.ReadYAML(reader)
if err != nil {
return nil, err
}
if obj == nil {
return nil, nil
}
config, ok := obj.(*api.RunOnceDurationConfig)
if !ok {
return nil, fmt.Errorf("unexpected config object %#v", obj)
}
errs := validation.ValidateRunOnceDurationConfig(config)
if len(errs) > 0 {
return nil, errs.ToAggregate()
}
return config, nil
}
// NewRunOnceDuration creates a new RunOnceDuration admission plugin
func NewRunOnceDuration(config *api.RunOnceDurationConfig) admission.Interface {
return &runOnceDuration{
Handler: admission.NewHandler(admission.Create),
config: config,
}
}
type runOnceDuration struct {
*admission.Handler
config *api.RunOnceDurationConfig
cache *projectcache.ProjectCache
}
var _ = oadmission.WantsProjectCache(&runOnceDuration{})
func (a *runOnceDuration) Admit(attributes admission.Attributes) error {
switch {
case a.config == nil,
attributes.GetResource().GroupResource() != kapi.Resource("pods"),
len(attributes.GetSubresource()) > 0:
return nil
}
pod, ok := attributes.GetObject().(*kapi.Pod)
if !ok {
return admission.NewForbidden(attributes, fmt.Errorf("unexpected object: %#v", attributes.GetObject()))
}
// Only update pods with a restart policy of Never or OnFailure
switch pod.Spec.RestartPolicy {
case kapi.RestartPolicyNever,
kapi.RestartPolicyOnFailure:
// continue
default:
return nil
}
appliedProjectLimit, err := a.applyProjectAnnotationLimit(attributes.GetNamespace(), pod)
if err != nil {
return admission.NewForbidden(attributes, err)
}
if !appliedProjectLimit && a.config.ActiveDeadlineSecondsLimit != nil {
pod.Spec.ActiveDeadlineSeconds = int64MinP(a.config.ActiveDeadlineSecondsLimit, pod.Spec.ActiveDeadlineSeconds)
}
return nil
}
func (a *runOnceDuration) SetProjectCache(cache *projectcache.ProjectCache) {
a.cache = cache
}
func (a *runOnceDuration) Validate() error {
if a.cache == nil {
return errors.New("RunOnceDuration plugin requires a project cache")
}
return nil
}
func (a *runOnceDuration) applyProjectAnnotationLimit(namespace string, pod *kapi.Pod) (bool, error) {
ns, err := a.cache.GetNamespace(namespace)
if err != nil {
return false, fmt.Errorf("error looking up pod namespace: %v", err)
}
if ns.Annotations == nil {
return false, nil
}
limit, hasLimit := ns.Annotations[api.ActiveDeadlineSecondsLimitAnnotation]
if !hasLimit {
return false, nil
}
limitInt64, err := strconv.ParseInt(limit, 10, 64)
if err != nil {
return false, fmt.Errorf("cannot parse the ActiveDeadlineSeconds limit (%s) for project %s: %v", limit, ns.Name, err)
}
pod.Spec.ActiveDeadlineSeconds = int64MinP(&limitInt64, pod.Spec.ActiveDeadlineSeconds)
return true, nil
}
func int64MinP(a, b *int64) *int64 {
switch {
case a == nil:
return b
case b == nil:
return a
default:
c := integer.Int64Min(*a, *b)
return &c
}
}