github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/patch.go (about) 1 // Copyright © 2020 Banzai Cloud 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reconciler 16 17 import ( 18 "encoding/json" 19 20 "emperror.dev/errors" 21 22 "github.com/banzaicloud/k8s-objectmatcher/patch" 23 ) 24 25 func IgnoreManagedFields() patch.CalculateOption { 26 return func(current, modified []byte) ([]byte, []byte, error) { 27 current, err := deleteManagedFields(current) 28 if err != nil { 29 return []byte{}, []byte{}, errors.WrapIf(err, "could not delete managed fields from modified byte sequence") 30 } 31 32 modified, err = deleteManagedFields(modified) 33 if err != nil { 34 return []byte{}, []byte{}, errors.WrapIf(err, "could not delete managed fields from modified byte sequence") 35 } 36 37 return current, modified, nil 38 } 39 } 40 41 func deleteManagedFields(obj []byte) ([]byte, error) { 42 var objectMap map[string]interface{} 43 err := json.Unmarshal(obj, &objectMap) 44 if err != nil { 45 return []byte{}, errors.WrapIf(err, "could not unmarshal byte sequence") 46 } 47 if metadata, ok := objectMap["metadata"].(map[string]interface{}); ok { 48 delete(metadata, "managedFields") 49 objectMap["metadata"] = metadata 50 } 51 obj, err = json.Marshal(objectMap) 52 if err != nil { 53 return []byte{}, errors.WrapIf(err, "could not marshal byte sequence") 54 } 55 56 return obj, nil 57 }