Skip to content

fix: use %g instead of %f for autoscaling annotation values#3904

Open
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/autoscaling-annotation-format
Open

fix: use %g instead of %f for autoscaling annotation values#3904
Elvand-Lie wants to merge 1 commit into
knative:mainfrom
Elvand-Lie:fix/autoscaling-annotation-format

Conversation

@Elvand-Lie

@Elvand-Lie Elvand-Lie commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Description

setServiceOptions in pkg/knative/deployer.go uses fmt.Sprintf("%f", ...) to format autoscaling Target and Utilization annotation values. Go's %f verb produces 6 trailing decimal places by default (e.g., "100.000000"), which is not the expected format for Knative autoscaling annotations.

Changed %f to %g which strips trailing zeros (100.0 becomes "100", 70.5 becomes "70.5").

Changes

  • pkg/knative/deployer.go, lines 592 and 598: %f replaced with %g
  • pkg/knative/deployer_test.go: added TestSetServiceOptions_ScaleAnnotationFormat regression test

Verification

  • go vet ./pkg/knative: clean
  • go test ./pkg/knative/ -count=1: all 20 tests pass
  • go test ./pkg/knative/ -run TestSetServiceOptions_ScaleAnnotationFormat -count=1: pass

Fixes #3901

@knative-prow

knative-prow Bot commented Jun 17, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

Details 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 requested review from dsimansk and jrangelramos June 17, 2026 20:50
@knative-prow knative-prow Bot added size/M 🤖 PR changes 30-99 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels Jun 17, 2026
@knative-prow

knative-prow Bot commented Jun 17, 2026

Copy link
Copy Markdown

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative 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.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

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.

Details

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.

@Elvand-Lie

Copy link
Copy Markdown
Contributor Author

Verification Evidence

1. go vet: clean

$ go vet ./pkg/knative
# (no output, clean)

2. Before vs After

// BEFORE fix (%f):
fmt.Sprintf("%f", 100.0)  // produces "100.000000"
fmt.Sprintf("%f", 70.0)   // produces "70.000000"
fmt.Sprintf("%f", 70.5)   // produces "70.500000"

// AFTER fix (%g):
fmt.Sprintf("%g", 100.0)  // produces "100"
fmt.Sprintf("%g", 70.0)   // produces "70"
fmt.Sprintf("%g", 70.5)   // produces "70.5"

3. Regression test output

$ go test ./pkg/knative/ -run TestSetServiceOptions_ScaleAnnotationFormat -count=1 -v
=== RUN   TestSetServiceOptions_ScaleAnnotationFormat
--- PASS: TestSetServiceOptions_ScaleAnnotationFormat (0.00s)
PASS
ok  	knative.dev/func/pkg/knative	0.280s

4. Full package test suite, all pass

$ go test ./pkg/knative/ -count=1 -v
=== RUN   TestValidateKubeconfigFile
--- PASS: TestValidateKubeconfigFile (1.21s)
=== RUN   TestCheckPullPermissions
--- PASS: TestCheckPullPermissions (4.83s)
=== RUN   TestCheckPullPermissions_FunctionImagePullSecret
--- PASS: TestCheckPullPermissions_FunctionImagePullSecret (0.01s)
=== RUN   TestUpdateService_EnvsPropagated
--- PASS: TestUpdateService_EnvsPropagated (0.00s)
=== RUN   TestGenerateNewService_ResourceSetsPopulated
--- PASS: TestGenerateNewService_ResourceSetsPopulated (0.00s)
=== RUN   TestSetServiceOptions_ScaleAnnotationFormat
--- PASS: TestSetServiceOptions_ScaleAnnotationFormat (0.00s)
PASS
ok  	knative.dev/func/pkg/knative	8.327s

5. What the regression test checks

The new TestSetServiceOptions_ScaleAnnotationFormat test:

  • Creates fn.Options with Scale.Target = 100.0 and Scale.Utilization = 70.0
  • Calls setServiceOptions with a minimal RevisionTemplateSpec
  • Asserts autoscaling.knative.dev/target equals "100" (not "100.000000")
  • Asserts autoscaling.knative.dev/target-utilization-percentage equals "70" (not "70.000000")

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 51.73%. Comparing base (813791a) to head (740583b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3904      +/-   ##
==========================================
- Coverage   53.93%   51.73%   -2.20%     
==========================================
  Files         200      200              
  Lines       23681    23681              
==========================================
- Hits        12772    12252     -520     
- Misses       9674    10282     +608     
+ Partials     1235     1147      -88     
Flag Coverage Δ
e2e 33.95% <0.00%> (-0.01%) ⬇️
e2e go 29.36% <0.00%> (-0.02%) ⬇️
e2e node 25.71% <0.00%> (ø)
e2e python 29.71% <0.00%> (ø)
e2e quarkus 25.83% <0.00%> (ø)
e2e rust 25.30% <0.00%> (ø)
e2e springboot 23.97% <0.00%> (+0.04%) ⬆️
e2e typescript 25.82% <0.00%> (ø)
e2e-config-ci 26.90% <0.00%> (ø)
integration ?
unit macos-14 43.08% <100.00%> (+0.04%) ⬆️
unit macos-latest 43.08% <100.00%> (+0.04%) ⬆️
unit ubuntu-24.04-arm 43.39% <100.00%> (+0.04%) ⬆️
unit ubuntu-latest 43.94% <100.00%> (+0.04%) ⬆️
unit windows-latest 43.15% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

setServiceOptions used fmt.Sprintf(%%f, ...) to format autoscaling
Target and Utilization annotations, producing values like 100.000000
instead of 100. Knative autoscaler expects clean numeric strings.

Changed to %%g which strips trailing zeros (100.0 -> 100, 70.5 -> 70.5).

Also adds regression test TestSetServiceOptions_ScaleAnnotationFormat.

Fixes knative#3901
@Elvand-Lie Elvand-Lie force-pushed the fix/autoscaling-annotation-format branch from bc022b5 to 740583b Compare June 18, 2026 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/M 🤖 PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setServiceOptions: %f format produces invalid autoscaling annotation values with trailing zeros

1 participant