Skip to content

Commit 7bf70a6

Browse files
committed
Fix up PDB patching test
1 parent 1155b00 commit 7bf70a6

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

operator/pkg/resources/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func Update(
138138
opts := []patch.CalculateOption{
139139
patch.IgnoreStatusFields(),
140140
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
141-
patch.IgnorePDBSelector(),
141+
utils.IgnorePDBSelector(), // we leverage the pdb selector for non-PDB resources, so we had to fork the one in "patch"
142142
utils.IgnoreAnnotation(redpandaAnnotatorKey),
143143
}
144144
annotator := patch.NewAnnotator(redpandaAnnotatorKey)

operator/pkg/utils/patch.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ package utils
1212
import (
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

Comments
 (0)