Skip to content

⚠️ Fakeclient: reject create/update if Name isn't provided #886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/testing"

Expand Down Expand Up @@ -85,6 +86,12 @@ func (t versionedTracker) Create(gvr schema.GroupVersionResource, obj runtime.Ob
if err != nil {
return err
}
if accessor.GetName() == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a NewInvalid, this is how that looks like from a real api server:

              ErrStatus: {
                  TypeMeta: {Kind: "", APIVersion: ""},
                  ListMeta: {
                      SelfLink: "",
                      ResourceVersion: "",
                      Continue: "",
                      RemainingItemCount: nil,
                  },
                  Status: "Failure",
                  Message: "ConfigMap \"\" is invalid: metadata.name: Required value: name or generateName is required",
                  Reason: "Invalid",
                  Details: {
                      Name: "",
                      Group: "",
                      Kind: "ConfigMap",
                      UID: "",
                      Causes: [
                          {
                              Type: "FieldValueRequired",
                              Message: "Required value: name or generateName is required",
                              Field: "metadata.name",
                          },
                      ],
                      RetryAfterSeconds: 0,
                  },
                  Code: 422,
              }

return apierrors.NewInvalid(
obj.GetObjectKind().GroupVersionKind().GroupKind(),
accessor.GetName(),
field.ErrorList{field.Required(field.NewPath("metadata.name"), "name is required")})
}
if accessor.GetResourceVersion() != "" {
return apierrors.NewBadRequest("resourceVersion can not be set for Create requests")
}
Expand All @@ -97,6 +104,12 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
if err != nil {
return fmt.Errorf("failed to get accessor for object: %v", err)
}
if accessor.GetName() == "" {
return apierrors.NewInvalid(
obj.GetObjectKind().GroupVersionKind().GroupKind(),
accessor.GetName(),
field.ErrorList{field.Required(field.NewPath("metadata.name"), "name is required")})
}
oldObject, err := t.ObjectTracker.Get(gvr, ns, accessor.GetName())
if err != nil {
return err
Expand Down
30 changes: 30 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,36 @@ var _ = Describe("Fake client", func() {
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
})

It("should error on Create with empty Name", func() {
By("Creating a new configmap")
newcm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns2",
},
}
err := cl.Create(context.Background(), newcm)
Expect(err.Error()).To(Equal("ConfigMap \"\" is invalid: metadata.name: Required value: name is required"))
})

It("should error on Update with empty Name", func() {
By("Creating a new configmap")
newcm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns2",
},
}
err := cl.Update(context.Background(), newcm)
Expect(err.Error()).To(Equal("ConfigMap \"\" is invalid: metadata.name: Required value: name is required"))
})

It("should be able to Create with GenerateName", func() {
By("Creating a new configmap")
newcm := &corev1.ConfigMap{
Expand Down