mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-08-21 05:26:56 +00:00
* feat: implement performance optimizations --------- Signed-off-by: Oliver Baehler <oliver@sudo-i.net>
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
// Copyright 2020-2026 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sanitize
|
|
|
|
import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
// SanitizeUnstructured Removes additional metadata we might not need when loading unstructured items into a context.
|
|
func SanitizeUnstructured(obj *unstructured.Unstructured, opts SanitizeOptions) {
|
|
if obj == nil {
|
|
return
|
|
}
|
|
|
|
if opts.StripUID {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "uid")
|
|
}
|
|
|
|
if opts.StripResourceVersion {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "resourceVersion")
|
|
}
|
|
|
|
if opts.StripOwnerreferences {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "ownerReferences")
|
|
}
|
|
|
|
if opts.StripGeneration {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "generation")
|
|
}
|
|
|
|
if opts.StripManagedFields {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "managedFields")
|
|
}
|
|
|
|
if opts.StripLastApplied {
|
|
anns, found, err := unstructured.NestedStringMap(obj.Object, "metadata", "annotations")
|
|
if err == nil && found && len(anns) > 0 {
|
|
// kubectl apply annotation.
|
|
delete(anns, "kubectl.kubernetes.io/last-applied-configuration")
|
|
|
|
if len(anns) == 0 {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "annotations")
|
|
} else {
|
|
_ = unstructured.SetNestedStringMap(obj.Object, anns, "metadata", "annotations")
|
|
}
|
|
}
|
|
}
|
|
|
|
if opts.StripStatus {
|
|
unstructured.RemoveNestedField(obj.Object, "status")
|
|
}
|
|
}
|