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

feat: delete managed fields #280

Merged
merged 3 commits into from
Jan 18, 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
7 changes: 7 additions & 0 deletions pkg/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artifacts
import (
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s"
"github.com/aquasecurity/trivy-kubernetes/pkg/k8s/docker"
"github.com/aquasecurity/trivy-kubernetes/utils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -41,6 +42,12 @@ func FromResource(resource unstructured.Unstructured, serverAuths map[string]doc
}
}

// delete managed fields from the resource
err := utils.DeleteManagedFields(&resource)
if err != nil {
return nil, err
}

// we don't check found here, if the name is not found it will be an empty string
name, _, err := unstructured.NestedString(resource.Object, "metadata", "name")
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions utils/managed_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func DeleteManagedFields(resource *unstructured.Unstructured) error {
// Check if the "metadata.managedFields" field exists

_, exists, err := unstructured.NestedSlice(resource.Object, "metadata", "managedFields")
if err != nil {
return err
}

// If the field exists, then delete it

if exists {
metadata, _, err := unstructured.NestedMap(resource.Object, "metadata")
if err != nil {
return err
}

delete(metadata, "managedFields")

// Update the "metadata" field in the original object
err = unstructured.SetNestedMap(resource.Object, metadata, "metadata")
if err != nil {
return err
}
}

return nil
}
101 changes: 101 additions & 0 deletions utils/managed_fields_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package utils

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestDeleteManagedFields(t *testing.T) {

// Dummy Kubernetes resource with managedFields
sampleResource := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"managedFields": []interface{}{
map[string]interface{}{
"apiVersion": "v1",
"fieldsType": "FieldsV1",
"fieldsV1": map[string]interface{}{
"f:metadata": map[string]interface{}{
"f:annotations": map[string]interface{}{
".": map[string]interface{}{
"f:kubectl.kubernetes.io/last-applied-configuration": "sample-pod.yaml",
},
},
},
"f:spec": map[string]interface{}{
"f:containers": []interface{}{
map[string]interface{}{
"f:image": "nginx:1.14",
},
},
},
},
"manager": "kubectl-client-side-apply",
"operation": "Update",
"time": "2021-03-04T15:51:05Z",

"resourceVersion": "123456",
"uid": "123456",
},
},
"name": "sample-pod",
},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"image": "nginx:latest",
},
},
},
},
}

t.Run("deletes managedFields in resources", func(t *testing.T) {

// Check if the "managedFields" field exists before deletion
_, managedFieldsExists, err := unstructured.NestedSlice(sampleResource.Object, "metadata", "managedFields")
assert.NoError(t, err)
assert.True(t, managedFieldsExists)

// Delete the "managedFields" field
err = DeleteManagedFields(sampleResource)
if err != nil {
fmt.Println("Error:", err)
return
}
assert.NoError(t, err)

// Check if the "managedFields" field exists after deletion
_, managedFieldsExists, err = unstructured.NestedSlice(sampleResource.Object, "metadata", "managedFields")
assert.NoError(t, err)
assert.False(t, managedFieldsExists)
})

t.Run("does not delete managedFields if it does not exist", func(t *testing.T) {

// Check if the "managedFields" field exists before deletion
_, managedFieldsExists, err := unstructured.NestedSlice(sampleResource.Object, "metadata", "managedFields")
assert.NoError(t, err)
assert.False(t, managedFieldsExists)

// Delete the "managedFields" field
err = DeleteManagedFields(sampleResource)
if err != nil {
fmt.Println("Error:", err)
return
}
assert.NoError(t, err)

// Check if the "managedFields" field exists after deletion
_, managedFieldsExists, err = unstructured.NestedSlice(sampleResource.Object, "metadata", "managedFields")
assert.NoError(t, err)
assert.False(t, managedFieldsExists)
})

}
Loading