Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor export function and add finalizers to be removed #2427

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}