Skip to content

Commit

Permalink
Fix patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus and add it as a …
Browse files Browse the repository at this point in the history
…default ignore calculation (#37)
  • Loading branch information
pepov authored Jan 14, 2021
1 parent 5d55e0c commit af0ea18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions patch/deletenull.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (

"emperror.dev/errors"
json "github.com/json-iterator/go"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

Expand Down Expand Up @@ -191,21 +189,31 @@ func deleteStatusField(obj []byte) ([]byte, error) {
}

func deleteVolumeClaimTemplateFields(obj []byte) ([]byte, error) {
sts := v1.StatefulSet{}
err := json.Unmarshal(obj, &sts)
resource := map[string]interface{}{}
err := json.Unmarshal(obj, &resource)
if err != nil {
return []byte{}, errors.Wrap(err, "could not unmarshal byte sequence")
}

for i := range sts.Spec.VolumeClaimTemplates {
sts.Spec.VolumeClaimTemplates[i].Kind = ""
sts.Spec.VolumeClaimTemplates[i].APIVersion = ""
sts.Spec.VolumeClaimTemplates[i].Status = corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimPending,
if spec, ok := resource["spec"]; ok {
if spec, ok := spec.(map[string]interface{}); ok {
if vcts, ok := spec["volumeClaimTemplates"]; ok {
if vcts, ok := vcts.([]interface{}); ok {
for _, vct := range vcts {
if vct, ok := vct.(map[string]interface{}); ok {
vct["kind"] = ""
vct["apiVersion"] = ""
vct["status"] = map[string]string{
"phase": "Pending",
}
}
}
}
}
}
}

obj, err = json.ConfigCompatibleWithStandardLibrary.Marshal(sts)
obj, err = json.ConfigCompatibleWithStandardLibrary.Marshal(resource)
if err != nil {
return []byte{}, errors.Wrap(err, "could not marshal byte sequence")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func testMatchOnObject(testItem *TestItem) error {
var err error
opts := []patch.CalculateOption{
patch.IgnoreStatusFields(),
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
}

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

0 comments on commit af0ea18

Please sign in to comment.