|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "k8s.io/dynamic-resource-allocation/kubeletplugin" |
| 7 | + drapbv1 "k8s.io/kubelet/pkg/apis/dra/v1beta1" |
| 8 | + "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" |
| 9 | +) |
| 10 | + |
| 11 | +// Legacy structs and methods to help with conversion as necessary. |
| 12 | +type PreparedDeviceList2503RC2 []PreparedDevice2503RC2 |
| 13 | +type PreparedDevices2503RC2 []*PreparedDeviceGroup2503RC2 |
| 14 | +type PreparedClaims2503RC2 map[string]PreparedDevices2503RC2 |
| 15 | + |
| 16 | +type PreparedDevice2503RC2 struct { |
| 17 | + Channel *PreparedComputeDomainChannel2503RC2 `json:"channel"` |
| 18 | + Daemon *PreparedComputeDomainDaemon2503RC2 `json:"daemon"` |
| 19 | +} |
| 20 | + |
| 21 | +type PreparedComputeDomainChannel2503RC2 struct { |
| 22 | + Info *ComputeDomainChannelInfo `json:"info"` |
| 23 | + Device *drapbv1.Device `json:"device"` |
| 24 | +} |
| 25 | + |
| 26 | +type PreparedComputeDomainDaemon2503RC2 struct { |
| 27 | + Info *ComputeDomainDaemonInfo `json:"info"` |
| 28 | + Device *drapbv1.Device `json:"device"` |
| 29 | +} |
| 30 | + |
| 31 | +type PreparedDeviceGroup2503RC2 struct { |
| 32 | + Devices PreparedDeviceList2503RC2 `json:"devices"` |
| 33 | + ConfigState DeviceConfigState `json:"configState"` |
| 34 | +} |
| 35 | + |
| 36 | +type Checkpoint2503RC2 struct { |
| 37 | + Checksum checksum.Checksum `json:"checksum"` |
| 38 | + V1 *Checkpoint2503RC2V1 `json:"v1,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +type Checkpoint2503RC2V1 struct { |
| 42 | + PreparedClaims PreparedClaims2503RC2 `json:"preparedClaims,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +func (cp *Checkpoint2503RC2) MarshalCheckpoint() ([]byte, error) { |
| 46 | + cp.Checksum = 0 |
| 47 | + out, err := json.Marshal(*cp) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + cp.Checksum = checksum.New(out) |
| 52 | + return json.Marshal(*cp) |
| 53 | +} |
| 54 | + |
| 55 | +func (cp *Checkpoint2503RC2) UnmarshalCheckpoint(data []byte) error { |
| 56 | + return json.Unmarshal(data, cp) |
| 57 | +} |
| 58 | + |
| 59 | +func (cp *Checkpoint2503RC2) VerifyChecksum() error { |
| 60 | + ck := cp.Checksum |
| 61 | + cp.Checksum = 0 |
| 62 | + defer func() { |
| 63 | + cp.Checksum = ck |
| 64 | + }() |
| 65 | + out, err := json.Marshal(*cp) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + return ck.Verify(out) |
| 70 | +} |
| 71 | + |
| 72 | +// ToV1 converts a PreparedDevice2503RC2 to a PreparedDevice. |
| 73 | +func (d *PreparedDevice2503RC2) ToV1() PreparedDevice { |
| 74 | + device := PreparedDevice{} |
| 75 | + if d.Channel != nil { |
| 76 | + device.Channel = d.Channel.ToV1() |
| 77 | + } |
| 78 | + if d.Daemon != nil { |
| 79 | + device.Daemon = d.Daemon.ToV1() |
| 80 | + } |
| 81 | + return device |
| 82 | +} |
| 83 | + |
| 84 | +// ToV1 converts a PreparedComputeDomainChannel2503RC2 to a PreparedComputeDomainChannel. |
| 85 | +func (c *PreparedComputeDomainChannel2503RC2) ToV1() *PreparedComputeDomainChannel { |
| 86 | + channel := &PreparedComputeDomainChannel{} |
| 87 | + if c.Info != nil { |
| 88 | + channel.Info = c.Info |
| 89 | + } |
| 90 | + if c.Device != nil { |
| 91 | + channel.Device = &kubeletplugin.Device{ |
| 92 | + Requests: c.Device.RequestNames, |
| 93 | + PoolName: c.Device.PoolName, |
| 94 | + DeviceName: c.Device.DeviceName, |
| 95 | + CDIDeviceIDs: c.Device.CDIDeviceIDs, |
| 96 | + } |
| 97 | + } |
| 98 | + return channel |
| 99 | +} |
| 100 | + |
| 101 | +// ToV1 converts a PreparedComputeDomainDaemon2503RC2 to a PreparedComputeDomainDaemon. |
| 102 | +func (d *PreparedComputeDomainDaemon2503RC2) ToV1() *PreparedComputeDomainDaemon { |
| 103 | + daemon := &PreparedComputeDomainDaemon{} |
| 104 | + if d.Info != nil { |
| 105 | + daemon.Info = d.Info |
| 106 | + } |
| 107 | + if d.Device != nil { |
| 108 | + daemon.Device = &kubeletplugin.Device{ |
| 109 | + Requests: d.Device.RequestNames, |
| 110 | + PoolName: d.Device.PoolName, |
| 111 | + DeviceName: d.Device.DeviceName, |
| 112 | + CDIDeviceIDs: d.Device.CDIDeviceIDs, |
| 113 | + } |
| 114 | + } |
| 115 | + return daemon |
| 116 | +} |
| 117 | + |
| 118 | +// ToV1 converts a PreparedDeviceGroup2503RC2 to a PreparedDeviceGroup. |
| 119 | +func (g *PreparedDeviceGroup2503RC2) ToV1() *PreparedDeviceGroup { |
| 120 | + group := &PreparedDeviceGroup{ |
| 121 | + Devices: make(PreparedDeviceList, 0, len(g.Devices)), |
| 122 | + ConfigState: g.ConfigState, |
| 123 | + } |
| 124 | + for _, d := range g.Devices { |
| 125 | + group.Devices = append(group.Devices, d.ToV1()) |
| 126 | + } |
| 127 | + return group |
| 128 | +} |
| 129 | + |
| 130 | +// ToV1 converts a Checkpoint2503RC2 to a Checkpoint. |
| 131 | +func (cp *Checkpoint2503RC2) ToV1() *Checkpoint { |
| 132 | + cpv1 := newCheckpoint() |
| 133 | + for k, v := range cp.V1.PreparedClaims { |
| 134 | + pds := make(PreparedDevices, 0, len(v)) |
| 135 | + for _, pd := range v { |
| 136 | + pds = append(pds, pd.ToV1()) |
| 137 | + } |
| 138 | + cpv1.V1.PreparedClaims[k] = PreparedClaim{ |
| 139 | + PreparedDevices: pds, |
| 140 | + } |
| 141 | + } |
| 142 | + return cpv1 |
| 143 | +} |
0 commit comments