11package controller
22
33import (
4+ "context"
45 "encoding/json"
56 "errors"
67 "fmt"
@@ -27,7 +28,7 @@ const (
2728 annotationAgentName = "lightrun.com/lightrunjavaagent"
2829)
2930
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 ) {
3132 populateTags (lightrunJavaAgent .Spec .AgentTags , lightrunJavaAgent .Spec .AgentName , & metadata )
3233 jsonString , err := json .Marshal (metadata )
3334 if err != nil {
@@ -42,6 +43,7 @@ func (r *LightrunJavaAgentReconciler) createAgentConfig(lightrunJavaAgent *agent
4243 Data : map [string ]string {
4344 "config" : parseAgentConfig (lightrunJavaAgent .Spec .AgentConfig ),
4445 "metadata" : string (jsonString ),
46+ "pinned_cert_hash" : string (secret .Data ["pinned_cert_hash" ]),
4547 },
4648 }
4749
@@ -98,54 +100,58 @@ func (r *LightrunJavaAgentReconciler) addVolume(deploymentApplyConfig *appsv1ac.
98100 )
99101}
100102
101- func (r * LightrunJavaAgentReconciler ) addInitContainer (deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) {
103+ func (r * LightrunJavaAgentReconciler ) createPinnedCertConfigMap (ctx context.Context , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) (* corev1.ConfigMap , error ) {
104+ configMap := & corev1.ConfigMap {
105+ ObjectMeta : metav1.ObjectMeta {
106+ Name : fmt .Sprintf ("%s-pinned-cert" , lightrunJavaAgent .Name ),
107+ Namespace : lightrunJavaAgent .Namespace ,
108+ },
109+ Data : map [string ]string {
110+ "pinned_cert_hash" : string (secret .Data ["pinned_cert_hash" ]),
111+ },
112+ }
102113
114+ err := r .Create (ctx , configMap )
115+ if err != nil {
116+ return nil , err
117+ }
118+
119+ return configMap , nil
120+ }
121+
122+ func (r * LightrunJavaAgentReconciler ) addInitContainer (deploymentApplyConfig * appsv1ac.DeploymentApplyConfiguration , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , secret * corev1.Secret ) {
103123 deploymentApplyConfig .Spec .Template .Spec .WithInitContainers (
104124 corev1ac .Container ().
105125 WithName (initContainerName ).
106126 WithImage (lightrunJavaAgent .Spec .InitContainer .Image ).
107127 WithVolumeMounts (
108128 corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath ("/tmp/" ),
109129 corev1ac .VolumeMount ().WithName (cmVolumeName ).WithMountPath ("/tmp/cm/" ),
130+ corev1ac .VolumeMount ().WithName ("lightrun-secret" ).WithMountPath ("/etc/lightrun/secret" ).WithReadOnly (true ),
110131 ).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- ),
121132 corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
122133 ).
123134 WithResources (
124135 corev1ac .ResourceRequirements ().
125136 WithLimits (
126137 corev1.ResourceList {
127138 corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
128- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 500 * 10^6 = 500M
139+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
129140 },
130141 ).WithRequests (
131142 corev1.ResourceList {
132143 corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
133144 corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
134145 },
135146 ),
136- ).
137- WithSecurityContext (
138- corev1ac .SecurityContext ().
139- WithCapabilities (
140- corev1ac .Capabilities ().WithDrop (corev1 .Capability ("ALL" )),
141- ).
142- WithAllowPrivilegeEscalation (false ).
143- WithRunAsNonRoot (true ).
144- WithSeccompProfile (
145- corev1ac .SeccompProfile ().
146- WithType (corev1 .SeccompProfileTypeRuntimeDefault ),
147- ),
148- ),
147+ )
148+
149+ // Add volume for secret
150+ deploymentApplyConfig .Spec .Template .Spec .WithVolumes (
151+ corev1ac .Volume ().WithName ("lightrun-secret" ).
152+ WithSecret (corev1ac .SecretVolumeSource ().
153+ WithSecretName (secret .Name ).
154+ WithItems (corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ))),
149155 )
150156}
151157
@@ -282,45 +288,31 @@ func (r *LightrunJavaAgentReconciler) addInitContainerToStatefulSet(statefulSetA
282288 WithVolumeMounts (
283289 corev1ac .VolumeMount ().WithName (lightrunJavaAgent .Spec .InitContainer .SharedVolumeName ).WithMountPath ("/tmp/" ),
284290 corev1ac .VolumeMount ().WithName (cmVolumeName ).WithMountPath ("/tmp/cm/" ),
291+ corev1ac .VolumeMount ().WithName ("lightrun-secret" ).WithMountPath ("/etc/lightrun/secret" ).WithReadOnly (true ),
285292 ).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- ),
296293 corev1ac .EnvVar ().WithName ("LIGHTRUN_SERVER" ).WithValue (lightrunJavaAgent .Spec .ServerHostname ),
297294 ).
298295 WithResources (
299296 corev1ac .ResourceRequirements ().
300297 WithLimits (
301298 corev1.ResourceList {
302299 corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
303- corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )), // 64M
300+ corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
304301 },
305302 ).WithRequests (
306303 corev1.ResourceList {
307304 corev1 .ResourceCPU : * resource .NewMilliQuantity (int64 (50 ), resource .BinarySI ),
308305 corev1 .ResourceMemory : * resource .NewScaledQuantity (int64 (64 ), resource .Scale (6 )),
309306 },
310307 ),
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- ),
308+ )
309+
310+ // Add volume for secret
311+ statefulSetApplyConfig .Spec .Template .Spec .WithVolumes (
312+ corev1ac .Volume ().WithName ("lightrun-secret" ).
313+ WithSecret (corev1ac .SecretVolumeSource ().
314+ WithSecretName (secret .Name ).
315+ WithItems (corev1ac .KeyToPath ().WithKey ("lightrun_key" ).WithPath ("lightrun_key" ))),
324316 )
325317}
326318
0 commit comments