Improve error message when name is omitted but generateName is available
This commit is contained in:
@@ -21,14 +21,16 @@ import (
|
||||
)
|
||||
|
||||
type attributesRecord struct {
|
||||
kind string
|
||||
namespace string
|
||||
resource string
|
||||
operation string
|
||||
object runtime.Object
|
||||
}
|
||||
|
||||
func NewAttributesRecord(object runtime.Object, namespace, resource, operation string) Attributes {
|
||||
func NewAttributesRecord(object runtime.Object, kind, namespace, resource, operation string) Attributes {
|
||||
return &attributesRecord{
|
||||
kind: kind,
|
||||
namespace: namespace,
|
||||
resource: resource,
|
||||
operation: operation,
|
||||
@@ -36,6 +38,10 @@ func NewAttributesRecord(object runtime.Object, namespace, resource, operation s
|
||||
}
|
||||
}
|
||||
|
||||
func (record *attributesRecord) GetKind() string {
|
||||
return record.kind
|
||||
}
|
||||
|
||||
func (record *attributesRecord) GetNamespace() string {
|
||||
return record.namespace
|
||||
}
|
||||
|
48
pkg/admission/errors.go
Normal file
48
pkg/admission/errors.go
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
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 (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
apierrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
name := "Unknown"
|
||||
kind := a.GetKind()
|
||||
obj := a.GetObject()
|
||||
if obj != nil {
|
||||
objectMeta, err := api.ObjectMetaFor(obj)
|
||||
if err != nil {
|
||||
return apierrors.NewForbidden(kind, name, err)
|
||||
}
|
||||
|
||||
// this is necessary because name object name generation has not occurred yet
|
||||
if len(objectMeta.Name) > 0 {
|
||||
name = objectMeta.Name
|
||||
} else if len(objectMeta.GenerateName) > 0 {
|
||||
name = objectMeta.GenerateName
|
||||
}
|
||||
}
|
||||
return apierrors.NewForbidden(kind, name, internalError)
|
||||
}
|
@@ -27,6 +27,7 @@ type Attributes interface {
|
||||
GetResource() string
|
||||
GetOperation() string
|
||||
GetObject() runtime.Object
|
||||
GetKind() string
|
||||
}
|
||||
|
||||
// Interface is an abstract, pluggable interface for Admission Control decisions.
|
||||
|
Reference in New Issue
Block a user