-
Notifications
You must be signed in to change notification settings - Fork 110
Encapsulate Checkpoint internal state #148
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
nojnhuh
wants to merge
13
commits into
kubernetes-sigs:main
Choose a base branch
from
nojnhuh:encapsulate_checkpoint
base: main
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
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e496575
encapsulate Checkpoint internal state
bg-chun bbb1488
Replace k/k checkpoint manager with file reads/writes
nojnhuh 8405685
Rework Checkpoint API
nojnhuh 848c141
Describe how to handle checkpoints
nojnhuh ceed6df
Merge branch 'main' of github.com:kubernetes-sigs/dra-example-driver …
nojnhuh 08a2c1a
revert docker build output
nojnhuh 1f1debb
json -> JSON in error message
nojnhuh 15f3412
rename resourceCheckpoint -> restoreClaimFromCheckpoint
nojnhuh 452ec5f
defer writing checkpoint until NodePrepareResources
nojnhuh 4ee44af
Use install package to create checkpoint scheme
nojnhuh 5c68cb5
Describe how to update the Checkpoint API
nojnhuh d102956
Justify strict/non-strict decoding
nojnhuh ff58c14
Rename Checkpoint v1alpha1 -> v1
nojnhuh 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
Some comments aren't visible on the classic Files Changed page.
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
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
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 |
|---|---|---|
| @@ -1,53 +1,74 @@ | ||
| /* | ||
| * Copyright 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 main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" | ||
| ) | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| type Checkpoint struct { | ||
| Checksum checksum.Checksum `json:"checksum"` | ||
| V1 *CheckpointV1 `json:"v1,omitempty"` | ||
| } | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/utils/ptr" | ||
|
|
||
| type CheckpointV1 struct { | ||
| PreparedClaims PreparedClaims `json:"preparedClaims,omitempty"` | ||
| } | ||
| checkpointapi "sigs.k8s.io/dra-example-driver/internal/api/checkpoint" | ||
| ) | ||
|
|
||
| func newCheckpoint() *Checkpoint { | ||
| pc := &Checkpoint{ | ||
| Checksum: 0, | ||
| V1: &CheckpointV1{ | ||
| PreparedClaims: make(PreparedClaims), | ||
| }, | ||
| // readCheckpoint returns the Checkpoint at the given path in the format | ||
| // expected by the given decoder. If the path doesn't exist, returns an empty | ||
| // Checkpoint and no error. | ||
| func readCheckpoint(path string, decoder runtime.Decoder) (*checkpointapi.Checkpoint, error) { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil && !errors.Is(err, fs.ErrNotExist) { | ||
| return nil, err | ||
| } | ||
| checkpoint := new(checkpointapi.Checkpoint) | ||
| if data != nil { | ||
| _, _, err = decoder.Decode(data, ptr.To(checkpointapi.SchemeGroupVersion.WithKind("Checkpoint")), checkpoint) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unmarshal JSON from %s: %w", path, err) | ||
| } | ||
| } | ||
| return pc | ||
| return checkpoint, nil | ||
| } | ||
|
|
||
| func (cp *Checkpoint) MarshalCheckpoint() ([]byte, error) { | ||
| cp.Checksum = 0 | ||
| out, err := json.Marshal(*cp) | ||
| // writeCheckpoint writes checkpoint to the file at path in | ||
| // the format prescribed by encoder. The file is overwritten if it already | ||
| // exists and is created if it does not already exist. | ||
| func writeCheckpoint(path string, encoder runtime.Encoder, checkpoint *checkpointapi.Checkpoint) (err error) { | ||
| dir := filepath.Dir(path) | ||
| tmp, err := os.CreateTemp(dir, "tmp-checkpoint-*") | ||
| if err != nil { | ||
| return nil, err | ||
| return fmt.Errorf("create temp file in %s: %w", dir, err) | ||
| } | ||
| cp.Checksum = checksum.New(out) | ||
| return json.Marshal(*cp) | ||
| } | ||
|
|
||
| func (cp *Checkpoint) UnmarshalCheckpoint(data []byte) error { | ||
| return json.Unmarshal(data, cp) | ||
| } | ||
|
|
||
| func (cp *Checkpoint) VerifyChecksum() error { | ||
| ck := cp.Checksum | ||
| cp.Checksum = 0 | ||
| defer func() { | ||
| cp.Checksum = ck | ||
| if err1 := tmp.Close(); err1 != nil && err == nil { | ||
| err = fmt.Errorf("close temp file: %w", err1) | ||
| } | ||
| }() | ||
| out, err := json.Marshal(*cp) | ||
| if err != nil { | ||
| return err | ||
| if err := encoder.Encode(checkpoint, tmp); err != nil { | ||
| return fmt.Errorf("encode to temp file %s: %w", tmp.Name(), err) | ||
| } | ||
| if err := tmp.Sync(); err != nil { | ||
| return fmt.Errorf("sync temp file: %w", err) | ||
| } | ||
| if err := os.Rename(tmp.Name(), path); err != nil { | ||
|
nojnhuh marked this conversation as resolved.
|
||
| return fmt.Errorf("rename %s to %s: %w", tmp.Name(), path, err) | ||
| } | ||
| return ck.Verify(out) | ||
| return 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,70 @@ | ||
| /* | ||
| * Copyright 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 main | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "k8s.io/apimachinery/pkg/types" | ||
|
|
||
| checkpointapi "sigs.k8s.io/dra-example-driver/internal/api/checkpoint" | ||
| ) | ||
|
|
||
| func TestReadWriteCheckpointRoundtrip(t *testing.T) { | ||
| tests := map[string]struct { | ||
| checkpoint *checkpointapi.Checkpoint | ||
| }{ | ||
| "new checkpoint": { | ||
| checkpoint: new(checkpointapi.Checkpoint), | ||
| }, | ||
| "populated checkpoint": { | ||
| checkpoint: &checkpointapi.Checkpoint{ | ||
| PreparedClaims: []checkpointapi.PreparedClaim{ | ||
| {UID: types.UID("123")}, | ||
| {UID: types.UID("456")}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, test := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, DriverPluginCheckpointFile) | ||
|
|
||
| decoder, encoder, err := checkpointSerializer() | ||
| if err != nil { | ||
| t.Fatal("failed to initialize checkpoint serializer:", err) | ||
| } | ||
|
|
||
| checkpoint, err := readCheckpoint(path, decoder) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, new(checkpointapi.Checkpoint), checkpoint) | ||
|
|
||
| checkpoint = test.checkpoint | ||
| err = writeCheckpoint(path, encoder, checkpoint) | ||
| require.NoError(t, err) | ||
|
|
||
| read, err := readCheckpoint(path, decoder) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, test.checkpoint, read) | ||
| }) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.