-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix for latestRevision routes may point to older revisions if latestCreated is not ready #5319
Merged
knative-prow-robot
merged 11 commits into
knative:master
from
taragu:bug-latestCreatedRevision-5285
Nov 8, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
607d266
Fix for latestRevision routes may point to older revisions if latestC…
f5ab443
Review
5bd93ab
Review consolidate code path
7ca7fb3
Consolidate and only use findAndSetLatestReadyRevision
c358a16
More review
28f613e
Revert "Consolidate and only use findAndSetLatestReadyRevision"
bcab3c3
Review
5afb61a
Revert "Revert "Consolidate and only use findAndSetLatestReadyRevision""
219f716
Fix start generation and sorting func
b6a22d0
Remove extra check
e3f6414
Remove unused var
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 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 |
---|---|---|
|
@@ -20,13 +20,16 @@ import ( | |
"context" | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"strconv" | ||
|
||
"go.uber.org/zap" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
"k8s.io/client-go/tools/cache" | ||
"knative.dev/pkg/controller" | ||
"knative.dev/pkg/logging" | ||
|
@@ -167,23 +170,14 @@ func (c *Reconciler) reconcile(ctx context.Context, config *v1alpha1.Configurati | |
|
||
case rc.Status == corev1.ConditionTrue: | ||
logger.Infof("Revision %q of configuration is ready", revName) | ||
|
||
created, ready := config.Status.LatestCreatedRevisionName, config.Status.LatestReadyRevisionName | ||
if ready == "" { | ||
if config.Status.LatestReadyRevisionName == "" { | ||
// Surface an event for the first revision becoming ready. | ||
c.Recorder.Event(config, corev1.EventTypeNormal, "ConfigurationReady", | ||
"Configuration becomes ready") | ||
} | ||
// Update the LatestReadyRevisionName and surface an event for the transition. | ||
config.Status.SetLatestReadyRevisionName(lcr.Name) | ||
if created != ready { | ||
c.Recorder.Eventf(config, corev1.EventTypeNormal, "LatestReadyUpdate", | ||
"LatestReadyRevisionName updated to %q", lcr.Name) | ||
} | ||
|
||
case rc.Status == corev1.ConditionFalse: | ||
logger.Infof("Revision %q of configuration has failed", revName) | ||
|
||
// TODO(mattmoor): Only emit the event the first time we see this. | ||
config.Status.MarkLatestCreatedFailed(lcr.Name, rc.Message) | ||
c.Recorder.Eventf(config, corev1.EventTypeWarning, "LatestCreatedFailed", | ||
|
@@ -193,9 +187,88 @@ func (c *Reconciler) reconcile(ctx context.Context, config *v1alpha1.Configurati | |
return fmt.Errorf("unrecognized condition status: %v on revision %q", rc.Status, revName) | ||
} | ||
|
||
if err = c.findAndSetLatestReadyRevision(config); err != nil { | ||
return fmt.Errorf("failed to find and set latest ready revision: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// findAndSetLatestReadyRevision finds the last ready revision and sets LatestReadyRevisionName to it. | ||
func (c *Reconciler) findAndSetLatestReadyRevision(config *v1alpha1.Configuration) error { | ||
sortedRevisions, err := c.getSortedCreatedRevisions(config) | ||
if err != nil { | ||
return err | ||
} | ||
for _, rev := range sortedRevisions { | ||
if rev.Status.IsReady() { | ||
old, new := config.Status.LatestReadyRevisionName, rev.Name | ||
config.Status.SetLatestReadyRevisionName(rev.Name) | ||
if old != new { | ||
c.Recorder.Eventf(config, corev1.EventTypeNormal, "LatestReadyUpdate", | ||
"LatestReadyRevisionName updated to %q", rev.Name) | ||
} | ||
return nil | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// getSortedCreatedRevisions returns the list of created revisions sorted in descending | ||
// generation order between the generation of the latest ready revision and config's generation (both inclusive). | ||
func (c *Reconciler) getSortedCreatedRevisions(config *v1alpha1.Configuration) ([]*v1alpha1.Revision, error) { | ||
lister := c.revisionLister.Revisions(config.Namespace) | ||
configSelector := labels.SelectorFromSet(map[string]string{ | ||
serving.ConfigurationLabelKey: config.Name, | ||
}) | ||
mattmoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if config.Status.LatestReadyRevisionName != "" { | ||
lrr, err := lister.Get(config.Status.LatestReadyRevisionName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
start := lrr.Generation | ||
var generations []string | ||
for i := start; i <= int64(config.Generation); i++ { | ||
generations = append(generations, strconv.FormatInt(i, 10)) | ||
} | ||
|
||
// Add an "In" filter so that the configurations we get back from List have generation | ||
// in range (config's latest ready generation, config's generation] | ||
generationKey := serving.ConfigurationGenerationLabelKey | ||
inReq, err := labels.NewRequirement(generationKey, | ||
selection.In, | ||
generations, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configSelector = configSelector.Add(*inReq) | ||
} | ||
|
||
list, err := lister.List(configSelector) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Return a sorted list with Generation in descending order | ||
if len(list) > 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this after Victor made this suggestion #5319 (comment) |
||
sort.Slice(list, func(i, j int) bool { | ||
// BYO name always be the first | ||
if config.Spec.Template.Name == list[i].Name { | ||
return true | ||
} | ||
if config.Spec.Template.Name == list[j].Name { | ||
return false | ||
} | ||
intI, errI := strconv.Atoi(list[i].Labels[serving.ConfigurationGenerationLabelKey]) | ||
intJ, errJ := strconv.Atoi(list[j].Labels[serving.ConfigurationGenerationLabelKey]) | ||
if errI != nil || errJ != nil { | ||
return true | ||
} | ||
return intI > intJ | ||
}) | ||
} | ||
return list, nil | ||
} | ||
|
||
// CheckNameAvailability checks that if the named Revision specified by the Configuration | ||
// is available (not found), exists (but matches), or exists with conflict (doesn't match). | ||
func CheckNameAvailability(config *v1alpha1.Configuration, lister listers.RevisionLister) (*v1alpha1.Revision, error) { | ||
|
This file contains 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
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.
Except for the
lcrReady
check, this is the same test as above, so this should be the only meaningful work we'd perform when not returning. Am I missing something?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.
I think part of the issue is that
SetLatestReadyRevisionName(..)
not only sets the LRR, but also marks configuration as ready. So there are cases where we want to callSetLatestReadyRevisionName
because we need to set the config to ready, but we don't really need to set the LRR because it's already the same as the current LRR.