1
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
38 type JobStorage struct {
39 Job *REST
40 Status *StatusREST
41 }
42
43
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
60 type REST struct {
61 *genericregistry.Store
62 }
63
64
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
93 var _ rest.CategoriesProvider = &REST{}
94
95
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
102
103 if options != nil && options.PropagationPolicy == nil && options.OrphanDependents == nil &&
104 job.Strategy.DefaultGarbageCollectionPolicy(ctx) == rest.OrphanDependents {
105
106
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
114 if deleteOptions.PropagationPolicy == nil && deleteOptions.OrphanDependents == nil &&
115 job.Strategy.DefaultGarbageCollectionPolicy(ctx) == rest.OrphanDependents {
116
117
118 warning.AddWarning(ctx, "", deleteOptionWarnings)
119 }
120 return r.Store.DeleteCollection(ctx, deleteValidation, deleteOptions, listOptions)
121 }
122
123
124 type StatusREST struct {
125 store *genericregistry.Store
126 }
127
128
129 func (r *StatusREST) New() runtime.Object {
130 return &batch.Job{}
131 }
132
133
134 func (r *StatusREST) Destroy() {
135
136
137 }
138
139
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
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
147
148 return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
149 }
150
151
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