Skip to content

Commit af0ea18

Browse files
authored
Fix patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus and add it as a default ignore calculation (#37)
1 parent 5d55e0c commit af0ea18

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

patch/deletenull.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020

2121
"emperror.dev/errors"
2222
json "github.com/json-iterator/go"
23-
v1 "k8s.io/api/apps/v1"
24-
corev1 "k8s.io/api/core/v1"
2523
"k8s.io/apimachinery/pkg/util/intstr"
2624
)
2725

@@ -191,21 +189,31 @@ func deleteStatusField(obj []byte) ([]byte, error) {
191189
}
192190

193191
func deleteVolumeClaimTemplateFields(obj []byte) ([]byte, error) {
194-
sts := v1.StatefulSet{}
195-
err := json.Unmarshal(obj, &sts)
192+
resource := map[string]interface{}{}
193+
err := json.Unmarshal(obj, &resource)
196194
if err != nil {
197195
return []byte{}, errors.Wrap(err, "could not unmarshal byte sequence")
198196
}
199197

200-
for i := range sts.Spec.VolumeClaimTemplates {
201-
sts.Spec.VolumeClaimTemplates[i].Kind = ""
202-
sts.Spec.VolumeClaimTemplates[i].APIVersion = ""
203-
sts.Spec.VolumeClaimTemplates[i].Status = corev1.PersistentVolumeClaimStatus{
204-
Phase: corev1.ClaimPending,
198+
if spec, ok := resource["spec"]; ok {
199+
if spec, ok := spec.(map[string]interface{}); ok {
200+
if vcts, ok := spec["volumeClaimTemplates"]; ok {
201+
if vcts, ok := vcts.([]interface{}); ok {
202+
for _, vct := range vcts {
203+
if vct, ok := vct.(map[string]interface{}); ok {
204+
vct["kind"] = ""
205+
vct["apiVersion"] = ""
206+
vct["status"] = map[string]string{
207+
"phase": "Pending",
208+
}
209+
}
210+
}
211+
}
212+
}
205213
}
206214
}
207215

208-
obj, err = json.ConfigCompatibleWithStandardLibrary.Marshal(sts)
216+
obj, err = json.ConfigCompatibleWithStandardLibrary.Marshal(resource)
209217
if err != nil {
210218
return []byte{}, errors.Wrap(err, "could not marshal byte sequence")
211219
}

tests/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func testMatchOnObject(testItem *TestItem) error {
190190
var err error
191191
opts := []patch.CalculateOption{
192192
patch.IgnoreStatusFields(),
193+
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
193194
}
194195

195196
newObject := testItem.object
@@ -403,7 +404,6 @@ func testMatchOnObject(testItem *TestItem) error {
403404
}
404405
}()
405406
case *appsv1.StatefulSet:
406-
opts = append(opts, patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus())
407407
existing, err = testContext.Client.AppsV1().StatefulSets(newObject.GetNamespace()).Create(context.Background(), newObject.(*appsv1.StatefulSet), metav1.CreateOptions{})
408408
if err != nil {
409409
return errors.WrapWithDetails(err, "failed to create object", "object", newObject)

0 commit comments

Comments
 (0)