@@ -62,10 +62,6 @@ impl GitSync {
6262 pub ( crate ) fn default_wait ( ) -> Duration {
6363 Duration :: from_secs ( 20 )
6464 }
65-
66- pub ( crate ) fn default_ssh_known_hosts ( ) -> bool {
67- true
68- }
6965}
7066
7167/// Kubernetes resources generated from `GitSync` specifications which should be added to the Pod.
@@ -88,9 +84,6 @@ pub struct GitSyncResources {
8884
8985 /// GitSync volumes containing the synchronized repository
9086 pub git_ssh_volumes : Vec < Volume > ,
91-
92- /// Volume mounts for the GitSync volumes
93- pub git_ssh_volume_mounts : Vec < VolumeMount > ,
9487}
9588
9689impl GitSyncResources {
@@ -146,12 +139,6 @@ impl GitSyncResources {
146139 value_from : None ,
147140 } ) ;
148141 }
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- // });
155142
156143 env_vars = insert_or_update_env_vars ( & env_vars, extra_env_vars) ;
157144
@@ -172,8 +159,18 @@ impl GitSyncResources {
172159
173160 let mut git_sync_container_volume_mounts =
174161 vec ! [ git_sync_root_volume_mount, log_volume_mount] ;
162+
175163 git_sync_container_volume_mounts. extend_from_slice ( extra_volume_mounts) ;
176164
165+ if git_sync. ssh_secret . is_some ( ) {
166+ let ssh_mount_path = format ! ( "{SSH_MOUNT_PATH_PREFIX}-{i}" ) ;
167+ let ssh_volume_name = format ! ( "{SSH_VOLUME_NAME_PREFIX}-{i}" ) ;
168+
169+ let ssh_volume_mount =
170+ VolumeMountBuilder :: new ( ssh_volume_name, ssh_mount_path) . build ( ) ;
171+ git_sync_container_volume_mounts. push ( ssh_volume_mount) ;
172+ }
173+
177174 let container = Self :: create_git_sync_container (
178175 & format ! ( "{CONTAINER_NAME_PREFIX}-{i}" ) ,
179176 resolved_product_image,
@@ -222,19 +219,11 @@ impl GitSyncResources {
222219
223220 if let Some ( get_ssh_secret) = & git_sync. ssh_secret {
224221 let ssh_volume_name = format ! ( "{SSH_VOLUME_NAME_PREFIX}-{i}" ) ;
225- let ssh_mount_path = format ! ( "{SSH_MOUNT_PATH_PREFIX}-{i}" ) ;
226222
227223 let ssh_secret_volume = VolumeBuilder :: new ( & ssh_volume_name)
228224 . with_secret ( get_ssh_secret, false )
229225 . build ( ) ;
230226 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) ;
238227 }
239228 }
240229
@@ -926,4 +915,209 @@ name: content-from-git-2
926915 . unwrap( )
927916 ) ;
928917 }
918+
919+ #[ test]
920+ fn test_git_sync_ssh ( ) {
921+ let git_sync_spec = r#"
922+ # GitSync using SSH
923+ - repo: ssh://git@github.com/stackabletech/repo.git
924+ branch: trunk
925+ gitFolder: ""
926+ depth: 3
927+ wait: 1m
928+ sshSecret: git-sync-ssh
929+ gitSyncConf:
930+ --rev: HEAD
931+ --git-config: http.sslCAInfo:/tmp/ca-cert/ca.crt
932+ "# ;
933+
934+ let git_syncs: Vec < GitSync > = yaml_from_str_singleton_map ( git_sync_spec) . unwrap ( ) ;
935+
936+ let resolved_product_image = ResolvedProductImage {
937+ image : "oci.stackable.tech/sdp/product:latest" . to_string ( ) ,
938+ app_version_label_value : "1.0.0-latest"
939+ . parse ( )
940+ . expect ( "static app version label is always valid" ) ,
941+ product_version : "1.0.0" . to_string ( ) ,
942+ image_pull_policy : "Always" . to_string ( ) ,
943+ pull_secrets : None ,
944+ } ;
945+
946+ let extra_env_vars = env_vars_from ( [ ( "VAR1" , "value1" ) ] ) ;
947+
948+ let extra_volume_mounts = [ VolumeMount {
949+ name : "extra-volume" . to_string ( ) ,
950+ mount_path : "/mnt/extra-volume" . to_string ( ) ,
951+ ..VolumeMount :: default ( )
952+ } ] ;
953+
954+ let git_sync_resources = GitSyncResources :: new (
955+ & git_syncs,
956+ & resolved_product_image,
957+ & extra_env_vars,
958+ & extra_volume_mounts,
959+ "log-volume" ,
960+ & validate ( default_container_log_config ( ) ) . unwrap ( ) ,
961+ )
962+ . unwrap ( ) ;
963+
964+ assert ! ( git_sync_resources. is_git_sync_enabled( ) ) ;
965+
966+ assert_eq ! ( 1 , git_sync_resources. git_sync_containers. len( ) ) ;
967+
968+ assert_eq ! (
969+ r#"args:
970+ - |-
971+ 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)
972+
973+ prepare_signal_handlers()
974+ {
975+ unset term_child_pid
976+ unset term_kill_needed
977+ trap 'handle_term_signal' TERM
978+ }
979+
980+ handle_term_signal()
981+ {
982+ if [ "${term_child_pid}" ]; then
983+ kill -TERM "${term_child_pid}" 2>/dev/null
984+ else
985+ term_kill_needed="yes"
986+ fi
987+ }
988+
989+ wait_for_termination()
990+ {
991+ set +e
992+ term_child_pid=$1
993+ if [[ -v term_kill_needed ]]; then
994+ kill -TERM "${term_child_pid}" 2>/dev/null
995+ fi
996+ wait ${term_child_pid} 2>/dev/null
997+ trap - TERM
998+ wait ${term_child_pid} 2>/dev/null
999+ set -e
1000+ }
1001+
1002+ prepare_signal_handlers
1003+ /stackable/git-sync --depth=3 --git-config='safe.directory:/tmp/git,http.sslCAInfo:/tmp/ca-cert/ca.crt' --link=current --one-time=false --period=60s --ref=trunk --repo=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git &
1004+ wait_for_termination $!
1005+ command:
1006+ - /bin/bash
1007+ - -x
1008+ - -euo
1009+ - pipefail
1010+ - -c
1011+ env:
1012+ - name: GITSYNC_SSH_KEY_FILE
1013+ value: /stackable/gitssh-0/key
1014+ - name: GITSYNC_SSH_KNOWN_HOSTS_FILE
1015+ value: /stackable/gitssh-0/knownHosts
1016+ - name: VAR1
1017+ value: value1
1018+ image: oci.stackable.tech/sdp/product:latest
1019+ imagePullPolicy: Always
1020+ name: git-sync-0
1021+ resources:
1022+ limits:
1023+ cpu: 200m
1024+ memory: 64Mi
1025+ requests:
1026+ cpu: 100m
1027+ memory: 64Mi
1028+ volumeMounts:
1029+ - mountPath: /tmp/git
1030+ name: content-from-git-0
1031+ - mountPath: /stackable/log
1032+ name: log-volume
1033+ - mountPath: /mnt/extra-volume
1034+ name: extra-volume
1035+ - mountPath: /stackable/gitssh-0
1036+ name: ssh-keys-info-0
1037+ "# ,
1038+ serde_yaml:: to_string( & git_sync_resources. git_sync_containers. get( 0 ) ) . unwrap( )
1039+ ) ;
1040+
1041+ assert_eq ! ( 1 , git_sync_resources. git_sync_init_containers. len( ) ) ;
1042+
1043+ assert_eq ! (
1044+ r#"args:
1045+ - |-
1046+ 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)
1047+ /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=ssh://git@github.com/stackabletech/repo.git --rev=HEAD --root=/tmp/git
1048+ command:
1049+ - /bin/bash
1050+ - -x
1051+ - -euo
1052+ - pipefail
1053+ - -c
1054+ env:
1055+ - name: GITSYNC_SSH_KEY_FILE
1056+ value: /stackable/gitssh-0/key
1057+ - name: GITSYNC_SSH_KNOWN_HOSTS_FILE
1058+ value: /stackable/gitssh-0/knownHosts
1059+ - name: VAR1
1060+ value: value1
1061+ image: oci.stackable.tech/sdp/product:latest
1062+ imagePullPolicy: Always
1063+ name: git-sync-0-init
1064+ resources:
1065+ limits:
1066+ cpu: 200m
1067+ memory: 64Mi
1068+ requests:
1069+ cpu: 100m
1070+ memory: 64Mi
1071+ volumeMounts:
1072+ - mountPath: /tmp/git
1073+ name: content-from-git-0
1074+ - mountPath: /stackable/log
1075+ name: log-volume
1076+ - mountPath: /mnt/extra-volume
1077+ name: extra-volume
1078+ - mountPath: /stackable/gitssh-0
1079+ name: ssh-keys-info-0
1080+ "# ,
1081+ serde_yaml:: to_string( & git_sync_resources. git_sync_init_containers. first( ) ) . unwrap( )
1082+ ) ;
1083+
1084+ assert_eq ! ( 1 , git_sync_resources. git_content_volumes. len( ) ) ;
1085+
1086+ assert_eq ! (
1087+ "emptyDir: {}
1088+ name: content-from-git-0
1089+ " ,
1090+ serde_yaml:: to_string( & git_sync_resources. git_content_volumes. first( ) ) . unwrap( )
1091+ ) ;
1092+
1093+ assert_eq ! ( 1 , git_sync_resources. git_content_volume_mounts. len( ) ) ;
1094+
1095+ assert_eq ! (
1096+ "mountPath: /stackable/app/git-0
1097+ name: content-from-git-0
1098+ " ,
1099+ serde_yaml:: to_string( & git_sync_resources. git_content_volume_mounts. first( ) ) . unwrap( )
1100+ ) ;
1101+
1102+ assert_eq ! ( 1 , git_sync_resources. git_content_folders. len( ) ) ;
1103+
1104+ assert_eq ! (
1105+ "/stackable/app/git-0/current/" ,
1106+ git_sync_resources
1107+ . git_content_folders_as_string( )
1108+ . first( )
1109+ . unwrap( )
1110+ ) ;
1111+
1112+ assert_eq ! ( 1 , git_sync_resources. git_ssh_volumes. len( ) ) ;
1113+
1114+ assert_eq ! (
1115+ "name: ssh-keys-info-0
1116+ secret:
1117+ optional: false
1118+ secretName: git-sync-ssh
1119+ " ,
1120+ serde_yaml:: to_string( & git_sync_resources. git_ssh_volumes. first( ) ) . unwrap( )
1121+ ) ;
1122+ }
9291123}
0 commit comments