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: add support for metricLabelSelector with matchLabels #10

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

Conversation

lelithium
Copy link

When scaling an HPA with a Custom Metric, it is possible to specify a selector to specify which metric to target in the API. See https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/horizontal-pod-autoscaler-v2/ section metrics.object.metric.selector
As a (truncated) example:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
...
spec:
...
  metrics:
  - type: Object
    object:
      metric:
        name: my_custom_metric
        selector: 
          matchLabels: 
            my_label: "my_value"
      describedObject:
        apiVersion: "/v1"
        kind: "Service"
        name: "my_service"
...

Will yield API calls to /apis/custom.metrics.k8s.io/v1beta1/namespaces/$NAMESPACE/services/my_service/my_custom_metric?metricLabelSelector=my_label%3Dmy_value

This PR adds support for such selectors.

@machine424
Copy link
Owner

Hello @lelithium

I knew someone would ask for this someday.

I'm no longer able to do manual testing before releasing anymore, I'll be adding some tests (maybe some e2e).

I'll ping you when it's done, you can then rebase and add yours.

selector = ""
if "selector" in custom_metric:
for key, val in custom_metric["selector"].get("matchLabels", {}).items():
selector += f"&{key}%3D{val}" # `=` Needs to be URLencoded for raw query
Copy link
Owner

Choose a reason for hiding this comment

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

I recommend we use the built-in urllib library for this

>>> import urllib.parse
>>> urllib.parse.urlencode({"foo": "bar", "bar": "foo"})
'foo=bar&bar=foo'

(no need to do all of this ourselves)

Copy link
Author

Choose a reason for hiding this comment

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

Good catch ! Will fix in next commit

@lelithium
Copy link
Author

Hey @machine424 , thanks for the update here !

I understand the need for some kind of testing. FYI, this particular version of the code I've been using in a staging environment for about a week, and I haven't encountered any issues with it

@machine424
Copy link
Owner

Hello @lelithium

I've just merged 8bddc0d adding the tests and their utils.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants