-
Notifications
You must be signed in to change notification settings - Fork 969
/
admit_job.go
194 lines (156 loc) · 5.44 KB
/
admit_job.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright 2018 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package admission
import (
"fmt"
"reflect"
"strings"
"github.com/golang/glog"
"k8s.io/api/admission/v1beta1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
k8score "k8s.io/kubernetes/pkg/apis/core"
k8scorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
k8scorevalid "k8s.io/kubernetes/pkg/apis/core/validation"
v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/job/plugins"
)
// job admit.
func AdmitJobs(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
glog.V(3).Infof("admitting jobs -- %s", ar.Request.Operation)
job, err := DecodeJob(ar.Request.Object, ar.Request.Resource)
if err != nil {
return ToAdmissionResponse(err)
}
var msg string
reviewResponse := v1beta1.AdmissionResponse{}
reviewResponse.Allowed = true
switch ar.Request.Operation {
case v1beta1.Create:
msg = validateJob(job, &reviewResponse)
break
case v1beta1.Update:
oldJob, err := DecodeJob(ar.Request.OldObject, ar.Request.Resource)
if err != nil {
return ToAdmissionResponse(err)
}
msg = specDeepEqual(job, oldJob, &reviewResponse)
break
default:
err := fmt.Errorf("expect operation to be 'CREATE' or 'UPDATE'")
return ToAdmissionResponse(err)
}
if !reviewResponse.Allowed {
reviewResponse.Result = &metav1.Status{Message: strings.TrimSpace(msg)}
}
return &reviewResponse
}
func validateJob(job v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) string {
var msg string
taskNames := map[string]string{}
var totalReplicas int32
if job.Spec.MinAvailable < 0 {
reviewResponse.Allowed = false
return fmt.Sprintf("'minAvailable' cannot be less than zero.")
}
if len(job.Spec.Tasks) == 0 {
reviewResponse.Allowed = false
return fmt.Sprintf("No task specified in job spec")
}
for index, task := range job.Spec.Tasks {
if task.Replicas <= 0 {
msg = msg + fmt.Sprintf(" 'replicas' is not set positive in task: %s;", task.Name)
}
// count replicas
totalReplicas = totalReplicas + task.Replicas
// validate task name
if errMsgs := validation.IsDNS1123Label(task.Name); len(errMsgs) > 0 {
msg = msg + fmt.Sprintf(" %v;", errMsgs)
}
// duplicate task name
if _, found := taskNames[task.Name]; found {
msg = msg + fmt.Sprintf(" duplicated task name %s;", task.Name)
break
} else {
taskNames[task.Name] = task.Name
}
//duplicate task event policies
if duplicateInfo, ok := CheckPolicyDuplicate(task.Policies); ok {
msg = msg + fmt.Sprintf(" duplicated task event policies: %s;", duplicateInfo)
}
if err := validatePolicies(task.Policies, field.NewPath("spec.tasks.policies")); err != nil {
msg = msg + err.Error() + fmt.Sprintf(" valid events are %v, valid actions are %v",
getValidEvents(), getValidActions())
}
msg += validateTaskTemplate(task, job, index)
}
if totalReplicas < job.Spec.MinAvailable {
msg = msg + " 'minAvailable' should not be greater than total replicas in tasks;"
}
//duplicate job event policies
if duplicateInfo, ok := CheckPolicyDuplicate(job.Spec.Policies); ok {
msg = msg + fmt.Sprintf(" duplicated job event policies: %s;", duplicateInfo)
}
if err := validatePolicies(job.Spec.Policies, field.NewPath("spec.policies")); err != nil {
msg = msg + err.Error() + fmt.Sprintf(" valid events are %v, valid actions are %v;",
getValidEvents(), getValidActions())
}
// invalid job plugins
if len(job.Spec.Plugins) != 0 {
for name := range job.Spec.Plugins {
if _, found := plugins.GetPluginBuilder(name); !found {
msg = msg + fmt.Sprintf(" unable to find job plugin: %s", name)
}
}
}
//TODO(tommylikehu): Fix me and enable it.
//if validateInfo, ok := ValidateIO(job.Spec.Volumes); ok {
// msg = msg + validateInfo
//}
if msg != "" {
reviewResponse.Allowed = false
}
return msg
}
func specDeepEqual(newJob v1alpha1.Job, oldJob v1alpha1.Job, reviewResponse *v1beta1.AdmissionResponse) string {
var msg string
if !reflect.DeepEqual(newJob.Spec, oldJob.Spec) {
reviewResponse.Allowed = false
msg = "job.spec is not allowed to modify when update jobs;"
}
return msg
}
func validateTaskTemplate(task v1alpha1.TaskSpec, job v1alpha1.Job, index int) string {
var v1PodTemplate v1.PodTemplate
v1PodTemplate.Template = *task.Template.DeepCopy()
k8scorev1.SetObjectDefaults_PodTemplate(&v1PodTemplate)
var coreTemplateSpec k8score.PodTemplateSpec
k8scorev1.Convert_v1_PodTemplateSpec_To_core_PodTemplateSpec(&v1PodTemplate.Template, &coreTemplateSpec, nil)
corePodTemplate := k8score.PodTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: task.Name,
Namespace: job.Namespace,
},
Template: coreTemplateSpec,
}
if allErrs := k8scorevalid.ValidatePodTemplate(&corePodTemplate); len(allErrs) > 0 {
msg := fmt.Sprintf("spec.task[%d].", index)
for index := range allErrs {
msg += allErrs[index].Error() + ". "
}
return msg
}
return ""
}