Skip to content

Commit 38f57ba

Browse files
author
eliranb
committed
Add imagePullPolicy to initContainer configuration
1 parent 2be38da commit 38f57ba

File tree

6 files changed

+105
-83
lines changed

6 files changed

+105
-83
lines changed

api/v1beta/lightrunjavaagent_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta
1818

1919
import (
20+
corev1 "k8s.io/api/core/v1"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
)
2223

@@ -41,6 +42,8 @@ type InitContainer struct {
4142
SharedVolumeMountPath string `json:"sharedVolumeMountPath"`
4243
// Image of the init container. Image name and tag will define platform and version of the agent
4344
Image string `json:"image"`
45+
// Pull policy of the init container. Can be one of: Always, IfNotPresent, or Never.
46+
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy"`
4447
}
4548

4649
// LightrunJavaAgentSpec defines the desired state of LightrunJavaAgent

charts/lightrun-agents/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The values.yaml file includes the following configurable parameters for each Jav
3333
| `javaAgents[].containerSelector` | Selector for containers within the deployment to inject the Lightrun Java Agent. | Required |
3434
| `javaAgents[].deploymentName` | Name of the Kubernetes deployment to attach the Lightrun Java Agent. | Required |
3535
| `javaAgents[].initContainer.image` | Image for the Lightrun Java Agent init container. | Required |
36+
| `javaAgents[].initContainer.imagePullPolicy` | Image pull policy for the init container. Can be one of: Always, IfNotPresent, or Never. | Optional (if not provided, defaults to `"IfNotPresent"`) |
3637
| `javaAgents[].initContainer.sharedVolumeMountPath` | Mount path for the shared volume in the init container. | Optional (if not provided, defaults to `"/lightrun"`" |
3738
| `javaAgents[].initContainer.sharedVolumeName` | Name of the shared volume for the init container. | Optional (if not provided, defaults to `"lightrun-agent-init"`" |
3839
| `javaAgents[].name` | Name of the Lightrun Java Agent custom resource. | Required |
@@ -91,6 +92,7 @@ javaAgents:
9192
serverHostname: 'lightrun.example.com'
9293
initContainer:
9394
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
95+
imagePullPolicy: "IfNotPresent"
9496
agentPoolCredentials:
9597
existingSecret: ""
9698
apiKey: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@@ -104,6 +106,7 @@ javaAgents:
104106
namespace: 'my-namespace-2'
105107
initContainer:
106108
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
109+
imagePullPolicy: "IfNotPresent"
107110
deploymentName: "my-deployment-2"
108111
containerSelector:
109112
- my-container-2
@@ -139,6 +142,7 @@ javaAgents:
139142
agentCliFlags: "--lightrun_extra_class_path=<PATH_TO_JAR>:<PATH_TO_JAR>,lightrun_init_wait_time_ms"
140143
initContainer:
141144
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
145+
imagePullPolicy: "IfNotPresent"
142146
sharedVolumeName: 'my-shared-volume'
143147
sharedVolumeMountPath: '/mypath'
144148
agentPoolCredentials:
@@ -154,6 +158,7 @@ javaAgents:
154158
namespace: 'my-namespace-2'
155159
initContainer:
156160
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
161+
imagePullPolicy: "IfNotPresent"
157162
sharedVolumeName: 'my-shared-volume'
158163
sharedVolumeMountPath: '/mypath'
159164
deploymentName: "my-deployment-2"

charts/lightrun-agents/templates/java-agent-cr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ metadata:
88
spec:
99
initContainer:
1010
image: {{ .initContainer.image }}
11+
imagePullPolicy: {{ .initContainer.imagePullPolicy | default "IfNotPresent" }}
1112
sharedVolumeName: {{ .initContainer.sharedVolumeName | default "lightrun-agent-init" }}
1213
sharedVolumeMountPath: {{ .initContainer.sharedVolumeMountPath | default "/lightrun" }}
1314
deploymentName: {{ .deploymentName }}

charts/lightrun-agents/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ javaAgents: []
1717
# serverHostname: 'lightrun.example.com'
1818
# initContainer:
1919
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
20+
# imagePullPolicy: "IfNotPresent"
2021
# agentPoolCredentials:
2122
# existingSecret: ""
2223
# apiKey: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@@ -30,6 +31,7 @@ javaAgents: []
3031
# namespace: 'my-namespace-2'
3132
# initContainer:
3233
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
34+
# imagePullPolicy: "IfNotPresent"
3335
# deploymentName: "my-deployment-2"
3436
# containerSelector:
3537
# - my-container-2
@@ -63,6 +65,7 @@ javaAgents: []
6365
# agentCliFlags: "--lightrun_extra_class_path=<PATH_TO_JAR>:<PATH_TO_JAR>,lightrun_init_wait_time_ms"
6466
# initContainer:
6567
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
68+
# imagePullPolicy: "IfNotPresent"
6669
# sharedVolumeName: 'my-shared-volume'
6770
# sharedVolumeMountPath: '/mypath'
6871
# agentPoolCredentials:
@@ -78,6 +81,7 @@ javaAgents: []
7881
# namespace: 'my-namespace-2'
7982
# initContainer:
8083
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
84+
# imagePullPolicy: "IfNotPresent"
8185
# sharedVolumeName: 'my-shared-volume'
8286
# sharedVolumeMountPath: '/mypath'
8387
# deploymentName: "my-deployment-2"

docs/custom_resource.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ spec:
1111
# agent version - first part of the tag (1.7.0)
1212
# init container sub-version - last part of the tag (init.0)
1313
image: "lightruncom/k8s-operator-init-java-agent-linux:1.7.0-init.0"
14+
# imagePullPolicy of the init container. Can be one of: Always, IfNotPresent, or Never.
15+
imagePullPolicy: "IfNotPresent"
1416
# Volume name in case you have some convention in the names
1517
sharedVolumeName: lightrun-agent-init
1618
# Mount path where volume will be parked. Various distributions may have it's limitations.

internal/controller/patch_funcs.go

Lines changed: 90 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -99,54 +99,57 @@ func (r *LightrunJavaAgentReconciler) addVolume(deploymentApplyConfig *appsv1ac.
9999
}
100100

101101
func (r *LightrunJavaAgentReconciler) addInitContainer(deploymentApplyConfig *appsv1ac.DeploymentApplyConfiguration, lightrunJavaAgent *agentv1beta.LightrunJavaAgent, secret *corev1.Secret) {
102-
103-
deploymentApplyConfig.Spec.Template.Spec.WithInitContainers(
104-
corev1ac.Container().
105-
WithName(initContainerName).
106-
WithImage(lightrunJavaAgent.Spec.InitContainer.Image).
107-
WithVolumeMounts(
108-
corev1ac.VolumeMount().WithName(lightrunJavaAgent.Spec.InitContainer.SharedVolumeName).WithMountPath("/tmp/"),
109-
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-
),
102+
spec := lightrunJavaAgent.Spec
103+
isImagePullPolicyConfigured := spec.InitContainer.ImagePullPolicy != ""
104+
initContainerApplyConfig := corev1ac.Container().
105+
WithName(initContainerName).
106+
WithImage(spec.InitContainer.Image).
107+
WithVolumeMounts(
108+
corev1ac.VolumeMount().WithName(spec.InitContainer.SharedVolumeName).WithMountPath("/tmp/"),
109+
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"),
115114
),
116-
corev1ac.EnvVar().WithName("PINNED_CERT").WithValueFrom(
117-
corev1ac.EnvVarSource().WithSecretKeyRef(
118-
corev1ac.SecretKeySelector().WithName(secret.Name).WithKey("pinned_cert_hash"),
119-
),
115+
),
116+
corev1ac.EnvVar().WithName("PINNED_CERT").WithValueFrom(
117+
corev1ac.EnvVarSource().WithSecretKeyRef(
118+
corev1ac.SecretKeySelector().WithName(secret.Name).WithKey("pinned_cert_hash"),
120119
),
121-
corev1ac.EnvVar().WithName("LIGHTRUN_SERVER").WithValue(lightrunJavaAgent.Spec.ServerHostname),
122-
).
123-
WithResources(
124-
corev1ac.ResourceRequirements().
125-
WithLimits(
126-
corev1.ResourceList{
127-
corev1.ResourceCPU: *resource.NewMilliQuantity(int64(50), resource.BinarySI),
128-
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)), // 500 * 10^6 = 500M
129-
},
130-
).WithRequests(
120+
),
121+
corev1ac.EnvVar().WithName("LIGHTRUN_SERVER").WithValue(spec.ServerHostname),
122+
).
123+
WithResources(
124+
corev1ac.ResourceRequirements().
125+
WithLimits(
131126
corev1.ResourceList{
132127
corev1.ResourceCPU: *resource.NewMilliQuantity(int64(50), resource.BinarySI),
133-
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)),
128+
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)), // 500 * 10^6 = 500M
134129
},
135-
),
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-
),
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+
},
148135
),
149-
)
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+
)
149+
if isImagePullPolicyConfigured {
150+
initContainerApplyConfig.WithImagePullPolicy(spec.InitContainer.ImagePullPolicy)
151+
}
152+
deploymentApplyConfig.Spec.Template.Spec.WithInitContainers(initContainerApplyConfig)
150153
}
151154

152155
func (r *LightrunJavaAgentReconciler) patchAppContainers(lightrunJavaAgent *agentv1beta.LightrunJavaAgent, origDeployment *appsv1.Deployment, deploymentApplyConfig *appsv1ac.DeploymentApplyConfiguration) error {
@@ -275,53 +278,57 @@ func (r *LightrunJavaAgentReconciler) addVolumeToStatefulSet(statefulSetApplyCon
275278
}
276279

277280
func (r *LightrunJavaAgentReconciler) addInitContainerToStatefulSet(statefulSetApplyConfig *appsv1ac.StatefulSetApplyConfiguration, lightrunJavaAgent *agentv1beta.LightrunJavaAgent, secret *corev1.Secret) {
278-
statefulSetApplyConfig.Spec.Template.Spec.WithInitContainers(
279-
corev1ac.Container().
280-
WithName(initContainerName).
281-
WithImage(lightrunJavaAgent.Spec.InitContainer.Image).
282-
WithVolumeMounts(
283-
corev1ac.VolumeMount().WithName(lightrunJavaAgent.Spec.InitContainer.SharedVolumeName).WithMountPath("/tmp/"),
284-
corev1ac.VolumeMount().WithName(cmVolumeName).WithMountPath("/tmp/cm/"),
285-
).WithEnv(
286-
corev1ac.EnvVar().WithName("LIGHTRUN_KEY").WithValueFrom(
287-
corev1ac.EnvVarSource().WithSecretKeyRef(
288-
corev1ac.SecretKeySelector().WithName(secret.Name).WithKey("lightrun_key"),
289-
),
281+
spec := lightrunJavaAgent.Spec
282+
isImagePullPolicyConfigured := spec.InitContainer.ImagePullPolicy != ""
283+
initContainerApplyConfig := corev1ac.Container().
284+
WithName(initContainerName).
285+
WithImage(spec.InitContainer.Image).
286+
WithVolumeMounts(
287+
corev1ac.VolumeMount().WithName(spec.InitContainer.SharedVolumeName).WithMountPath("/tmp/"),
288+
corev1ac.VolumeMount().WithName(cmVolumeName).WithMountPath("/tmp/cm/"),
289+
).WithEnv(
290+
corev1ac.EnvVar().WithName("LIGHTRUN_KEY").WithValueFrom(
291+
corev1ac.EnvVarSource().WithSecretKeyRef(
292+
corev1ac.SecretKeySelector().WithName(secret.Name).WithKey("lightrun_key"),
290293
),
291-
corev1ac.EnvVar().WithName("PINNED_CERT").WithValueFrom(
292-
corev1ac.EnvVarSource().WithSecretKeyRef(
293-
corev1ac.SecretKeySelector().WithName(secret.Name).WithKey("pinned_cert_hash"),
294-
),
294+
),
295+
corev1ac.EnvVar().WithName("PINNED_CERT").WithValueFrom(
296+
corev1ac.EnvVarSource().WithSecretKeyRef(
297+
corev1ac.SecretKeySelector().WithName(secret.Name).WithKey("pinned_cert_hash"),
295298
),
296-
corev1ac.EnvVar().WithName("LIGHTRUN_SERVER").WithValue(lightrunJavaAgent.Spec.ServerHostname),
297-
).
298-
WithResources(
299-
corev1ac.ResourceRequirements().
300-
WithLimits(
301-
corev1.ResourceList{
302-
corev1.ResourceCPU: *resource.NewMilliQuantity(int64(50), resource.BinarySI),
303-
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)), // 64M
304-
},
305-
).WithRequests(
299+
),
300+
corev1ac.EnvVar().WithName("LIGHTRUN_SERVER").WithValue(spec.ServerHostname),
301+
).
302+
WithResources(
303+
corev1ac.ResourceRequirements().
304+
WithLimits(
306305
corev1.ResourceList{
307306
corev1.ResourceCPU: *resource.NewMilliQuantity(int64(50), resource.BinarySI),
308-
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)),
307+
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)), // 64M
309308
},
310-
),
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-
),
309+
).WithRequests(
310+
corev1.ResourceList{
311+
corev1.ResourceCPU: *resource.NewMilliQuantity(int64(50), resource.BinarySI),
312+
corev1.ResourceMemory: *resource.NewScaledQuantity(int64(64), resource.Scale(6)),
313+
},
323314
),
324-
)
315+
).
316+
WithSecurityContext(
317+
corev1ac.SecurityContext().
318+
WithCapabilities(
319+
corev1ac.Capabilities().WithDrop(corev1.Capability("ALL")),
320+
).
321+
WithAllowPrivilegeEscalation(false).
322+
WithRunAsNonRoot(true).
323+
WithSeccompProfile(
324+
corev1ac.SeccompProfile().
325+
WithType(corev1.SeccompProfileTypeRuntimeDefault),
326+
),
327+
)
328+
if isImagePullPolicyConfigured {
329+
initContainerApplyConfig.WithImagePullPolicy(spec.InitContainer.ImagePullPolicy)
330+
}
331+
statefulSetApplyConfig.Spec.Template.Spec.WithInitContainers(initContainerApplyConfig)
325332
}
326333

327334
func (r *LightrunJavaAgentReconciler) patchStatefulSetAppContainers(lightrunJavaAgent *agentv1beta.LightrunJavaAgent, origStatefulSet *appsv1.StatefulSet, statefulSetApplyConfig *appsv1ac.StatefulSetApplyConfiguration) error {

0 commit comments

Comments
 (0)