github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/kcc/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 kcc
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"sync"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
    26  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
    27  
    28  	"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic/crd"
    29  )
    30  
    31  type ConfigManagerCheckpoint interface {
    32  	checkpointmanager.Checkpoint
    33  	GetData(kind string) (reflect.Value, metav1.Time)
    34  	SetData(kind string, v reflect.Value, t metav1.Time)
    35  }
    36  
    37  type TargetConfigData struct {
    38  	// Value only store spec of dynamic config crd
    39  	Value     *crd.DynamicConfigCRD
    40  	Timestamp int64
    41  }
    42  
    43  // Data holds checkpoint data and its checksum
    44  type Data struct {
    45  	sync.Mutex
    46  	Item *DataItem
    47  }
    48  
    49  type DataItem struct {
    50  	// Data maps from kind to target config data
    51  	Data     map[string]TargetConfigData
    52  	Checksum checksum.Checksum
    53  }
    54  
    55  // NewCheckpoint returns an instance of Checkpoint
    56  func NewCheckpoint(configResponses map[string]TargetConfigData) ConfigManagerCheckpoint {
    57  	return &Data{
    58  		Item: &DataItem{
    59  			Data: configResponses,
    60  		},
    61  	}
    62  }
    63  
    64  func (d *Data) MarshalCheckpoint() ([]byte, error) {
    65  	d.Lock()
    66  	defer d.Unlock()
    67  
    68  	d.Item.Checksum = checksum.New(d.Item.Data)
    69  	return json.Marshal(*(d.Item))
    70  }
    71  
    72  func (d *Data) UnmarshalCheckpoint(blob []byte) error {
    73  	d.Lock()
    74  	defer d.Unlock()
    75  
    76  	return json.Unmarshal(blob, d.Item)
    77  }
    78  
    79  func (d *Data) VerifyChecksum() error {
    80  	d.Lock()
    81  	defer d.Unlock()
    82  
    83  	return d.Item.Checksum.Verify(d.Item.Data)
    84  }
    85  
    86  func (d *Data) GetData(kind string) (reflect.Value, metav1.Time) {
    87  	d.Lock()
    88  	defer d.Unlock()
    89  
    90  	if data, ok := d.Item.Data[kind]; ok {
    91  		configField := reflect.ValueOf(data.Value).Elem().FieldByName(kind)
    92  		return configField, metav1.Unix(data.Timestamp, 0)
    93  	}
    94  
    95  	return reflect.Value{}, metav1.Time{}
    96  }
    97  
    98  func (d *Data) SetData(kind string, val reflect.Value, t metav1.Time) {
    99  	d.Lock()
   100  	defer d.Unlock()
   101  
   102  	if d.Item.Data == nil {
   103  		d.Item.Data = make(map[string]TargetConfigData)
   104  	}
   105  
   106  	// get target dynamic configField by kind
   107  	dynamicConfiguration := &crd.DynamicConfigCRD{}
   108  	configField := reflect.ValueOf(dynamicConfiguration).Elem().FieldByName(kind)
   109  	configField.Set(reflect.New(configField.Type().Elem()))
   110  
   111  	// only set target dynamic configField's spec field
   112  	specValue := val.Elem().FieldByName("Spec")
   113  	specField := configField.Elem().FieldByName("Spec")
   114  	specField.Set(specValue)
   115  
   116  	d.Item.Data[kind] = TargetConfigData{
   117  		Value:     dynamicConfiguration,
   118  		Timestamp: t.Unix(),
   119  	}
   120  }