🐛 Use CRD version prior to remap_crd_version backup item action (#2683)

* 🐛 preserve crd version before remapping

Signed-off-by: Ashish Amarnath <ashisham@vmware.com>
pull/2696/head
Ashish Amarnath 2020-07-07 10:59:41 -07:00 committed by GitHub
parent 2d48ac79c2
commit b4465e92b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 6 deletions

View File

@ -0,0 +1 @@
capture version of the CRD prior before invoking the remap_crd_version backup item action

View File

@ -160,6 +160,12 @@ func (ib *itemBackupper) backupItem(logger logrus.FieldLogger, obj runtime.Unstr
} }
} }
// capture the version of the object before invoking plugin actions as the plugin may update
// the group version of the object.
// group version of this object
// Used on filepath to backup up all groups and versions
version := resourceVersion(obj)
updatedObj, err := ib.executeActions(log, obj, groupResource, name, namespace, metadata) updatedObj, err := ib.executeActions(log, obj, groupResource, name, namespace, metadata)
if err != nil { if err != nil {
backupErrs = append(backupErrs, err) backupErrs = append(backupErrs, err)
@ -204,10 +210,6 @@ func (ib *itemBackupper) backupItem(logger logrus.FieldLogger, obj runtime.Unstr
return false, kubeerrs.NewAggregate(backupErrs) return false, kubeerrs.NewAggregate(backupErrs)
} }
// group version of this object
// Used on filepath to backup up all groups and versions
version := resourceVersion(obj)
// Getting the preferred group version of this resource // Getting the preferred group version of this resource
preferredVersion := preferredGVR.Version preferredVersion := preferredGVR.Version
@ -250,6 +252,7 @@ func (ib *itemBackupper) backupItem(logger logrus.FieldLogger, obj runtime.Unstr
// backing up the preferred version backup without API Group version on path - this is for backward compability // backing up the preferred version backup without API Group version on path - this is for backward compability
log.Debugf("Resource %s/%s, version= %s, preferredVersion=%s", groupResource.String(), name, version, preferredVersion)
if version == preferredVersion { if version == preferredVersion {
if namespace != "" { if namespace != "" {
filePath = filepath.Join(velerov1api.ResourcesDir, groupResource.String(), velerov1api.NamespaceScopedDir, namespace, name+".json") filePath = filepath.Join(velerov1api.ResourcesDir, groupResource.String(), velerov1api.NamespaceScopedDir, namespace, name+".json")
@ -272,7 +275,6 @@ func (ib *itemBackupper) backupItem(logger logrus.FieldLogger, obj runtime.Unstr
if _, err := ib.tarWriter.Write(itemBytes); err != nil { if _, err := ib.tarWriter.Write(itemBytes); err != nil {
return false, errors.WithStack(err) return false, errors.WithStack(err)
} }
} }
return true, nil return true, nil

View File

@ -170,6 +170,7 @@ func TestRemapCRDVersionActionData(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "apiextensions.k8s.io/v1beta1", item.UnstructuredContent()["apiVersion"]) assert.Equal(t, "apiextensions.k8s.io/v1beta1", item.UnstructuredContent()["apiVersion"])
assert.Equal(t, crd.Kind, item.GetObjectKind().GroupVersionKind().GroupKind().Kind)
name, _, err := unstructured.NestedString(item.UnstructuredContent(), "metadata", "name") name, _, err := unstructured.NestedString(item.UnstructuredContent(), "metadata", "name")
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, crd.Name, name) assert.Equal(t, crd.Name, name)

View File

@ -23,7 +23,7 @@ import (
var ( var (
ClusterRoleBindings = schema.GroupResource{Group: "rbac.authorization.k8s.io", Resource: "clusterrolebindings"} ClusterRoleBindings = schema.GroupResource{Group: "rbac.authorization.k8s.io", Resource: "clusterrolebindings"}
ClusterRoles = schema.GroupResource{Group: "rbac.authorization.k8s.io", Resource: "clusterroles"} ClusterRoles = schema.GroupResource{Group: "rbac.authorization.k8s.io", Resource: "clusterroles"}
CustomResourceDefinitions = schema.GroupResource{Group: "apiextensions.k8s.io", Resource: "customresourcedefinitions"} CustomResourceDefinitions = schema.GroupResource{Group: "apiextensions.k8s.io", Resource: "CustomResourceDefinition"}
Jobs = schema.GroupResource{Group: "batch", Resource: "jobs"} Jobs = schema.GroupResource{Group: "batch", Resource: "jobs"}
Namespaces = schema.GroupResource{Group: "", Resource: "namespaces"} Namespaces = schema.GroupResource{Group: "", Resource: "namespaces"}
PersistentVolumeClaims = schema.GroupResource{Group: "", Resource: "persistentvolumeclaims"} PersistentVolumeClaims = schema.GroupResource{Group: "", Resource: "persistentvolumeclaims"}