Skip to content

Commit e268ad5

Browse files
committed
update pod labels when claiming
Signed-off-by: 守辰 <shouchen.zz@alibaba-inc.com>
1 parent dbd97a7 commit e268ad5

13 files changed

Lines changed: 859 additions & 45 deletions

api/v1alpha1/sandbox_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ import (
2424

2525
const (
2626
// SandboxHashWithoutImageAndResources represents the key of sandbox hash without image and resources.
27+
// Deprecated, use SandboxHashImmutablePart instead
2728
SandboxHashWithoutImageAndResources = "sandbox.agents.kruise.io/hash-without-image-resources"
2829

30+
// SandboxHashImmutablePart represents the key of sandbox hash than exclude immutable part of sandbox
31+
// e.g. metadata, image and resources
32+
SandboxHashImmutablePart = "sandbox.agents.kruise.io/hash-immutable-part"
33+
2934
// PodLabelTemplateHash is pod template hash
3035
PodLabelTemplateHash = "pod-template-hash"
3136

pkg/controller/sandbox/core/common_control_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ func TestCommonControl_handleInplaceUpdateSandbox(t *testing.T) {
11631163
Name: "test-sandbox",
11641164
Namespace: "default",
11651165
Annotations: map[string]string{
1166-
agentsv1alpha1.SandboxHashWithoutImageAndResources: "different-hash",
1166+
agentsv1alpha1.SandboxHashImmutablePart: "different-hash",
11671167
},
11681168
},
11691169
Spec: agentsv1alpha1.SandboxSpec{
@@ -1210,7 +1210,7 @@ func TestCommonControl_handleInplaceUpdateSandbox(t *testing.T) {
12101210
Name: "test-sandbox",
12111211
Namespace: "default",
12121212
Annotations: map[string]string{
1213-
agentsv1alpha1.SandboxHashWithoutImageAndResources: "same-hash",
1213+
agentsv1alpha1.SandboxHashImmutablePart: "same-hash",
12141214
},
12151215
},
12161216
Spec: agentsv1alpha1.SandboxSpec{

pkg/controller/sandbox/core/common_inplace_update_handler.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ func handleInPlaceUpdateCommon(
4343
) (bool, error) {
4444
logger := handler.GetLogger(ctx, box)
4545

46-
_, hashWithoutImageAndResource := HashSandbox(box)
46+
_, hashImmutablePart := HashSandbox(box)
47+
4748
// old Pod do not include Labels[pod-template-hash] and do not support inplace update.
4849
// Check if inplace update is supported
4950
if pod.Labels[agentsv1alpha1.PodLabelTemplateHash] == "" {
5051
return true, nil
5152
// todo, update inplaceupdate condition
52-
} else if box.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources] != hashWithoutImageAndResource {
53-
logger.Info("sandbox hash-without-image-resources changed, and does not permit in-place upgrades",
54-
"old hash", box.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources],
55-
"new hash", hashWithoutImageAndResource)
53+
} else if box.Annotations[agentsv1alpha1.SandboxHashImmutablePart] != hashImmutablePart {
54+
logger.Info("sandbox hash-immutable-part changed, and does not permit in-place upgrades",
55+
"old hash", box.Annotations[agentsv1alpha1.SandboxHashImmutablePart],
56+
"new hash", hashImmutablePart)
5657
handler.GetRecorder().Eventf(box, corev1.EventTypeWarning, "InplaceUpdateForbidden",
57-
"InplaceUpdate only support image, resources")
58+
"InplaceUpdate only support image, resources, metadata")
5859
return true, nil
5960
}
6061

pkg/controller/sandbox/core/common_inplace_update_handler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestHandleInPlaceUpdateCommon(t *testing.T) {
106106
box: &agentsv1alpha1.Sandbox{
107107
ObjectMeta: metav1.ObjectMeta{
108108
Annotations: map[string]string{
109-
agentsv1alpha1.SandboxHashWithoutImageAndResources: "new-hash", // Mismatch with Pod label
109+
agentsv1alpha1.SandboxHashImmutablePart: "new-hash", // Mismatch with Pod label
110110
},
111111
},
112112
Spec: agentsv1alpha1.SandboxSpec{
@@ -151,7 +151,7 @@ func TestHandleInPlaceUpdateCommon(t *testing.T) {
151151
box: &agentsv1alpha1.Sandbox{
152152
ObjectMeta: metav1.ObjectMeta{
153153
Annotations: map[string]string{
154-
agentsv1alpha1.SandboxHashWithoutImageAndResources: "test-revision",
154+
agentsv1alpha1.SandboxHashImmutablePart: "test-revision",
155155
},
156156
},
157157
Spec: agentsv1alpha1.SandboxSpec{
@@ -237,7 +237,7 @@ func TestHandleInPlaceUpdateCommon_WithUpdateInProgress(t *testing.T) {
237237
Name: "test-sandbox",
238238
Namespace: "default",
239239
Annotations: map[string]string{
240-
agentsv1alpha1.SandboxHashWithoutImageAndResources: "current-hash",
240+
agentsv1alpha1.SandboxHashImmutablePart: "current-hash",
241241
},
242242
},
243243
Spec: agentsv1alpha1.SandboxSpec{
@@ -301,7 +301,7 @@ func TestHandleInPlaceUpdateCommon_InitialState(t *testing.T) {
301301
Name: "test-sandbox",
302302
Namespace: "default",
303303
Annotations: map[string]string{
304-
agentsv1alpha1.SandboxHashWithoutImageAndResources: "current-hash",
304+
agentsv1alpha1.SandboxHashImmutablePart: "current-hash",
305305
},
306306
},
307307
Spec: agentsv1alpha1.SandboxSpec{

pkg/controller/sandbox/core/util.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func HashSandbox(box *agentsv1alpha1.Sandbox) (string, string) {
5151

5252
// hash using sandbox.spec.template without image and resources
5353
tempClone := box.Spec.Template.DeepCopy()
54+
tempClone.Labels = nil
55+
tempClone.Annotations = nil
5456
for i := range tempClone.Spec.Containers {
5557
container := &tempClone.Spec.Containers[i]
5658
container.Image = ""
@@ -62,8 +64,8 @@ func HashSandbox(box *agentsv1alpha1.Sandbox) (string, string) {
6264
container.Resources = corev1.ResourceRequirements{}
6365
}
6466
by, _ = json.Marshal(*tempClone)
65-
hashWithoutImageResources := utils.HashData(by)
66-
return hash, hashWithoutImageResources
67+
hashImmutablePart := utils.HashData(by)
68+
return hash, hashImmutablePart
6769
}
6870

6971
// GeneratePVCName generates a persistent volume claim name from template name and sandbox name

pkg/controller/sandbox/core/util_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ func TestHashSandbox(t *testing.T) {
155155
},
156156
validateDifferentHashes: false, // Both hashes should be the same when no containers have images/resources
157157
},
158+
{
159+
name: "sandbox with labels in template but empty containers",
160+
sandbox: &agentsv1alpha1.Sandbox{
161+
Spec: agentsv1alpha1.SandboxSpec{
162+
EmbeddedSandboxTemplate: agentsv1alpha1.EmbeddedSandboxTemplate{
163+
Template: &corev1.PodTemplateSpec{
164+
ObjectMeta: metav1.ObjectMeta{
165+
Labels: map[string]string{
166+
"app": "test",
167+
},
168+
},
169+
Spec: corev1.PodSpec{
170+
Containers: []corev1.Container{},
171+
},
172+
},
173+
},
174+
},
175+
},
176+
validateDifferentHashes: true, // Both hashes should be the same when no containers have images/resources
177+
},
158178
{
159179
name: "sandbox with volumes and other fields",
160180
sandbox: &agentsv1alpha1.Sandbox{

pkg/controller/sandbox/sandbox_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ func (r *SandboxReconciler) addSandboxFinalizerAndHash(ctx context.Context, box
264264
if originObj.Annotations == nil {
265265
originObj.Annotations = make(map[string]string)
266266
}
267-
_, hashWithoutImageAndResource := core.HashSandbox(box)
268-
originObj.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources] = hashWithoutImageAndResource
267+
_, hashImmutablePart := core.HashSandbox(box)
268+
originObj.Annotations[agentsv1alpha1.SandboxHashImmutablePart] = hashImmutablePart
269269
if err := client.IgnoreNotFound(r.Patch(ctx, originObj, patch)); err != nil {
270270
logger.Error(err, "failed to patch sandbox finalizer and hash")
271271
return nil, fmt.Errorf("failed to patch finalizer: %w", err)

pkg/controller/sandbox/sandbox_controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ func TestSandboxReconciler_AddSandboxFinalizerAndHash(t *testing.T) {
17901790
if result.Annotations == nil {
17911791
t.Fatalf("Annotations should not be nil")
17921792
}
1793-
if result.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources] == "" {
1793+
if result.Annotations[agentsv1alpha1.SandboxHashImmutablePart] == "" {
17941794
t.Errorf("Hash annotation should be set")
17951795
}
17961796
},
@@ -1891,7 +1891,7 @@ func TestSandboxReconciler_AddSandboxFinalizerAndHash(t *testing.T) {
18911891
t.Errorf("Annotations map should be created")
18921892
}
18931893
// Check hash annotation
1894-
if result.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources] == "" {
1894+
if result.Annotations[agentsv1alpha1.SandboxHashImmutablePart] == "" {
18951895
t.Errorf("Hash annotation should be set")
18961896
}
18971897
},
@@ -1929,7 +1929,7 @@ func TestSandboxReconciler_AddSandboxFinalizerAndHash(t *testing.T) {
19291929
t.Errorf("Existing annotation should be preserved")
19301930
}
19311931
// Check hash annotation is added
1932-
if result.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources] == "" {
1932+
if result.Annotations[agentsv1alpha1.SandboxHashImmutablePart] == "" {
19331933
t.Errorf("Hash annotation should be added")
19341934
}
19351935
},
@@ -1994,7 +1994,7 @@ func TestSandboxReconciler_AddSandboxFinalizerAndHash(t *testing.T) {
19941994
if tt.expectHashAnnotation {
19951995
if updatedSandbox.Annotations == nil {
19961996
t.Errorf("Annotations should not be nil in persisted sandbox")
1997-
} else if updatedSandbox.Annotations[agentsv1alpha1.SandboxHashWithoutImageAndResources] == "" {
1997+
} else if updatedSandbox.Annotations[agentsv1alpha1.SandboxHashImmutablePart] == "" {
19981998
t.Errorf("Hash annotation should be set in persisted sandbox")
19991999
}
20002000
}

pkg/controller/sandboxclaim/core/common_control.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,19 @@ func (c *commonControl) buildClaimOptions(ctx context.Context, claim *agentsv1al
242242
User: string(claim.UID), // Use UID to ensure uniqueness across claim recreations
243243
Template: sandboxSet.Name,
244244
Modifier: func(sbx infra.Sandbox) {
245-
// 1. apply labels
245+
// propagate annotations to sandbox
246+
if len(claim.Spec.Annotations) > 0 {
247+
annotations := sbx.GetAnnotations()
248+
if annotations == nil {
249+
annotations = make(map[string]string)
250+
}
251+
for k, v := range claim.Spec.Annotations {
252+
annotations[k] = v
253+
}
254+
sbx.SetAnnotations(annotations)
255+
}
256+
257+
// propagate labels to sandbox
246258
labels := sbx.GetLabels()
247259
if labels == nil {
248260
labels = make(map[string]string)
@@ -254,19 +266,18 @@ func (c *commonControl) buildClaimOptions(ctx context.Context, claim *agentsv1al
254266
}
255267
sbx.SetLabels(labels)
256268

257-
// 2. apply annotations
258-
if len(claim.Spec.Annotations) > 0 {
259-
annotations := sbx.GetAnnotations()
260-
if annotations == nil {
261-
annotations = make(map[string]string)
262-
}
263-
for k, v := range claim.Spec.Annotations {
264-
annotations[k] = v
265-
}
266-
sbx.SetAnnotations(annotations)
269+
// propagate annotations to podtemplate
270+
labels = sbx.GetPodLabels()
271+
if labels == nil {
272+
labels = make(map[string]string)
273+
}
274+
275+
for k, v := range claim.Spec.Labels {
276+
labels[k] = v
267277
}
278+
sbx.SetPodLabels(labels)
268279

269-
// 3. apply shutdownTime
280+
// apply shutdownTime
270281
if claim.Spec.ShutdownTime != nil {
271282
sbx.SetTimeout(infra.TimeoutOptions{
272283
ShutdownTime: claim.Spec.ShutdownTime.Time,

pkg/servers/e2b/create.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func (sc *Controller) basicSandboxCreateModifier(ctx context.Context, sbx infra.
254254
sbx.SetTimeout(timeoutOptions)
255255
log.Info("timeout options calculated", "options", timeoutOptions)
256256

257+
// propagate annotations to sandbox
257258
annotations := sbx.GetAnnotations()
258259
if annotations == nil {
259260
annotations = make(map[string]string)
@@ -263,7 +264,18 @@ func (sc *Controller) basicSandboxCreateModifier(ctx context.Context, sbx infra.
263264
}
264265
sbx.SetAnnotations(annotations)
265266

266-
labels := sbx.GetPodLabels()
267+
// propagate labels to sandbox
268+
labels := sbx.GetLabels()
269+
if labels == nil {
270+
labels = make(map[string]string)
271+
}
272+
for k, v := range request.Extensions.Labels {
273+
labels[k] = v
274+
}
275+
sbx.SetLabels(labels)
276+
277+
// propagate annotations to podtemplate
278+
labels = sbx.GetPodLabels()
267279
if labels == nil {
268280
labels = make(map[string]string)
269281
}

0 commit comments

Comments
 (0)