Skip to content

Commit c174820

Browse files
imeliraneliranb
andauthored
DEVOPS-2693-lightrun-k-8-s-operator-add-ability-to-set-image-pull-policy-for-init-container (#45)
* Add imagePullPolicy to initContainer configuration * Make imagePullPolicy field optional in InitContainer configuration * update crd * fix PR comments --------- Co-authored-by: eliranb <[email protected]>
1 parent 560e389 commit c174820

File tree

10 files changed

+122
-83
lines changed

10 files changed

+122
-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,omitempty"`
4447
}
4548

4649
// LightrunJavaAgentSpec defines the desired state of LightrunJavaAgent

charts/lightrun-agents/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The values.yaml file includes the following configurable parameters for each Jav
3535
| `javaAgents[].workloadType` | Type of the Kubernetes workload. Must be either `"Deployment"` or `"StatefulSet"`. **Required when using `workloadName`**. | Required (if `workloadName` is used) |
3636
| `javaAgents[].deploymentName` | **[DEPRECATED]** Name of the Kubernetes deployment to attach the Lightrun Java Agent. Use `workloadName` and `workloadType` instead. | Required (if `workloadName` not used) |
3737
| `javaAgents[].initContainer.image` | Image for the Lightrun Java Agent init container. | Required |
38+
| `javaAgents[].initContainer.imagePullPolicy` | Image pull policy for the init container. Can be one of: Always, IfNotPresent, or Never. | Optional (if not provided, defaults according to [Kubernetes Default Image Pull Policy](https://kubernetes.io/docs/concepts/containers/images/#imagepullpolicy-defaulting)) |
3839
| `javaAgents[].initContainer.sharedVolumeMountPath` | Mount path for the shared volume in the init container. | Optional (if not provided, defaults to `"/lightrun"`" |
3940
| `javaAgents[].initContainer.sharedVolumeName` | Name of the shared volume for the init container. | Optional (if not provided, defaults to `"lightrun-agent-init"`" |
4041
| `javaAgents[].name` | Name of the Lightrun Java Agent custom resource. | Required |
@@ -130,6 +131,7 @@ javaAgents:
130131
serverHostname: 'lightrun.example.com'
131132
initContainer:
132133
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
134+
imagePullPolicy: "IfNotPresent"
133135
agentPoolCredentials:
134136
existingSecret: ""
135137
apiKey: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@@ -197,6 +199,7 @@ javaAgents:
197199
agentCliFlags: "--lightrun_extra_class_path=<PATH_TO_JAR>:<PATH_TO_JAR>,lightrun_init_wait_time_ms"
198200
initContainer:
199201
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
202+
imagePullPolicy: "IfNotPresent"
200203
sharedVolumeName: 'my-shared-volume'
201204
sharedVolumeMountPath: '/mypath'
202205
agentPoolCredentials:
@@ -212,6 +215,7 @@ javaAgents:
212215
namespace: 'my-namespace-2'
213216
initContainer:
214217
image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
218+
imagePullPolicy: "IfNotPresent"
215219
sharedVolumeName: 'my-shared-volume'
216220
sharedVolumeMountPath: '/mypath'
217221
# StatefulSet configuration with full options

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ metadata:
88
spec:
99
initContainer:
1010
image: {{ .initContainer.image }}
11+
{{- if .initContainer.imagePullPolicy }}
12+
imagePullPolicy: {{ .initContainer.imagePullPolicy }}
13+
{{- end }}
1114
sharedVolumeName: {{ .initContainer.sharedVolumeName | default "lightrun-agent-init" }}
1215
sharedVolumeMountPath: {{ .initContainer.sharedVolumeMountPath | default "/lightrun" }}
1316
{{- if .workloadName }}

charts/lightrun-agents/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ javaAgents: []
2121
# serverHostname: 'lightrun.example.com'
2222
# initContainer:
2323
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
24+
# imagePullPolicy: "IfNotPresent"
2425
# agentPoolCredentials:
2526
# existingSecret: ""
2627
# apiKey: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
@@ -34,6 +35,7 @@ javaAgents: []
3435
# namespace: 'my-namespace-2'
3536
# initContainer:
3637
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
38+
# imagePullPolicy: "IfNotPresent"
3739
# # Example of StatefulSet configuration
3840
# workloadName: "my-statefulset-2"
3941
# workloadType: "StatefulSet"
@@ -73,6 +75,7 @@ javaAgents: []
7375
# agentCliFlags: "--lightrun_extra_class_path=<PATH_TO_JAR>:<PATH_TO_JAR>,lightrun_init_wait_time_ms"
7476
# initContainer:
7577
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
78+
# imagePullPolicy: "IfNotPresent"
7679
# sharedVolumeName: 'my-shared-volume'
7780
# sharedVolumeMountPath: '/mypath'
7881
# agentPoolCredentials:
@@ -88,6 +91,7 @@ javaAgents: []
8891
# namespace: 'my-namespace-2'
8992
# initContainer:
9093
# image: "lightruncom/k8s-operator-init-java-agent-linux:latest"
94+
# imagePullPolicy: "IfNotPresent"
9195
# sharedVolumeName: 'my-shared-volume'
9296
# sharedVolumeMountPath: '/mypath'
9397
# # Example of StatefulSet configuration

charts/lightrun-operator/crds/lightrunjavaagent_crd.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ spec:
9898
description: Image of the init container. Image name and tag will
9999
define platform and version of the agent
100100
type: string
101+
imagePullPolicy:
102+
description: 'Pull policy of the init container. Can be one of:
103+
Always, IfNotPresent, or Never.'
104+
type: string
101105
sharedVolumeMountPath:
102106
description: Path in the app container where volume with agent
103107
will be mounted

config/crd/bases/agents.lightrun.com_lightrunjavaagents.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ spec:
9999
description: Image of the init container. Image name and tag will
100100
define platform and version of the agent
101101
type: string
102+
imagePullPolicy:
103+
description: 'Pull policy of the init container. Can be one of:
104+
Always, IfNotPresent, or Never.'
105+
type: string
102106
sharedVolumeMountPath:
103107
description: Path in the app container where volume with agent
104108
will be mounted

config/samples/operator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ spec:
110110
description: Image of the init container. Image name and tag will
111111
define platform and version of the agent
112112
type: string
113+
imagePullPolicy:
114+
description: 'Pull policy of the init container. Can be one of:
115+
Always, IfNotPresent, or Never.'
116+
type: string
113117
sharedVolumeMountPath:
114118
description: Path in the app container where volume with agent
115119
will be mounted

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.

examples/operator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ spec:
100100
description: Image of the init container. Image name and tag will
101101
define platform and version of the agent
102102
type: string
103+
imagePullPolicy:
104+
description: 'Pull policy of the init container. Can be one of:
105+
Always, IfNotPresent, or Never.'
106+
type: string
103107
sharedVolumeMountPath:
104108
description: Path in the app container where volume with agent
105109
will be mounted

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)