github.com/kubewharf/katalyst-core@v0.5.3/pkg/webhook/validating/vpa/overlap_validator.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 vpa 18 19 import ( 20 "fmt" 21 22 apiequality "k8s.io/apimachinery/pkg/api/equality" 23 "k8s.io/apimachinery/pkg/labels" 24 "k8s.io/klog/v2" 25 26 apis "github.com/kubewharf/katalyst-api/pkg/apis/autoscaling/v1alpha1" 27 apiListers "github.com/kubewharf/katalyst-api/pkg/client/listers/autoscaling/v1alpha1" 28 "github.com/kubewharf/katalyst-core/pkg/util/native" 29 ) 30 31 // WebhookVPAOverlapValidator validate if pod for one vpa overlap with other pods by checking their target reference 32 type WebhookVPAOverlapValidator struct { 33 // vpaLister can list/get VerticalPodAutoscaler from the shared informer's store 34 vpaLister apiListers.KatalystVerticalPodAutoscalerLister 35 } 36 37 func NewWebhookVPAOverlapValidator(vpaLister apiListers.KatalystVerticalPodAutoscalerLister) *WebhookVPAOverlapValidator { 38 return &WebhookVPAOverlapValidator{ 39 vpaLister: vpaLister, 40 } 41 } 42 43 func (wo *WebhookVPAOverlapValidator) ValidateVPA(vpa *apis.KatalystVerticalPodAutoscaler) (valid bool, message string, err error) { 44 if vpa == nil { 45 err := fmt.Errorf("vpa is nil") 46 return false, err.Error(), err 47 } 48 49 // todo: add cache here to avoid list all vpa 50 vpas, err := wo.vpaLister.List(labels.Everything()) 51 if err != nil { 52 return false, "failed to list all vpas", err 53 } 54 klog.V(5).Infof("find %d vpa existing", len(vpas)) 55 56 for _, anotherVPA := range vpas { 57 if anotherVPA == nil { 58 err := fmt.Errorf("vpa can not be nil") 59 return false, err.Error(), err 60 } 61 if native.CheckObjectEqual(vpa, anotherVPA) { 62 klog.Infof("ignore same vpa (%s/%s/%s)", anotherVPA.Namespace, anotherVPA.Name, anotherVPA.UID) 63 continue 64 } 65 if apiequality.Semantic.DeepEqual(vpa.Spec.TargetRef, anotherVPA.Spec.TargetRef) { 66 klog.Info("different vpa have same target reference") 67 return false, "different vpa have same target reference", nil 68 } 69 } 70 71 return true, "", nil 72 }