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 RequiredAntiAffinityTerms []AffinityTerm
PreferredAffinityTerms []WeightedAffinityTerm PreferredAffinityTerms []WeightedAffinityTerm
PreferredAntiAffinityTerms []WeightedAffinityTerm PreferredAntiAffinityTerms []WeightedAffinityTerm
ParseError error
} }
// DeepCopy returns a deep copy of the PodInfo object. // DeepCopy returns a deep copy of the PodInfo object.
@ -134,18 +133,17 @@ func (pi *PodInfo) DeepCopy() *PodInfo {
RequiredAntiAffinityTerms: pi.RequiredAntiAffinityTerms, RequiredAntiAffinityTerms: pi.RequiredAntiAffinityTerms,
PreferredAffinityTerms: pi.PreferredAffinityTerms, PreferredAffinityTerms: pi.PreferredAffinityTerms,
PreferredAntiAffinityTerms: pi.PreferredAntiAffinityTerms, PreferredAntiAffinityTerms: pi.PreferredAntiAffinityTerms,
ParseError: pi.ParseError,
} }
} }
// Update creates a full new PodInfo by default. And only updates the pod when the PodInfo // 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. // 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 { 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 // PodInfo includes immutable information, and so it is safe to update the pod in place if it is
// the exact same pod // the exact same pod
pi.Pod = pod pi.Pod = pod
return return nil
} }
var preferredAffinityTerms []v1.WeightedPodAffinityTerm var preferredAffinityTerms []v1.WeightedPodAffinityTerm
var preferredAntiAffinityTerms []v1.WeightedPodAffinityTerm var preferredAntiAffinityTerms []v1.WeightedPodAffinityTerm
@ -183,7 +181,7 @@ func (pi *PodInfo) Update(pod *v1.Pod) {
pi.RequiredAntiAffinityTerms = requiredAntiAffinityTerms pi.RequiredAntiAffinityTerms = requiredAntiAffinityTerms
pi.PreferredAffinityTerms = weightedAffinityTerms pi.PreferredAffinityTerms = weightedAffinityTerms
pi.PreferredAntiAffinityTerms = weightedAntiAffinityTerms pi.PreferredAntiAffinityTerms = weightedAntiAffinityTerms
pi.ParseError = utilerrors.NewAggregate(parseErrs) return utilerrors.NewAggregate(parseErrs)
} }
// AffinityTerm is a processed version of v1.PodAffinityTerm. // 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. // NewPodInfo returns a new PodInfo.
func NewPodInfo(pod *v1.Pod) *PodInfo { func NewPodInfo(pod *v1.Pod) (*PodInfo, error) {
pInfo := &PodInfo{} pInfo := &PodInfo{}
pInfo.Update(pod) err := pInfo.Update(pod)
return pInfo return pInfo, err
} }
func getPodAffinityTerms(affinity *v1.Affinity) (terms []v1.PodAffinityTerm) { func getPodAffinityTerms(affinity *v1.Affinity) (terms []v1.PodAffinityTerm) {
@ -609,7 +607,10 @@ func (n *NodeInfo) AddPodInfo(podInfo *PodInfo) {
// AddPod is a wrapper around AddPodInfo. // AddPod is a wrapper around AddPodInfo.
func (n *NodeInfo) AddPod(pod *v1.Pod) { 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 { func podWithAffinity(p *v1.Pod) bool {