function changes

This commit is contained in:
kidddddddddddddddddddddd 2022-09-05 12:38:57 +08:00
parent 525280d285
commit 1eb9d42c3f

View File

@ -123,7 +123,6 @@ type PodInfo struct {
RequiredAntiAffinityTerms []AffinityTerm
PreferredAffinityTerms []WeightedAffinityTerm
PreferredAntiAffinityTerms []WeightedAffinityTerm
ParseError error
}
// DeepCopy returns a deep copy of the PodInfo object.
@ -134,18 +133,17 @@ func (pi *PodInfo) DeepCopy() *PodInfo {
RequiredAntiAffinityTerms: pi.RequiredAntiAffinityTerms,
PreferredAffinityTerms: pi.PreferredAffinityTerms,
PreferredAntiAffinityTerms: pi.PreferredAntiAffinityTerms,
ParseError: pi.ParseError,
}
}
// Update creates a full new PodInfo by default. And only updates the pod when the PodInfo
// has been instantiated and the passed pod is the exact same one as the original pod.
func (pi *PodInfo) Update(pod *v1.Pod) {
func (pi *PodInfo) Update(pod *v1.Pod) error {
if pod != nil && pi.Pod != nil && pi.Pod.UID == pod.UID {
// PodInfo includes immutable information, and so it is safe to update the pod in place if it is
// the exact same pod
pi.Pod = pod
return
return nil
}
var preferredAffinityTerms []v1.WeightedPodAffinityTerm
var preferredAntiAffinityTerms []v1.WeightedPodAffinityTerm
@ -183,7 +181,7 @@ func (pi *PodInfo) Update(pod *v1.Pod) {
pi.RequiredAntiAffinityTerms = requiredAntiAffinityTerms
pi.PreferredAffinityTerms = weightedAffinityTerms
pi.PreferredAntiAffinityTerms = weightedAntiAffinityTerms
pi.ParseError = utilerrors.NewAggregate(parseErrs)
return utilerrors.NewAggregate(parseErrs)
}
// AffinityTerm is a processed version of v1.PodAffinityTerm.
@ -321,10 +319,10 @@ func getWeightedAffinityTerms(pod *v1.Pod, v1Terms []v1.WeightedPodAffinityTerm)
}
// NewPodInfo returns a new PodInfo.
func NewPodInfo(pod *v1.Pod) *PodInfo {
func NewPodInfo(pod *v1.Pod) (*PodInfo, error) {
pInfo := &PodInfo{}
pInfo.Update(pod)
return pInfo
err := pInfo.Update(pod)
return pInfo, err
}
func getPodAffinityTerms(affinity *v1.Affinity) (terms []v1.PodAffinityTerm) {
@ -609,7 +607,10 @@ func (n *NodeInfo) AddPodInfo(podInfo *PodInfo) {
// AddPod is a wrapper around AddPodInfo.
func (n *NodeInfo) AddPod(pod *v1.Pod) {
n.AddPodInfo(NewPodInfo(pod))
// ignore this err since apiserver doesn't properly validate affinity terms
// and we can't fix the validation for backwards compatibility.
podInfo, _ := NewPodInfo(pod)
n.AddPodInfo(podInfo)
}
func podWithAffinity(p *v1.Pod) bool {