@@ -27,7 +27,7 @@ const (
27
27
annotationAgentName = "lightrun.com/lightrunjavaagent"
28
28
)
29
29
30
- func (r * LightrunJavaAgentReconciler ) createAgentConfig (lightrunJavaAgent * agentv1beta.LightrunJavaAgent ) (corev1.ConfigMap , error ) {
30
+ func (r * LightrunJavaAgentReconciler ) createAgentConfig (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1. Secret ) (corev1.ConfigMap , error ) {
31
31
populateTags (lightrunJavaAgent .Spec .AgentTags , lightrunJavaAgent .Spec .AgentName , & metadata )
32
32
jsonString , err := json .Marshal (metadata )
33
33
if err != nil {
@@ -52,26 +52,28 @@ func (r *LightrunJavaAgentReconciler) createAgentConfig(lightrunJavaAgent *agent
52
52
}
53
53
54
54
func (r * LightrunJavaAgentReconciler ) patchDeployment (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret , origDeployment * appsv1.Deployment , deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , cmDataHash uint64 ) error {
55
-
56
55
// init spec.template.spec
57
56
deploymentApplyConfig .WithSpec (
58
57
appsv1ac .DeploymentSpec ().WithTemplate (
59
58
corev1ac .PodTemplateSpec ().WithSpec (
60
59
corev1ac .PodSpec (),
61
60
).WithAnnotations (map [string ]string {
62
61
annotationConfigMapHash : fmt .Sprint (cmDataHash ),
63
- },
64
- ),
62
+ }),
65
63
),
66
64
).WithAnnotations (map [string ]string {
67
65
annotationAgentName : lightrunJavaAgent .Name ,
68
66
})
69
67
r .addVolume (deploymentApplyConfig , lightrunJavaAgent )
70
68
r .addInitContainer (deploymentApplyConfig , lightrunJavaAgent , secret )
71
- err = r .patchAppContainers (lightrunJavaAgent , origDeployment , deploymentApplyConfig )
69
+ err : = r .patchAppContainers (lightrunJavaAgent , origDeployment , deploymentApplyConfig )
72
70
if err != nil {
73
71
return err
74
72
}
73
+ deploymentApplyConfig .Spec .Template .Spec .WithSecurityContext (
74
+ corev1ac .PodSecurityContext ().
75
+ WithFSGroup (1000 ),
76
+ )
75
77
return nil
76
78
}
77
79
@@ -99,54 +101,53 @@ func (r *LightrunJavaAgentReconciler) addVolume(deploymentApplyConfig *appsv1ac.
99
101
}
100
102
101
103
func (r * LightrunJavaAgentReconciler ) addInitContainer (deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) {
102
-
103
104
deploymentApplyConfig .Spec .Template .Spec .WithInitContainers (
104
105
corev1ac .Container ().
105
106
WithName (initContainerName ).
106
107
WithImage (lightrunJavaAgent .Spec .InitContainer .Image ).
107
108
WithVolumeMounts (
108
109
corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath ("/tmp/" ),
109
110
corev1ac .VolumeMount ().WithName (cmVolumeName ).WithMountPath ("/tmp/cm/" ),
110
- ).WithEnv (
111
- corev1ac .EnvVar ().WithName ("LIGHTRUN_KEY" ).WithValueFrom (
112
- corev1ac .EnvVarSource ().WithSecretKeyRef (
113
- corev1ac .SecretKeySelector ().WithName (secret .Name ).WithKey ("lightrun_key" ),
114
- ),
115
- ),
116
- corev1ac .EnvVar ().WithName ("PINNED_CERT" ).WithValueFrom (
117
- corev1ac .EnvVarSource ().WithSecretKeyRef (
118
- corev1ac .SecretKeySelector ().WithName (secret .Name ).WithKey ("pinned_cert_hash" ),
119
- ),
120
- ),
121
- corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
122
- ).
111
+ corev1ac .VolumeMount ().WithName ("lightrun-secret" ).WithMountPath ("/etc/lightrun/secret" ).WithReadOnly (true ),
112
+ ).
113
+ WithEnv (
114
+ corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
115
+ ).
116
+ WithSecurityContext (
117
+ corev1ac .SecurityContext ().
118
+ WithReadOnlyRootFilesystem (true ).
119
+ WithAllowPrivilegeEscalation (false ).
120
+ WithRunAsNonRoot (true ).
121
+ WithRunAsUser (1000 ),
122
+ ).
123
123
WithResources (
124
124
corev1ac .ResourceRequirements ().
125
125
WithLimits (
126
126
corev1.ResourceList {
127
127
corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
128
- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 500 * 10^6 = 500M
128
+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
129
129
},
130
- ).WithRequests (
131
- corev1.ResourceList {
132
- corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
133
- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
134
- },
135
- ),
136
- ).
137
- WithSecurityContext (
138
- corev1ac .SecurityContext ().
139
- WithCapabilities (
140
- corev1ac .Capabilities ().WithDrop (corev1 .Capability ("ALL" )),
141
130
).
142
- WithAllowPrivilegeEscalation ( false ).
143
- WithRunAsNonRoot ( true ).
144
- WithSeccompProfile (
145
- corev1ac . SeccompProfile ().
146
- WithType ( corev1 . SeccompProfileTypeRuntimeDefault ) ,
131
+ WithRequests (
132
+ corev1. ResourceList {
133
+ corev1 . ResourceCPU : * resource . NewMilliQuantity ( int64 ( 50 ), resource . BinarySI ),
134
+ corev1 . ResourceMemory : * resource . NewScaledQuantity ( int64 ( 64 ), resource . Scale ( 6 )),
135
+ } ,
147
136
),
148
137
),
149
138
)
139
+
140
+ // Add volume for secret with proper permissions
141
+ deploymentApplyConfig .Spec .Template .Spec .WithVolumes (
142
+ corev1ac .Volume ().WithName ("lightrun-secret" ).
143
+ WithSecret (corev1ac .SecretVolumeSource ().
144
+ WithSecretName (secret .Name ).
145
+ WithItems (
146
+ corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ),
147
+ corev1ac .KeyToPath ().WithKey ("pinned_cert_hash" ).WithPath ("pinned_cert_hash" ),
148
+ ).
149
+ WithDefaultMode (0440 )),
150
+ )
150
151
}
151
152
152
153
func (r * LightrunJavaAgentReconciler ) patchAppContainers (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , origDeployment * appsv1.Deployment , deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration ) error {
@@ -167,8 +168,7 @@ func (r *LightrunJavaAgentReconciler) patchAppContainers(lightrunJavaAgent *agen
167
168
}
168
169
}
169
170
if ! found {
170
- err = errors .New ("unable to find matching container to patch" )
171
- return err
171
+ return errors .New ("unable to find matching container to patch" )
172
172
}
173
173
return nil
174
174
}
@@ -230,21 +230,18 @@ func (r *LightrunJavaAgentReconciler) patchStatefulSet(lightrunJavaAgent *agentv
230
230
corev1ac .PodSpec (),
231
231
).WithAnnotations (map [string ]string {
232
232
annotationConfigMapHash : fmt .Sprint (cmDataHash ),
233
- },
234
- ),
233
+ }),
235
234
),
236
235
).WithAnnotations (map [string ]string {
237
236
annotationAgentName : lightrunJavaAgent .Name ,
238
237
})
239
238
240
239
// Add volumes to the StatefulSet
241
240
r .addVolumeToStatefulSet (statefulSetApplyConfig , lightrunJavaAgent )
242
-
243
241
// Add init container to the StatefulSet
244
242
r .addInitContainerToStatefulSet (statefulSetApplyConfig , lightrunJavaAgent , secret )
245
-
246
243
// Patch app containers in the StatefulSet
247
- err = r .patchStatefulSetAppContainers (lightrunJavaAgent , origStatefulSet , statefulSetApplyConfig )
244
+ err : = r .patchStatefulSetAppContainers (lightrunJavaAgent , origStatefulSet , statefulSetApplyConfig )
248
245
if err != nil {
249
246
return err
250
247
}
@@ -271,6 +268,15 @@ func (r *LightrunJavaAgentReconciler) addVolumeToStatefulSet(statefulSetApplyCon
271
268
corev1ac .KeyToPath ().WithKey ("metadata" ).WithPath ("agent.metadata.json" ),
272
269
),
273
270
),
271
+ ).WithVolumes (
272
+ corev1ac .Volume ().WithName ("lightrun-secret" ).
273
+ WithSecret (corev1ac .SecretVolumeSource ().
274
+ WithSecretName (secret .Name ).
275
+ WithItems (
276
+ corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ),
277
+ corev1ac .KeyToPath ().WithKey ("pinned_cert_hash" ).WithPath ("pinned_cert_hash" ),
278
+ ).
279
+ WithDefaultMode (0440 )),
274
280
)
275
281
}
276
282
@@ -282,46 +288,43 @@ func (r *LightrunJavaAgentReconciler) addInitContainerToStatefulSet(statefulSetA
282
288
WithVolumeMounts (
283
289
corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath ("/tmp/" ),
284
290
corev1ac .VolumeMount ().WithName (cmVolumeName ).WithMountPath ("/tmp/cm/" ),
291
+ corev1ac .VolumeMount ().WithName ("lightrun-secret" ).WithMountPath ("/etc/lightrun/secret" ).WithReadOnly (true ),
285
292
).WithEnv (
286
- corev1ac .EnvVar ().WithName ("LIGHTRUN_KEY" ).WithValueFrom (
287
- corev1ac .EnvVarSource ().WithSecretKeyRef (
288
- corev1ac .SecretKeySelector ().WithName (secret .Name ).WithKey ("lightrun_key" ),
289
- ),
290
- ),
291
- corev1ac .EnvVar ().WithName ("PINNED_CERT" ).WithValueFrom (
292
- corev1ac .EnvVarSource ().WithSecretKeyRef (
293
- corev1ac .SecretKeySelector ().WithName (secret .Name ).WithKey ("pinned_cert_hash" ),
294
- ),
295
- ),
296
293
corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
297
294
).
295
+ WithSecurityContext (
296
+ corev1ac .SecurityContext ().
297
+ WithReadOnlyRootFilesystem (true ).
298
+ WithAllowPrivilegeEscalation (false ).
299
+ WithRunAsNonRoot (true ).
300
+ WithRunAsUser (1000 ),
301
+ ).
298
302
WithResources (
299
303
corev1ac .ResourceRequirements ().
300
304
WithLimits (
301
305
corev1.ResourceList {
302
306
corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
303
- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 64M
307
+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 500 * 10^6 = 500M
304
308
},
305
309
).WithRequests (
306
310
corev1.ResourceList {
307
311
corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
308
- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
312
+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 64M
309
313
},
310
314
),
311
- ).
312
- WithSecurityContext (
313
- corev1ac .SecurityContext ().
314
- WithCapabilities (
315
- corev1ac .Capabilities ().WithDrop (corev1 .Capability ("ALL" )),
316
- ).
317
- WithAllowPrivilegeEscalation (false ).
318
- WithRunAsNonRoot (true ).
319
- WithSeccompProfile (
320
- corev1ac .SeccompProfile ().
321
- WithType (corev1 .SeccompProfileTypeRuntimeDefault ),
322
- ),
323
315
),
324
316
)
317
+
318
+ statefulSetApplyConfig .Spec .Template .Spec .WithVolumes (
319
+ corev1ac .Volume ().WithName ("lightrun-secret" ).
320
+ WithSecret (corev1ac .SecretVolumeSource ().
321
+ WithSecretName (secret .Name ).
322
+ WithItems (
323
+ corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ),
324
+ corev1ac .KeyToPath ().WithKey ("pinned_cert_hash" ).WithPath ("pinned_cert_hash" ),
325
+ ).
326
+ WithDefaultMode (0440 )),
327
+ )
325
328
}
326
329
327
330
func (r * LightrunJavaAgentReconciler ) patchStatefulSetAppContainers (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , origStatefulSet * appsv1.StatefulSet , statefulSetApplyConfig * appsv1ac.StatefulSetApplyConfiguration ) error {
@@ -335,22 +338,20 @@ func (r *LightrunJavaAgentReconciler) patchStatefulSetAppContainers(lightrunJava
335
338
WithName (container .Name ).
336
339
WithImage (container .Image ).
337
340
WithVolumeMounts (
338
- corev1ac .VolumeMount ().WithMountPath (lightrunJavaAgent .Spec .InitContainer .SharedVolumeMountPath ). WithName ( lightrunJavaAgent . Spec . InitContainer . SharedVolumeName ),
341
+ corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ). WithMountPath ( "/tmp/" ),
339
342
),
340
343
)
341
344
}
342
345
}
343
346
}
344
347
if ! found {
345
- err = errors .New ("unable to find matching container to patch" )
346
- return err
348
+ return errors .New ("unable to find matching container to patch" )
347
349
}
348
350
return nil
349
351
}
350
352
351
353
// configMapDataHash calculates a hash of the ConfigMap data to detect changes
352
354
func configMapDataHash (cmData map [string ]string ) uint64 {
353
- // Combine all data values into a single string for hashing
354
355
var hashString string
355
356
for _ , v := range cmData {
356
357
hashString += v
0 commit comments