github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/rest/delete.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rest
    18  
    19  import (
    20  	"time"
    21  
    22  	"k8s.io/kubernetes/pkg/api"
    23  	"k8s.io/kubernetes/pkg/api/unversioned"
    24  	"k8s.io/kubernetes/pkg/runtime"
    25  )
    26  
    27  // RESTDeleteStrategy defines deletion behavior on an object that follows Kubernetes
    28  // API conventions.
    29  type RESTDeleteStrategy interface {
    30  	runtime.ObjectTyper
    31  
    32  	// CheckGracefulDelete should return true if the object can be gracefully deleted and set
    33  	// any default values on the DeleteOptions.
    34  	CheckGracefulDelete(obj runtime.Object, options *api.DeleteOptions) bool
    35  }
    36  
    37  // BeforeDelete tests whether the object can be gracefully deleted. If graceful is set the object
    38  // should be gracefully deleted, if gracefulPending is set the object has already been gracefully deleted
    39  // (and the provided grace period is longer than the time to deletion), and an error is returned if the
    40  // condition cannot be checked or the gracePeriodSeconds is invalid. The options argument may be updated with
    41  // default values if graceful is true.
    42  func BeforeDelete(strategy RESTDeleteStrategy, ctx api.Context, obj runtime.Object, options *api.DeleteOptions) (graceful, gracefulPending bool, err error) {
    43  	if strategy == nil {
    44  		return false, false, nil
    45  	}
    46  	objectMeta, _, kerr := objectMetaAndKind(strategy, obj)
    47  	if kerr != nil {
    48  		return false, false, kerr
    49  	}
    50  
    51  	// if the object is already being deleted
    52  	if objectMeta.DeletionTimestamp != nil {
    53  		// if we are already being deleted, we may only shorten the deletion grace period
    54  		// this means the object was gracefully deleted previously but deletionGracePeriodSeconds was not set,
    55  		// so we force deletion immediately
    56  		if objectMeta.DeletionGracePeriodSeconds == nil {
    57  			return false, false, nil
    58  		}
    59  		// only a shorter grace period may be provided by a user
    60  		if options.GracePeriodSeconds != nil {
    61  			period := int64(*options.GracePeriodSeconds)
    62  			if period > *objectMeta.DeletionGracePeriodSeconds {
    63  				return false, true, nil
    64  			}
    65  			now := unversioned.NewTime(unversioned.Now().Add(time.Second * time.Duration(*options.GracePeriodSeconds)))
    66  			objectMeta.DeletionTimestamp = &now
    67  			objectMeta.DeletionGracePeriodSeconds = &period
    68  			options.GracePeriodSeconds = &period
    69  			return true, false, nil
    70  		}
    71  		// graceful deletion is pending, do nothing
    72  		options.GracePeriodSeconds = objectMeta.DeletionGracePeriodSeconds
    73  		return false, true, nil
    74  	}
    75  
    76  	if !strategy.CheckGracefulDelete(obj, options) {
    77  		return false, false, nil
    78  	}
    79  	now := unversioned.NewTime(unversioned.Now().Add(time.Second * time.Duration(*options.GracePeriodSeconds)))
    80  	objectMeta.DeletionTimestamp = &now
    81  	objectMeta.DeletionGracePeriodSeconds = options.GracePeriodSeconds
    82  	return true, false, nil
    83  }