forked from volcano-sh/volcano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmit_pod.go
149 lines (125 loc) · 3.97 KB
/
admit_pod.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
/*
Copyright 2019 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 pods
import (
"fmt"
"strings"
"k8s.io/api/admission/v1beta1"
whv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
"volcano.sh/volcano/pkg/apis/helpers"
"volcano.sh/volcano/pkg/apis/scheduling/v1alpha1"
"volcano.sh/volcano/pkg/apis/scheduling/v1alpha2"
"volcano.sh/volcano/pkg/webhooks/router"
"volcano.sh/volcano/pkg/webhooks/schema"
"volcano.sh/volcano/pkg/webhooks/util"
)
func init() {
router.RegisterAdmission(service)
}
var service = &router.AdmissionService{
Path: "/pods/validate",
Func: AdmitPods,
Config: config,
ValidatingConfig: &whv1beta1.ValidatingWebhookConfiguration{
Webhooks: []whv1beta1.Webhook{{
Name: "validatepod.volcano.sh",
Rules: []whv1beta1.RuleWithOperations{
{
Operations: []whv1beta1.OperationType{whv1beta1.Create},
Rule: whv1beta1.Rule{
APIGroups: []string{""},
APIVersions: []string{"v1"},
Resources: []string{"pods"},
},
},
},
}},
},
}
var config = &router.AdmissionServiceConfig{}
// AdmitPods is to admit pods and return response
func AdmitPods(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
klog.V(3).Infof("admitting pods -- %s", ar.Request.Operation)
pod, err := schema.DecodePod(ar.Request.Object, ar.Request.Resource)
if err != nil {
return util.ToAdmissionResponse(err)
}
var msg string
reviewResponse := v1beta1.AdmissionResponse{}
reviewResponse.Allowed = true
switch ar.Request.Operation {
case v1beta1.Create:
msg = validatePod(pod, &reviewResponse)
break
default:
err := fmt.Errorf("expect operation to be 'CREATE'")
return util.ToAdmissionResponse(err)
}
if !reviewResponse.Allowed {
reviewResponse.Result = &metav1.Status{Message: strings.TrimSpace(msg)}
}
return &reviewResponse
}
// allow pods to create when
// 1. schedulerName of pod isn't volcano
// 2. pod has Podgroup whose phase isn't Pending
// 3. normal pods whose schedulerName is volcano don't have podgroup
func validatePod(pod *v1.Pod, reviewResponse *v1beta1.AdmissionResponse) string {
if pod.Spec.SchedulerName != config.SchedulerName {
return ""
}
pgName := ""
msg := ""
// vc-job, SN == volcano
if pod.Annotations != nil {
pgName = pod.Annotations[v1alpha2.GroupNameAnnotationKey]
}
if pgName != "" {
if err := checkPGPhase(pod, pgName, true); err != nil {
msg = err.Error()
reviewResponse.Allowed = false
}
return msg
}
// normal pod, SN == volcano
pgName = helpers.GeneratePodgroupName(pod)
if err := checkPGPhase(pod, pgName, false); err != nil {
msg = err.Error()
reviewResponse.Allowed = false
}
return msg
}
func checkPGPhase(pod *v1.Pod, pgName string, isVCJob bool) error {
pg, err := config.VolcanoClient.SchedulingV1alpha2().PodGroups(pod.Namespace).Get(pgName, metav1.GetOptions{})
if err != nil {
pg, err := config.VolcanoClient.SchedulingV1alpha1().PodGroups(pod.Namespace).Get(pgName, metav1.GetOptions{})
if err != nil {
if isVCJob || (!isVCJob && !apierrors.IsNotFound(err)) {
return fmt.Errorf("failed to get PodGroup for pod <%s/%s>: %v", pod.Namespace, pod.Name, err)
}
return nil
}
if pg.Status.Phase != v1alpha1.PodGroupPending {
return nil
}
}
if pg.Status.Phase != v1alpha2.PodGroupPending {
return nil
}
return fmt.Errorf("failed to create pod <%s/%s> as the podgroup phase is Pending",
pod.Namespace, pod.Name)
}