1
1
package controller
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"errors"
6
7
"fmt"
@@ -27,7 +28,7 @@ const (
27
28
annotationAgentName = "lightrun.com/lightrunjavaagent"
28
29
)
29
30
30
- func (r * LightrunJavaAgentReconciler ) createAgentConfig (lightrunJavaAgent * agentv1beta.LightrunJavaAgent ) (corev1.ConfigMap , error ) {
31
+ func (r * LightrunJavaAgentReconciler ) createAgentConfig (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1. Secret ) (corev1.ConfigMap , error ) {
31
32
populateTags (lightrunJavaAgent .Spec .AgentTags , lightrunJavaAgent .Spec .AgentName , & metadata )
32
33
jsonString , err := json .Marshal (metadata )
33
34
if err != nil {
@@ -52,26 +53,28 @@ func (r *LightrunJavaAgentReconciler) createAgentConfig(lightrunJavaAgent *agent
52
53
}
53
54
54
55
func (r * LightrunJavaAgentReconciler ) patchDeployment (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret , origDeployment * appsv1.Deployment , deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , cmDataHash uint64 ) error {
55
-
56
56
// init spec.template.spec
57
57
deploymentApplyConfig .WithSpec (
58
58
appsv1ac .DeploymentSpec ().WithTemplate (
59
59
corev1ac .PodTemplateSpec ().WithSpec (
60
60
corev1ac .PodSpec (),
61
61
).WithAnnotations (map [string ]string {
62
62
annotationConfigMapHash : fmt .Sprint (cmDataHash ),
63
- },
64
- ),
63
+ }),
65
64
),
66
65
).WithAnnotations (map [string ]string {
67
66
annotationAgentName : lightrunJavaAgent .Name ,
68
67
})
69
68
r .addVolume (deploymentApplyConfig , lightrunJavaAgent )
70
69
r .addInitContainer (deploymentApplyConfig , lightrunJavaAgent , secret )
71
- err = r .patchAppContainers (lightrunJavaAgent , origDeployment , deploymentApplyConfig )
70
+ err : = r .patchAppContainers (lightrunJavaAgent , origDeployment , deploymentApplyConfig )
72
71
if err != nil {
73
72
return err
74
73
}
74
+ deploymentApplyConfig .Spec .Template .Spec .WithSecurityContext (
75
+ corev1ac .PodSecurityContext ().
76
+ WithFSGroup (1000 ),
77
+ )
75
78
return nil
76
79
}
77
80
@@ -98,55 +101,73 @@ func (r *LightrunJavaAgentReconciler) addVolume(deploymentApplyConfig *appsv1ac.
98
101
)
99
102
}
100
103
101
- func (r * LightrunJavaAgentReconciler ) addInitContainer (deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) {
104
+ func (r * LightrunJavaAgentReconciler ) createPinnedCertConfigMap (ctx context.Context , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) (* corev1.ConfigMap , error ) {
105
+ configMap := & corev1.ConfigMap {
106
+ ObjectMeta : metav1.ObjectMeta {
107
+ Name : fmt .Sprintf ("%s-pinned-cert" , lightrunJavaAgent .Name ),
108
+ Namespace : lightrunJavaAgent .Namespace ,
109
+ },
110
+ Data : map [string ]string {
111
+ "pinned_cert_hash" : string (secret .Data ["pinned_cert_hash" ]),
112
+ },
113
+ }
102
114
115
+ err := r .Create (ctx , configMap )
116
+ if err != nil {
117
+ return nil , err
118
+ }
119
+
120
+ return configMap , nil
121
+ }
122
+
123
+ func (r * LightrunJavaAgentReconciler ) addInitContainer (deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) {
103
124
deploymentApplyConfig .Spec .Template .Spec .WithInitContainers (
104
125
corev1ac .Container ().
105
126
WithName (initContainerName ).
106
127
WithImage (lightrunJavaAgent .Spec .InitContainer .Image ).
107
128
WithVolumeMounts (
108
- corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath ("/tmp/" ),
129
+ corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath (lightrunJavaAgent . Spec . InitContainer . SharedVolumeMountPath ),
109
130
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
- ).
131
+ corev1ac .VolumeMount ().WithName ("lightrun-secret" ).WithMountPath ("/etc/lightrun/secret" ).WithReadOnly (true ),
132
+ ).
133
+ WithEnv (
134
+ corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
135
+ ).
136
+ WithSecurityContext (
137
+ corev1ac .SecurityContext ().
138
+ WithReadOnlyRootFilesystem (true ).
139
+ WithAllowPrivilegeEscalation (false ).
140
+ WithRunAsNonRoot (true ).
141
+ WithRunAsUser (1000 ),
142
+ ).
123
143
WithResources (
124
144
corev1ac .ResourceRequirements ().
125
145
WithLimits (
126
146
corev1.ResourceList {
127
147
corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
128
- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 500 * 10^6 = 500M
148
+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
129
149
},
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
150
).
142
- WithAllowPrivilegeEscalation ( false ).
143
- WithRunAsNonRoot ( true ).
144
- WithSeccompProfile (
145
- corev1ac . SeccompProfile ().
146
- WithType ( corev1 . SeccompProfileTypeRuntimeDefault ) ,
151
+ WithRequests (
152
+ corev1. ResourceList {
153
+ corev1 . ResourceCPU : * resource . NewMilliQuantity ( int64 ( 50 ), resource . BinarySI ),
154
+ corev1 . ResourceMemory : * resource . NewScaledQuantity ( int64 ( 64 ), resource . Scale ( 6 )),
155
+ } ,
147
156
),
148
157
),
149
158
)
159
+
160
+ // Add volume for secret with proper permissions
161
+ deploymentApplyConfig .Spec .Template .Spec .WithVolumes (
162
+ corev1ac .Volume ().WithName ("lightrun-secret" ).
163
+ WithSecret (corev1ac .SecretVolumeSource ().
164
+ WithSecretName (secret .Name ).
165
+ WithItems (
166
+ corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ),
167
+ corev1ac .KeyToPath ().WithKey ("pinned_cert_hash" ).WithPath ("pinned_cert_hash" ),
168
+ ).
169
+ WithDefaultMode (0440 )),
170
+ )
150
171
}
151
172
152
173
func (r * LightrunJavaAgentReconciler ) patchAppContainers (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , origDeployment * appsv1.Deployment , deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration ) error {
@@ -167,26 +188,22 @@ func (r *LightrunJavaAgentReconciler) patchAppContainers(lightrunJavaAgent *agen
167
188
}
168
189
}
169
190
if ! found {
170
- err = errors .New ("unable to find matching container to patch" )
171
- return err
191
+ return errors .New ("unable to find matching container to patch" )
172
192
}
173
193
return nil
174
194
}
175
195
176
196
// Client side patch, as we can't update value from 2 sources
177
197
func (r * LightrunJavaAgentReconciler ) patchJavaToolEnv (deplAnnotations map [string ]string , container * corev1.Container , targetEnvVar string , agentArg string ) error {
178
- // Check if some env was already patched before
179
198
patchedEnv := deplAnnotations [annotationPatchedEnvName ]
180
199
patchedEnvValue := deplAnnotations [annotationPatchedEnvValue ]
181
200
182
201
if patchedEnv != targetEnvVar || patchedEnvValue != agentArg {
183
- // If different env was patched before - unpatch it
184
202
r .unpatchJavaToolEnv (deplAnnotations , container )
185
203
}
186
204
187
205
targetEnvVarIndex := findEnvVarIndex (targetEnvVar , container .Env )
188
206
if targetEnvVarIndex == - 1 {
189
- // No such env - add new
190
207
container .Env = append (container .Env , corev1.EnvVar {
191
208
Name : targetEnvVar ,
192
209
Value : agentArg ,
@@ -223,28 +240,22 @@ func (r *LightrunJavaAgentReconciler) unpatchJavaToolEnv(deplAnnotations map[str
223
240
224
241
// patchStatefulSet applies changes to a StatefulSet to inject the Lightrun agent
225
242
func (r * LightrunJavaAgentReconciler ) patchStatefulSet (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret , origStatefulSet * appsv1.StatefulSet , statefulSetApplyConfig * appsv1ac.StatefulSetApplyConfiguration , cmDataHash uint64 ) error {
226
- // init spec.template.spec
227
243
statefulSetApplyConfig .WithSpec (
228
244
appsv1ac .StatefulSetSpec ().WithTemplate (
229
245
corev1ac .PodTemplateSpec ().WithSpec (
230
246
corev1ac .PodSpec (),
231
247
).WithAnnotations (map [string ]string {
232
248
annotationConfigMapHash : fmt .Sprint (cmDataHash ),
233
- },
234
- ),
249
+ }),
235
250
),
236
251
).WithAnnotations (map [string ]string {
237
252
annotationAgentName : lightrunJavaAgent .Name ,
238
253
})
239
254
240
- // Add volumes to the StatefulSet
241
255
r .addVolumeToStatefulSet (statefulSetApplyConfig , lightrunJavaAgent )
242
-
243
- // Add init container to the StatefulSet
244
256
r .addInitContainerToStatefulSet (statefulSetApplyConfig , lightrunJavaAgent , secret )
245
257
246
- // Patch app containers in the StatefulSet
247
- err = r .patchStatefulSetAppContainers (lightrunJavaAgent , origStatefulSet , statefulSetApplyConfig )
258
+ err := r .patchStatefulSetAppContainers (lightrunJavaAgent , origStatefulSet , statefulSetApplyConfig )
248
259
if err != nil {
249
260
return err
250
261
}
@@ -271,6 +282,15 @@ func (r *LightrunJavaAgentReconciler) addVolumeToStatefulSet(statefulSetApplyCon
271
282
corev1ac .KeyToPath ().WithKey ("metadata" ).WithPath ("agent.metadata.json" ),
272
283
),
273
284
),
285
+ ).WithVolumes (
286
+ corev1ac .Volume ().WithName ("lightrun-secret" ).
287
+ WithSecret (corev1ac .SecretVolumeSource ().
288
+ WithSecretName (secret .Name ).
289
+ WithItems (
290
+ corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ),
291
+ corev1ac .KeyToPath ().WithKey ("pinned_cert_hash" ).WithPath ("pinned_cert_hash" ),
292
+ ).
293
+ WithDefaultMode (0440 )),
274
294
)
275
295
}
276
296
@@ -280,48 +300,45 @@ func (r *LightrunJavaAgentReconciler) addInitContainerToStatefulSet(statefulSetA
280
300
WithName (initContainerName ).
281
301
WithImage (lightrunJavaAgent .Spec .InitContainer .Image ).
282
302
WithVolumeMounts (
283
- corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath ("/tmp/" ),
303
+ corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath (lightrunJavaAgent . Spec . InitContainer . SharedVolumeMountPath ),
284
304
corev1ac .VolumeMount ().WithName (cmVolumeName ).WithMountPath ("/tmp/cm/" ),
305
+ corev1ac .VolumeMount ().WithName ("lightrun-secret" ).WithMountPath ("/etc/lightrun/secret" ).WithReadOnly (true ),
285
306
).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
307
corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
297
308
).
309
+ WithSecurityContext (
310
+ corev1ac .SecurityContext ().
311
+ WithReadOnlyRootFilesystem (true ).
312
+ WithAllowPrivilegeEscalation (false ).
313
+ WithRunAsNonRoot (true ).
314
+ WithRunAsUser (1000 ),
315
+ ).
298
316
WithResources (
299
317
corev1ac .ResourceRequirements ().
300
318
WithLimits (
301
319
corev1.ResourceList {
302
320
corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
303
- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 64M
321
+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
304
322
},
305
323
).WithRequests (
306
324
corev1.ResourceList {
307
325
corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
308
326
corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
309
327
},
310
328
),
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
329
),
324
330
)
331
+
332
+ statefulSetApplyConfig .Spec .Template .Spec .WithVolumes (
333
+ corev1ac .Volume ().WithName ("lightrun-secret" ).
334
+ WithSecret (corev1ac .SecretVolumeSource ().
335
+ WithSecretName (secret .Name ).
336
+ WithItems (
337
+ corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ),
338
+ corev1ac .KeyToPath ().WithKey ("pinned_cert_hash" ).WithPath ("pinned_cert_hash" ),
339
+ ).
340
+ WithDefaultMode (0440 )),
341
+ )
325
342
}
326
343
327
344
func (r * LightrunJavaAgentReconciler ) patchStatefulSetAppContainers (lightrunJavaAgent * agentv1beta.LightrunJavaAgent , origStatefulSet * appsv1.StatefulSet , statefulSetApplyConfig * appsv1ac.StatefulSetApplyConfiguration ) error {
@@ -342,15 +359,13 @@ func (r *LightrunJavaAgentReconciler) patchStatefulSetAppContainers(lightrunJava
342
359
}
343
360
}
344
361
if ! found {
345
- err = errors .New ("unable to find matching container to patch" )
346
- return err
362
+ return errors .New ("unable to find matching container to patch" )
347
363
}
348
364
return nil
349
365
}
350
366
351
367
// configMapDataHash calculates a hash of the ConfigMap data to detect changes
352
368
func configMapDataHash (cmData map [string ]string ) uint64 {
353
- // Combine all data values into a single string for hashing
354
369
var hashString string
355
370
for _ , v := range cmData {
356
371
hashString += v
0 commit comments