@@ -12,7 +12,9 @@ package utils
1212import (
1313 "encoding/json"
1414 "fmt"
15+ "reflect"
1516
17+ "emperror.dev/errors"
1618 "github.com/cisco-open/k8s-objectmatcher/patch"
1719 jsoniter "github.com/json-iterator/go"
1820)
@@ -72,3 +74,59 @@ func removeElement(m map[string]interface{}, path ...string) bool {
7274 delete (cur , lastKey )
7375 return exists
7476}
77+
78+ // taken from github.com/cisco-open/k8s-objectmatcher/patch but with some modifications to work
79+ // with newer typed PDBs
80+ func IgnorePDBSelector () patch.CalculateOption {
81+ return func (current , modified []byte ) ([]byte , []byte , error ) {
82+ currentResource := map [string ]any {}
83+ if err := jsoniter .Unmarshal (current , & currentResource ); err != nil {
84+ return []byte {}, []byte {}, errors .Wrap (err , "could not unmarshal byte sequence for current" )
85+ }
86+
87+ modifiedResource := map [string ]any {}
88+ if err := jsoniter .Unmarshal (modified , & modifiedResource ); err != nil {
89+ return []byte {}, []byte {}, errors .Wrap (err , "could not unmarshal byte sequence for modified" )
90+ }
91+
92+ if reflect .DeepEqual (getPDBSelector (currentResource ), getPDBSelector (modifiedResource )) {
93+ var err error
94+ current , err = deletePDBSelector (currentResource )
95+ if err != nil {
96+ return nil , nil , errors .Wrap (err , "delete pdb selector from current" )
97+ }
98+ modified , err = deletePDBSelector (modifiedResource )
99+ if err != nil {
100+ return nil , nil , errors .Wrap (err , "delete pdb selector from modified" )
101+ }
102+ }
103+
104+ return current , modified , nil
105+ }
106+ }
107+
108+ func getPDBSelector (resource map [string ]interface {}) interface {} {
109+ if spec , ok := resource ["spec" ]; ok {
110+ if spec , ok := spec .(map [string ]any ); ok {
111+ if selector , ok := spec ["selector" ]; ok {
112+ return selector
113+ }
114+ }
115+ }
116+ return nil
117+ }
118+
119+ func deletePDBSelector (resource map [string ]interface {}) ([]byte , error ) {
120+ if spec , ok := resource ["spec" ]; ok {
121+ if spec , ok := spec .(map [string ]any ); ok {
122+ delete (spec , "selector" )
123+ }
124+ }
125+
126+ obj , err := jsoniter .ConfigCompatibleWithStandardLibrary .Marshal (resource )
127+ if err != nil {
128+ return []byte {}, errors .Wrap (err , "could not marshal byte sequence" )
129+ }
130+
131+ return obj , nil
132+ }
0 commit comments