|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + protoV2 "google.golang.org/protobuf/proto" |
| 5 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 6 | + "k8s.io/apimachinery/pkg/runtime" |
| 7 | + "reflect" |
| 8 | +) |
| 9 | + |
| 10 | +// returns true if "relevant" parts of obj1 and obj2 have equal: |
| 11 | +// - labels, |
| 12 | +// - annotations, |
| 13 | +// - namespace+name, |
| 14 | +// - non-metadata, non-status fields |
| 15 | +// Note that Status fields are not compared. |
| 16 | +// To compare status fields, use ObjectStatusesEqual |
| 17 | +func ObjectsEqual(obj1, obj2 runtime.Object) bool { |
| 18 | + value1, value2 := reflect.ValueOf(obj1), reflect.ValueOf(obj2) |
| 19 | + |
| 20 | + if value1.Type() != value2.Type() { |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + if value1.Kind() == reflect.Ptr { |
| 25 | + value1 = value1.Elem() |
| 26 | + value2 = value2.Elem() |
| 27 | + } |
| 28 | + |
| 29 | + if meta1, hasMeta := obj1.(metav1.Object); hasMeta { |
| 30 | + if !ObjectMetasEqual(meta1, obj2.(metav1.Object)) { |
| 31 | + return false |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // recurse through fields of both, comparing each: |
| 36 | + for i := 0; i < value1.NumField(); i++ { |
| 37 | + field1Name := value1.Type().Field(i).Name |
| 38 | + if field1Name == "ObjectMeta" { |
| 39 | + // skip ObjectMeta field, as we already asserted relevant fields are equal |
| 40 | + continue |
| 41 | + } |
| 42 | + if field1Name == "TypeMeta" { |
| 43 | + // skip TypeMeta field, as it is set by the server and not relevant for object comparison |
| 44 | + continue |
| 45 | + } |
| 46 | + if field1Name == "Status" { |
| 47 | + // skip Status field, as it is considered a separate and not relevant for object comparison |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + field1 := mkPointer(value1.Field(i)) |
| 52 | + field2 := mkPointer(value2.Field(i)) |
| 53 | + |
| 54 | + // assert DeepEquality any other fields |
| 55 | + if !DeepEqual(field1, field2) { |
| 56 | + return false |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return true |
| 61 | +} |
| 62 | + |
| 63 | +// returns true if "relevant" parts of obj1 and obj2 have equal: |
| 64 | +// -labels |
| 65 | +// -annotations |
| 66 | +// -namespace+name |
| 67 | +// or if the objects are not metav1.Objects |
| 68 | +func ObjectMetasEqual(obj1, obj2 metav1.Object) bool { |
| 69 | + return obj1.GetNamespace() == obj2.GetNamespace() && |
| 70 | + obj1.GetName() == obj2.GetName() && |
| 71 | + mapStringEqual(obj1.GetLabels(), obj2.GetLabels()) && |
| 72 | + mapStringEqual(obj1.GetAnnotations(), obj2.GetAnnotations()) |
| 73 | +} |
| 74 | + |
| 75 | +func mapStringEqual(map1, map2 map[string]string) bool { |
| 76 | + if map1 == nil && map2 == nil { |
| 77 | + return true |
| 78 | + } |
| 79 | + |
| 80 | + if len(map1) != len(map2) { |
| 81 | + return false |
| 82 | + } |
| 83 | + |
| 84 | + for key1, val1 := range map1 { |
| 85 | + val2, ok := map2[key1] |
| 86 | + if !ok { |
| 87 | + return false |
| 88 | + } |
| 89 | + if val1 != val2 { |
| 90 | + return false |
| 91 | + } |
| 92 | + } |
| 93 | + return true |
| 94 | +} |
| 95 | + |
| 96 | +// if i is a pointer, just return the value. |
| 97 | +// if i is addressable, return that. |
| 98 | +// if i is a struct passed in by value, make a new instance of the type and copy the contents to that and return |
| 99 | +// the pointer to that. |
| 100 | +func mkPointer(val reflect.Value) interface{} { |
| 101 | + if val.Kind() == reflect.Ptr { |
| 102 | + return val.Interface() |
| 103 | + } |
| 104 | + if val.CanAddr() { |
| 105 | + return val.Addr().Interface() |
| 106 | + } |
| 107 | + if val.Kind() == reflect.Struct { |
| 108 | + nv := reflect.New(val.Type()) |
| 109 | + nv.Elem().Set(val) |
| 110 | + return nv.Interface() |
| 111 | + } |
| 112 | + return val.Interface() |
| 113 | +} |
| 114 | + |
| 115 | +// DeepEqual should be used in place of reflect.DeepEqual when the type of an object is unknown and may be a proto message. |
| 116 | +// see https://github.com/golang/protobuf/issues/1173 for details on why reflect.DeepEqual no longer works for proto messages |
| 117 | +func DeepEqual(val1, val2 interface{}) bool { |
| 118 | + protoVal1, isProto := val1.(protoV2.Message) |
| 119 | + if isProto { |
| 120 | + protoVal2, isProto := val2.(protoV2.Message) |
| 121 | + if !isProto { |
| 122 | + return false // different types |
| 123 | + } |
| 124 | + return protoV2.Equal(protoVal1, protoVal2) |
| 125 | + } |
| 126 | + return reflect.DeepEqual(val1, val2) |
| 127 | +} |
0 commit comments