Skip to content

Commit 97b0a32

Browse files
committed
scheduler should set private key for gerrit jobs
1 parent 2b4b27e commit 97b0a32

4 files changed

Lines changed: 45 additions & 31 deletions

File tree

deploy/infrabox/templates/_helpers.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ https://{{- required "host is required" .Values.host -}}:{{- .Values.port -}}
9595
{{ end }}
9696

9797
{{ define "mounts_gerrit" }}
98+
{{ if .Values.gerrit.enabled }}
9899
-
99100
name: gerrit-ssh
100101
mountPath: /tmp/gerrit
101102
readOnly: true
102103
{{ end }}
104+
{{ end }}
103105

104106
{{ define "volumes_gerrit" }}
105107
{{ if .Values.gerrit.enabled }}

deploy/infrabox/templates/function_crd.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ spec:
1515
memory: 1Gi
1616
env:
1717
{{ include "env_general" . | indent 4 }}
18-
{{ include "env_gerrit" . | indent 4 }}
1918
-
2019
name: INFRABOX_JOB_STORAGE_DRIVER
2120
value: {{ .Values.job.storage_driver }}

deploy/infrabox/templates/scheduler/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ spec:
3535
value: "443"
3636
volumeMounts:
3737
{{ include "mounts_rsa_private" . | indent 16 }}
38+
{{ include "mounts_gerrit" . | indent 16 }}
3839
volumes:
3940
{{ include "volumes_database" . | indent 16 }}
4041
{{ include "volumes_rsa" . | indent 16 }}
42+
{{ include "volumes_gerrit" . | indent 16 }}

src/scheduler/kubernetes/scheduler.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def kube_delete_job(self, job_id):
542542
except:
543543
pass
544544

545-
def kube_job(self, job_id, cpu, mem, private_key, services=None):
545+
def kube_job(self, job_id, cpu, mem, services=None):
546546
h = {'Authorization': 'Bearer %s' % self.args.token}
547547

548548
job_token = encode_job_token(job_id).decode()
@@ -561,7 +561,33 @@ def kube_job(self, job_id, cpu, mem, private_key, services=None):
561561
'value': str(cpu)
562562
}]
563563

564-
if private_key:
564+
# Get ssh key for private repos
565+
cursor = self.conn.cursor()
566+
cursor.execute('''
567+
SELECT p.type, p.id
568+
FROM project p
569+
JOIN job j
570+
ON j.project_id = p.id
571+
WHERE j.id = %s
572+
''', [job_id])
573+
result = cursor.fetchone()
574+
cursor.close()
575+
576+
project_type = result[0]
577+
project_id = result[1]
578+
579+
private_key = None
580+
if project_type == 'github':
581+
cursor = self.conn.cursor()
582+
cursor.execute('''
583+
SELECT r.private_key
584+
FROM repository r
585+
WHERE r.project_id = %s
586+
''', [project_id])
587+
result = cursor.fetchone()
588+
cursor.close()
589+
private_key = result[0]
590+
565591
env += [{
566592
'name': 'INFRABOX_GIT_PORT',
567593
'value': '443'
@@ -572,6 +598,18 @@ def kube_job(self, job_id, cpu, mem, private_key, services=None):
572598
'name': 'INFRABOX_GIT_PRIVATE_KEY',
573599
'value': private_key
574600
}]
601+
elif project_type == 'gerrit':
602+
with open(os.environ['INFRABOX_GERRIT_KEY_FILENAME']) as key:
603+
env += [{
604+
'name': 'INFRABOX_GIT_PORT',
605+
'value': os.environ['INFRABOX_GERRIT_PORT']
606+
}, {
607+
'name': 'INFRABOX_GIT_HOSTNAME',
608+
'value': os.environ['INFRABOX_GERRIT_HOSTNAME']
609+
}, {
610+
'name': 'INFRABOX_GIT_PRIVATE_KEY',
611+
'value': key.read()
612+
}]
575613

576614
root_url = os.environ['INFRABOX_ROOT_URL']
577615

@@ -634,34 +672,7 @@ def schedule_job(self, job_id, cpu, memory):
634672
if definition and 'services' in definition:
635673
services = definition['services']
636674

637-
# Get ssh key for private repos
638-
cursor = self.conn.cursor()
639-
cursor.execute('''
640-
SELECT p.type, p.id
641-
FROM project p
642-
JOIN job j
643-
ON j.project_id = p.id
644-
WHERE j.id = %s
645-
''', [job_id])
646-
result = cursor.fetchone()
647-
cursor.close()
648-
649-
project_type = result[0]
650-
project_id = result[1]
651-
652-
private_key = None
653-
if project_type == 'github':
654-
cursor = self.conn.cursor()
655-
cursor.execute('''
656-
SELECT r.private_key
657-
FROM repository r
658-
WHERE r.project_id = %s
659-
''', [project_id])
660-
result = cursor.fetchone()
661-
cursor.close()
662-
private_key = result[0]
663-
664-
if not self.kube_job(job_id, cpu, memory, private_key, services=services):
675+
if not self.kube_job(job_id, cpu, memory, services=services):
665676
return
666677

667678
cursor = self.conn.cursor()

0 commit comments

Comments
 (0)