Skip to content

Commit 035f6ac

Browse files
authored
Merge pull request #2199 from shiftstack/utils-hash
🌱 Remove pkg/utils/hash
2 parents e31a757 + 0f945e1 commit 035f6ac

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

pkg/cloud/services/compute/instance.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
4040
capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors"
4141
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/filterconvert"
42-
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/hash"
4342
)
4443

4544
const (
@@ -592,11 +591,3 @@ func getTimeout(name string, timeout int) time.Duration {
592591
}
593592
return time.Duration(timeout)
594593
}
595-
596-
func HashInstanceSpec(computeInstance *InstanceSpec) (string, error) {
597-
instanceHash, err := hash.ComputeSpewHash(computeInstance)
598-
if err != nil {
599-
return "", err
600-
}
601-
return strconv.Itoa(int(instanceHash)), nil
602-
}

pkg/utils/hash/hash.go renamed to pkg/scope/hash.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package hash
17+
package scope
1818

1919
import (
2020
"fmt"
2121
"hash"
2222
"hash/fnv"
2323

2424
"github.com/davecgh/go-spew/spew"
25-
"k8s.io/apimachinery/pkg/util/dump"
2625
)
2726

28-
// SpewHashObject writes specified object to hash using the spew library
27+
// spewHashObject writes specified object to hash using the spew library
2928
// which follows pointers and prints actual values of the nested objects
3029
// ensuring the hash does not change when a pointer changes.
31-
func SpewHashObject(hasher hash.Hash, objectToWrite interface{}) error {
30+
func spewHashObject(hasher hash.Hash, objectToWrite interface{}) error {
3231
hasher.Reset()
3332
printer := spew.ConfigState{
3433
Indent: " ",
@@ -43,20 +42,11 @@ func SpewHashObject(hasher hash.Hash, objectToWrite interface{}) error {
4342
return nil
4443
}
4544

46-
// ComputeSpewHash computes the hash of a InstanceSpec using the spew library.
47-
func ComputeSpewHash(objectToWrite interface{}) (uint32, error) {
45+
// computeSpewHash computes the hash of an object using the spew library.
46+
func computeSpewHash(objectToWrite interface{}) (uint32, error) {
4847
instanceSpecHasher := fnv.New32a()
49-
if err := SpewHashObject(instanceSpecHasher, objectToWrite); err != nil {
48+
if err := spewHashObject(instanceSpecHasher, objectToWrite); err != nil {
5049
return 0, err
5150
}
5251
return instanceSpecHasher.Sum32(), nil
5352
}
54-
55-
// DeepHashObject writes specified object to hash using the spew library
56-
// which follows pointers and prints actual values of the nested objects
57-
// ensuring the hash does not change when a pointer changes.
58-
// This function is taken from https://github.com/kubernetes/kubernetes/blob/v1.29.2/pkg/util/hash/hash.go#L26-L32
59-
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
60-
hasher.Reset()
61-
fmt.Fprintf(hasher, "%v", dump.ForHash(objectToWrite))
62-
}

pkg/scope/provider.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939

4040
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
4141
"sigs.k8s.io/cluster-api-provider-openstack/pkg/clients"
42-
"sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/hash"
4342
"sigs.k8s.io/cluster-api-provider-openstack/version"
4443
)
4544

@@ -88,7 +87,7 @@ func (f *providerScopeFactory) NewClientScopeFromObject(ctx context.Context, ctr
8887
}
8988

9089
func getScopeCacheKey(cloud clientconfig.Cloud) (string, error) {
91-
key, err := hash.ComputeSpewHash(cloud)
90+
key, err := computeSpewHash(cloud)
9291
if err != nil {
9392
return "", err
9493
}

pkg/utils/conversion/restore.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ package conversion
1919
import (
2020
"bytes"
2121
"encoding/json"
22+
"fmt"
23+
"hash"
2224
"hash/crc64"
2325
"reflect"
2426

2527
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
conversion "k8s.io/apimachinery/pkg/conversion"
29+
"k8s.io/apimachinery/pkg/util/dump"
2730
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
28-
29-
hasher "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/hash"
3031
)
3132

3233
// RestorerFor holds all field restorers for a given type T.
@@ -109,6 +110,18 @@ type hashedFieldRestorer[T any, F any] struct {
109110

110111
var _ FieldRestorerFor[any] = hashedFieldRestorer[any, any]{}
111112

113+
// deepHashObject writes the specified object to hash using the spew library
114+
// which follows pointers and prints actual values of the nested objects
115+
// ensuring the hash does not change when a pointer changes. This function is
116+
// taken from
117+
// https://github.com/kubernetes/kubernetes/blob/v1.29.2/pkg/util/hash/hash.go#L26-L32
118+
//
119+
//nolint:unused
120+
func deepHashObject(hasher hash.Hash, objectToWrite interface{}) {
121+
hasher.Reset()
122+
fmt.Fprintf(hasher, "%v", dump.ForHash(objectToWrite))
123+
}
124+
112125
//nolint:unused
113126
type hashedFieldRestoreState struct {
114127
Hash []byte `json:"h,omitempty"`
@@ -124,7 +137,7 @@ func (r hashedFieldRestorer[T, F]) getHash(obj T) []byte {
124137

125138
table := crc64.MakeTable(crc64.ECMA)
126139
hash := crc64.New(table)
127-
hasher.DeepHashObject(hash, f)
140+
deepHashObject(hash, f)
128141
return hash.Sum(make([]byte, 0, 8))
129142
}
130143

0 commit comments

Comments
 (0)