1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package lifecycle 18 19 import "k8s.io/api/core/v1" 20 21 // PodAdmitAttributes is the context for a pod admission decision. 22 // The member fields of this struct should never be mutated. 23 type PodAdmitAttributes struct { 24 // the pod to evaluate for admission 25 Pod *v1.Pod 26 // all pods bound to the kubelet excluding the pod being evaluated 27 OtherPods []*v1.Pod 28 } 29 30 // PodAdmitResult provides the result of a pod admission decision. 31 type PodAdmitResult struct { 32 // if true, the pod should be admitted. 33 Admit bool 34 // a brief single-word reason why the pod could not be admitted. 35 Reason string 36 // a brief message explaining why the pod could not be admitted. 37 Message string 38 } 39 40 // PodAdmitHandler is notified during pod admission. 41 type PodAdmitHandler interface { 42 // Admit evaluates if a pod can be admitted. 43 Admit(attrs *PodAdmitAttributes) PodAdmitResult 44 } 45 46 // PodAdmitTarget maintains a list of handlers to invoke. 47 type PodAdmitTarget interface { 48 // AddPodAdmitHandler adds the specified handler. 49 AddPodAdmitHandler(a PodAdmitHandler) 50 } 51 52 // PodSyncLoopHandler is invoked during each sync loop iteration. 53 type PodSyncLoopHandler interface { 54 // ShouldSync returns true if the pod needs to be synced. 55 // This operation must return immediately as its called for each pod. 56 // The provided pod should never be modified. 57 ShouldSync(pod *v1.Pod) bool 58 } 59 60 // PodSyncLoopTarget maintains a list of handlers to pod sync loop. 61 type PodSyncLoopTarget interface { 62 // AddPodSyncLoopHandler adds the specified handler. 63 AddPodSyncLoopHandler(a PodSyncLoopHandler) 64 } 65 66 // ShouldEvictResponse provides the result of a should evict request. 67 type ShouldEvictResponse struct { 68 // if true, the pod should be evicted. 69 Evict bool 70 // a brief CamelCase reason why the pod should be evicted. 71 Reason string 72 // a brief message why the pod should be evicted. 73 Message string 74 } 75 76 // PodSyncHandler is invoked during each sync pod operation. 77 type PodSyncHandler interface { 78 // ShouldEvict is invoked during each sync pod operation to determine 79 // if the pod should be evicted from the kubelet. If so, the pod status 80 // is updated to mark its phase as failed with the provided reason and message, 81 // and the pod is immediately killed. 82 // This operation must return immediately as its called for each sync pod. 83 // The provided pod should never be modified. 84 ShouldEvict(pod *v1.Pod) ShouldEvictResponse 85 } 86 87 // PodSyncTarget maintains a list of handlers to pod sync. 88 type PodSyncTarget interface { 89 // AddPodSyncHandler adds the specified handler 90 AddPodSyncHandler(a PodSyncHandler) 91 } 92 93 // PodLifecycleTarget groups a set of lifecycle interfaces for convenience. 94 type PodLifecycleTarget interface { 95 PodAdmitTarget 96 PodSyncLoopTarget 97 PodSyncTarget 98 } 99 100 // PodAdmitHandlers maintains a list of handlers to pod admission. 101 type PodAdmitHandlers []PodAdmitHandler 102 103 // AddPodAdmitHandler adds the specified observer. 104 func (handlers *PodAdmitHandlers) AddPodAdmitHandler(a PodAdmitHandler) { 105 *handlers = append(*handlers, a) 106 } 107 108 // PodSyncLoopHandlers maintains a list of handlers to pod sync loop. 109 type PodSyncLoopHandlers []PodSyncLoopHandler 110 111 // AddPodSyncLoopHandler adds the specified observer. 112 func (handlers *PodSyncLoopHandlers) AddPodSyncLoopHandler(a PodSyncLoopHandler) { 113 *handlers = append(*handlers, a) 114 } 115 116 // PodSyncHandlers maintains a list of handlers to pod sync. 117 type PodSyncHandlers []PodSyncHandler 118 119 // AddPodSyncHandler adds the specified handler. 120 func (handlers *PodSyncHandlers) AddPodSyncHandler(a PodSyncHandler) { 121 *handlers = append(*handlers, a) 122 } 123