checksum.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package checksum
  14. import (
  15. "hash/fnv"
  16. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
  17. hashutil "k8s.io/kubernetes/pkg/util/hash"
  18. )
  19. // Checksum is the data to be stored as checkpoint
  20. type Checksum uint64
  21. // Verify verifies that passed checksum is same as calculated checksum
  22. func (cs Checksum) Verify(data interface{}) error {
  23. if cs != New(data) {
  24. return errors.ErrCorruptCheckpoint
  25. }
  26. return nil
  27. }
  28. // New returns the Checksum of checkpoint data
  29. func New(data interface{}) Checksum {
  30. return Checksum(getChecksum(data))
  31. }
  32. // Get returns calculated checksum of checkpoint data
  33. func getChecksum(data interface{}) uint64 {
  34. hash := fnv.New32a()
  35. hashutil.DeepHashObject(hash, data)
  36. return uint64(hash.Sum32())
  37. }