Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"knative.dev/serving/pkg/reconciler/route"
"knative.dev/serving/pkg/reconciler/serverlessservice"
"knative.dev/serving/pkg/reconciler/service"
"knative.dev/serving/pkg/reconciler/gc"

// This defines the shared main for injected controllers.
"knative.dev/pkg/injection/sharedmain"
Expand All @@ -37,5 +38,6 @@ func main() {
route.NewController,
serverlessservice.NewController,
service.NewController,
gc.NewController,
)
}
2 changes: 1 addition & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ${GOPATH}/bin/deepcopy-gen \
-i knative.dev/serving/pkg/apis/config \
-i knative.dev/serving/pkg/reconciler/ingress/config \
-i knative.dev/serving/pkg/reconciler/certificate/config \
-i knative.dev/serving/pkg/reconciler/configuration/config \
-i knative.dev/serving/pkg/reconciler/gc/config \
-i knative.dev/serving/pkg/reconciler/revision/config \
-i knative.dev/serving/pkg/reconciler/route/config \
-i knative.dev/serving/pkg/tracing/config \
Expand Down
76 changes: 1 addition & 75 deletions pkg/reconciler/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"context"
"fmt"
"reflect"
"sort"
"time"

"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
Expand All @@ -37,7 +35,6 @@ import (
"knative.dev/serving/pkg/apis/serving/v1beta1"
listers "knative.dev/serving/pkg/client/listers/serving/v1alpha1"
"knative.dev/serving/pkg/reconciler"
configns "knative.dev/serving/pkg/reconciler/configuration/config"
"knative.dev/serving/pkg/reconciler/configuration/resources"
)

Expand Down Expand Up @@ -67,8 +64,6 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
}
logger := logging.FromContext(ctx)

ctx = c.configStore.ToContext(ctx)

// Get the Configuration resource with this namespace/name.
original, err := c.configurationLister.Configurations(namespace).Get(name)
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -203,7 +198,7 @@ func (c *Reconciler) reconcile(ctx context.Context, config *v1alpha1.Configurati
return err
}

return c.gcRevisions(ctx, config)
return nil
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think there's a bunch more code you can blast from orbit because config-gc is the only configmap consumed by this controller:

gc.ConfigName: gc.NewConfigFromConfigMapFunc(logger, minRevisionTimeout),

If you do it here, then the same logic for the GC controller will show up as moves and be easier to review

}

// CheckNameAvailability checks that if the named Revision specified by the Configuration
Expand Down Expand Up @@ -292,72 +287,3 @@ func (c *Reconciler) updateStatus(desired *v1alpha1.Configuration) (*v1alpha1.Co
existing.Status = desired.Status
return c.ServingClientSet.ServingV1alpha1().Configurations(desired.Namespace).UpdateStatus(existing)
}

func (c *Reconciler) gcRevisions(ctx context.Context, config *v1alpha1.Configuration) error {
cfg := configns.FromContext(ctx).RevisionGC
logger := logging.FromContext(ctx)

selector := labels.Set{serving.ConfigurationLabelKey: config.Name}.AsSelector()
revs, err := c.revisionLister.Revisions(config.Namespace).List(selector)
if err != nil {
return err
}

gcSkipOffset := cfg.StaleRevisionMinimumGenerations

if gcSkipOffset >= int64(len(revs)) {
return nil
}

// Sort by creation timestamp descending
sort.Slice(revs, func(i, j int) bool {
return revs[j].CreationTimestamp.Before(&revs[i].CreationTimestamp)
})

for _, rev := range revs[gcSkipOffset:] {
if isRevisionStale(ctx, rev, config) {
err := c.ServingClientSet.ServingV1alpha1().Revisions(rev.Namespace).Delete(rev.Name, &metav1.DeleteOptions{})
if err != nil {
logger.Errorf("Failed to delete stale revision: %v", err)
return err
}
}
}
return nil
}

func isRevisionStale(ctx context.Context, rev *v1alpha1.Revision, config *v1alpha1.Configuration) bool {
cfg := configns.FromContext(ctx).RevisionGC
logger := logging.FromContext(ctx)

if config.Status.LatestReadyRevisionName == rev.Name {
return false
}

curTime := time.Now()
if rev.ObjectMeta.CreationTimestamp.Add(cfg.StaleRevisionCreateDelay).After(curTime) {
// Revision was created sooner than staleRevisionCreateDelay. Ignore it.
return false
}

lastPin, err := rev.GetLastPinned()
if err != nil {
if err.(v1alpha1.LastPinnedParseError).Type != v1alpha1.AnnotationParseErrorTypeMissing {
logger.Errorf("Failed to determine revision last pinned: %v", err)
} else {
// Revision was never pinned and its RevisionConditionReady is not true after staleRevisionCreateDelay.
// It usually happens when ksvc was deployed with wrong configuration.
rc := rev.Status.GetCondition(v1beta1.RevisionConditionReady)
if rc == nil || rc.Status != corev1.ConditionTrue {
return true
}
}
return false
}

ret := lastPin.Add(cfg.StaleRevisionTimeout).Before(curTime)
if ret {
logger.Infof("Detected stale revision %v with creation time %v and lastPinned time %v.", rev.ObjectMeta.Name, rev.ObjectMeta.CreationTimestamp, lastPin)
}
return ret
}
Loading