Skip to content

Commit 4b892dd

Browse files
committed
add ssh key information
1 parent d74c0ce commit 4b892dd

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,15 @@ pub mod versioned {
6363
/// [example]: DOCS_BASE_URL_PLACEHOLDER/airflow/usage-guide/mounting-dags#_example
6464
#[serde(default)]
6565
pub git_sync_conf: BTreeMap<String, String>,
66+
67+
/// The name of the Secret used for SSH access to the repository.
68+
///
69+
/// The referenced Secret must include two fields: `key` and `knownHosts`.
70+
///
71+
/// [documentation]: https://github.com/kubernetes/git-sync/tree/v4.2.4?tab=readme-ov-file#manual
72+
pub ssh_secret: Option<String>,
73+
74+
#[serde(default = "GitSync::default_ssh_known_hosts")]
75+
pub ssh_known_hosts: bool,
6676
}
6777
}

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use strum::{EnumDiscriminants, IntoStaticStr};
99

1010
use crate::{
1111
builder::pod::{
12-
container::ContainerBuilder, resources::ResourceRequirementsBuilder, volume::VolumeBuilder,
12+
container::ContainerBuilder,
13+
resources::ResourceRequirementsBuilder,
14+
volume::{VolumeBuilder, VolumeMountBuilder},
1315
},
1416
commons::product_image_selection::ResolvedProductImage,
1517
crd::git_sync::v1alpha1::GitSync,
@@ -24,6 +26,8 @@ use crate::{
2426
pub const CONTAINER_NAME_PREFIX: &str = "git-sync";
2527
pub const VOLUME_NAME_PREFIX: &str = "content-from-git";
2628
pub const MOUNT_PATH_PREFIX: &str = "/stackable/app/git";
29+
pub const SSH_VOLUME_NAME_PREFIX: &str = "ssh-keys-info";
30+
pub const SSH_MOUNT_PATH_PREFIX: &str = "/stackable/gitssh";
2731
pub const GIT_SYNC_SAFE_DIR_OPTION: &str = "safe.directory";
2832
pub const GIT_SYNC_ROOT_DIR: &str = "/tmp/git";
2933
pub const GIT_SYNC_LINK: &str = "current";
@@ -58,6 +62,10 @@ impl GitSync {
5862
pub(crate) fn default_wait() -> Duration {
5963
Duration::from_secs(20)
6064
}
65+
66+
pub(crate) fn default_ssh_known_hosts() -> bool {
67+
true
68+
}
6169
}
6270

6371
/// Kubernetes resources generated from `GitSync` specifications which should be added to the Pod.
@@ -77,6 +85,12 @@ pub struct GitSyncResources {
7785

7886
/// Absolute paths to the Git contents in the mounted volumes
7987
pub git_content_folders: Vec<PathBuf>,
88+
89+
/// GitSync volumes containing the synchronized repository
90+
pub git_ssh_volumes: Vec<Volume>,
91+
92+
/// Volume mounts for the GitSync volumes
93+
pub git_ssh_volume_mounts: Vec<VolumeMount>,
8094
}
8195

8296
impl GitSyncResources {
@@ -120,6 +134,25 @@ impl GitSyncResources {
120134
"password",
121135
));
122136
}
137+
if git_sync.ssh_secret.is_some() {
138+
env_vars.push(EnvVar {
139+
name: "GITSYNC_SSH_KEY_FILE".to_owned(),
140+
value: Some(format!("{SSH_MOUNT_PATH_PREFIX}-{i}/key").to_owned()),
141+
value_from: None,
142+
});
143+
env_vars.push(EnvVar {
144+
name: "GITSYNC_SSH_KNOWN_HOSTS_FILE".to_owned(),
145+
value: Some(format!("{SSH_MOUNT_PATH_PREFIX}-{i}/knownHosts").to_owned()),
146+
value_from: None,
147+
});
148+
}
149+
// TODO should we leave to the defaults?
150+
// env_vars.push(EnvVar {
151+
// name: "GITSYNC_SSH_KNOWN_HOSTS".to_owned(),
152+
// value: Some(git_sync.ssh_known_hosts.to_string()),
153+
// value_from: None,
154+
// });
155+
123156
env_vars = insert_or_update_env_vars(&env_vars, extra_env_vars);
124157

125158
let volume_name = format!("{VOLUME_NAME_PREFIX}-{i}");
@@ -186,6 +219,23 @@ impl GitSyncResources {
186219
.git_content_volume_mounts
187220
.push(git_content_volume_mount);
188221
resources.git_content_folders.push(git_content_folder);
222+
223+
if let Some(get_ssh_secret) = &git_sync.ssh_secret {
224+
let ssh_volume_name = format!("{SSH_VOLUME_NAME_PREFIX}-{i}");
225+
let ssh_mount_path = format!("{SSH_MOUNT_PATH_PREFIX}-{i}");
226+
227+
let ssh_secret_volume = VolumeBuilder::new(&ssh_volume_name)
228+
.with_secret(get_ssh_secret, false)
229+
.build();
230+
resources.git_ssh_volumes.push(ssh_secret_volume);
231+
232+
let ssh_secret_volume_mount =
233+
VolumeMountBuilder::new(ssh_volume_name, ssh_mount_path).build();
234+
235+
resources
236+
.git_ssh_volume_mounts
237+
.push(ssh_secret_volume_mount);
238+
}
189239
}
190240

191241
Ok(resources)

0 commit comments

Comments
 (0)