Skip to content

Commit

Permalink
refactor RemoveFieldForExport function
Browse files Browse the repository at this point in the history
Refactor the RemoveFieldForExport function to improve readability and
maintainability.

add finalizers to be removed from the metadata and spec fields

Grouped metadata and spec fields into slices and used
loops to remove them. Added unit tests to verify the functionality.
  • Loading branch information
chmouel committed Nov 7, 2024
1 parent 08bf9e9 commit e6591b3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 16 deletions.
42 changes: 26 additions & 16 deletions pkg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
72 changes: 72 additions & 0 deletions pkg/export/export_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit e6591b3

Please sign in to comment.