@@ -17,11 +17,21 @@ import (
1717)
1818
1919func 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
2328func 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
2737func fetchInjectionConfiguration (ctx context.Context , cli client.Client ) (map [string ]string , error ) {
@@ -150,23 +160,29 @@ func setAgentRuntimeContainer(ctx context.Context, podSpec *corev1.PodSpec, conf
150160func 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