-
Notifications
You must be signed in to change notification settings - Fork 37
Feat/record csi mount config to sandbox annotation #224
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
Merged
furykerry
merged 1 commit into
openkruise:master
from
BH4AWS:feat/record_csi_mount_config_to_sandbox_annotation
Mar 28, 2026
+201
−5
Merged
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
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,174 @@ | ||
| package e2b | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1" | ||
| "github.com/openkruise/agents/pkg/sandbox-manager/infra/sandboxcr" | ||
| "github.com/openkruise/agents/pkg/servers/e2b/models" | ||
| ) | ||
|
|
||
| // TestCsiMountOptionsConfigRecord tests the csiMountOptionsConfigRecord function | ||
| func TestCsiMountOptionsConfigRecord(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| request models.NewSandboxRequest | ||
| initialAnnotations map[string]string | ||
| expectedAnnotationKey string | ||
| expectedAnnotationVal string | ||
| shouldSet bool | ||
| }{ | ||
| { | ||
| name: "empty mount configs", | ||
| request: models.NewSandboxRequest{ | ||
| Extensions: models.NewSandboxRequestExtension{ | ||
| CSIMount: models.CSIMountExtension{ | ||
| MountConfigs: []models.CSIMountConfig{}, | ||
| }, | ||
| }, | ||
| }, | ||
| shouldSet: false, | ||
| }, | ||
| { | ||
| name: "single mount config with all fields", | ||
| request: models.NewSandboxRequest{ | ||
| Extensions: models.NewSandboxRequestExtension{ | ||
| CSIMount: models.CSIMountExtension{ | ||
| MountConfigs: []models.CSIMountConfig{ | ||
| { | ||
| MountID: "mount-123", | ||
| PvName: "pv-nas-001", | ||
| MountPath: "/data", | ||
| SubPath: "subdir", | ||
| ReadOnly: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Metadata: map[string]string{ | ||
| "user-id": "user-456", | ||
| }, | ||
| }, | ||
| initialAnnotations: map[string]string{}, | ||
| expectedAnnotationKey: models.ExtensionKeyClaimWithCSIMount_MountConfig, | ||
| expectedAnnotationVal: `[{"mountID":"mount-123","pvName":"pv-nas-001","mountPath":"/data","subPath":"subdir","readOnly":true}]`, | ||
| shouldSet: true, | ||
| }, | ||
| { | ||
| name: "multiple mount configs with optional fields omitted", | ||
| request: models.NewSandboxRequest{ | ||
| Extensions: models.NewSandboxRequestExtension{ | ||
| CSIMount: models.CSIMountExtension{ | ||
| MountConfigs: []models.CSIMountConfig{ | ||
| { | ||
| PvName: "pv-nas-001", | ||
| MountPath: "/data", | ||
| }, | ||
| { | ||
| PvName: "pv-oss-002", | ||
| MountPath: "/models", | ||
| ReadOnly: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| initialAnnotations: map[string]string{"existing-key": "existing-val"}, | ||
| expectedAnnotationKey: models.ExtensionKeyClaimWithCSIMount_MountConfig, | ||
| expectedAnnotationVal: `[{"pvName":"pv-nas-001","mountPath":"/data"},{"pvName":"pv-oss-002","mountPath":"/models","readOnly":true}]`, | ||
| shouldSet: true, | ||
| }, | ||
| { | ||
| name: "with metadata merging", | ||
| request: models.NewSandboxRequest{ | ||
| Extensions: models.NewSandboxRequestExtension{ | ||
| CSIMount: models.CSIMountExtension{ | ||
| MountConfigs: []models.CSIMountConfig{ | ||
| { | ||
| PvName: "pv-test", | ||
| MountPath: "/workspace", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| initialAnnotations: map[string]string{ | ||
| "old-key": "old-val", | ||
| }, | ||
| expectedAnnotationKey: models.ExtensionKeyClaimWithCSIMount_MountConfig, | ||
| expectedAnnotationVal: `[{"pvName":"pv-test","mountPath":"/workspace"}]`, | ||
| shouldSet: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create mock sandbox | ||
| mockSbx := &sandboxcr.Sandbox{ | ||
| Sandbox: &agentsv1alpha1.Sandbox{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-sandbox", | ||
| Namespace: "default", | ||
| Annotations: tt.initialAnnotations, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Create controller instance | ||
| ctrl := &Controller{} | ||
|
|
||
| // Call the function | ||
| ctx := context.Background() | ||
| ctrl.csiMountOptionsConfigRecord(ctx, mockSbx, tt.request) | ||
|
|
||
| // Verify results | ||
| annotations := mockSbx.GetAnnotations() | ||
|
|
||
| if !tt.shouldSet { | ||
| // Should not set any annotation when mount configs are empty | ||
| if len(annotations) != len(tt.initialAnnotations) { | ||
| t.Errorf("expected no annotations to be added, got %d", len(annotations)) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // Check if expected annotation is set | ||
| val, exists := annotations[tt.expectedAnnotationKey] | ||
| if !exists { | ||
| t.Errorf("expected annotation %q to exist", tt.expectedAnnotationKey) | ||
| return | ||
| } | ||
|
|
||
| // Verify the annotation value (parse JSON for comparison to avoid ordering issues) | ||
| var expectedConfigs, actualConfigs []models.CSIMountConfig | ||
| if err := json.Unmarshal([]byte(tt.expectedAnnotationVal), &expectedConfigs); err != nil { | ||
| t.Fatalf("failed to unmarshal expected value: %v", err) | ||
| } | ||
| if err := json.Unmarshal([]byte(val), &actualConfigs); err != nil { | ||
| t.Fatalf("failed to unmarshal actual value: %v", err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(expectedConfigs, actualConfigs) { | ||
| t.Errorf("csi mount config mismatch:\nexpected: %#v\ngot: %#v", expectedConfigs, actualConfigs) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(expectedConfigs, actualConfigs) { | ||
| t.Errorf("csi mount config mismatch:\nexpected: %#v\ngot: %#v", expectedConfigs, actualConfigs) | ||
| } | ||
|
|
||
| // Verify existing annotations are preserved | ||
| if tt.initialAnnotations != nil { | ||
| for k, v := range tt.initialAnnotations { | ||
| if annotations[k] != v { | ||
| t.Errorf("expected existing annotation %q=%q, got %q", k, v, annotations[k]) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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,9 +1,9 @@ | ||
| package models | ||
|
|
||
| type CSIMountConfig struct { | ||
| MountID string `json:"mountID"` // mount id | ||
| PvName string `json:"pvName"` // persistent volume name for mounting | ||
| MountPath string `json:"mountPath"` // mount target in container to mount the persistent volume | ||
| SubPath string `json:"subPath"` // sub path address in persistent volume | ||
| ReadOnly bool `json:"readOnly"` // whether to mount the persistent volume as read-only | ||
| MountID string `json:"mountID,omitempty"` // mount id | ||
| PvName string `json:"pvName"` // persistent volume name for mounting | ||
| MountPath string `json:"mountPath"` // mount target in container to mount the persistent volume | ||
| SubPath string `json:"subPath,omitempty"` // sub path address in persistent volume | ||
| ReadOnly bool `json:"readOnly,omitempty"` // whether to mount the persistent volume as read-only | ||
| } |
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.