Skip to content

Commit

Permalink
storage: set backend depending on env var
Browse files Browse the repository at this point in the history
* Detects which storage backend should be configured using the
  `STORAGE_BACKEND` env variable instead of hardwiring CEPHFS.
  (closes #37)

Signed-off-by: Diego Rodriguez <diego.rodriguez@cern.ch>
  • Loading branch information
Diego Rodriguez committed Apr 25, 2017
1 parent 106ea57 commit dfa2e19
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
19 changes: 12 additions & 7 deletions reana_job_controller/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"""Kubernetes wrapper."""

import logging
import os
import time

import pykube
from flask import current_app as app

from reana_job_controller import volume_templates


Expand All @@ -38,15 +38,20 @@ def create_api_client(config):
return api_client


def add_shared_volume(job, namespace):
def add_shared_volume(job):
"""Add shared CephFS volume to a given job spec.
:param job: Kubernetes job spec.
:param namespace: Job's namespace FIXME namespace is already
inside job spec.
"""
volume = volume_templates.get_k8s_cephfs_volume(namespace)
mount_path = volume_templates.CEPHFS_MOUNT_PATH
storage_backend = os.getenv('REANA_STORAGE_BACKEND', 'LOCAL')
if storage_backend == 'CEPHFS':
volume = volume_templates.get_k8s_cephfs_volume(
job['metadata']['namespace'])
else:
volume = volume_templates.get_k8s_hostpath_volume(
job['metadata']['namespace'])
mount_path = volume_templates.REANA_STORAGE_MOUNT_PATH

job['spec']['template']['spec']['containers'][0]['volumeMounts'].append(
{'name': volume['name'], 'mountPath': mount_path}
)
Expand Down Expand Up @@ -110,7 +115,7 @@ def instantiate_job(job_id, docker_img, cmd, cvmfs_repos, env_vars, namespace,
)

if shared_file_system:
add_shared_volume(job, namespace)
add_shared_volume(job)

if cvmfs_repos:
for num, repo in enumerate(cvmfs_repos):
Expand Down
51 changes: 35 additions & 16 deletions reana_job_controller/volume_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import json
from string import Template

CEPH_SECRET_NAME = 'ceph-secret'

CEPHFS_PATHS = {
'alice': '/k8s/alice',
'atlas': '/k8s/atlas',
'cms': '/k8s/cms',
'lhcb': '/k8s/lhcb',
'recast': '/k8s/recast'
CEPHFS_SECRET_NAME = 'ceph-secret'

REANA_STORAGE_PATHS = {
'alice': '/reana/alice',
'atlas': '/reana/atlas',
'cms': '/reana/cms',
'lhcb': '/reana/lhcb',
'recast': '/reana/recast'
}

CVMFS_REPOSITORIES = {
Expand All @@ -49,10 +49,10 @@
'geant4': 'geant4.cern.ch'
}

CEPHFS_MOUNT_PATH = '/data'
REANA_STORAGE_MOUNT_PATH = '/data'

k8s_cephfs_template = Template("""{
"name": "cephfs-$experiment",
K8S_CEPHFS_TEMPLATE = Template("""{
"name": "$experiment-shared-volume",
"cephfs": {
"monitors": [
"128.142.36.227:6790",
Expand All @@ -68,7 +68,7 @@
}
}""")

k8s_cvmfs_template = Template("""{
K8S_CVMFS_TEMPLATE = Template("""{
"name": "cvmfs-$experiment",
"flexVolume": {
"driver": "cern/cvmfs",
Expand All @@ -78,6 +78,13 @@
}
}""")

K8S_HOSTPATH_TEMPLATE = Template("""{
"name": "$experiment-shared-volume",
"hostPath": {
"path": "$path"
}
}""")


def get_cvmfs_mount_point(repository_name):
"""Generate mount point for a given CVMFS repository.
Expand All @@ -97,9 +104,9 @@ def get_k8s_cephfs_volume(experiment):
:returns: k8s CephFS volume spec as a dictionary.
"""
return json.loads(
k8s_cephfs_template.substitute(experiment=experiment,
path=CEPHFS_PATHS[experiment],
secret_name=CEPH_SECRET_NAME)
K8S_CEPHFS_TEMPLATE.substitute(experiment=experiment,
path=REANA_STORAGE_PATHS[experiment],
secret_name=CEPHFS_SECRET_NAME)
)


Expand All @@ -110,7 +117,19 @@ def get_k8s_cvmfs_volume(experiment, repository):
:returns: k8s CVMFS volume spec as a dictionary.
"""
if repository in CVMFS_REPOSITORIES:
return json.loads(k8s_cvmfs_template.substitute(experiment=experiment,
return json.loads(K8S_CVMFS_TEMPLATE.substitute(experiment=experiment,
repository=repository))
else:
raise ValueError('The provided repository doesn\'t exist')


def get_k8s_hostpath_volume(experiment):
"""Render k8s HostPath volume template.
:param experiment: Experiment name.
:returns: k8s HostPath spec as a dictionary.
"""
return json.loads(
K8S_HOSTPATH_TEMPLATE.substitute(experiment=experiment,
path=REANA_STORAGE_PATHS[experiment])
)

0 comments on commit dfa2e19

Please sign in to comment.