Skip to content

Commit 6619fe4

Browse files
Add logging support to git-sync containers
1 parent 613e58d commit 6619fe4

File tree

1 file changed

+86
-13
lines changed

1 file changed

+86
-13
lines changed

crates/stackable-operator/src/crd/git_sync/v1alpha1_impl.rs

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use crate::{
1313
commons::product_image_selection::ResolvedProductImage,
1414
crd::git_sync::v1alpha1::GitSync,
1515
product_config_utils::insert_or_update_env_vars,
16+
product_logging::{
17+
framework::capture_shell_output,
18+
spec::{ContainerLogConfig, ContainerLogConfigChoice},
19+
},
1620
time::Duration,
1721
utils::COMMON_BASH_TRAP_FUNCTIONS,
1822
};
@@ -72,6 +76,8 @@ pub struct GitSyncResources {
7276
}
7377

7478
impl GitSyncResources {
79+
const LOG_VOLUME_MOUNT_PATH: &str = "/stackable/log";
80+
7581
/// Returns whether or not GitSync is enabled.
7682
pub fn is_git_sync_enabled(&self) -> bool {
7783
!self.git_sync_containers.is_empty()
@@ -91,6 +97,8 @@ impl GitSyncResources {
9197
resolved_product_image: &ResolvedProductImage,
9298
extra_env_vars: &[EnvVar],
9399
extra_volume_mounts: &[VolumeMount],
100+
log_volume_name: &str,
101+
container_log_config: &ContainerLogConfig,
94102
) -> Result<GitSyncResources, Error> {
95103
let mut resources = GitSyncResources::default();
96104

@@ -118,7 +126,15 @@ impl GitSyncResources {
118126
mount_path: GIT_SYNC_ROOT_DIR.to_string(),
119127
..VolumeMount::default()
120128
};
121-
let mut git_sync_container_volume_mounts = vec![git_sync_root_volume_mount];
129+
130+
let log_volume_mount = VolumeMount {
131+
name: log_volume_name.to_string(),
132+
mount_path: Self::LOG_VOLUME_MOUNT_PATH.to_string(),
133+
..VolumeMount::default()
134+
};
135+
136+
let mut git_sync_container_volume_mounts =
137+
vec![git_sync_root_volume_mount, log_volume_mount];
122138
git_sync_container_volume_mounts.extend_from_slice(extra_volume_mounts);
123139

124140
let container = Self::create_git_sync_container(
@@ -128,6 +144,7 @@ impl GitSyncResources {
128144
false,
129145
&env_vars,
130146
&git_sync_container_volume_mounts,
147+
container_log_config,
131148
)?;
132149

133150
let init_container = Self::create_git_sync_container(
@@ -137,6 +154,7 @@ impl GitSyncResources {
137154
true,
138155
&env_vars,
139156
&git_sync_container_volume_mounts,
157+
container_log_config,
140158
)?;
141159

142160
let volume = VolumeBuilder::new(volume_name.to_owned())
@@ -176,6 +194,7 @@ impl GitSyncResources {
176194
one_time: bool,
177195
env_vars: &[EnvVar],
178196
volume_mounts: &[VolumeMount],
197+
container_log_config: &ContainerLogConfig,
179198
) -> Result<k8s_openapi::api::core::v1::Container, Error> {
180199
let container = ContainerBuilder::new(container_name)
181200
.context(InvalidContainerNameSnafu)?
@@ -187,7 +206,12 @@ impl GitSyncResources {
187206
"pipefail".to_string(),
188207
"-c".to_string(),
189208
])
190-
.args(vec![Self::create_git_sync_command(git_sync, one_time)])
209+
.args(vec![Self::create_git_sync_shell_script(
210+
container_name,
211+
git_sync,
212+
one_time,
213+
container_log_config,
214+
)])
191215
.add_env_vars(env_vars.into())
192216
.add_volume_mounts(volume_mounts.to_vec())
193217
.context(AddVolumeMountSnafu)?
@@ -203,7 +227,12 @@ impl GitSyncResources {
203227
Ok(container)
204228
}
205229

206-
fn create_git_sync_command(git_sync: &GitSync, one_time: bool) -> String {
230+
fn create_git_sync_shell_script(
231+
container_name: &str,
232+
git_sync: &GitSync,
233+
one_time: bool,
234+
container_log_config: &ContainerLogConfig,
235+
) -> String {
207236
let internal_args = [
208237
Some(("--repo".to_string(), git_sync.repo.to_owned())),
209238
Some(("--ref".to_string(), git_sync.branch.to_owned())),
@@ -275,19 +304,35 @@ impl GitSyncResources {
275304
.collect::<Vec<_>>()
276305
.join(" ");
277306

307+
let mut shell_script = String::new();
308+
309+
if let ContainerLogConfig {
310+
choice: Some(ContainerLogConfigChoice::Automatic(log_config)),
311+
} = container_log_config
312+
{
313+
shell_script.push_str(&capture_shell_output(
314+
Self::LOG_VOLUME_MOUNT_PATH,
315+
container_name,
316+
log_config,
317+
));
318+
shell_script.push('\n');
319+
};
320+
278321
let git_sync_command = format!("/stackable/git-sync {args_string}");
279322

280323
if one_time {
281-
git_sync_command
324+
shell_script.push_str(&git_sync_command);
282325
} else {
283326
// Run the git-sync command in the background
284-
format!(
327+
shell_script.push_str(&format!(
285328
"{COMMON_BASH_TRAP_FUNCTIONS}
286329
prepare_signal_handlers
287330
{git_sync_command} &
288331
wait_for_termination $!"
289-
)
332+
))
290333
}
334+
335+
shell_script
291336
}
292337

293338
fn env_var_from_secret(var_name: &str, secret: &str, secret_key: &str) -> EnvVar {
@@ -309,7 +354,10 @@ wait_for_termination $!"
309354
#[cfg(test)]
310355
mod tests {
311356
use super::*;
312-
use crate::product_config_utils::env_vars_from;
357+
use crate::{
358+
config::fragment::validate, product_config_utils::env_vars_from,
359+
product_logging::spec::default_container_log_config,
360+
};
313361

314362
#[test]
315363
fn test_no_git_sync() {
@@ -332,6 +380,8 @@ mod tests {
332380
&resolved_product_image,
333381
&extra_env_vars,
334382
&extra_volume_mounts,
383+
"log-volume",
384+
&validate(default_container_log_config()).unwrap(),
335385
)
336386
.unwrap();
337387

@@ -407,6 +457,8 @@ mod tests {
407457
&resolved_product_image,
408458
&extra_env_vars,
409459
&extra_volume_mounts,
460+
"log-volume",
461+
&validate(default_container_log_config()).unwrap(),
410462
)
411463
.unwrap();
412464

@@ -416,7 +468,8 @@ mod tests {
416468

417469
assert_eq!(
418470
r#"args:
419-
- |2-
471+
- |-
472+
mkdir --parents /stackable/log/git-sync-0 && exec > >(tee /stackable/log/git-sync-0/container.stdout.log) 2> >(tee /stackable/log/git-sync-0/container.stderr.log >&2)
420473
421474
prepare_signal_handlers()
422475
{
@@ -474,6 +527,8 @@ resources:
474527
volumeMounts:
475528
- mountPath: /tmp/git
476529
name: content-from-git-0
530+
- mountPath: /stackable/log
531+
name: log-volume
477532
- mountPath: /mnt/extra-volume
478533
name: extra-volume
479534
"#,
@@ -482,7 +537,8 @@ volumeMounts:
482537

483538
assert_eq!(
484539
r#"args:
485-
- |2-
540+
- |-
541+
mkdir --parents /stackable/log/git-sync-1 && exec > >(tee /stackable/log/git-sync-1/container.stdout.log) 2> >(tee /stackable/log/git-sync-1/container.stderr.log >&2)
486542
487543
prepare_signal_handlers()
488544
{
@@ -545,6 +601,8 @@ resources:
545601
volumeMounts:
546602
- mountPath: /tmp/git
547603
name: content-from-git-1
604+
- mountPath: /stackable/log
605+
name: log-volume
548606
- mountPath: /mnt/extra-volume
549607
name: extra-volume
550608
"#,
@@ -553,7 +611,8 @@ volumeMounts:
553611

554612
assert_eq!(
555613
r#"args:
556-
- |2-
614+
- |-
615+
mkdir --parents /stackable/log/git-sync-2 && exec > >(tee /stackable/log/git-sync-2/container.stdout.log) 2> >(tee /stackable/log/git-sync-2/container.stderr.log >&2)
557616
558617
prepare_signal_handlers()
559618
{
@@ -611,6 +670,8 @@ resources:
611670
volumeMounts:
612671
- mountPath: /tmp/git
613672
name: content-from-git-2
673+
- mountPath: /stackable/log
674+
name: log-volume
614675
- mountPath: /mnt/extra-volume
615676
name: extra-volume
616677
"#,
@@ -621,7 +682,9 @@ volumeMounts:
621682

622683
assert_eq!(
623684
r#"args:
624-
- /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=true --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git
685+
- |-
686+
mkdir --parents /stackable/log/git-sync-0-init && exec > >(tee /stackable/log/git-sync-0-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-0-init/container.stderr.log >&2)
687+
/stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git' --link=current --one-time=true --period=20s --ref=main --repo=https://github.com/stackabletech/repo1 --root=/tmp/git
625688
command:
626689
- /bin/bash
627690
- -x
@@ -646,6 +709,8 @@ resources:
646709
volumeMounts:
647710
- mountPath: /tmp/git
648711
name: content-from-git-0
712+
- mountPath: /stackable/log
713+
name: log-volume
649714
- mountPath: /mnt/extra-volume
650715
name: extra-volume
651716
"#,
@@ -654,7 +719,9 @@ volumeMounts:
654719

655720
assert_eq!(
656721
r#"args:
657-
- /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=true --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git
722+
- |-
723+
mkdir --parents /stackable/log/git-sync-1-init && exec > >(tee /stackable/log/git-sync-1-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-1-init/container.stderr.log >&2)
724+
/stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=true --period=60s --ref=trunk --repo=https://github.com/stackabletech/repo2 --rev=HEAD --root=/tmp/git
658725
command:
659726
- /bin/bash
660727
- -x
@@ -684,6 +751,8 @@ resources:
684751
volumeMounts:
685752
- mountPath: /tmp/git
686753
name: content-from-git-1
754+
- mountPath: /stackable/log
755+
name: log-volume
687756
- mountPath: /mnt/extra-volume
688757
name: extra-volume
689758
"#,
@@ -692,7 +761,9 @@ volumeMounts:
692761

693762
assert_eq!(
694763
r#"args:
695-
- /stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,k1:v1,k2:v2,safe.directory:/safe-dir,k3:v3,k4:v4' --link=current --one-time=true --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git
764+
- |-
765+
mkdir --parents /stackable/log/git-sync-2-init && exec > >(tee /stackable/log/git-sync-2-init/container.stdout.log) 2> >(tee /stackable/log/git-sync-2-init/container.stderr.log >&2)
766+
/stackable/git-sync --depth=1 --git-config='safe.directory:/tmp/git,k1:v1,k2:v2,safe.directory:/safe-dir,k3:v3,k4:v4' --link=current --one-time=true --period=20s --ref=feat/git-sync --repo=https://github.com/stackabletech/repo3 --root=/tmp/git
696767
command:
697768
- /bin/bash
698769
- -x
@@ -717,6 +788,8 @@ resources:
717788
volumeMounts:
718789
- mountPath: /tmp/git
719790
name: content-from-git-2
791+
- mountPath: /stackable/log
792+
name: log-volume
720793
- mountPath: /mnt/extra-volume
721794
name: extra-volume
722795
"#,

0 commit comments

Comments
 (0)