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

RevisionName templating and substitution. #84

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

muscionig
Copy link

DRAFT up for discussion

Changes

This PR adds support for replacing the revisionName in the PrometheusMetric field. I discussed this with @skonto in the slack channel and it seemed a necessary feature to separate the scaling of different revisions. This is still in draft, it is missing unit tests and complete functional testing.

Minor Changes

  • 🎁 adds a templating feature that allows to substitute the revisionName under the PrometheusQuery annotation. The feature uses template and sprig. I thought of using sprig to make sure the variable substitution is solid and to potentially support additional substitutions in the future.

Usage:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: metrics-test
spec:
  template:
    metadata:
      labels:
        app: metrics-test
      annotations:
        ...
        autoscaling.knative.dev/metric: "http_requests_total"
        autoscaling.knative.dev/prometheus-query: "sum(rate(http_requests_total{pod=~\"{{ .revisionName }}.*\"}[1m]))"

I tested this with Istio

Output of the controller:

### service with no substitution
autoscaler-keda 2024/11/20 06:42:59 PrometheusQuery rendered: sum(rate(http_requests_total{}[1m]))                                    
autoscaler-keda {"severity":"INFO","timestamp":"2024-11-20T06:42:59.698756553Z","logger":"hpaautoscaler","caller":"controller/controller.go:548","message":"Reconcile succeeded","commit":"6f54184-dirty","knative.dev/controller":"knative.dev.autoscaler-keda.pkg.reconciler.autoscaling.hpa.Reconciler","knative.dev/kind":"autoscaling.internal.knative.dev.PodAutoscaler","knative.dev/traceid":"12a29044-6fe7-442b-a31c-7913633203cf","knative.dev/key":"metrics-test-istio/metrics-test-istio-00001","duration":"10.55125ms"}


### service with substitution
autoscaler-keda 2024/11/20 06:43:12 PrometheusQuery rendered: sum(rate(http_requests_total{pod=~"metrics-test-00001.*"}[1m]))
autoscaler-keda {"severity":"INFO","timestamp":"2024-11-20T06:43:12.122399087Z","logger":"hpaautoscaler","caller":"hpa/keda_hpa.go:98","message":"Creating Scaled Object \"metrics-test-00001\"","commit":"6f54184-dirty","knative.dev/controller":"knative.dev.autoscaler-keda.pkg.reconciler.autoscaling.hpa.Reconciler","knative.dev/kind":"autoscaling.internal.knative.dev.PodAutoscaler","knative.dev/traceid":"0fbaf41d-ecbf-4cf5-b417-64b2543e2e7f","knative.dev/key":"default/metrics-test-00001"}

Scaling of only one service:
Screenshot 2024-11-19 at 11 26 47 PM

/kind documentation
/kind enhancement

@knative-prow knative-prow bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/documentation kind/enhancement labels Nov 20, 2024
Copy link

knative-prow bot commented Nov 20, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: muscionig
Once this PR has been reviewed and has the lgtm label, please assign dprotaso for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 20, 2024
Copy link

knative-prow bot commented Nov 20, 2024

Hi @muscionig. Thanks for your PR.

I'm waiting for a knative-extensions member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@@ -128,6 +131,20 @@ func DesiredScaledObject(ctx context.Context, pa *autoscalingv1alpha1.PodAutosca
return nil, fmt.Errorf("query is missing for custom metric: %w", err)
}

tmpl, err := template.New("query").Funcs(sprig.TxtFuncMap()).Parse(query)
Copy link
Contributor

@skonto skonto Nov 22, 2024

Choose a reason for hiding this comment

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

Sprig has more functions but for now let's keep this minimal, let's just use the Golang templates.

dario.cat/mergo v1.0.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
Copy link
Contributor

@skonto skonto Nov 22, 2024

Choose a reason for hiding this comment

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

After updating go.mod it is required to run ./hack/update-deps.sh.

return nil, fmt.Errorf("template initialization failed: %w", err)
}
values := map[string]string{
"revisionName": pa.Name,
Copy link
Contributor

@skonto skonto Nov 22, 2024

Choose a reason for hiding this comment

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

As a side note Serving supports byo revision names instead of being auto-generated.

kind: Service
metadata:
  name: hello
spec:
  template:
    metadata:
      name: hello-1

If you want to change that service above, then you need to manually change hello-1 to something else. PA name is always the same as with revision name.

@skonto skonto requested review from dsimansk and removed request for upodroid November 22, 2024 12:24
@skonto
Copy link
Contributor

skonto commented Nov 22, 2024

@muscionig let's proceed with this and add some tests to verify that it works as expected. Next we can add some docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/documentation kind/enhancement needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants