Skip to content

Commit

Permalink
allow data field and empty field when calculating config hash
Browse files Browse the repository at this point in the history
Signed-off-by: haoqing0110 <[email protected]>
  • Loading branch information
haoqing0110 committed Mar 8, 2024
1 parent 6c27796 commit 8dfa4b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pkg/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,15 @@ func IsOwnedByCMA(addon *addonapiv1alpha1.ManagedClusterAddOn) bool {
return false
}

// GetSpecHash returns the sha256 hash of the spec field of the given object
// GetSpecHash returns the sha256 hash of the spec field or data field of the given object
func GetSpecHash(obj *unstructured.Unstructured) (string, error) {
if obj == nil {
return "", fmt.Errorf("object is nil")
}
spec, ok := obj.Object["spec"]
if !ok {
return "", fmt.Errorf("object has no spec field")
// handle cases like secret and configmaps
spec = obj.Object["data"]
}

specBytes, err := json.Marshal(spec)
Expand Down
21 changes: 17 additions & 4 deletions pkg/utils/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ func TestGetSpecHash(t *testing.T) {
expectedErr: fmt.Errorf("object is nil"),
},
{
name: "no spec",
obj: &unstructured.Unstructured{},
expectedErr: fmt.Errorf("object has no spec field"),
name: "no spec and data",
obj: &unstructured.Unstructured{},
expectedErr: nil,
expectedHash: "74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
},
{
name: "hash",
name: "has spec",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
Expand All @@ -104,6 +105,18 @@ func TestGetSpecHash(t *testing.T) {
expectedErr: nil,
expectedHash: "1da06016289bd76a5ada4f52fc805ae0c394612f17ec6d0f0c29b636473c8a9d",
},
{
name: "has data",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"data": map[string]interface{}{
"test": 1,
},
},
},
expectedErr: nil,
expectedHash: "1da06016289bd76a5ada4f52fc805ae0c394612f17ec6d0f0c29b636473c8a9d",
},
}

for _, c := range cases {
Expand Down

0 comments on commit 8dfa4b6

Please sign in to comment.