forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor checkpointing #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bart0sh
wants to merge
1
commit into
pohly:dra-1.31
Choose a base branch
from
bart0sh:PR150-dra-refactor-checkpoint
base: dra-1.31
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package checkpoint | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" | ||
state "k8s.io/kubernetes/pkg/kubelet/cm/dra/state/v1" | ||
) | ||
|
||
const ( | ||
CheckpointAPIGroup = "checkpoint.dra.kubelet.k8s.io" | ||
CheckpointKind = "DRACheckpoint" | ||
CheckpointAPIVersion = CheckpointAPIGroup + "/" + state.Version | ||
) | ||
|
||
// Checkpoint represents a structure to store DRA checkpoint data | ||
type Checkpoint struct { | ||
// Data is a JSON serialized checkpoint data | ||
// See state.CheckpointData for the details | ||
Data string | ||
// Checksum is a checksum of Data | ||
Checksum checksum.Checksum | ||
} | ||
|
||
type CheckpointData struct { | ||
metav1.TypeMeta | ||
Entries state.ClaimInfoStateList | ||
} | ||
|
||
// NewCheckpoint creates a new checkpoint from a list of claim info states | ||
func NewCheckpoint(data state.ClaimInfoStateList) (*Checkpoint, error) { | ||
cpData := &CheckpointData{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: CheckpointKind, | ||
APIVersion: CheckpointAPIVersion, | ||
}, | ||
Entries: data, | ||
} | ||
|
||
cpDataBytes, err := json.Marshal(cpData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Checkpoint{ | ||
Data: string(cpDataBytes), | ||
Checksum: checksum.New(string(cpDataBytes)), | ||
}, nil | ||
} | ||
|
||
// MarshalCheckpoint marshals checkpoint to JSON | ||
func (cp *Checkpoint) MarshalCheckpoint() ([]byte, error) { | ||
return json.Marshal(cp) | ||
} | ||
|
||
// UnmarshalCheckpoint unmarshals checkpoint from JSON | ||
// and verifies its data checksum | ||
func (cp *Checkpoint) UnmarshalCheckpoint(blob []byte) error { | ||
if err := json.Unmarshal(blob, cp); err != nil { | ||
return err | ||
} | ||
|
||
// verify checksum | ||
if err := cp.VerifyChecksum(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// VerifyChecksum verifies that current checksum | ||
// of checkpointed Data is valid | ||
func (cp *Checkpoint) VerifyChecksum() error { | ||
return cp.Checksum.Verify(cp.Data) | ||
} | ||
|
||
// GetEntries returns list of claim info states from checkpoint | ||
func (cp *Checkpoint) GetEntries() (state.ClaimInfoStateList, error) { | ||
var cpData CheckpointData | ||
if err := json.Unmarshal([]byte(cp.Data), &cpData); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cpData.Entries, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package checkpoint | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager" | ||
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors" | ||
state "k8s.io/kubernetes/pkg/kubelet/cm/dra/state/v1" | ||
) | ||
|
||
type Checkpointer interface { | ||
GetOrCreate() (*Checkpoint, error) | ||
Store(*Checkpoint) error | ||
} | ||
|
||
type checkpointer struct { | ||
sync.RWMutex | ||
checkpointManager checkpointmanager.CheckpointManager | ||
checkpointName string | ||
} | ||
|
||
// NewCheckpointer creates new checkpointer for keeping track of claim info with checkpoint backend | ||
func NewCheckpointer(stateDir, checkpointName string) (Checkpointer, error) { | ||
if len(checkpointName) == 0 { | ||
return nil, fmt.Errorf("received empty string instead of checkpointName") | ||
} | ||
|
||
checkpointManager, err := checkpointmanager.NewCheckpointManager(stateDir) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialize checkpoint manager: %v", err) | ||
} | ||
checkpointer := &checkpointer{ | ||
checkpointManager: checkpointManager, | ||
checkpointName: checkpointName, | ||
} | ||
|
||
return checkpointer, nil | ||
} | ||
|
||
// GetOrCreate gets list of claim info states from a checkpoint | ||
// or creates empty list it checkpoint doesn't exist yet | ||
func (sc *checkpointer) GetOrCreate() (*Checkpoint, error) { | ||
sc.Lock() | ||
defer sc.Unlock() | ||
|
||
checkpoint, err := NewCheckpoint(nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create new checkpoint: %w", err) | ||
} | ||
|
||
err = sc.checkpointManager.GetCheckpoint(sc.checkpointName, checkpoint) | ||
if err == errors.ErrCheckpointNotFound { | ||
sc.store(checkpoint) | ||
return NewCheckpoint(state.ClaimInfoStateList{}) | ||
} | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get checkpoint %v: %w", sc.checkpointName, err) | ||
} | ||
|
||
return checkpoint, nil | ||
} | ||
|
||
// Store stores checkpoint to the file | ||
func (sc *checkpointer) Store(checkpoint *Checkpoint) error { | ||
sc.Lock() | ||
defer sc.Unlock() | ||
|
||
return sc.store(checkpoint) | ||
} | ||
|
||
// store saves state to a checkpoint, caller is responsible for locking | ||
func (sc *checkpointer) store(checkpoint *Checkpoint) error { | ||
if err := sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint); err != nil { | ||
return fmt.Errorf("could not save checkpoint %s: %v", sc.checkpointName, err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be called
checkpointerV1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it should always work with the latest API version.