Skip to content

Commit fade933

Browse files
author
dingrong.wwk
committed
sandbox claim support PauseTime
Change-Id: Ie509d168fb67b243e78d4f2486f5622398cec1d0
1 parent dc4bfc5 commit fade933

File tree

6 files changed

+364
-5
lines changed

6 files changed

+364
-5
lines changed

api/v1alpha1/sandboxclaim_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ type SandboxClaimSpec struct {
4040
// +optional
4141
ShutdownTime *metav1.Time `json:"shutdownTime,omitempty"`
4242

43+
// PauseTime specifies the absolute time when the sandbox should be paused automatically
44+
// This will be set as spec.pauseTime (absolute time) on the Sandbox
45+
// +optional
46+
PauseTime *metav1.Time `json:"pauseTime,omitempty"`
47+
4348
// ClaimTimeout specifies the maximum duration to wait for claiming sandboxes
4449
// If the timeout is reached, the claim will be marked as Completed regardless of
4550
// whether all replicas were successfully claimed

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/agents.kruise.io_sandboxclaims.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ spec:
146146
- name
147147
type: object
148148
type: array
149+
pauseTime:
150+
description: |-
151+
PauseTime specifies the absolute time when the sandbox should be paused automatically
152+
This will be set as spec.pauseTime (absolute time) on the Sandbox
153+
format: date-time
154+
type: string
149155
shutdownTime:
150156
description: |-
151157
ShutdownTime specifies the absolute time when the sandbox should be shut down

pkg/controller/sandboxclaim/core/common_control.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,16 @@ func (c *commonControl) buildClaimOptions(ctx context.Context, claim *agentsv1al
261261
sbx.SetAnnotations(annotations)
262262
}
263263

264-
// 3. apply shutdownTime
265-
if claim.Spec.ShutdownTime != nil {
266-
sbx.SetTimeout(infra.TimeoutOptions{
267-
ShutdownTime: claim.Spec.ShutdownTime.Time,
268-
})
264+
// 3. apply shutdownTime and pauseTime
265+
if claim.Spec.ShutdownTime != nil || claim.Spec.PauseTime != nil {
266+
opts := infra.TimeoutOptions{}
267+
if claim.Spec.ShutdownTime != nil {
268+
opts.ShutdownTime = claim.Spec.ShutdownTime.Time
269+
}
270+
if claim.Spec.PauseTime != nil {
271+
opts.PauseTime = claim.Spec.PauseTime.Time
272+
}
273+
sbx.SetTimeout(opts)
269274
}
270275
},
271276
ReserveFailedSandbox: claim.Spec.ReserveFailedSandbox,

pkg/controller/sandboxclaim/core/common_control_test.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,65 @@ func TestCommonControl_buildClaimOptions(t *testing.T) {
802802
}
803803
},
804804
},
805+
{
806+
name: "claim with pauseTime",
807+
claim: &agentsv1alpha1.SandboxClaim{
808+
ObjectMeta: metav1.ObjectMeta{
809+
Name: "test-claim",
810+
Namespace: "default",
811+
UID: "test-uid-pause",
812+
},
813+
Spec: agentsv1alpha1.SandboxClaimSpec{
814+
TemplateName: "test-template",
815+
PauseTime: &shutdownTime,
816+
},
817+
},
818+
sandboxSet: &agentsv1alpha1.SandboxSet{
819+
ObjectMeta: metav1.ObjectMeta{
820+
Name: "test-template",
821+
Namespace: "default",
822+
},
823+
},
824+
expectError: false,
825+
validate: func(t *testing.T, opts infra.ClaimSandboxOptions) {
826+
if opts.User != "test-uid-pause" {
827+
t.Errorf("User = %v, want %v", opts.User, "test-uid-pause")
828+
}
829+
if opts.Modifier == nil {
830+
t.Error("Modifier should not be nil")
831+
}
832+
},
833+
},
834+
{
835+
name: "claim with both shutdownTime and pauseTime",
836+
claim: &agentsv1alpha1.SandboxClaim{
837+
ObjectMeta: metav1.ObjectMeta{
838+
Name: "test-claim",
839+
Namespace: "default",
840+
UID: "test-uid-both-times",
841+
},
842+
Spec: agentsv1alpha1.SandboxClaimSpec{
843+
TemplateName: "test-template",
844+
ShutdownTime: &shutdownTime,
845+
PauseTime: &shutdownTime,
846+
},
847+
},
848+
sandboxSet: &agentsv1alpha1.SandboxSet{
849+
ObjectMeta: metav1.ObjectMeta{
850+
Name: "test-template",
851+
Namespace: "default",
852+
},
853+
},
854+
expectError: false,
855+
validate: func(t *testing.T, opts infra.ClaimSandboxOptions) {
856+
if opts.User != "test-uid-both-times" {
857+
t.Errorf("User = %v, want %v", opts.User, "test-uid-both-times")
858+
}
859+
if opts.Modifier == nil {
860+
t.Error("Modifier should not be nil")
861+
}
862+
},
863+
},
805864
{
806865
name: "claim with inplaceUpdate - image only",
807866
claim: &agentsv1alpha1.SandboxClaim{
@@ -1119,6 +1178,121 @@ func TestCommonControl_buildClaimOptions_Modifier(t *testing.T) {
11191178
t.Errorf("Expected custom-annotation = %v, got %v",
11201179
"value2", mockSandbox.Annotations["custom-annotation"])
11211180
}
1181+
1182+
// Verify shutdownTime was set correctly on the sandbox
1183+
if mockSandbox.Sandbox.Spec.ShutdownTime == nil {
1184+
t.Fatal("Expected ShutdownTime to be set on sandbox")
1185+
}
1186+
if !mockSandbox.Sandbox.Spec.ShutdownTime.Time.Equal(shutdownTime.Time) {
1187+
t.Errorf("Expected ShutdownTime = %v, got %v", shutdownTime.Time, mockSandbox.Sandbox.Spec.ShutdownTime.Time)
1188+
}
1189+
}
1190+
1191+
// TestCommonControl_buildClaimOptions_TimeoutModifier tests that shutdownTime and pauseTime
1192+
// are correctly applied to the sandbox via the modifier.
1193+
func TestCommonControl_buildClaimOptions_TimeoutModifier(t *testing.T) {
1194+
scheme := runtime.NewScheme()
1195+
_ = agentsv1alpha1.AddToScheme(scheme)
1196+
1197+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
1198+
fakeRecorder := record.NewFakeRecorder(10)
1199+
control := NewCommonControl(fakeClient, fakeRecorder, nil, nil).(*commonControl)
1200+
ctx := context.Background()
1201+
1202+
now := metav1.Now()
1203+
pauseTime := metav1.NewTime(now.Add(10 * time.Minute))
1204+
shutdownTime := metav1.NewTime(now.Add(30 * time.Minute))
1205+
1206+
tests := []struct {
1207+
name string
1208+
shutdownTime *metav1.Time
1209+
pauseTime *metav1.Time
1210+
wantShutdownTime *metav1.Time
1211+
wantPauseTime *metav1.Time
1212+
}{
1213+
{
1214+
name: "only shutdownTime",
1215+
shutdownTime: &shutdownTime,
1216+
wantShutdownTime: &shutdownTime,
1217+
wantPauseTime: nil,
1218+
},
1219+
{
1220+
name: "only pauseTime",
1221+
pauseTime: &pauseTime,
1222+
wantPauseTime: &pauseTime,
1223+
wantShutdownTime: nil,
1224+
},
1225+
{
1226+
name: "both shutdownTime and pauseTime",
1227+
shutdownTime: &shutdownTime,
1228+
pauseTime: &pauseTime,
1229+
wantShutdownTime: &shutdownTime,
1230+
wantPauseTime: &pauseTime,
1231+
},
1232+
{
1233+
name: "neither shutdownTime nor pauseTime",
1234+
wantShutdownTime: nil,
1235+
wantPauseTime: nil,
1236+
},
1237+
}
1238+
1239+
for _, tt := range tests {
1240+
t.Run(tt.name, func(t *testing.T) {
1241+
claim := &agentsv1alpha1.SandboxClaim{
1242+
ObjectMeta: metav1.ObjectMeta{
1243+
Name: "test-claim",
1244+
Namespace: "default",
1245+
UID: "test-uid",
1246+
},
1247+
Spec: agentsv1alpha1.SandboxClaimSpec{
1248+
TemplateName: "test-template",
1249+
ShutdownTime: tt.shutdownTime,
1250+
PauseTime: tt.pauseTime,
1251+
},
1252+
}
1253+
sandboxSet := &agentsv1alpha1.SandboxSet{
1254+
ObjectMeta: metav1.ObjectMeta{Name: "test-template", Namespace: "default"},
1255+
}
1256+
1257+
opts, err := control.buildClaimOptions(ctx, claim, sandboxSet)
1258+
if err != nil {
1259+
t.Fatalf("buildClaimOptions() error = %v", err)
1260+
}
1261+
1262+
mockSandbox := &sandboxcr.Sandbox{
1263+
Sandbox: &agentsv1alpha1.Sandbox{
1264+
ObjectMeta: metav1.ObjectMeta{Name: "test-sandbox", Namespace: "default"},
1265+
},
1266+
}
1267+
opts.Modifier(mockSandbox)
1268+
1269+
if tt.wantShutdownTime == nil {
1270+
if mockSandbox.Sandbox.Spec.ShutdownTime != nil {
1271+
t.Errorf("expected ShutdownTime to be nil, got %v", mockSandbox.Sandbox.Spec.ShutdownTime)
1272+
}
1273+
} else {
1274+
if mockSandbox.Sandbox.Spec.ShutdownTime == nil {
1275+
t.Fatal("expected ShutdownTime to be set, got nil")
1276+
}
1277+
if !mockSandbox.Sandbox.Spec.ShutdownTime.Time.Equal(tt.wantShutdownTime.Time) {
1278+
t.Errorf("ShutdownTime = %v, want %v", mockSandbox.Sandbox.Spec.ShutdownTime.Time, tt.wantShutdownTime.Time)
1279+
}
1280+
}
1281+
1282+
if tt.wantPauseTime == nil {
1283+
if mockSandbox.Sandbox.Spec.PauseTime != nil {
1284+
t.Errorf("expected PauseTime to be nil, got %v", mockSandbox.Sandbox.Spec.PauseTime)
1285+
}
1286+
} else {
1287+
if mockSandbox.Sandbox.Spec.PauseTime == nil {
1288+
t.Fatal("expected PauseTime to be set, got nil")
1289+
}
1290+
if !mockSandbox.Sandbox.Spec.PauseTime.Time.Equal(tt.wantPauseTime.Time) {
1291+
t.Errorf("PauseTime = %v, want %v", mockSandbox.Sandbox.Spec.PauseTime.Time, tt.wantPauseTime.Time)
1292+
}
1293+
}
1294+
})
1295+
}
11221296
}
11231297

11241298
// Helper function for tests

0 commit comments

Comments
 (0)