remove unwanted values returned from dry-run
Remove the uid and the resourceVersion from dry-run results per kep 576 https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/576-dry-run/README.md#generated-values Kubernetes-commit: 60c1d58d02c7374645c00281dda3fd656264e1c5
This commit is contained in:
parent
cd64b6709e
commit
7cac225c86
|
|
@ -437,6 +437,11 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,41 @@ 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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue