Skip to content

Commit 150cfb6

Browse files
committed
update using sandbox spec runtimeconfig instend of annotation flag
Signed-off-by: jicheng.sk <jicheng.sk@alibaba-inc.com>
1 parent a89a9f2 commit 150cfb6

File tree

7 files changed

+341
-81
lines changed

7 files changed

+341
-81
lines changed

api/v1alpha1/sandbox_types.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ const (
3636
// Sandbox Manager or Sandbox Claim creates high-priority sandboxes by default.
3737
SandboxAnnotationPriority = "agents.kruise.io/sandbox-priority"
3838

39-
// ShouldInjectCsiMount is the annotation key for inject csi mount plugin container.
40-
// If set, the csi sidecar will be injected into the pod when the sandbox is created.
41-
// The csi mount sidecar is used to mount the remote oss/nas storage to the sandbox container.
42-
ShouldInjectCsiMount = "agents.kruise.io/inject-csi-plugin"
43-
44-
// ShouldInjectAgentRuntime is the annotation key for inject agent runtime sidecar in init container.
45-
// If set, the agent runtime sidecar will be injected into the pod when the sandbox is created.
46-
// Some binary tools which are contained in the init agent runtime container. These are the basic tools for sandbox running.
47-
ShouldInjectAgentRuntime = "agents.kruise.io/inject-agent-runtime"
39+
// RuntimeConfigForInjectCsiMount is a valid value for RuntimeConfig.Name.
40+
// When set, enables CSI mount sidecar injection for the sandbox.
41+
RuntimeConfigForInjectCsiMount = "csi"
42+
// RuntimeConfigForInjectAgentRuntime is a valid value for RuntimeConfig.Name.
43+
// When set, enables agent runtime sidecar injection for the sandbox.
44+
RuntimeConfigForInjectAgentRuntime = "agent-runtime"
4845
)
4946

47+
type RuntimeConfig struct {
48+
Name string `json:"name"`
49+
}
50+
5051
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
5152
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
5253

@@ -69,6 +70,10 @@ type SandboxSpec struct {
6970
// +kubebuilder:validation:Format="date-time"
7071
ShutdownTime *metav1.Time `json:"shutdownTime,omitempty"`
7172

73+
// Runtimes - Runtime configuration for sandbox object
74+
// +optional
75+
Runtimes []RuntimeConfig `json:"runtimes,omitempty"`
76+
7277
// PauseTime - Absolute time when the sandbox will be paused automatically.
7378
// +kubebuilder:validation:Format="date-time"
7479
PauseTime *metav1.Time `json:"pauseTime,omitempty"`

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 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_sandboxes.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ spec:
7171
items:
7272
type: string
7373
type: array
74+
runtimes:
75+
description: Runtimes - Runtime configuration for sandbox object
76+
items:
77+
properties:
78+
name:
79+
type: string
80+
required:
81+
- name
82+
type: object
83+
type: array
7484
shutdownTime:
7585
description: |-
7686
ShutdownTime - Absolute time when the sandbox is deleted.

pkg/controller/sandbox/core/common_control_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,11 +1305,13 @@ func TestCommonControl_createPod_WithSidecarInjection(t *testing.T) {
13051305
ObjectMeta: metav1.ObjectMeta{
13061306
Name: "test-sandbox",
13071307
Namespace: "default",
1308-
Annotations: map[string]string{
1309-
agentsv1alpha1.ShouldInjectAgentRuntime: "true",
1310-
},
13111308
},
13121309
Spec: agentsv1alpha1.SandboxSpec{
1310+
Runtimes: []agentsv1alpha1.RuntimeConfig{
1311+
{
1312+
Name: agentsv1alpha1.RuntimeConfigForInjectAgentRuntime,
1313+
},
1314+
},
13131315
EmbeddedSandboxTemplate: agentsv1alpha1.EmbeddedSandboxTemplate{
13141316
Template: &corev1.PodTemplateSpec{
13151317
Spec: corev1.PodSpec{
@@ -1359,11 +1361,13 @@ func TestCommonControl_createPod_WithSidecarInjection(t *testing.T) {
13591361
ObjectMeta: metav1.ObjectMeta{
13601362
Name: "test-sandbox",
13611363
Namespace: "default",
1362-
Annotations: map[string]string{
1363-
agentsv1alpha1.ShouldInjectCsiMount: "true",
1364-
},
13651364
},
13661365
Spec: agentsv1alpha1.SandboxSpec{
1366+
Runtimes: []agentsv1alpha1.RuntimeConfig{
1367+
{
1368+
Name: agentsv1alpha1.RuntimeConfigForInjectCsiMount,
1369+
},
1370+
},
13671371
EmbeddedSandboxTemplate: agentsv1alpha1.EmbeddedSandboxTemplate{
13681372
Template: &corev1.PodTemplateSpec{
13691373
Spec: corev1.PodSpec{
@@ -1416,12 +1420,16 @@ func TestCommonControl_createPod_WithSidecarInjection(t *testing.T) {
14161420
ObjectMeta: metav1.ObjectMeta{
14171421
Name: "test-sandbox",
14181422
Namespace: "default",
1419-
Annotations: map[string]string{
1420-
agentsv1alpha1.ShouldInjectAgentRuntime: "true",
1421-
agentsv1alpha1.ShouldInjectCsiMount: "true",
1422-
},
14231423
},
14241424
Spec: agentsv1alpha1.SandboxSpec{
1425+
Runtimes: []agentsv1alpha1.RuntimeConfig{
1426+
{
1427+
Name: agentsv1alpha1.RuntimeConfigForInjectAgentRuntime,
1428+
},
1429+
{
1430+
Name: agentsv1alpha1.RuntimeConfigForInjectCsiMount,
1431+
},
1432+
},
14251433
EmbeddedSandboxTemplate: agentsv1alpha1.EmbeddedSandboxTemplate{
14261434
Template: &corev1.PodTemplateSpec{
14271435
Spec: corev1.PodSpec{

pkg/utils/sidecarutils/sidecar_config_inject.go

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ import (
1717
)
1818

1919
func enableInjectCsiMountConfig(sandbox *agentsv1alpha1.Sandbox) bool {
20-
return sandbox.Annotations[agentsv1alpha1.ShouldInjectCsiMount] == "true"
20+
for _, runtime := range sandbox.Spec.Runtimes {
21+
if runtime.Name == agentsv1alpha1.RuntimeConfigForInjectCsiMount {
22+
return true
23+
}
24+
}
25+
return false
2126
}
2227

2328
func enableInjectAgentRuntimeConfig(sandbox *agentsv1alpha1.Sandbox) bool {
24-
return sandbox.Annotations[agentsv1alpha1.ShouldInjectAgentRuntime] == "true"
29+
for _, runtime := range sandbox.Spec.Runtimes {
30+
if runtime.Name == agentsv1alpha1.RuntimeConfigForInjectAgentRuntime {
31+
return true
32+
}
33+
}
34+
return false
2535
}
2636

2737
func fetchInjectionConfiguration(ctx context.Context, cli client.Client) (map[string]string, error) {
@@ -150,23 +160,29 @@ func setAgentRuntimeContainer(ctx context.Context, podSpec *corev1.PodSpec, conf
150160
func setMainContainerConfigWhenInjectRuntimeSidecar(ctx context.Context, mainContainer *corev1.Container, config SidecarInjectConfig) {
151161
log := logf.FromContext(ctx)
152162

153-
// Check if main container already has a postStart hook
154-
if mainContainer.Lifecycle != nil && mainContainer.Lifecycle.PostStart != nil {
155-
if config.MainContainer.Lifecycle != nil && config.MainContainer.Lifecycle.PostStart != nil {
156-
log.Error(nil, "conflicting postStart hooks detected, main container already has a postStart hook defined",
163+
// Check if main container already has a valid postStart hook (with actual handler)
164+
mainContainerHasValidPostStart := mainContainer.Lifecycle != nil &&
165+
mainContainer.Lifecycle.PostStart != nil &&
166+
hasValidLifecycleHandler(mainContainer.Lifecycle.PostStart)
167+
168+
configHasValidPostStart := config.MainContainer.Lifecycle != nil &&
169+
config.MainContainer.Lifecycle.PostStart != nil &&
170+
hasValidLifecycleHandler(config.MainContainer.Lifecycle.PostStart)
171+
172+
if mainContainerHasValidPostStart {
173+
if configHasValidPostStart {
174+
log.V(consts.DebugLogLevel).Info("conflicting postStart hooks detected, main container already has a postStart hook defined",
157175
"existingHook", mainContainer.Lifecycle.PostStart,
158176
"injectedHook", config.MainContainer.Lifecycle.PostStart)
177+
return
159178
}
160179
} else {
161-
// set main container lifecycle
162-
if mainContainer.Lifecycle == nil {
163-
mainContainer.Lifecycle = &corev1.Lifecycle{}
164-
}
165-
if mainContainer.Lifecycle.PostStart == nil {
166-
mainContainer.Lifecycle.PostStart = &corev1.LifecycleHandler{}
167-
}
168-
// Main container doesn't have postStart, apply config if available
169-
if config.MainContainer.Lifecycle != nil && config.MainContainer.Lifecycle.PostStart != nil {
180+
// Main container doesn't have valid postStart, apply config if available
181+
if configHasValidPostStart {
182+
// set main container lifecycle
183+
if mainContainer.Lifecycle == nil {
184+
mainContainer.Lifecycle = &corev1.Lifecycle{}
185+
}
170186
mainContainer.Lifecycle.PostStart = config.MainContainer.Lifecycle.PostStart
171187
}
172188
}
@@ -227,3 +243,13 @@ func isContainersExists(podContainers []corev1.Container, injectContainers []cor
227243
}
228244
return false
229245
}
246+
247+
// hasValidLifecycleHandler checks if the lifecycle handler has at least one valid action defined.
248+
// A valid handler must have at least one of: Exec, HTTPGet, or TCPSocket.
249+
// Returns false if the handler is nil or all actions are nil (empty handler).
250+
func hasValidLifecycleHandler(handler *corev1.LifecycleHandler) bool {
251+
if handler == nil {
252+
return false
253+
}
254+
return handler.Exec != nil || handler.HTTPGet != nil || handler.TCPSocket != nil
255+
}

0 commit comments

Comments
 (0)