...
1
16
17 package podschedulingcontext
18
19 import (
20 "context"
21 "errors"
22
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/apimachinery/pkg/fields"
25 "k8s.io/apimachinery/pkg/labels"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/apimachinery/pkg/util/validation/field"
28 "k8s.io/apiserver/pkg/registry/generic"
29 "k8s.io/apiserver/pkg/storage"
30 "k8s.io/apiserver/pkg/storage/names"
31 "k8s.io/kubernetes/pkg/api/legacyscheme"
32 "k8s.io/kubernetes/pkg/apis/resource"
33 "k8s.io/kubernetes/pkg/apis/resource/validation"
34 "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
35 )
36
37
38 type podSchedulingStrategy struct {
39 runtime.ObjectTyper
40 names.NameGenerator
41 }
42
43
44
45 var Strategy = podSchedulingStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
46
47 func (podSchedulingStrategy) NamespaceScoped() bool {
48 return true
49 }
50
51
52
53
54 func (podSchedulingStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
55 fields := map[fieldpath.APIVersion]*fieldpath.Set{
56 "resource.k8s.io/v1alpha2": fieldpath.NewSet(
57 fieldpath.MakePathOrDie("status"),
58 ),
59 }
60
61 return fields
62 }
63
64 func (podSchedulingStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
65 scheduling := obj.(*resource.PodSchedulingContext)
66
67 scheduling.Status = resource.PodSchedulingContextStatus{}
68 }
69
70 func (podSchedulingStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
71 scheduling := obj.(*resource.PodSchedulingContext)
72 return validation.ValidatePodSchedulingContexts(scheduling)
73 }
74
75 func (podSchedulingStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
76 return nil
77 }
78
79 func (podSchedulingStrategy) Canonicalize(obj runtime.Object) {
80 }
81
82 func (podSchedulingStrategy) AllowCreateOnUpdate() bool {
83 return false
84 }
85
86 func (podSchedulingStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
87 newScheduling := obj.(*resource.PodSchedulingContext)
88 oldScheduling := old.(*resource.PodSchedulingContext)
89 newScheduling.Status = oldScheduling.Status
90 }
91
92 func (podSchedulingStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
93 newScheduling := obj.(*resource.PodSchedulingContext)
94 oldScheduling := old.(*resource.PodSchedulingContext)
95 errorList := validation.ValidatePodSchedulingContexts(newScheduling)
96 return append(errorList, validation.ValidatePodSchedulingContextUpdate(newScheduling, oldScheduling)...)
97 }
98
99 func (podSchedulingStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
100 return nil
101 }
102
103 func (podSchedulingStrategy) AllowUnconditionalUpdate() bool {
104 return true
105 }
106
107 type podSchedulingStatusStrategy struct {
108 podSchedulingStrategy
109 }
110
111 var StatusStrategy = podSchedulingStatusStrategy{Strategy}
112
113
114
115 func (podSchedulingStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
116 fields := map[fieldpath.APIVersion]*fieldpath.Set{
117 "resource.k8s.io/v1alpha2": fieldpath.NewSet(
118 fieldpath.MakePathOrDie("spec"),
119 ),
120 }
121
122 return fields
123 }
124
125 func (podSchedulingStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
126 newScheduling := obj.(*resource.PodSchedulingContext)
127 oldScheduling := old.(*resource.PodSchedulingContext)
128 newScheduling.Spec = oldScheduling.Spec
129 metav1.ResetObjectMetaForStatus(&newScheduling.ObjectMeta, &oldScheduling.ObjectMeta)
130 }
131
132 func (podSchedulingStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
133 newScheduling := obj.(*resource.PodSchedulingContext)
134 oldScheduling := old.(*resource.PodSchedulingContext)
135 return validation.ValidatePodSchedulingContextStatusUpdate(newScheduling, oldScheduling)
136 }
137
138
139 func (podSchedulingStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
140 return nil
141 }
142
143
144 func Match(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
145 return storage.SelectionPredicate{
146 Label: label,
147 Field: field,
148 GetAttrs: GetAttrs,
149 }
150 }
151
152
153 func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
154 scheduling, ok := obj.(*resource.PodSchedulingContext)
155 if !ok {
156 return nil, nil, errors.New("not a PodSchedulingContext")
157 }
158 return labels.Set(scheduling.Labels), toSelectableFields(scheduling), nil
159 }
160
161
162 func toSelectableFields(scheduling *resource.PodSchedulingContext) fields.Set {
163 fields := generic.ObjectMetaFieldsSet(&scheduling.ObjectMeta, true)
164 return fields
165 }
166
View as plain text