k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/admission/errors.go (about) 1 /* 2 Copyright 2021 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 admission 18 19 import ( 20 "errors" 21 "fmt" 22 23 "k8s.io/kubernetes/pkg/kubelet/lifecycle" 24 ) 25 26 const ( 27 ErrorReasonUnexpected = "UnexpectedAdmissionError" 28 ) 29 30 type Error interface { 31 Error() string 32 Type() string 33 } 34 35 type unexpectedAdmissionError struct{ Err error } 36 37 var _ Error = (*unexpectedAdmissionError)(nil) 38 39 func (e *unexpectedAdmissionError) Error() string { 40 return fmt.Sprintf("Allocate failed due to %v, which is unexpected", e.Err) 41 } 42 43 func (e *unexpectedAdmissionError) Type() string { 44 return ErrorReasonUnexpected 45 } 46 47 func GetPodAdmitResult(err error) lifecycle.PodAdmitResult { 48 if err == nil { 49 return lifecycle.PodAdmitResult{Admit: true} 50 } 51 52 var admissionErr Error 53 if !errors.As(err, &admissionErr) { 54 admissionErr = &unexpectedAdmissionError{err} 55 } 56 57 return lifecycle.PodAdmitResult{ 58 Message: admissionErr.Error(), 59 Reason: admissionErr.Type(), 60 Admit: false, 61 } 62 }