2015-04-14 22:03:26 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-04-14 22:03:26 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package admission
|
|
|
|
|
|
|
|
import (
|
2015-08-05 22:03:47 +00:00
|
|
|
apierrors "k8s.io/kubernetes/pkg/api/errors"
|
2016-03-25 08:57:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
2015-11-30 17:02:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-10-14 05:18:37 +00:00
|
|
|
utilerrors "k8s.io/kubernetes/pkg/util/errors"
|
2015-04-14 22:03:26 +00:00
|
|
|
)
|
|
|
|
|
2015-12-10 18:32:29 +00:00
|
|
|
func extractResourceName(a Attributes) (name string, resource unversioned.GroupResource, err error) {
|
2015-09-15 20:38:03 +00:00
|
|
|
name = "Unknown"
|
2015-12-10 18:32:29 +00:00
|
|
|
resource = a.GetResource()
|
2015-04-14 22:03:26 +00:00
|
|
|
obj := a.GetObject()
|
|
|
|
if obj != nil {
|
2016-03-25 08:57:45 +00:00
|
|
|
accessor, err := meta.Accessor(obj)
|
2015-04-14 22:03:26 +00:00
|
|
|
if err != nil {
|
2015-12-10 18:32:29 +00:00
|
|
|
return "", unversioned.GroupResource{}, err
|
2015-04-14 22:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is necessary because name object name generation has not occurred yet
|
2016-03-25 08:57:45 +00:00
|
|
|
if len(accessor.GetName()) > 0 {
|
|
|
|
name = accessor.GetName()
|
|
|
|
} else if len(accessor.GetGenerateName()) > 0 {
|
|
|
|
name = accessor.GetGenerateName()
|
2015-04-14 22:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
return name, resource, nil
|
2015-09-15 20:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewForbidden is a utility function to return a well-formatted admission control error response
|
|
|
|
func NewForbidden(a Attributes, internalError error) error {
|
|
|
|
// do not double wrap an error of same type
|
|
|
|
if apierrors.IsForbidden(internalError) {
|
|
|
|
return internalError
|
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
name, resource, err := extractResourceName(a)
|
2015-09-15 20:38:03 +00:00
|
|
|
if err != nil {
|
2015-10-14 05:18:37 +00:00
|
|
|
return apierrors.NewInternalError(utilerrors.NewAggregate([]error{internalError, err}))
|
2015-09-15 20:38:03 +00:00
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
return apierrors.NewForbidden(resource, name, internalError)
|
2015-04-14 22:03:26 +00:00
|
|
|
}
|
2015-09-15 20:38:03 +00:00
|
|
|
|
|
|
|
// NewNotFound is a utility function to return a well-formatted admission control error response
|
|
|
|
func NewNotFound(a Attributes) error {
|
2015-12-10 18:32:29 +00:00
|
|
|
name, resource, err := extractResourceName(a)
|
2015-09-15 20:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return apierrors.NewInternalError(err)
|
|
|
|
}
|
2015-12-10 18:32:29 +00:00
|
|
|
return apierrors.NewNotFound(resource, name)
|
2015-09-15 20:38:03 +00:00
|
|
|
}
|