Skip to content

Prevent running two HPA cachers for different API versions #132924

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

Merged
merged 1 commit into from
Jul 16, 2025

Conversation

serathius
Copy link
Contributor

/kind regression

NONE

Reuse single REST for different HPA versions, reducing resource usage.
Allocating REST is not free as if watch cache is enabled, each will LIST and WATCH etcd independently and cache all objects in memory.

/assign @jpbetz

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. kind/regression Categorizes issue or PR as related to a regression from a prior release. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 14, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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.

@k8s-ci-robot k8s-ci-robot added the needs-priority Indicates a PR lacks a `priority/foo` label and requires one. label Jul 14, 2025
@k8s-ci-robot k8s-ci-robot requested review from deads2k and soltysh July 14, 2025 10:17
@serathius
Copy link
Contributor Author

/cc @deads2k

Copy link
Contributor

@jpbetz jpbetz left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 14, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: f2421c7febc6ee4a403d8957149ab3f589a9f8cd

@serathius
Copy link
Contributor Author

/assign @smarterclayton

@serathius
Copy link
Contributor Author

/cc @liggitt

@k8s-ci-robot k8s-ci-robot requested a review from liggitt July 15, 2025 07:30
@liggitt
Copy link
Member

liggitt commented Jul 15, 2025

I don't think this is a regression, this has always been this way, right?

@serathius
Copy link
Contributor Author

I don't think this is a regression, this has always been this way, right?

Right, it dates to introduction of v2 horizontalpodautoscalers API in K8s 1.6.

/kind feature
/remove-kind regression

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. and removed kind/regression Categorizes issue or PR as related to a regression from a prior release. labels Jul 15, 2025
Comment on lines 38 to 48
if storageMap, err := p.v2Storage(apiResourceConfigSource, restOptionsGetter); err != nil {
return genericapiserver.APIGroupInfo{}, err
} else if len(storageMap) > 0 {
apiGroupInfo.VersionedResourcesStorageMap[autoscalingapiv2.SchemeGroupVersion.Version] = storageMap
}

if storageMap, err := p.v1Storage(apiResourceConfigSource, restOptionsGetter); err != nil {
return genericapiserver.APIGroupInfo{}, err
} else if len(storageMap) > 0 {
apiGroupInfo.VersionedResourcesStorageMap[autoscalingapiv1.SchemeGroupVersion.Version] = storageMap
}
Copy link
Member

Choose a reason for hiding this comment

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

we really want the API group/version setup to be isolated / independent... combining these two into a single function makes it easier to accidentally reuse shared variables incorrectly. If the thing we want to accomplish is only constructing HPA storage once, let's structure the code to accomplish that without losing isolated version setup:

var (
	hpaOnce sync.Once
	hpaStorage *REST
	hpaStatusStorage *StatusREST
	hpaStorageErr error
)
hpaStorageGetter := func() (*REST, *StatusREST, error) {
  hpaOnce.Do(func(){
    hpaStorage, hpaStatusStorage, hpaStorageErr := horizontalpodautoscalerstore.NewREST(restOptionsGetter)
  })
  return hpaStorage, hpaStatusStorage, hpaStorageErr
}

if storageMap, err := p.v2Storage(apiResourceConfigSource, hpaStorageGetter); err != nil {
	return genericapiserver.APIGroupInfo{}, err
} else if len(storageMap) > 0 {
	apiGroupInfo.VersionedResourcesStorageMap[autoscalingapiv2.SchemeGroupVersion.Version] = storageMap
}

if storageMap, err := p.v1Storage(apiResourceConfigSource, hpaStorageGetter); err != nil {
	return genericapiserver.APIGroupInfo{}, err
} else if len(storageMap) > 0 {
	apiGroupInfo.VersionedResourcesStorageMap[autoscalingapiv1.SchemeGroupVersion.Version] = storageMap
}

Copy link
Member

@liggitt liggitt Jul 15, 2025

Choose a reason for hiding this comment

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

the existing code is also ~identical to the versioned setup code used for ~every other API group currently ... this isn't an issue isolated to HPA, it just happens to only manifest with default config for HPA because it's the only thing that has two GA API versions of the same group

it would be ideal to fix this systemically and consistently so we don't have the same issue everywhere

edit: actually, I think events has the same issue in default configs, constructing twice to serve via events/v1 and core/v1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I didn't notice the events as it doesn't have watch cache enabled. I noticed that we create 2 cachers for serviceaccounts but we shutdown one of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Jul 15, 2025
@k8s-ci-robot k8s-ci-robot requested a review from jpbetz July 15, 2025 14:17
Comment on lines 42 to 44
var hpaStorage *horizontalpodautoscalerstore.REST
var hpaStatusStorage *horizontalpodautoscalerstore.StatusREST
var hpaStorageErr error
Copy link
Member

Choose a reason for hiding this comment

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

Making these local means that on subsequent invocations, this function will return nil, nil, nil. These variables have to live outside the closure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right

@@ -50,12 +63,12 @@ func (p RESTStorageProvider) NewRESTStorage(apiResourceConfigSource serverstorag
return apiGroupInfo, nil
}

func (p RESTStorageProvider) v1Storage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) (map[string]rest.Storage, error) {
func (p RESTStorageProvider) v1Storage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter hpaStorageGetter) (map[string]rest.Storage, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (p RESTStorageProvider) v1Storage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter hpaStorageGetter) (map[string]rest.Storage, error) {
func (p RESTStorageProvider) v1Storage(apiResourceConfigSource serverstorage.APIResourceConfigSource, storageGetter hpaStorageGetter) (map[string]rest.Storage, error) {

@liggitt
Copy link
Member

liggitt commented Jul 15, 2025

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 15, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 42dee923e5c40eeeee1d8c312c0dd493baa9837a

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jpbetz, liggitt, serathius

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 15, 2025
@pacoxu
Copy link
Member

pacoxu commented Jul 16, 2025

/sig autoscaling api-machinery

@k8s-ci-robot k8s-ci-robot added sig/autoscaling Categorizes an issue or PR as relevant to SIG Autoscaling. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jul 16, 2025
@k8s-ci-robot k8s-ci-robot merged commit b77dd78 into kubernetes:master Jul 16, 2025
13 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.34 milestone Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/autoscaling Categorizes an issue or PR as relevant to SIG Autoscaling. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants