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

Apiserver lease garbage collector #95895

Merged
merged 4 commits into from
Nov 11, 2020

Conversation

roycaihw
Copy link
Member

@roycaihw roycaihw commented Oct 27, 2020

Implements KEP. Split off from #95222.

Does this PR introduce a user-facing change?:

kube-apiserver now deletes expired kube-apiserver Lease objects:
- The feature is under feature gate `APIServerIdentity`.
- A flag is added to kube-apiserver: `identity-lease-garbage-collection-check-period-seconds`

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/1965-kube-apiserver-identity/README.md

/sig api-machinery
/assign @caesarxuchao

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 27, 2020
@k8s-ci-robot k8s-ci-robot added area/apiserver area/test kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API sig/apps Categorizes an issue or PR as relevant to SIG Apps. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 27, 2020
@fejta-bot
Copy link

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

@fedebongio
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 27, 2020
@roycaihw roycaihw force-pushed the apiserver-lease-gc branch 5 times, most recently from 78533eb to 5d45512 Compare November 4, 2020 22:45
@lavalamp
Copy link
Member

lavalamp commented Nov 6, 2020

Actually, even in that case. I don't think the identity system should have external-to-apiserver dependencies.

@roycaihw
Copy link
Member Author

roycaihw commented Nov 9, 2020

It only manages apiserver leases. The reason for running the GC in KCM was that nothing in KAS today runs leader election-- but that's totally achievable.

Leases of apiservers other than KAS:

  1. aggregated server: aggregated servers always require KAS to exist
  2. extensions server: extensions server running standalone is a non-goal, and it won't work with KCM today. If we ever want to do that, it would be a beta/GA requirement.

I like the idea that identity system should have external-to-apiserver dependencies. I will update the PR to do that.

@roycaihw
Copy link
Member Author

roycaihw commented Nov 9, 2020

nothing in KAS today runs leader election

Actually I won't add leader election to KAS. This GC has no retry logic, since all it does is a few GET and DELETE every hour. Adding leader election to KAS will cost more leader election traffic.

@lavalamp
Copy link
Member

lavalamp commented Nov 9, 2020

Yeah we shouldn't need leader election for this, it is (or should be) safe to be run multiple times concurrently.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Nov 9, 2020
leaseInformer := informers.NewFilteredLeaseInformer(
clientset,
metav1.NamespaceSystem,
12*time.Hour,
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to resync?

Copy link
Member Author

Choose a reason for hiding this comment

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

no. Changed to 0

}
selector = selector.Add(*r)

leases, err := c.leaseLister.Leases(metav1.NamespaceSystem).List(selector)
Copy link
Member

Choose a reason for hiding this comment

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

The informer has already filtered with the labelselector, do you need the selector here?

Copy link
Member Author

Choose a reason for hiding this comment

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

removed the selector and added test cases to make sure we don't GC leases that we don't intend to manage

continue
}
if errors.IsNotFound(err) || lease == nil {
continue
Copy link
Member

Choose a reason for hiding this comment

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

Is there any component other than this GC expected to delete a lease? If not, let's log a warning.

Copy link
Member Author

Choose a reason for hiding this comment

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

multiple GC controllers can delete the lease simultaneously. Added comments

// Copied from controlplane.IdentityLeaseComponentLabelKey and controlplane.KubeAPIServer
// to avoid import cycle.
identityLeaseComponentLabelKey = "k8s.io/component"
kubeAPIServer = "kube-apiserver"
Copy link
Member

Choose a reason for hiding this comment

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

Can we move them to a common package?

Copy link
Member Author

@roycaihw roycaihw Nov 10, 2020

Choose a reason for hiding this comment

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

made both the label selector and the namespace parameters to the gc controller. I think this way better reflects what the controller is capable of-- it operates on Leases in one namespace with a specific label set. Not saying we will make the code public/reusable, but we could if we want in future.

test/integration/master/apiserver_identity_test.go Outdated Show resolved Hide resolved
@spiffxp
Copy link
Member

spiffxp commented Nov 10, 2020

possible github issue, please link if you run into this on other PR's kubernetes/test-infra#19910

@roycaihw
Copy link
Member Author

@spiffxp Got it. Thanks

@@ -117,6 +118,7 @@ func NewServerRunOptions() *ServerRunOptions {
EndpointReconcilerType: string(reconcilers.LeaseEndpointReconcilerType),
IdentityLeaseDurationSeconds: 3600,
IdentityLeaseRenewIntervalSeconds: 10,
IdentityLeaseGCCheckPeriodSeconds: 3600,
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be separate from IdentityLeaseDurationSeconds?

Copy link
Member Author

@roycaihw roycaihw Nov 10, 2020

Choose a reason for hiding this comment

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

good point. If we know all leases have the same duration (which is the case here), there is no point to configure the check period differently. Reverted

the knob made sense when/if:

  1. the controller lived in KCM-- not true anymore
  2. we want the controller to manage leases from aggregated servers (with different duration)-- the more I think about it, aggregated servers shouldn't rely on this GC controller:
    a. they don't need heartbeat-based identity at all-- downward API plus owner reference is more efficient
    b. if they really want, they can start a similar GC controller that operates their leases in a user namespace

Copy link
Member Author

Choose a reason for hiding this comment

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

(just adding some thoughts why we don't need a separate flag) another benefit of having this flag is to make the GC more responsive-- the controller deletes a lease if the apiserver failed to refresh its lease in:

  • (leaseDuration, leaseDuration+checkPeriod] seconds

instead of (without the flag)

  • (leaseDuration, 2*leaseDuration] seconds

but that is not our goal. We prefer that expired Leases remain for a longer duration as opposed to collecting them quickly. That's why we choice a super long leaseDuration by default (1h, compared with 10s renewInterval). If people want to GC leases sooner after an apiserver is not active, they should start with shortening the leaseDuration. Having a separate flag for this level of control seems like an overkill for an alpha feature.

if err := c.kubeclientset.CoordinationV1().Leases(c.leaseNamespace).Delete(context.TODO(), lease.Name, metav1.DeleteOptions{}); err != nil && !errors.IsNotFound(err) {
// If we get a 404, the lease was deleted by the same GC controller
// in another apiserver
if !errors.IsNotFound(err) {
Copy link
Member

Choose a reason for hiding this comment

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

It's probably not needed to call IsNotFound twice :)

Copy link
Member Author

Choose a reason for hiding this comment

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

now I see why @caesarxuchao asks me to break the long lines.. this is what happens :)

@lavalamp
Copy link
Member

/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 Nov 10, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: lavalamp, roycaihw

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 Nov 10, 2020
Copy link
Member

@caesarxuchao caesarxuchao left a comment

Choose a reason for hiding this comment

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

One nit. Otherwise lgtm.

/lgtm

continue
}
if errors.IsNotFound(err) || lease == nil {
// the lease was deleted by the same GC controller in another apiserver
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you state this could be the case if it's an HA-master? Also state that we don't expect other components to delete the lease?

Copy link
Member

Choose a reason for hiding this comment

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

Can you add a klog.V(4)?

Copy link
Member Author

@roycaihw roycaihw Nov 10, 2020

Choose a reason for hiding this comment

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

done. Also squashed the commits

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 10, 2020
@caesarxuchao
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 10, 2020
@k8s-ci-robot k8s-ci-robot merged commit f102cc8 into kubernetes:master Nov 11, 2020
@k8s-ci-robot k8s-ci-robot added this to the v1.20 milestone Nov 11, 2020
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. area/apiserver area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API 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. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/apps Categorizes an issue or PR as relevant to SIG Apps. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants