Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(overcommit): add node overcommit webhook #229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/katalyst-webhook/app/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func NewWebhookInitializers() map[string]InitFunc {
webhooks := make(map[string]InitFunc)
webhooks[validating.VPAWebhookName] = validating.StartVPAWebhook
webhooks[mutating.PodWebhookName] = mutating.StartPodWebhook
webhooks[mutating.NodeWebhookName] = mutating.StartNodeWebhook
return webhooks
}

Expand Down
50 changes: 50 additions & 0 deletions cmd/katalyst-webhook/app/webhook/mutating/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2022 The Katalyst Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mutating

import (
"context"

katalyst "github.com/kubewharf/katalyst-core/cmd/base"
webhookconsts "github.com/kubewharf/katalyst-core/cmd/katalyst-webhook/app/webhook"
"github.com/kubewharf/katalyst-core/pkg/config/generic"
webhookconfig "github.com/kubewharf/katalyst-core/pkg/config/webhook"
"github.com/kubewharf/katalyst-core/pkg/webhook/mutating/node"
)

const (
NodeWebhookName = "node"
)

func StartNodeWebhook(
ctx context.Context,
webhookCtx *katalyst.GenericContext,
genericConf *generic.GenericConfiguration,
webhookGenericConf *webhookconfig.GenericWebhookConfiguration,
webhookConf *webhookconfig.WebhooksConfiguration,
name string,
) (*webhookconsts.WebhookWrapper, error) {
v, run, err := node.NewWebhookNode(ctx, webhookCtx, genericConf, webhookGenericConf, webhookConf)
if err != nil {
return nil, err
}
return &webhookconsts.WebhookWrapper{
Name: name,
StartFunc: run,
Webhook: v,
}, nil
}
42 changes: 42 additions & 0 deletions pkg/util/native/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,45 @@ func ResourceQuantityToInt64Value(resourceName v1.ResourceName, quantity resourc
func getResourceMetricsName(resourceName v1.ResourceName, quantity resource.Quantity) (string, int64) {
return resourceName.String(), ResourceQuantityToInt64Value(resourceName, quantity)
}

// MultiplyResourceQuantity scales quantity according to its resource name.
func MultiplyResourceQuantity(resourceName v1.ResourceName, quantity resource.Quantity, y float64) resource.Quantity {
switch resourceName {
case v1.ResourceCPU:
return MultiplyMilliQuantity(quantity, y)
case v1.ResourceMemory:
fallthrough
default:
return MultiplyQuantity(quantity, y)
}
}

// MultiplyMilliQuantity scales quantity by y.
func MultiplyMilliQuantity(quantity resource.Quantity, y float64) resource.Quantity {
if 0 == y {
return *resource.NewMilliQuantity(0, quantity.Format)
}

milliValue := quantity.MilliValue()
if 0 == milliValue {
return quantity
}

milliValue = int64(float64(milliValue) * y)
return *resource.NewMilliQuantity(milliValue, quantity.Format)
}

// MultiplyQuantity scales quantity by y.
func MultiplyQuantity(quantity resource.Quantity, y float64) resource.Quantity {
if 0 == y {
return *resource.NewQuantity(0, quantity.Format)
}

value := quantity.Value()
if 0 == value {
return quantity
}

value = int64(float64(value) * y)
return *resource.NewQuantity(value, quantity.Format)
}
60 changes: 60 additions & 0 deletions pkg/util/native/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,63 @@ func TestNeedUpdateResources(t *testing.T) {
})
}
}

func TestMultiplyResourceQuantity(t *testing.T) {
t.Parallel()

cases := []struct {
name string
resourceName v1.ResourceName
quant resource.Quantity
factor float64
res resource.Quantity
want bool
}{
{
name: "resource CPU",
resourceName: v1.ResourceCPU,
quant: *resource.NewQuantity(2, resource.DecimalSI),
factor: 1.5,
res: *resource.NewQuantity(3, resource.DecimalSI),
want: true,
},
{
name: "resource memory",
resourceName: v1.ResourceMemory,
quant: resource.MustParse("200Gi"),
factor: 1.5,
res: resource.MustParse("300Gi"),
want: true,
},
{
name: "zero value",
resourceName: v1.ResourceCPU,
quant: *resource.NewQuantity(0, resource.DecimalSI),
factor: 2,
res: *resource.NewQuantity(0, resource.DecimalSI),
want: true,
},
{
name: "zero factor",
resourceName: v1.ResourceCPU,
quant: *resource.NewQuantity(2, resource.DecimalSI),
factor: 0,
res: *resource.NewQuantity(0, resource.DecimalSI),
want: true,
},
{
name: "round down",
resourceName: v1.ResourceCPU,
quant: *resource.NewQuantity(2, resource.DecimalSI),
factor: 1.23456,
res: *resource.NewMilliQuantity(2469, resource.DecimalSI),
want: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
quant := MultiplyResourceQuantity(c.resourceName, c.quant, c.factor)
assert.Equal(t, c.want, quant.Equal(c.res))
})
}
}
126 changes: 126 additions & 0 deletions pkg/webhook/mutating/node/allocatable_mutator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright 2022 The Katalyst Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package node

import (
"fmt"
"strconv"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
core "k8s.io/api/core/v1"
"k8s.io/klog/v2"

"github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)

const (
nodeAllocatableMutatorName = "nodeAllocatableMutator"
)

// WebhookNodeAllocatableMutator mutate node allocatable according to overcommit annotation
type WebhookNodeAllocatableMutator struct{}

func NewWebhookNodeAllocatableMutator() *WebhookNodeAllocatableMutator {
return &WebhookNodeAllocatableMutator{}
}

func (na *WebhookNodeAllocatableMutator) MutateNode(node *core.Node, admissionRequest *admissionv1beta1.AdmissionRequest) error {
if admissionv1beta1.Update != admissionRequest.Operation || admissionRequest.SubResource != "status" {
return nil
}

if node == nil {
err := fmt.Errorf("node is nil")
klog.Error(err)
return err
}

nodeAnnotations := node.Annotations
if nodeAnnotations == nil {
nodeAnnotations = make(map[string]string)
}

nodeAnnotations[consts.NodeAnnotationOriginalCapacityCPUKey] = node.Status.Capacity.Cpu().String()
nodeAnnotations[consts.NodeAnnotationOriginalCapacityMemoryKey] = node.Status.Capacity.Memory().String()
nodeAnnotations[consts.NodeAnnotationOriginalAllocatableCPUKey] = node.Status.Allocatable.Cpu().String()
nodeAnnotations[consts.NodeAnnotationOriginalAllocatableMemoryKey] = node.Status.Allocatable.Memory().String()
node.Annotations = nodeAnnotations

CPUOvercommitRatioValue, ok := node.Annotations[consts.NodeAnnotationCPUOvercommitRatioKey]
if ok {
CPUOvercommitRatio, err := overcommitRatioValidate(CPUOvercommitRatioValue)
if err != nil {
klog.Errorf("node %s %s validate fail, value: %s, err: %v", node.Name, consts.NodeAnnotationCPUOvercommitRatioKey, CPUOvercommitRatioValue, err)
} else {
if CPUOvercommitRatio > 1.0 {
allocatable := node.Status.Allocatable.Cpu()
capacity := node.Status.Capacity.Cpu()
newAllocatable := native.MultiplyResourceQuantity(core.ResourceCPU, *allocatable, CPUOvercommitRatio)
newCapacity := native.MultiplyResourceQuantity(core.ResourceCPU, *capacity, CPUOvercommitRatio)
klog.V(6).Infof(
"node %s %s capacity: %v, allocatable: %v, newCapacity: %v, newAllocatable: %v",
node.Name, core.ResourceCPU,
capacity.String(), newCapacity.String(),
allocatable.String(), newAllocatable.String())
node.Status.Allocatable[core.ResourceCPU] = newAllocatable
node.Status.Capacity[core.ResourceCPU] = newCapacity
}
}
}

memoryOvercommitRatioValue, ok := node.Annotations[consts.NodeAnnotationMemoryOvercommitRatioKey]
if ok {
memoryOvercommitRatio, err := overcommitRatioValidate(memoryOvercommitRatioValue)
if err != nil {
klog.Errorf("node %s %s validate fail, value: %s, err: %v", node.Name, consts.NodeAnnotationMemoryOvercommitRatioKey, memoryOvercommitRatioValue, err)
} else {
if memoryOvercommitRatio > 1.0 {
allocatable := node.Status.Allocatable.Memory()
capacity := node.Status.Capacity.Memory()
newAllocatable := native.MultiplyResourceQuantity(core.ResourceMemory, *allocatable, memoryOvercommitRatio)
newCapacity := native.MultiplyResourceQuantity(core.ResourceMemory, *capacity, memoryOvercommitRatio)
klog.V(6).Infof("node %s %s capacity: %v, allocatable: %v, newCapacity: %v, newAllocatable: %v",
node.Name, core.ResourceMemory,
capacity.String(), newCapacity.String(),
allocatable.String(), newAllocatable.String())
node.Status.Allocatable[core.ResourceMemory] = newAllocatable
node.Status.Capacity[core.ResourceMemory] = newCapacity
}
}
}

return nil
}

func (na *WebhookNodeAllocatableMutator) Name() string {
return nodeAllocatableMutatorName
}

func overcommitRatioValidate(overcommitRatioAnnotation string) (float64, error) {
overcommitRatio, err := strconv.ParseFloat(overcommitRatioAnnotation, 64)
if err != nil {
return 1, err
}

if overcommitRatio < 1.0 {
err = fmt.Errorf("overcommitRatio should be greater than 1")
return 1, err
}

return overcommitRatio, nil
}
Loading