Skip to content

Commit

Permalink
Ignore resources when DeletionTimestamp is set. (#3036)
Browse files Browse the repository at this point in the history
When we had finalizers previously we would race with K8s GC to recreate our children as K8s reaped them.

The simplest way to test this is to enable "foreground" deletion in our e2e tests, which is implemented as a finalizer.

Fixes: #2678
  • Loading branch information
mattmoor authored and knative-prow-robot committed Jan 30, 2019
1 parent c822138 commit f133e07
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/reconciler/v1alpha1/autoscaling/hpa/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
func (c *Reconciler) reconcile(ctx context.Context, key string, pa *pav1alpha1.PodAutoscaler) error {
logger := logging.FromContext(ctx)

if pa.GetDeletionTimestamp() != nil {
return nil
}

// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
// in this getting written back to the API Server, but lets downstream logic make
Expand Down
7 changes: 7 additions & 0 deletions pkg/reconciler/v1alpha1/autoscaling/hpa/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func TestReconcile(t *testing.T) {
pa(testRevision, testNamespace, WithKPAClass),
},
Key: key(testRevision, testNamespace),
}, {
Name: "nop deletion reconcile",
// Test that with a DeletionTimestamp we do nothing.
Objects: []runtime.Object{
pa(testRevision, testNamespace, WithHPAClass, WithPADeletionTimestamp),
},
Key: key(testRevision, testNamespace),
}, {
Name: "delete when pa does not exist",
Objects: []runtime.Object{},
Expand Down
4 changes: 4 additions & 0 deletions pkg/reconciler/v1alpha1/autoscaling/kpa/kpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
func (c *Reconciler) reconcile(ctx context.Context, pa *pav1alpha1.PodAutoscaler) error {
logger := logging.FromContext(ctx)

if pa.GetDeletionTimestamp() != nil {
return nil
}

// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
// in this getting written back to the API Server, but lets downstream logic make
Expand Down
3 changes: 3 additions & 0 deletions pkg/reconciler/v1alpha1/clusteringress/clusteringress.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func (c *Reconciler) updateStatus(desired *v1alpha1.ClusterIngress) (*v1alpha1.C

func (c *Reconciler) reconcile(ctx context.Context, ci *v1alpha1.ClusterIngress) error {
logger := logging.FromContext(ctx)
if ci.GetDeletionTimestamp() != nil {
return nil
}

// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
Expand Down
3 changes: 3 additions & 0 deletions pkg/reconciler/v1alpha1/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {

func (c *Reconciler) reconcile(ctx context.Context, config *v1alpha1.Configuration) error {
logger := logging.FromContext(ctx)
if config.GetDeletionTimestamp() != nil {
return nil
}

// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
Expand Down
7 changes: 7 additions & 0 deletions pkg/reconciler/v1alpha1/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func TestReconcile(t *testing.T) {
}, {
Name: "key not found",
Key: "foo/not-found",
}, {
Name: "nop deletion reconcile",
// Test that with a DeletionTimestamp we do nothing.
Objects: []runtime.Object{
cfg("foo", "delete-pending", 1234, WithConfigDeletionTimestamp),
},
Key: "foo/delete-pending",
}, {
Name: "create revision matching generation",
Objects: []runtime.Object{
Expand Down
3 changes: 3 additions & 0 deletions pkg/reconciler/v1alpha1/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ func (c *Reconciler) reconcileDigest(ctx context.Context, rev *v1alpha1.Revision

func (c *Reconciler) reconcile(ctx context.Context, rev *v1alpha1.Revision) error {
logger := commonlogging.FromContext(ctx)
if rev.GetDeletionTimestamp() != nil {
return nil
}

// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
Expand Down
7 changes: 7 additions & 0 deletions pkg/reconciler/v1alpha1/revision/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func TestReconcile(t *testing.T) {
Name: "key not found",
// Make sure Reconcile handles good keys that don't exist.
Key: "foo/not-found",
}, {
Name: "nop deletion reconcile",
// Test that with a DeletionTimestamp we do nothing.
Objects: []runtime.Object{
rev("foo", "delete-pending", WithRevisionDeletionTimestamp),
},
Key: "foo/delete-pending",
}, {
Name: "first revision reconciliation",
// Test the simplest successful reconciliation flow.
Expand Down
1 change: 0 additions & 1 deletion pkg/reconciler/v1alpha1/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {

func (c *Reconciler) reconcile(ctx context.Context, r *v1alpha1.Route) error {
logger := logging.FromContext(ctx)

if r.GetDeletionTimestamp() != nil {
// Check for a DeletionTimestamp. If present, elide the normal reconcile logic.
return c.reconcileDeletion(ctx, r)
Expand Down
3 changes: 3 additions & 0 deletions pkg/reconciler/v1alpha1/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {

func (c *Reconciler) reconcile(ctx context.Context, service *v1alpha1.Service) error {
logger := logging.FromContext(ctx)
if service.GetDeletionTimestamp() != nil {
return nil
}

// We may be reading a version of the object that was stored at an older version
// and may not have had all of the assumed defaults specified. This won't result
Expand Down
7 changes: 7 additions & 0 deletions pkg/reconciler/v1alpha1/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func TestReconcile(t *testing.T) {
}, {
Name: "key not found",
Key: "foo/not-found",
}, {
Name: "nop deletion reconcile",
// Test that with a DeletionTimestamp we do nothing.
Objects: []runtime.Object{
svc("delete-pending", "foo", WithServiceDeletionTimestamp),
},
Key: "foo/delete-pending",
}, {
Name: "incomplete service",
Objects: []runtime.Object{
Expand Down
24 changes: 24 additions & 0 deletions pkg/reconciler/v1alpha1/testing/functional.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ var (
}
)

// WithServiceDeletionTimestamp will set the DeletionTimestamp on the Service.
func WithServiceDeletionTimestamp(r *v1alpha1.Service) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}

// WithRunLatestRollout configures the Service to use a "runLatest" rollout.
func WithRunLatestRollout(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
Expand Down Expand Up @@ -422,6 +428,12 @@ func WithRouteLabel(key, value string) RouteOption {
// ConfigOption enables further configuration of a Configuration.
type ConfigOption func(*v1alpha1.Configuration)

// WithConfigDeletionTimestamp will set the DeletionTimestamp on the Config.
func WithConfigDeletionTimestamp(r *v1alpha1.Configuration) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}

// WithBuild adds a Build to the provided Configuration.
func WithBuild(cfg *v1alpha1.Configuration) {
cfg.Spec.Build = &v1alpha1.RawExtension{
Expand Down Expand Up @@ -517,6 +529,12 @@ func WithConfigLabel(key, value string) ConfigOption {
// RevisionOption enables further configuration of a Revision.
type RevisionOption func(*v1alpha1.Revision)

// WithRevisionDeletionTimestamp will set the DeletionTimestamp on the Revision.
func WithRevisionDeletionTimestamp(r *v1alpha1.Revision) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}

// WithInitRevConditions calls .Status.InitializeConditions() on a Revision.
func WithInitRevConditions(r *v1alpha1.Revision) {
r.Status.InitializeConditions()
Expand Down Expand Up @@ -722,6 +740,12 @@ func WithNoTraffic(reason, message string) PodAutoscalerOption {
}
}

// WithPADeletionTimestamp will set the DeletionTimestamp on the PodAutoscaler.
func WithPADeletionTimestamp(r *autoscalingv1alpha1.PodAutoscaler) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}

// WithHPAClass updates the PA to add the hpa class annotation.
func WithHPAClass(pa *autoscalingv1alpha1.PodAutoscaler) {
if pa.Annotations == nil {
Expand Down
7 changes: 6 additions & 1 deletion test/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func (clients *ServingClients) Delete(routes []string, configs []string, service
{clients.Services, services},
}

propPolicy := v1.DeletePropagationForeground
dopt := &v1.DeleteOptions{
PropagationPolicy: &propPolicy,
}

for _, deletion := range deletions {
if deletion.client == nil {
continue
Expand All @@ -141,7 +146,7 @@ func (clients *ServingClients) Delete(routes []string, configs []string, service
continue
}

if err := deletion.client.Delete(item, nil); err != nil {
if err := deletion.client.Delete(item, dopt); err != nil {
return err
}
}
Expand Down

0 comments on commit f133e07

Please sign in to comment.