Merge pull request #109340 from liggitt/revert-generate-name

Revert generate name
This commit is contained in:
Kubernetes Prow Robot 2022-04-06 10:38:55 -07:00 committed by GitHub
commit 21184400a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 68 deletions

View File

@ -439,11 +439,6 @@ func (e *Store) Create(ctx context.Context, obj runtime.Object, createValidation
if e.Decorator != nil { if e.Decorator != nil {
e.Decorator(out) e.Decorator(out)
} }
if dryrun.IsDryRun(options.DryRun) {
if err := dryrun.ResetMetadata(obj, out); err != nil {
return nil, err
}
}
return out, nil return out, nil
} }

View File

@ -16,41 +16,7 @@ limitations under the License.
package dryrun package dryrun
import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
)
// IsDryRun returns true if the DryRun flag is an actual dry-run. // IsDryRun returns true if the DryRun flag is an actual dry-run.
func IsDryRun(flag []string) bool { func IsDryRun(flag []string) bool {
return len(flag) > 0 return len(flag) > 0
} }
// ResetMetadata resets metadata fields that are not allowed to be set by dry-run.
func ResetMetadata(originalObj, newObj runtime.Object) error {
originalObjMeta, err := meta.Accessor(originalObj)
if err != nil {
return errors.NewInternalError(err)
}
newObjMeta, err := meta.Accessor(newObj)
if err != nil {
return errors.NewInternalError(err)
}
// If a resource is created with dry-run enabled where generateName is set, the
// store will set the name to the generated name. We need to reset the name and restore
// the generateName metadata fields in order for the returned object to match the intent
// of the original template.
if originalObjMeta.GetGenerateName() != "" {
newObjMeta.SetName("")
}
newObjMeta.SetGenerateName(originalObjMeta.GetGenerateName())
// If UID is set in the dry-run output then that output cannot be used to create a resource. Reset
// the UID to allow the output to be used to create resources.
newObjMeta.SetUID("")
// If the resourceVersion is set in the dry-run output then that output cannot be used to create
// a resource. Reset the resourceVersion to allow the output to be used to create resources.
newObjMeta.SetResourceVersion("")
return nil
}

View File

@ -18,7 +18,6 @@ package dryrun
import ( import (
"context" "context"
"strings"
"testing" "testing"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
@ -47,43 +46,19 @@ var kindAllowList = sets.NewString()
// namespace used for all tests, do not change this // namespace used for all tests, do not change this
const testNamespace = "dryrunnamespace" const testNamespace = "dryrunnamespace"
func DryRunCreateWithGenerateNameTest(t *testing.T, rsc dynamic.ResourceInterface, obj *unstructured.Unstructured, gvResource schema.GroupVersionResource) {
// special resources with dots in the name cannot use generateName
if strings.Contains(obj.GetName(), ".") {
return
}
// Create a new object with generateName to ensure we don't taint the original object
gnObj := obj.DeepCopy()
gnObj.SetGenerateName(obj.GetName() + "-")
gnObj.SetName("")
DryRunCreateTest(t, rsc, gnObj, gvResource)
}
func DryRunCreateTest(t *testing.T, rsc dynamic.ResourceInterface, obj *unstructured.Unstructured, gvResource schema.GroupVersionResource) { func DryRunCreateTest(t *testing.T, rsc dynamic.ResourceInterface, obj *unstructured.Unstructured, gvResource schema.GroupVersionResource) {
createdObj, err := rsc.Create(context.TODO(), obj, metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}}) createdObj, err := rsc.Create(context.TODO(), obj, metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}})
if err != nil { if err != nil {
t.Fatalf("failed to dry-run create stub for %s: %#v: %v", gvResource, err, obj) t.Fatalf("failed to dry-run create stub for %s: %#v", gvResource, err)
} }
if obj.GroupVersionKind() != createdObj.GroupVersionKind() { if obj.GroupVersionKind() != createdObj.GroupVersionKind() {
t.Fatalf("created object doesn't have the same gvk as original object: got %v, expected %v", t.Fatalf("created object doesn't have the same gvk as original object: got %v, expected %v",
createdObj.GroupVersionKind(), createdObj.GroupVersionKind(),
obj.GroupVersionKind()) obj.GroupVersionKind())
} }
if createdObj.GetUID() != "" {
t.Fatalf("created object shouldn't have a uid: %v", createdObj)
}
if createdObj.GetResourceVersion() != "" {
t.Fatalf("created object shouldn't have a resource version: %v", createdObj)
}
if obj.GetGenerateName() != "" && createdObj.GetName() != "" {
t.Fatalf("created object's name should be an empty string if using GenerateName: %v", createdObj)
}
// we won't have a generated name here, so we won't check for this case if _, err := rsc.Get(context.TODO(), obj.GetName(), metav1.GetOptions{}); !apierrors.IsNotFound(err) {
if obj.GetGenerateName() == "" { t.Fatalf("object shouldn't exist: %v", err)
if _, err := rsc.Get(context.TODO(), obj.GetName(), metav1.GetOptions{}); !apierrors.IsNotFound(err) {
t.Fatalf("object shouldn't exist: %v, %v", obj, err)
}
} }
} }
@ -307,7 +282,6 @@ func TestDryRun(t *testing.T) {
name := obj.GetName() name := obj.GetName()
DryRunCreateTest(t, rsc, obj, gvResource) DryRunCreateTest(t, rsc, obj, gvResource)
DryRunCreateWithGenerateNameTest(t, rsc, obj, gvResource)
if _, err := rsc.Create(context.TODO(), obj, metav1.CreateOptions{}); err != nil { if _, err := rsc.Create(context.TODO(), obj, metav1.CreateOptions{}); err != nil {
t.Fatalf("failed to create stub for %s: %#v", gvResource, err) t.Fatalf("failed to create stub for %s: %#v", gvResource, err)