mirror of https://github.com/k3s-io/k3s
fixes operation for "create on update"
remove create-on-update logic for quota controller review: add more error check remove unused args revert changes in patch.go use hasUID to judge if it's a create-on-updatepull/8/head
parent
e3fa9133af
commit
ccb1ec7a36
|
@ -223,7 +223,7 @@ func (qm *QuotaMonitor) SyncMonitors(resources map[schema.GroupVersionResource]s
|
||||||
if evaluator == nil {
|
if evaluator == nil {
|
||||||
listerFunc := generic.ListerFuncForResourceFunc(qm.informerFactory.ForResource)
|
listerFunc := generic.ListerFuncForResourceFunc(qm.informerFactory.ForResource)
|
||||||
listResourceFunc := generic.ListResourceUsingListerFunc(listerFunc, resource)
|
listResourceFunc := generic.ListResourceUsingListerFunc(listerFunc, resource)
|
||||||
evaluator = generic.NewObjectCountEvaluator(false, resource.GroupResource(), listResourceFunc, "")
|
evaluator = generic.NewObjectCountEvaluator(resource.GroupResource(), listResourceFunc, "")
|
||||||
qm.registry.Add(evaluator)
|
qm.registry.Add(evaluator)
|
||||||
glog.Infof("QuotaMonitor created object count evaluator for %s", resource.GroupResource())
|
glog.Infof("QuotaMonitor created object count evaluator for %s", resource.GroupResource())
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ func NewEvaluators(f quota.ListerForResourceFunc) []quota.Evaluator {
|
||||||
// these evaluators require an alias for backwards compatibility
|
// these evaluators require an alias for backwards compatibility
|
||||||
for gvr, alias := range legacyObjectCountAliases {
|
for gvr, alias := range legacyObjectCountAliases {
|
||||||
result = append(result,
|
result = append(result,
|
||||||
generic.NewObjectCountEvaluator(false, gvr.GroupResource(), generic.ListResourceUsingListerFunc(f, gvr), alias))
|
generic.NewObjectCountEvaluator(gvr.GroupResource(), generic.ListResourceUsingListerFunc(f, gvr), alias))
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,9 +167,6 @@ func CalculateUsageStats(options quota.UsageStatsOptions,
|
||||||
// that associates usage of the specified resource based on the number of items
|
// that associates usage of the specified resource based on the number of items
|
||||||
// returned by the specified listing function.
|
// returned by the specified listing function.
|
||||||
type objectCountEvaluator struct {
|
type objectCountEvaluator struct {
|
||||||
// allowCreateOnUpdate if true will ensure the evaluator tracks create
|
|
||||||
// and update operations.
|
|
||||||
allowCreateOnUpdate bool
|
|
||||||
// GroupResource that this evaluator tracks.
|
// GroupResource that this evaluator tracks.
|
||||||
// It is used to construct a generic object count quota name
|
// It is used to construct a generic object count quota name
|
||||||
groupResource schema.GroupResource
|
groupResource schema.GroupResource
|
||||||
|
@ -189,7 +186,7 @@ func (o *objectCountEvaluator) Constraints(required []api.ResourceName, item run
|
||||||
// Handles returns true if the object count evaluator needs to track this attributes.
|
// Handles returns true if the object count evaluator needs to track this attributes.
|
||||||
func (o *objectCountEvaluator) Handles(a admission.Attributes) bool {
|
func (o *objectCountEvaluator) Handles(a admission.Attributes) bool {
|
||||||
operation := a.GetOperation()
|
operation := a.GetOperation()
|
||||||
return operation == admission.Create || (o.allowCreateOnUpdate && operation == admission.Update)
|
return operation == admission.Create
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matches returns true if the evaluator matches the specified quota with the provided input item
|
// Matches returns true if the evaluator matches the specified quota with the provided input item
|
||||||
|
@ -241,7 +238,6 @@ var _ quota.Evaluator = &objectCountEvaluator{}
|
||||||
// purposes for the legacy object counting names in quota. Unless its supporting
|
// purposes for the legacy object counting names in quota. Unless its supporting
|
||||||
// backward compatibility, alias should not be used.
|
// backward compatibility, alias should not be used.
|
||||||
func NewObjectCountEvaluator(
|
func NewObjectCountEvaluator(
|
||||||
allowCreateOnUpdate bool,
|
|
||||||
groupResource schema.GroupResource, listFuncByNamespace ListFuncByNamespace,
|
groupResource schema.GroupResource, listFuncByNamespace ListFuncByNamespace,
|
||||||
alias api.ResourceName) quota.Evaluator {
|
alias api.ResourceName) quota.Evaluator {
|
||||||
|
|
||||||
|
@ -251,7 +247,6 @@ func NewObjectCountEvaluator(
|
||||||
}
|
}
|
||||||
|
|
||||||
return &objectCountEvaluator{
|
return &objectCountEvaluator{
|
||||||
allowCreateOnUpdate: allowCreateOnUpdate,
|
|
||||||
groupResource: groupResource,
|
groupResource: groupResource,
|
||||||
listFuncByNamespace: listFuncByNamespace,
|
listFuncByNamespace: listFuncByNamespace,
|
||||||
resourceNames: resourceNames,
|
resourceNames: resourceNames,
|
||||||
|
|
|
@ -592,7 +592,7 @@ func (e *quotaEvaluator) Evaluate(a admission.Attributes) error {
|
||||||
if evaluator == nil {
|
if evaluator == nil {
|
||||||
// create an object count evaluator if no evaluator previously registered
|
// create an object count evaluator if no evaluator previously registered
|
||||||
// note, we do not need aggregate usage here, so we pass a nil informer func
|
// note, we do not need aggregate usage here, so we pass a nil informer func
|
||||||
evaluator = generic.NewObjectCountEvaluator(false, gr, nil, "")
|
evaluator = generic.NewObjectCountEvaluator(gr, nil, "")
|
||||||
e.registry.Add(evaluator)
|
e.registry.Add(evaluator)
|
||||||
glog.Infof("quota admission added evaluator for: %s", gr)
|
glog.Infof("quota admission added evaluator for: %s", gr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,7 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
|
||||||
userInfo, _ := request.UserFrom(ctx)
|
userInfo, _ := request.UserFrom(ctx)
|
||||||
staticAdmissionAttributes := admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo)
|
staticAdmissionAttributes := admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo)
|
||||||
admissionCheck := func(updatedObject runtime.Object, currentObject runtime.Object) error {
|
admissionCheck := func(updatedObject runtime.Object, currentObject runtime.Object) error {
|
||||||
|
// if we allow create-on-patch, we have this TODO: call the mutating admission chain with the CREATE verb instead of UPDATE
|
||||||
if mutatingAdmission, ok := admit.(admission.MutationInterface); ok && admit.Handles(admission.Update) {
|
if mutatingAdmission, ok := admit.(admission.MutationInterface); ok && admit.Handles(admission.Update) {
|
||||||
return mutatingAdmission.Admit(admission.NewAttributesRecord(updatedObject, currentObject, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo))
|
return mutatingAdmission.Admit(admission.NewAttributesRecord(updatedObject, currentObject, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo))
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,12 +96,24 @@ func UpdateResource(r rest.Updater, scope RequestScope, admit admission.Interfac
|
||||||
}
|
}
|
||||||
|
|
||||||
userInfo, _ := request.UserFrom(ctx)
|
userInfo, _ := request.UserFrom(ctx)
|
||||||
staticAdmissionAttributes := admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo)
|
|
||||||
var transformers []rest.TransformFunc
|
var transformers []rest.TransformFunc
|
||||||
if mutatingAdmission, ok := admit.(admission.MutationInterface); ok && mutatingAdmission.Handles(admission.Update) {
|
if mutatingAdmission, ok := admit.(admission.MutationInterface); ok {
|
||||||
transformers = append(transformers, func(ctx context.Context, newObj, oldObj runtime.Object) (runtime.Object, error) {
|
transformers = append(transformers, func(ctx context.Context, newObj, oldObj runtime.Object) (runtime.Object, error) {
|
||||||
return newObj, mutatingAdmission.Admit(admission.NewAttributesRecord(newObj, oldObj, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo))
|
isNotZeroObject, err := hasUID(oldObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unexpected error when extracting UID from oldObj: %v", err.Error())
|
||||||
|
} else if !isNotZeroObject {
|
||||||
|
if mutatingAdmission.Handles(admission.Create) {
|
||||||
|
return newObj, mutatingAdmission.Admit(admission.NewAttributesRecord(newObj, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Create, userInfo))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if mutatingAdmission.Handles(admission.Update) {
|
||||||
|
return newObj, mutatingAdmission.Admit(admission.NewAttributesRecord(newObj, oldObj, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newObj, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createAuthorizerAttributes := authorizer.AttributesRecord{
|
createAuthorizerAttributes := authorizer.AttributesRecord{
|
||||||
|
@ -124,8 +136,13 @@ func UpdateResource(r rest.Updater, scope RequestScope, admit admission.Interfac
|
||||||
ctx,
|
ctx,
|
||||||
name,
|
name,
|
||||||
rest.DefaultUpdatedObjectInfo(obj, transformers...),
|
rest.DefaultUpdatedObjectInfo(obj, transformers...),
|
||||||
withAuthorization(rest.AdmissionToValidateObjectFunc(admit, staticAdmissionAttributes), scope.Authorizer, createAuthorizerAttributes),
|
withAuthorization(rest.AdmissionToValidateObjectFunc(
|
||||||
rest.AdmissionToValidateObjectUpdateFunc(admit, staticAdmissionAttributes),
|
admit,
|
||||||
|
admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Create, userInfo)),
|
||||||
|
scope.Authorizer, createAuthorizerAttributes),
|
||||||
|
rest.AdmissionToValidateObjectUpdateFunc(
|
||||||
|
admit,
|
||||||
|
admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Update, userInfo)),
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
wasCreated = created
|
wasCreated = created
|
||||||
|
|
Loading…
Reference in New Issue