...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/dra/state/state_checkpoint.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/dra/state

     1  /*
     2  Copyright 2023 The Kubernetes 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  	"fmt"
    21  	"sync"
    22  
    23  	resourcev1alpha2 "k8s.io/api/resource/v1alpha2"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
    27  	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
    28  )
    29  
    30  var _ CheckpointState = &stateCheckpoint{}
    31  
    32  // CheckpointState interface provides to get and store state
    33  type CheckpointState interface {
    34  	GetOrCreate() (ClaimInfoStateList, error)
    35  	Store(ClaimInfoStateList) error
    36  }
    37  
    38  // ClaimInfoState is used to store claim info state in a checkpoint
    39  type ClaimInfoState struct {
    40  	// Name of the DRA driver
    41  	DriverName string
    42  
    43  	// ClassName is a resource class of the claim
    44  	ClassName string
    45  
    46  	// ClaimUID is an UID of the resource claim
    47  	ClaimUID types.UID
    48  
    49  	// ClaimName is a name of the resource claim
    50  	ClaimName string
    51  
    52  	// Namespace is a claim namespace
    53  	Namespace string
    54  
    55  	// PodUIDs is a set of pod UIDs that reference a resource
    56  	PodUIDs sets.Set[string]
    57  
    58  	// ResourceHandles is a list of opaque resource data for processing by a specific kubelet plugin
    59  	ResourceHandles []resourcev1alpha2.ResourceHandle
    60  
    61  	// CDIDevices is a map of DriverName --> CDI devices returned by the
    62  	// GRPC API call NodePrepareResource
    63  	CDIDevices map[string][]string
    64  }
    65  
    66  // ClaimInfoStateWithoutResourceHandles is an old implementation of the ClaimInfoState
    67  // TODO: remove in Beta
    68  type ClaimInfoStateWithoutResourceHandles struct {
    69  	// Name of the DRA driver
    70  	DriverName string
    71  
    72  	// ClassName is a resource class of the claim
    73  	ClassName string
    74  
    75  	// ClaimUID is an UID of the resource claim
    76  	ClaimUID types.UID
    77  
    78  	// ClaimName is a name of the resource claim
    79  	ClaimName string
    80  
    81  	// Namespace is a claim namespace
    82  	Namespace string
    83  
    84  	// PodUIDs is a set of pod UIDs that reference a resource
    85  	PodUIDs sets.Set[string]
    86  
    87  	// CDIDevices is a map of DriverName --> CDI devices returned by the
    88  	// GRPC API call NodePrepareResource
    89  	CDIDevices map[string][]string
    90  }
    91  
    92  type stateCheckpoint struct {
    93  	sync.RWMutex
    94  	checkpointManager checkpointmanager.CheckpointManager
    95  	checkpointName    string
    96  }
    97  
    98  // NewCheckpointState creates new State for keeping track of claim info  with checkpoint backend
    99  func NewCheckpointState(stateDir, checkpointName string) (*stateCheckpoint, error) {
   100  	if len(checkpointName) == 0 {
   101  		return nil, fmt.Errorf("received empty string instead of checkpointName")
   102  	}
   103  
   104  	checkpointManager, err := checkpointmanager.NewCheckpointManager(stateDir)
   105  	if err != nil {
   106  		return nil, fmt.Errorf("failed to initialize checkpoint manager: %v", err)
   107  	}
   108  	stateCheckpoint := &stateCheckpoint{
   109  		checkpointManager: checkpointManager,
   110  		checkpointName:    checkpointName,
   111  	}
   112  
   113  	return stateCheckpoint, nil
   114  }
   115  
   116  // get state from a checkpoint and creates it if it doesn't exist
   117  func (sc *stateCheckpoint) GetOrCreate() (ClaimInfoStateList, error) {
   118  	sc.Lock()
   119  	defer sc.Unlock()
   120  
   121  	checkpoint := NewDRAManagerCheckpoint()
   122  	err := sc.checkpointManager.GetCheckpoint(sc.checkpointName, checkpoint)
   123  	if err == errors.ErrCheckpointNotFound {
   124  		sc.store(ClaimInfoStateList{})
   125  		return ClaimInfoStateList{}, nil
   126  	}
   127  	if err != nil {
   128  		return nil, fmt.Errorf("failed to get checkpoint %v: %v", sc.checkpointName, err)
   129  	}
   130  
   131  	return checkpoint.Entries, nil
   132  }
   133  
   134  // saves state to a checkpoint
   135  func (sc *stateCheckpoint) Store(claimInfoStateList ClaimInfoStateList) error {
   136  	sc.Lock()
   137  	defer sc.Unlock()
   138  
   139  	return sc.store(claimInfoStateList)
   140  }
   141  
   142  // saves state to a checkpoint, caller is responsible for locking
   143  func (sc *stateCheckpoint) store(claimInfoStateList ClaimInfoStateList) error {
   144  	checkpoint := NewDRAManagerCheckpoint()
   145  	checkpoint.Entries = claimInfoStateList
   146  
   147  	err := sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint)
   148  	if err != nil {
   149  		return fmt.Errorf("could not save checkpoint %s: %v", sc.checkpointName, err)
   150  	}
   151  	return nil
   152  }
   153  

View as plain text