-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathadmit_job.go
140 lines (112 loc) · 3.69 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
/*
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"
"volcano.sh/volcano/pkg/controllers/job/plugins"
"github.com/golang/glog"
"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation"
v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
)
// 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 = validateJobSpec(job.Spec, &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 validateJobSpec(jobSpec v1alpha1.JobSpec, reviewResponse *v1beta1.AdmissionResponse) string {
var msg string
taskNames := map[string]string{}
var totalReplicas int32
if len(jobSpec.Tasks) == 0 {
reviewResponse.Allowed = false
return fmt.Sprintf("No task specified in job spec")
}
for _, task := range jobSpec.Tasks {
if task.Replicas == 0 {
msg = msg + fmt.Sprintf(" 'replicas' is set '0' 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 totalReplicas < jobSpec.MinAvailable {
msg = msg + " 'minAvailable' should not be greater than total replicas in tasks;"
}
//duplicate job event policies
if duplicateInfo, ok := CheckPolicyDuplicate(jobSpec.Policies); ok {
msg = msg + fmt.Sprintf(" duplicated job event policies: %s;", duplicateInfo)
}
//invalid job plugins
if len(jobSpec.Plugins) != 0 {
for name := range jobSpec.Plugins {
if _, found := plugins.GetPluginBuilder(name); !found {
msg = msg + fmt.Sprintf(" unable to find job plugin: %s", name)
}
}
}
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
}