storage.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storage
  14. import (
  15. "context"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apiserver/pkg/registry/generic"
  19. genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
  20. "k8s.io/apiserver/pkg/registry/rest"
  21. "k8s.io/kubernetes/pkg/apis/batch"
  22. "k8s.io/kubernetes/pkg/printers"
  23. printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
  24. printerstorage "k8s.io/kubernetes/pkg/printers/storage"
  25. "k8s.io/kubernetes/pkg/registry/batch/job"
  26. )
  27. // JobStorage includes dummy storage for Job.
  28. type JobStorage struct {
  29. Job *REST
  30. Status *StatusREST
  31. }
  32. // NewStorage creates a new JobStorage against etcd.
  33. func NewStorage(optsGetter generic.RESTOptionsGetter) (JobStorage, error) {
  34. jobRest, jobStatusRest, err := NewREST(optsGetter)
  35. if err != nil {
  36. return JobStorage{}, err
  37. }
  38. return JobStorage{
  39. Job: jobRest,
  40. Status: jobStatusRest,
  41. }, nil
  42. }
  43. // REST implements a RESTStorage for jobs against etcd
  44. type REST struct {
  45. *genericregistry.Store
  46. }
  47. // NewREST returns a RESTStorage object that will work against Jobs.
  48. func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) {
  49. store := &genericregistry.Store{
  50. NewFunc: func() runtime.Object { return &batch.Job{} },
  51. NewListFunc: func() runtime.Object { return &batch.JobList{} },
  52. PredicateFunc: job.MatchJob,
  53. DefaultQualifiedResource: batch.Resource("jobs"),
  54. CreateStrategy: job.Strategy,
  55. UpdateStrategy: job.Strategy,
  56. DeleteStrategy: job.Strategy,
  57. TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)},
  58. }
  59. options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: job.GetAttrs}
  60. if err := store.CompleteWithOptions(options); err != nil {
  61. return nil, nil, err
  62. }
  63. statusStore := *store
  64. statusStore.UpdateStrategy = job.StatusStrategy
  65. return &REST{store}, &StatusREST{store: &statusStore}, nil
  66. }
  67. // Implement CategoriesProvider
  68. var _ rest.CategoriesProvider = &REST{}
  69. // Categories implements the CategoriesProvider interface. Returns a list of categories a resource is part of.
  70. func (r *REST) Categories() []string {
  71. return []string{"all"}
  72. }
  73. // StatusREST implements the REST endpoint for changing the status of a resourcequota.
  74. type StatusREST struct {
  75. store *genericregistry.Store
  76. }
  77. // New creates a new Job object.
  78. func (r *StatusREST) New() runtime.Object {
  79. return &batch.Job{}
  80. }
  81. // Get retrieves the object from the storage. It is required to support Patch.
  82. func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  83. return r.store.Get(ctx, name, options)
  84. }
  85. // Update alters the status subset of an object.
  86. 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) {
  87. // We are explicitly setting forceAllowCreate to false in the call to the underlying storage because
  88. // subresources should never allow create on update.
  89. return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
  90. }