mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
replenish quota for service optimize, add testcase
This commit is contained in:
parent
4fdde68f78
commit
3973856ac2
@ -59,7 +59,7 @@ func PodReplenishmentUpdateFunc(options *ReplenishmentControllerOptions) func(ol
|
|||||||
oldPod := oldObj.(*api.Pod)
|
oldPod := oldObj.(*api.Pod)
|
||||||
newPod := newObj.(*api.Pod)
|
newPod := newObj.(*api.Pod)
|
||||||
if core.QuotaPod(oldPod) && !core.QuotaPod(newPod) {
|
if core.QuotaPod(oldPod) && !core.QuotaPod(newPod) {
|
||||||
options.ReplenishmentFunc(options.GroupKind, newPod.Namespace, newPod)
|
options.ReplenishmentFunc(options.GroupKind, newPod.Namespace, oldPod)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,13 +219,13 @@ func (r *replenishmentControllerFactory) NewController(options *ReplenishmentCon
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceReplenishmentUpdateFunc will replenish if the old service was quota tracked but the new is not
|
// ServiceReplenishmentUpdateFunc will replenish if the service was quota tracked has changed service type
|
||||||
func ServiceReplenishmentUpdateFunc(options *ReplenishmentControllerOptions) func(oldObj, newObj interface{}) {
|
func ServiceReplenishmentUpdateFunc(options *ReplenishmentControllerOptions) func(oldObj, newObj interface{}) {
|
||||||
return func(oldObj, newObj interface{}) {
|
return func(oldObj, newObj interface{}) {
|
||||||
oldService := oldObj.(*api.Service)
|
oldService := oldObj.(*api.Service)
|
||||||
newService := newObj.(*api.Service)
|
newService := newObj.(*api.Service)
|
||||||
if core.QuotaServiceType(oldService) || core.QuotaServiceType(newService) {
|
if core.GetQuotaServiceType(oldService) != core.GetQuotaServiceType(newService) {
|
||||||
options.ReplenishmentFunc(options.GroupKind, newService.Namespace, newService)
|
options.ReplenishmentFunc(options.GroupKind, newService.Namespace, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,4 +118,38 @@ func TestServiceReplenishmentUpdateFunc(t *testing.T) {
|
|||||||
if mockReplenish.namespace != oldService.Namespace {
|
if mockReplenish.namespace != oldService.Namespace {
|
||||||
t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
|
t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mockReplenish = &testReplenishment{}
|
||||||
|
options = ReplenishmentControllerOptions{
|
||||||
|
GroupKind: api.Kind("Service"),
|
||||||
|
ReplenishmentFunc: mockReplenish.Replenish,
|
||||||
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
||||||
|
}
|
||||||
|
oldService = &api.Service{
|
||||||
|
ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "mysvc"},
|
||||||
|
Spec: api.ServiceSpec{
|
||||||
|
Type: api.ServiceTypeNodePort,
|
||||||
|
Ports: []api.ServicePort{{
|
||||||
|
Port: 80,
|
||||||
|
TargetPort: intstr.FromInt(80),
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
newService = &api.Service{
|
||||||
|
ObjectMeta: api.ObjectMeta{Namespace: "test", Name: "mysvc"},
|
||||||
|
Spec: api.ServiceSpec{
|
||||||
|
Type: api.ServiceTypeNodePort,
|
||||||
|
Ports: []api.ServicePort{{
|
||||||
|
Port: 81,
|
||||||
|
TargetPort: intstr.FromInt(81),
|
||||||
|
}}},
|
||||||
|
}
|
||||||
|
updateFunc = ServiceReplenishmentUpdateFunc(&options)
|
||||||
|
updateFunc(oldService, newService)
|
||||||
|
if mockReplenish.groupKind == api.Kind("Service") {
|
||||||
|
t.Errorf("Unexpected group kind %v", mockReplenish.groupKind)
|
||||||
|
}
|
||||||
|
if mockReplenish.namespace == oldService.Namespace {
|
||||||
|
t.Errorf("Unexpected namespace %v", mockReplenish.namespace)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,3 +73,14 @@ func QuotaServiceType(service *api.Service) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GetQuotaServiceType returns ServiceType if the service type is eligible to track against a quota, nor return ""
|
||||||
|
func GetQuotaServiceType(service *api.Service) api.ServiceType {
|
||||||
|
switch service.Spec.Type {
|
||||||
|
case api.ServiceTypeNodePort:
|
||||||
|
return api.ServiceTypeNodePort
|
||||||
|
case api.ServiceTypeLoadBalancer:
|
||||||
|
return api.ServiceTypeLoadBalancer
|
||||||
|
}
|
||||||
|
return api.ServiceType("")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user