github.com/kubewharf/katalyst-core@v0.5.3/pkg/controller/spd/spd_baseline.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 spd 18 19 import ( 20 "math" 21 "sort" 22 23 core "k8s.io/api/core/v1" 24 25 "github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1" 26 "github.com/kubewharf/katalyst-api/pkg/consts" 27 "github.com/kubewharf/katalyst-core/pkg/util" 28 "github.com/kubewharf/katalyst-core/pkg/util/native" 29 ) 30 31 // updateBaselineSentinel update baseline sentinel annotation for spd 32 func (sc *SPDController) updateBaselineSentinel(spd *v1alpha1.ServiceProfileDescriptor) error { 33 if spd == nil { 34 return nil 35 } 36 37 // delete baseline sentinel annotation if baseline percent or extended indicator not set 38 if spd.Spec.BaselinePercent == nil && len(spd.Spec.ExtendedIndicator) == 0 { 39 util.SetSPDBaselineSentinel(spd, nil) 40 util.SetSPDExtendedBaselineSentinel(spd, nil) 41 return nil 42 } 43 44 podMetaList, err := sc.getSPDPodMetaList(spd) 45 if err != nil { 46 return err 47 } 48 49 // calculate baseline sentinel 50 baselineSentinel := calculateBaselineSentinel(podMetaList, spd.Spec.BaselinePercent) 51 52 // calculate extended baseline sentinel for each extended indicator 53 extendedBaselineSentinel := make(map[string]util.SPDBaselinePodMeta) 54 for _, indicator := range spd.Spec.ExtendedIndicator { 55 sentinel := calculateBaselineSentinel(podMetaList, indicator.BaselinePercent) 56 if sentinel == nil { 57 continue 58 } 59 60 extendedBaselineSentinel[indicator.Name] = *sentinel 61 } 62 63 util.SetSPDBaselineSentinel(spd, baselineSentinel) 64 util.SetSPDExtendedBaselineSentinel(spd, extendedBaselineSentinel) 65 return nil 66 } 67 68 // getSPDPodMetaList get spd pod meta list in order 69 func (sc *SPDController) getSPDPodMetaList(spd *v1alpha1.ServiceProfileDescriptor) ([]util.SPDBaselinePodMeta, error) { 70 podList, err := util.GetPodListForSPD(spd, sc.podIndexer, sc.conf.SPDPodLabelIndexerKeys, sc.workloadLister, sc.podLister) 71 if err != nil { 72 return nil, err 73 } 74 75 podList = native.FilterPods(podList, func(pod *core.Pod) (bool, error) { 76 return native.PodIsActive(pod), nil 77 }) 78 if len(podList) == 0 { 79 return nil, nil 80 } 81 82 podMetaList := make([]util.SPDBaselinePodMeta, 0, len(podList)) 83 for _, p := range podList { 84 podMetaList = append(podMetaList, util.GetSPDBaselinePodMeta(p.ObjectMeta)) 85 } 86 sort.SliceStable(podMetaList, func(i, j int) bool { 87 return podMetaList[i].Cmp(podMetaList[j]) < 0 88 }) 89 90 return podMetaList, nil 91 } 92 93 // calculateBaselineSentinel returns the sentinel one for a list of pods 94 // referenced by the SPD. If one pod's createTime is less than the sentinel pod 95 func calculateBaselineSentinel(podMetaList []util.SPDBaselinePodMeta, baselinePercent *int32) *util.SPDBaselinePodMeta { 96 if baselinePercent == nil || *baselinePercent >= consts.SPDBaselinePercentMax || 97 *baselinePercent <= consts.SPDBaselinePercentMin { 98 return nil 99 } 100 101 if len(podMetaList) == 0 { 102 return nil 103 } 104 105 baselineIndex := int(math.Floor(float64(len(podMetaList)-1) * float64(*baselinePercent) / 100)) 106 return &podMetaList[baselineIndex] 107 }