2023-08-30 23:42:02 +02:00
|
|
|
package unstructured
|
|
|
|
|
|
|
|
import (
|
2023-08-31 02:24:41 +02:00
|
|
|
"strings"
|
|
|
|
|
2023-08-30 23:42:02 +02:00
|
|
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
2023-09-04 10:31:59 +02:00
|
|
|
type (
|
|
|
|
marshaler = func(*unstructured.Unstructured) ([]byte, error)
|
|
|
|
patcher = func(originalJSON, modifiedJSON []byte) ([]byte, error)
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultMarshaler = (*unstructured.Unstructured).MarshalJSON
|
|
|
|
defaultPatcher = jsonpatch.CreateMergePatch
|
|
|
|
)
|
|
|
|
|
2023-08-30 23:42:02 +02:00
|
|
|
func TidyObject(obj interface{}) interface{} {
|
|
|
|
switch typedPatternElement := obj.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
tidy := map[string]interface{}{}
|
|
|
|
for k, v := range typedPatternElement {
|
|
|
|
v = TidyObject(v)
|
|
|
|
if v != nil {
|
|
|
|
tidy[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tidy) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tidy
|
|
|
|
case []interface{}:
|
|
|
|
var tidy []interface{}
|
|
|
|
for _, v := range typedPatternElement {
|
|
|
|
v = TidyObject(v)
|
|
|
|
if v != nil {
|
|
|
|
tidy = append(tidy, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tidy) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tidy
|
|
|
|
default:
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Tidy(obj unstructured.Unstructured) unstructured.Unstructured {
|
|
|
|
if obj.Object == nil {
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
return unstructured.Unstructured{
|
|
|
|
Object: TidyObject(obj.UnstructuredContent()).(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 02:24:41 +02:00
|
|
|
func FixupGenerateLabels(obj unstructured.Unstructured) {
|
|
|
|
tidy := map[string]string{
|
|
|
|
"app.kubernetes.io/managed-by": "kyverno",
|
|
|
|
}
|
|
|
|
if labels := obj.GetLabels(); labels != nil {
|
|
|
|
for k, v := range labels {
|
|
|
|
if !strings.HasPrefix(k, "generate.kyverno.io/") {
|
|
|
|
tidy[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj.SetLabels(tidy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Compare(a, e unstructured.Unstructured, tidy bool) (bool, error) {
|
2023-08-30 23:42:02 +02:00
|
|
|
if tidy {
|
|
|
|
a = Tidy(a)
|
2023-08-31 02:24:41 +02:00
|
|
|
e = Tidy(e)
|
2023-08-30 23:42:02 +02:00
|
|
|
}
|
2023-09-04 10:31:59 +02:00
|
|
|
return compare(a, e, defaultMarshaler, defaultPatcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compare(a, e unstructured.Unstructured, marshaler marshaler, patcher patcher) (bool, error) {
|
|
|
|
if marshaler == nil {
|
|
|
|
marshaler = defaultMarshaler
|
|
|
|
}
|
|
|
|
actual, err := marshaler(&a)
|
2023-08-30 23:42:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-09-04 10:31:59 +02:00
|
|
|
expected, err := marshaler(&e)
|
2023-08-30 23:42:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-09-04 10:31:59 +02:00
|
|
|
if patcher == nil {
|
|
|
|
patcher = defaultPatcher
|
|
|
|
}
|
|
|
|
patch, err := patcher(actual, expected)
|
2023-08-30 23:42:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(patch) == 2, nil
|
|
|
|
}
|