github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/network/state/checkpoint.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package state
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
    23  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
    24  )
    25  
    26  var _ checkpointmanager.Checkpoint = &NetworkPluginCheckpoint{}
    27  
    28  type NetworkPluginCheckpoint struct {
    29  	PolicyName   string            `json:"policyName"`
    30  	MachineState NICMap            `json:"machineState"`
    31  	PodEntries   PodEntries        `json:"pod_entries"`
    32  	Checksum     checksum.Checksum `json:"checksum"`
    33  }
    34  
    35  func NewNetworkPluginCheckpoint() *NetworkPluginCheckpoint {
    36  	return &NetworkPluginCheckpoint{
    37  		PodEntries:   make(PodEntries),
    38  		MachineState: make(NICMap),
    39  	}
    40  }
    41  
    42  // MarshalCheckpoint returns marshaled checkpoint
    43  func (cp *NetworkPluginCheckpoint) MarshalCheckpoint() ([]byte, error) {
    44  	// make sure checksum wasn't set before, so it doesn't affect output checksum
    45  	cp.Checksum = 0
    46  	cp.Checksum = checksum.New(cp)
    47  	return json.Marshal(*cp)
    48  }
    49  
    50  // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint
    51  func (cp *NetworkPluginCheckpoint) UnmarshalCheckpoint(blob []byte) error {
    52  	return json.Unmarshal(blob, cp)
    53  }
    54  
    55  // VerifyChecksum verifies that current checksum of checkpoint is valid
    56  func (cp *NetworkPluginCheckpoint) VerifyChecksum() error {
    57  	ck := cp.Checksum
    58  	cp.Checksum = 0
    59  	err := ck.Verify(cp)
    60  	cp.Checksum = ck
    61  	return err
    62  }