checkpoint.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2017 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 checkpoint
  14. import (
  15. "encoding/json"
  16. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  17. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
  18. )
  19. // DeviceManagerCheckpoint defines the operations to retrieve pod devices
  20. type DeviceManagerCheckpoint interface {
  21. checkpointmanager.Checkpoint
  22. GetData() ([]PodDevicesEntry, map[string][]string)
  23. }
  24. // PodDevicesEntry connects pod information to devices
  25. type PodDevicesEntry struct {
  26. PodUID string
  27. ContainerName string
  28. ResourceName string
  29. DeviceIDs []string
  30. AllocResp []byte
  31. }
  32. // checkpointData struct is used to store pod to device allocation information
  33. // in a checkpoint file.
  34. // TODO: add version control when we need to change checkpoint format.
  35. type checkpointData struct {
  36. PodDeviceEntries []PodDevicesEntry
  37. RegisteredDevices map[string][]string
  38. }
  39. // Data holds checkpoint data and its checksum
  40. type Data struct {
  41. Data checkpointData
  42. Checksum checksum.Checksum
  43. }
  44. // New returns an instance of Checkpoint
  45. func New(devEntries []PodDevicesEntry,
  46. devices map[string][]string) DeviceManagerCheckpoint {
  47. return &Data{
  48. Data: checkpointData{
  49. PodDeviceEntries: devEntries,
  50. RegisteredDevices: devices,
  51. },
  52. }
  53. }
  54. // MarshalCheckpoint returns marshalled data
  55. func (cp *Data) MarshalCheckpoint() ([]byte, error) {
  56. cp.Checksum = checksum.New(cp.Data)
  57. return json.Marshal(*cp)
  58. }
  59. // UnmarshalCheckpoint returns unmarshalled data
  60. func (cp *Data) UnmarshalCheckpoint(blob []byte) error {
  61. return json.Unmarshal(blob, cp)
  62. }
  63. // VerifyChecksum verifies that passed checksum is same as calculated checksum
  64. func (cp *Data) VerifyChecksum() error {
  65. return cp.Checksum.Verify(cp.Data)
  66. }
  67. // GetData returns device entries and registered devices
  68. func (cp *Data) GetData() ([]PodDevicesEntry, map[string][]string) {
  69. return cp.Data.PodDeviceEntries, cp.Data.RegisteredDevices
  70. }