mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 23:19:26 +00:00
Improve error message when name is omitted but generateName is available
This commit is contained in:
@@ -40,6 +40,8 @@ type Interface interface {
|
||||
SetNamespace(namespace string)
|
||||
Name() string
|
||||
SetName(name string)
|
||||
GenerateName() string
|
||||
SetGenerateName(name string)
|
||||
UID() types.UID
|
||||
SetUID(uid types.UID)
|
||||
ResourceVersion() string
|
||||
@@ -79,6 +81,9 @@ type MetadataAccessor interface {
|
||||
Name(obj runtime.Object) (string, error)
|
||||
SetName(obj runtime.Object, name string) error
|
||||
|
||||
GenerateName(obj runtime.Object) (string, error)
|
||||
SetGenerateName(obj runtime.Object, name string) error
|
||||
|
||||
UID(obj runtime.Object) (types.UID, error)
|
||||
SetUID(obj runtime.Object, uid types.UID) error
|
||||
|
||||
|
@@ -178,6 +178,23 @@ func (resourceAccessor) SetName(obj runtime.Object, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (resourceAccessor) GenerateName(obj runtime.Object) (string, error) {
|
||||
accessor, err := Accessor(obj)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return accessor.GenerateName(), nil
|
||||
}
|
||||
|
||||
func (resourceAccessor) SetGenerateName(obj runtime.Object, name string) error {
|
||||
accessor, err := Accessor(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accessor.SetGenerateName(name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (resourceAccessor) UID(obj runtime.Object) (types.UID, error) {
|
||||
accessor, err := Accessor(obj)
|
||||
if err != nil {
|
||||
@@ -268,6 +285,7 @@ func (resourceAccessor) SetResourceVersion(obj runtime.Object, version string) e
|
||||
type genericAccessor struct {
|
||||
namespace *string
|
||||
name *string
|
||||
generateName *string
|
||||
uid *types.UID
|
||||
apiVersion *string
|
||||
kind *string
|
||||
@@ -305,6 +323,20 @@ func (a genericAccessor) SetName(name string) {
|
||||
*a.name = name
|
||||
}
|
||||
|
||||
func (a genericAccessor) GenerateName() string {
|
||||
if a.generateName == nil {
|
||||
return ""
|
||||
}
|
||||
return *a.generateName
|
||||
}
|
||||
|
||||
func (a genericAccessor) SetGenerateName(generateName string) {
|
||||
if a.generateName == nil {
|
||||
return
|
||||
}
|
||||
*a.generateName = generateName
|
||||
}
|
||||
|
||||
func (a genericAccessor) UID() types.UID {
|
||||
if a.uid == nil {
|
||||
return ""
|
||||
@@ -392,6 +424,9 @@ func extractFromObjectMeta(v reflect.Value, a *genericAccessor) error {
|
||||
if err := runtime.FieldPtr(v, "Name", &a.name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.FieldPtr(v, "GenerateName", &a.generateName); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.FieldPtr(v, "UID", &a.uid); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -29,6 +29,7 @@ func TestGenericTypeMeta(t *testing.T) {
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
GenerateName string `json:"generateName,omitempty"`
|
||||
UID string `json:"uid,omitempty"`
|
||||
CreationTimestamp util.Time `json:"creationTimestamp,omitempty"`
|
||||
SelfLink string `json:"selfLink,omitempty"`
|
||||
@@ -44,6 +45,7 @@ func TestGenericTypeMeta(t *testing.T) {
|
||||
TypeMeta{
|
||||
Namespace: "bar",
|
||||
Name: "foo",
|
||||
GenerateName: "prefix",
|
||||
UID: "uid",
|
||||
APIVersion: "a",
|
||||
Kind: "b",
|
||||
@@ -63,6 +65,9 @@ func TestGenericTypeMeta(t *testing.T) {
|
||||
if e, a := "foo", accessor.Name(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "prefix", accessor.GenerateName(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "uid", string(accessor.UID()); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
@@ -92,6 +97,7 @@ func TestGenericTypeMeta(t *testing.T) {
|
||||
|
||||
accessor.SetNamespace("baz")
|
||||
accessor.SetName("bar")
|
||||
accessor.SetGenerateName("generate")
|
||||
accessor.SetUID("other")
|
||||
accessor.SetAPIVersion("c")
|
||||
accessor.SetKind("d")
|
||||
@@ -105,6 +111,9 @@ func TestGenericTypeMeta(t *testing.T) {
|
||||
if e, a := "bar", j.Name; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "generate", j.GenerateName; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "other", j.UID; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
@@ -135,6 +144,7 @@ type InternalTypeMeta struct {
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
GenerateName string `json:"generateName,omitempty"`
|
||||
UID string `json:"uid,omitempty"`
|
||||
CreationTimestamp util.Time `json:"creationTimestamp,omitempty"`
|
||||
SelfLink string `json:"selfLink,omitempty"`
|
||||
@@ -154,6 +164,7 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
|
||||
InternalTypeMeta{
|
||||
Namespace: "bar",
|
||||
Name: "foo",
|
||||
GenerateName: "prefix",
|
||||
UID: "uid",
|
||||
APIVersion: "a",
|
||||
Kind: "b",
|
||||
@@ -178,6 +189,13 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
|
||||
if e, a := "foo", name; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
generateName, err := accessor.GenerateName(j)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if e, a := "prefix", generateName; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
uid, err := accessor.UID(j)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -234,6 +252,9 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
|
||||
if err := accessor.SetName(j, "bar"); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if err := accessor.SetGenerateName(j, "generate"); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if err := accessor.SetUID(j, "other"); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -264,6 +285,9 @@ func TestGenericTypeMetaAccessor(t *testing.T) {
|
||||
if e, a := "bar", j.TypeMeta.Name; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "generate", j.TypeMeta.GenerateName; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "other", j.TypeMeta.UID; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
@@ -295,6 +319,7 @@ func TestGenericObjectMeta(t *testing.T) {
|
||||
type ObjectMeta struct {
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
GenerateName string `json:"generateName,omitempty"`
|
||||
UID string `json:"uid,omitempty"`
|
||||
CreationTimestamp util.Time `json:"creationTimestamp,omitempty"`
|
||||
SelfLink string `json:"selfLink,omitempty"`
|
||||
@@ -314,6 +339,7 @@ func TestGenericObjectMeta(t *testing.T) {
|
||||
ObjectMeta{
|
||||
Namespace: "bar",
|
||||
Name: "foo",
|
||||
GenerateName: "prefix",
|
||||
UID: "uid",
|
||||
ResourceVersion: "1",
|
||||
SelfLink: "some/place/only/we/know",
|
||||
@@ -331,6 +357,9 @@ func TestGenericObjectMeta(t *testing.T) {
|
||||
if e, a := "foo", accessor.Name(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "prefix", accessor.GenerateName(); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "uid", string(accessor.UID()); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
@@ -355,6 +384,7 @@ func TestGenericObjectMeta(t *testing.T) {
|
||||
|
||||
accessor.SetNamespace("baz")
|
||||
accessor.SetName("bar")
|
||||
accessor.SetGenerateName("generate")
|
||||
accessor.SetUID("other")
|
||||
accessor.SetAPIVersion("c")
|
||||
accessor.SetKind("d")
|
||||
@@ -370,6 +400,9 @@ func TestGenericObjectMeta(t *testing.T) {
|
||||
if e, a := "bar", j.Name; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "generate", j.GenerateName; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
if e, a := "other", j.UID; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
|
Reference in New Issue
Block a user