Repository navigation
fix(controllers): keep reconciling when trafficDistribution is unsupported #378
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
Open
Andrei Kvapil (kvaps)
wants to merge
3
commits into
main
Choose a base branch
from
fix/client-service-traffic-distribution-compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| Copyright 2023 Timofey Larkin. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/types" | ||
|
|
||
| lll "github.com/cozystack/etcd-operator/api/v1alpha2" | ||
| ) | ||
|
|
||
| const ( | ||
| // EventReasonTrafficDistributionRejected is emitted when the apiserver | ||
| // refuses spec.trafficDistribution on the client Service, e.g. | ||
| // PreferSameZone / PreferSameNode before Kubernetes 1.34. | ||
| EventReasonTrafficDistributionRejected = "TrafficDistributionRejected" | ||
| // EventReasonTrafficDistributionNotPersisted is emitted when the | ||
| // apiserver accepts the write but drops the field, which is what an | ||
| // apiserver that does not know spec.trafficDistribution does. | ||
| EventReasonTrafficDistributionNotPersisted = "TrafficDistributionNotPersisted" | ||
|
|
||
| // trafficDistributionRetryInterval is how long a value the apiserver | ||
| // refused or dropped is left off the client Service before the | ||
| // operator tries it again. It bounds the cost of an unsupported value | ||
| // to one failed write per interval, and lets the value land on its own | ||
| // once the apiserver is upgraded. | ||
| trafficDistributionRetryInterval = time.Hour | ||
|
|
||
| trafficDistributionField = "spec.trafficDistribution" | ||
| ) | ||
|
|
||
| // trafficDistributionBackoff remembers, per client Service, a | ||
| // trafficDistribution value the apiserver would not keep, so the operator | ||
| // stops writing it on every reconcile. It is in-memory only: after an | ||
| // operator restart each unsupported value costs one more failed write. | ||
| type trafficDistributionBackoff struct { | ||
| mu sync.Mutex | ||
| entries map[types.NamespacedName]trafficDistributionBackoffEntry | ||
| } | ||
|
|
||
| type trafficDistributionBackoffEntry struct { | ||
| value string | ||
| until time.Time | ||
| } | ||
|
|
||
| // suppressed reports whether value is still backed off for key. An entry | ||
| // for a different value (the spec changed) or an expired one does not | ||
| // count and is forgotten. | ||
| func (b *trafficDistributionBackoff) suppressed(key types.NamespacedName, value string, now time.Time) bool { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| e, ok := b.entries[key] | ||
| if !ok { | ||
| return false | ||
| } | ||
| if e.value != value || !now.Before(e.until) { | ||
| delete(b.entries, key) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func (b *trafficDistributionBackoff) suppress(key types.NamespacedName, value string, now time.Time) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| if b.entries == nil { | ||
| b.entries = map[types.NamespacedName]trafficDistributionBackoffEntry{} | ||
| } | ||
| b.entries[key] = trafficDistributionBackoffEntry{value: value, until: now.Add(trafficDistributionRetryInterval)} | ||
| } | ||
|
|
||
| // isTrafficDistributionRejected reports whether err is the apiserver | ||
| // refusing spec.trafficDistribution. Other validation failures on the | ||
| // Service are not matched: they are not the operator's to work around. | ||
| func isTrafficDistributionRejected(err error) bool { | ||
| if !errors.IsInvalid(err) { | ||
| return false | ||
| } | ||
| status, ok := err.(errors.APIStatus) | ||
| if !ok || status.Status().Details == nil { | ||
| return false | ||
| } | ||
| causes := status.Status().Details.Causes | ||
| if len(causes) == 0 { | ||
| return false | ||
| } | ||
| for _, c := range causes { | ||
| if c.Field != trafficDistributionField { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // clientServiceTrafficDistribution returns the trafficDistribution the | ||
| // client Service should carry this reconcile: the spec's value, unless that | ||
| // value is backed off. | ||
| func (r *EtcdClusterReconciler) clientServiceTrafficDistribution(cluster *lll.EtcdCluster, key types.NamespacedName) *string { | ||
| cs := cluster.Spec.ClientService | ||
| if cs == nil || cs.TrafficDistribution == nil { | ||
| return nil | ||
| } | ||
| if r.tdBackoff.suppressed(key, *cs.TrafficDistribution, r.now()) { | ||
| return nil | ||
| } | ||
| td := *cs.TrafficDistribution | ||
| return &td | ||
| } | ||
|
|
||
| // trafficDistributionPersisted reports whether the Service returned by the | ||
| // apiserver carries the value the operator wrote. | ||
| func trafficDistributionPersisted(svc *corev1.Service, want string) bool { | ||
| return svc.Spec.TrafficDistribution != nil && *svc.Spec.TrafficDistribution == want | ||
| } | ||
|
|
||
| func (r *EtcdClusterReconciler) now() time.Time { | ||
| if r.Now != nil { | ||
| return r.Now() | ||
| } | ||
| return time.Now() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: cozystack/etcd-operator
Length of output: 5441
🏁 Script executed:
Repository: cozystack/etcd-operator
Length of output: 41811
🏁 Script executed:
Repository: cozystack/etcd-operator
Length of output: 26884
🏁 Script executed:
Repository: cozystack/etcd-operator
Length of output: 5299
Clear the trafficDistribution backoff when the spec is unset.
After an unsupported value is rejected, removing
spec.clientService.trafficDistributionleaves the backoff entry active. Restoring the same value before the one-hour expiry then suppresses it, so the value is not retried on the next reconcile. Clear the entry when the configuration is absent.Suggested fix
🤖 Prompt for AI Agents