diff --git a/pkg/export/export.go b/pkg/export/export.go index 30ae6d7524..8bdee44d6b 100644 --- a/pkg/export/export.go +++ b/pkg/export/export.go @@ -19,29 +19,39 @@ import ( ) func RemoveFieldForExport(obj *unstructured.Unstructured) error { + content := obj.UnstructuredContent() + // remove the status from pipelinerun and taskrun - unstructured.RemoveNestedField(obj.UnstructuredContent(), "status") + unstructured.RemoveNestedField(content, "status") // remove some metadata information of previous resource - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "managedFields") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "resourceVersion") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "uid") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "generation") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "namespace") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "creationTimestamp") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "ownerReferences") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration") - _, exist, err := unstructured.NestedString(obj.UnstructuredContent(), "metadata", "generateName") - if err != nil { - return err + metadataFields := []string{ + "managedFields", + "resourceVersion", + "uid", + "finalizers", + "generation", + "namespace", + "creationTimestamp", + "ownerReferences", + } + for _, field := range metadataFields { + unstructured.RemoveNestedField(content, "metadata", field) } - if exist { - unstructured.RemoveNestedField(obj.UnstructuredContent(), "metadata", "name") + unstructured.RemoveNestedField(content, "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration") + + // check if generateName exists and remove name if it does + if _, exist, err := unstructured.NestedString(content, "metadata", "generateName"); err != nil { + return err + } else if exist { + unstructured.RemoveNestedField(content, "metadata", "name") } // remove the status from spec which are related to status - unstructured.RemoveNestedField(obj.UnstructuredContent(), "spec", "status") - unstructured.RemoveNestedField(obj.UnstructuredContent(), "spec", "statusMessage") + specFields := []string{"status", "statusMessage"} + for _, field := range specFields { + unstructured.RemoveNestedField(content, "spec", field) + } return nil } diff --git a/pkg/export/export_test.go b/pkg/export/export_test.go new file mode 100644 index 0000000000..2507c6c5ac --- /dev/null +++ b/pkg/export/export_test.go @@ -0,0 +1,72 @@ +package export + +import ( + "testing" + + "gotest.tools/v3/assert" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func TestRemoveFieldForExport(t *testing.T) { + tests := []struct { + name string + input *unstructured.Unstructured + expected map[string]interface{} + }{ + { + name: "Remove status and metadata fields", + input: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "status": "some-status", + "metadata": map[string]interface{}{ + "managedFields": "some-managed-fields", + "resourceVersion": "some-resource-version", + "uid": "some-uid", + "finalizers": "some-finalizers", + "generation": "some-generation", + "namespace": "some-namespace", + "creationTimestamp": "some-timestamp", + "ownerReferences": "some-owner-references", + "annotations": map[string]interface{}{ + "kubectl.kubernetes.io/last-applied-configuration": "some-configuration", + }, + }, + "spec": map[string]interface{}{ + "status": "some-spec-status", + "statusMessage": "some-status-message", + }, + }, + }, + expected: map[string]interface{}{ + "metadata": map[string]interface{}{ + "annotations": map[string]interface{}{}, + }, + "spec": map[string]interface{}{}, + }, + }, + { + name: "Remove name if generateName exists", + input: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "generateName": "some-generate-name", + "name": "some-name", + }, + }, + }, + expected: map[string]interface{}{ + "metadata": map[string]interface{}{ + "generateName": "some-generate-name", + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := RemoveFieldForExport(tt.input) + assert.NilError(t, err) + assert.DeepEqual(t, tt.expected, tt.input.Object) + }) + } +}