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 1 commit
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
36 changes: 36 additions & 0 deletions utils/managed_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

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

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

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

// If the "metadata" field exists, check if the "managedFields" field exists

if metadataExists {
_, managedFieldsExists, err := unstructured.NestedSlice(metadata, "managedFields")
if err != nil {
return err
}

// If the "managedFields" field exists, delete it
if managedFieldsExists {
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
}
Loading