Skip to content

Commit

Permalink
Revert "remove unwanted values returned from dry-run"
Browse files Browse the repository at this point in the history
This reverts commit 60c1d58.
  • Loading branch information
liggitt committed Apr 6, 2022
1 parent 820247a commit adb7621
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,6 @@ func (e *Store) Create(ctx context.Context, obj runtime.Object, createValidation
if e.Decorator != nil {
e.Decorator(out)
}
if dryrun.IsDryRun(options.DryRun) {
if err := dryrun.ResetMetadata(obj, out); err != nil {
return nil, err
}
}
return out, nil
}

Expand Down
34 changes: 0 additions & 34 deletions staging/src/k8s.io/apiserver/pkg/util/dryrun/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,7 @@ limitations under the License.

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.
func IsDryRun(flag []string) bool {
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
}
14 changes: 3 additions & 11 deletions test/integration/dryrun/dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package dryrun

import (
"context"
"strings"
"testing"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -48,11 +47,7 @@ var kindAllowList = sets.NewString()
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
// Create a new object with generateName
gnObj := obj.DeepCopy()
gnObj.SetGenerateName(obj.GetName() + "-")
gnObj.SetName("")
Expand All @@ -79,11 +74,8 @@ func DryRunCreateTest(t *testing.T, rsc dynamic.ResourceInterface, obj *unstruct
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 obj.GetGenerateName() == "" {
if _, err := rsc.Get(context.TODO(), obj.GetName(), metav1.GetOptions{}); !apierrors.IsNotFound(err) {
t.Fatalf("object shouldn't exist: %v, %v", obj, err)
}
if _, err := rsc.Get(context.TODO(), obj.GetName(), metav1.GetOptions{}); !apierrors.IsNotFound(err) {
t.Fatalf("object shouldn't exist: %v", err)
}
}

Expand Down

0 comments on commit adb7621

Please sign in to comment.