...

Source file src/k8s.io/kubernetes/pkg/registry/batch/job/storage/storage.go

Documentation: k8s.io/kubernetes/pkg/registry/batch/job/storage

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     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 storage
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/apimachinery/pkg/apis/meta/internalversion"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apiserver/pkg/registry/generic"
    26  	genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
    27  	"k8s.io/apiserver/pkg/registry/rest"
    28  	"k8s.io/apiserver/pkg/warning"
    29  	"k8s.io/kubernetes/pkg/apis/batch"
    30  	"k8s.io/kubernetes/pkg/printers"
    31  	printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
    32  	printerstorage "k8s.io/kubernetes/pkg/printers/storage"
    33  	"k8s.io/kubernetes/pkg/registry/batch/job"
    34  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    35  )
    36  
    37  // JobStorage includes dummy storage for Job.
    38  type JobStorage struct {
    39  	Job    *REST
    40  	Status *StatusREST
    41  }
    42  
    43  // NewStorage creates a new JobStorage against etcd.
    44  func NewStorage(optsGetter generic.RESTOptionsGetter) (JobStorage, error) {
    45  	jobRest, jobStatusRest, err := NewREST(optsGetter)
    46  	if err != nil {
    47  		return JobStorage{}, err
    48  	}
    49  
    50  	return JobStorage{
    51  		Job:    jobRest,
    52  		Status: jobStatusRest,
    53  	}, nil
    54  }
    55  
    56  var deleteOptionWarnings = "child pods are preserved by default when jobs are deleted; " +
    57  	"set propagationPolicy=Background to remove them or set propagationPolicy=Orphan to suppress this warning"
    58  
    59  // REST implements a RESTStorage for jobs against etcd
    60  type REST struct {
    61  	*genericregistry.Store
    62  }
    63  
    64  // NewREST returns a RESTStorage object that will work against Jobs.
    65  func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) {
    66  	store := &genericregistry.Store{
    67  		NewFunc:                   func() runtime.Object { return &batch.Job{} },
    68  		NewListFunc:               func() runtime.Object { return &batch.JobList{} },
    69  		PredicateFunc:             job.MatchJob,
    70  		DefaultQualifiedResource:  batch.Resource("jobs"),
    71  		SingularQualifiedResource: batch.Resource("job"),
    72  
    73  		CreateStrategy:      job.Strategy,
    74  		UpdateStrategy:      job.Strategy,
    75  		DeleteStrategy:      job.Strategy,
    76  		ResetFieldsStrategy: job.Strategy,
    77  
    78  		TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
    79  	}
    80  	options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: job.GetAttrs}
    81  	if err := store.CompleteWithOptions(options); err != nil {
    82  		return nil, nil, err
    83  	}
    84  
    85  	statusStore := *store
    86  	statusStore.UpdateStrategy = job.StatusStrategy
    87  	statusStore.ResetFieldsStrategy = job.StatusStrategy
    88  
    89  	return &REST{store}, &StatusREST{store: &statusStore}, nil
    90  }
    91  
    92  // Implement CategoriesProvider
    93  var _ rest.CategoriesProvider = &REST{}
    94  
    95  // Categories implements the CategoriesProvider interface. Returns a list of categories a resource is part of.
    96  func (r *REST) Categories() []string {
    97  	return []string{"all"}
    98  }
    99  
   100  func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
   101  	//nolint:staticcheck // SA1019 backwards compatibility
   102  	//nolint: staticcheck
   103  	if options != nil && options.PropagationPolicy == nil && options.OrphanDependents == nil &&
   104  		job.Strategy.DefaultGarbageCollectionPolicy(ctx) == rest.OrphanDependents {
   105  		// Throw a warning if delete options are not explicitly set as Job deletion strategy by default is orphaning
   106  		// pods in v1.
   107  		warning.AddWarning(ctx, "", deleteOptionWarnings)
   108  	}
   109  	return r.Store.Delete(ctx, name, deleteValidation, options)
   110  }
   111  
   112  func (r *REST) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, deleteOptions *metav1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) {
   113  	//nolint:staticcheck // SA1019 backwards compatibility
   114  	if deleteOptions.PropagationPolicy == nil && deleteOptions.OrphanDependents == nil &&
   115  		job.Strategy.DefaultGarbageCollectionPolicy(ctx) == rest.OrphanDependents {
   116  		// Throw a warning if delete options are not explicitly set as Job deletion strategy by default is orphaning
   117  		// pods in v1.
   118  		warning.AddWarning(ctx, "", deleteOptionWarnings)
   119  	}
   120  	return r.Store.DeleteCollection(ctx, deleteValidation, deleteOptions, listOptions)
   121  }
   122  
   123  // StatusREST implements the REST endpoint for changing the status of a resourcequota.
   124  type StatusREST struct {
   125  	store *genericregistry.Store
   126  }
   127  
   128  // New creates a new Job object.
   129  func (r *StatusREST) New() runtime.Object {
   130  	return &batch.Job{}
   131  }
   132  
   133  // Destroy cleans up resources on shutdown.
   134  func (r *StatusREST) Destroy() {
   135  	// Given that underlying store is shared with REST,
   136  	// we don't destroy it here explicitly.
   137  }
   138  
   139  // Get retrieves the object from the storage. It is required to support Patch.
   140  func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
   141  	return r.store.Get(ctx, name, options)
   142  }
   143  
   144  // Update alters the status subset of an object.
   145  func (r *StatusREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
   146  	// We are explicitly setting forceAllowCreate to false in the call to the underlying storage because
   147  	// subresources should never allow create on update.
   148  	return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
   149  }
   150  
   151  // GetResetFields implements rest.ResetFieldsStrategy
   152  func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
   153  	return r.store.GetResetFields()
   154  }
   155  
   156  func (r *StatusREST) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
   157  	return r.store.ConvertToTable(ctx, object, tableOptions)
   158  }
   159  

View as plain text