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

Add debug logs tracking validation of ScaledObjects on webhook #6498

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ New deprecation(s):

### Other

- **General**: Add debug logs tracking validation of ScaledObjects on webhook ([#6498](https://github.com/kedacore/keda/pull/6498))
- **General**: New eventreason KEDAScalersInfo to display important information ([#6328](https://github.com/kedacore/keda/issues/6328))

## v2.16.1
Expand Down
22 changes: 16 additions & 6 deletions apis/keda/v1alpha1/scaledobject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"

Expand All @@ -31,7 +33,7 @@ import (
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -81,7 +83,7 @@ func (so *ScaledObject) SetupWebhookWithManager(mgr ctrl.Manager, cacheMissFallb
// ScaledObjectCustomValidator is a custom validator for ScaledObject objects
type ScaledObjectCustomValidator struct{}

func (socv ScaledObjectCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
func (socv ScaledObjectCustomValidator) ValidateCreate(ctx context.Context, obj kruntime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
Expand All @@ -90,7 +92,7 @@ func (socv ScaledObjectCustomValidator) ValidateCreate(ctx context.Context, obj
return so.ValidateCreate(request.DryRun)
}

func (socv ScaledObjectCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
func (socv ScaledObjectCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj kruntime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
Expand All @@ -100,7 +102,7 @@ func (socv ScaledObjectCustomValidator) ValidateUpdate(ctx context.Context, oldO
return so.ValidateUpdate(old, request.DryRun)
}

func (socv ScaledObjectCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
func (socv ScaledObjectCustomValidator) ValidateDelete(ctx context.Context, obj kruntime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
Expand All @@ -118,7 +120,7 @@ func (so *ScaledObject) ValidateCreate(dryRun *bool) (admission.Warnings, error)
return validateWorkload(so, "create", *dryRun)
}

func (so *ScaledObject) ValidateUpdate(old runtime.Object, dryRun *bool) (admission.Warnings, error) {
func (so *ScaledObject) ValidateUpdate(old kruntime.Object, dryRun *bool) (admission.Warnings, error) {
val, _ := json.MarshalIndent(so, "", " ")
scaledobjectlog.V(1).Info(fmt.Sprintf("validating scaledobject update for %s", string(val)))

Expand All @@ -134,7 +136,7 @@ func (so *ScaledObject) ValidateDelete(_ *bool) (admission.Warnings, error) {
return nil, nil
}

func isRemovingFinalizer(so *ScaledObject, old runtime.Object) bool {
func isRemovingFinalizer(so *ScaledObject, old kruntime.Object) bool {
oldSo := old.(*ScaledObject)

soSpec, _ := json.MarshalIndent(so.Spec, "", " ")
Expand All @@ -157,6 +159,8 @@ func validateWorkload(so *ScaledObject, action string, dryRun bool) (admission.W
}

for i := range verifyFunctions {
functionName := getFunctionName(verifyFunctions[i])
scaledobjectlog.V(1).Info(fmt.Sprintf("calling %s to validate %s", functionName, so.Name))
err := verifyFunctions[i](so, action, dryRun)
if err != nil {
return nil, err
Expand All @@ -168,6 +172,8 @@ func validateWorkload(so *ScaledObject, action string, dryRun bool) (admission.W
}

for i := range verifyCommonFunctions {
functionName := getFunctionName(verifyFunctions[i])
scaledobjectlog.V(1).Info(fmt.Sprintf("calling %s to validate %s", functionName, so.Name))
err := verifyCommonFunctions[i](so, action, dryRun)
if err != nil {
return nil, err
Expand Down Expand Up @@ -588,3 +594,7 @@ func getHpaName(so ScaledObject) string {

return so.Spec.Advanced.HorizontalPodAutoscalerConfig.Name
}

func getFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, if we go forward with FuncForPC, the code should probably check for nil ptr because FuncForPC can return nil, docs. This will future-proof the code a bit in case of go compiler optimizations changing this behavior

}
Loading