Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable specifying the storage class and access modes of the created PVCs #255

5 changes: 5 additions & 0 deletions backend/kale/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def main():
metadata_group.add_argument('--kfp_host', type=str,
help='KFP endpoint. Provide address as '
'<host>:<port>.')
metadata_group.add_argument('--storage-class-name', type=str,
help='The storage class name for the created'
' volumes')
metadata_group.add_argument('--volume-access-mode', type=str,
help='The access mode for the created volumes')

args = parser.parse_args()

Expand Down
5 changes: 5 additions & 0 deletions backend/kale/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def set_value(self, value):

def validate(self):
"""Run the validators over the field's value."""
# If the Field's value is `None` don't run the validation process.
# Validators are *not* supposed to check if the value is set or not,
# we should control that by setting the `required` flag to True.
if self._value is None:
return
for v in self.validators:
validator = v() if inspect.isclass(v) else v
# XXX: The validator is supposed to raise an Exception if
Expand Down
13 changes: 13 additions & 0 deletions backend/kale/config/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ class VolumeTypeValidator(EnumValidator):
"""Validates the type of a Volume."""

enum = ('pv', 'pvc', 'new_pvc', 'clone')


class VolumeAccessModeValidator(EnumValidator):
"""Validates the access mode of a Volume."""

enum = ("", "rom", "rwo", "rwm")


class IsLowerValidator(Validator):
"""Validates if a string is all lowercase."""

def _validate(self, value: str):
return value == value.lower()
46 changes: 45 additions & 1 deletion backend/kale/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
log = logging.getLogger(__name__)


VOLUME_ACCESS_MODE_MAP = {"rom": ["ReadOnlyMany"], "rwo": ["ReadWriteOnce"],
"rwm": ["ReadWriteMany"]}
DEFAULT_VOLUME_ACCESS_MODE = VOLUME_ACCESS_MODE_MAP["rwm"]


class PipelineParam(NamedTuple):
"""A pipeline parameter."""

param_type: str
param_value: any

Expand All @@ -46,8 +52,13 @@ class VolumeConfig(Config):
type = Field(type=str, required=True,
validators=[validators.VolumeTypeValidator])
annotations = Field(type=list, default=list())
storage_class_name = Field(type=str,
validators=[validators.K8sNameValidator])
volume_access_mode = Field(
type=str, validators=[validators.IsLowerValidator,
validators.VolumeAccessModeValidator])

def _postprocess(self):
def _parse_annotations(self):
# Convert annotations to a {k: v} dictionary
try:
# TODO: Make JupyterLab annotate with {k: v} instead of
Expand All @@ -62,6 +73,15 @@ def _postprocess(self):
else:
raise e

def _parse_access_mode(self):
if self.volume_access_mode:
self.volume_access_mode = (
VOLUME_ACCESS_MODE_MAP[self.volume_access_mode])

def _postprocess(self):
self._parse_annotations()
self._parse_access_mode()


class KatibConfig(Config):
"""Used to validate the `katib_metadata` field of NotebookConfig."""
Expand Down Expand Up @@ -94,6 +114,11 @@ class PipelineConfig(Config):
autosnapshot = Field(type=bool, default=True)
steps_defaults = Field(type=dict, default=dict())
kfp_host = Field(type=str)
storage_class_name = Field(type=str,
validators=[validators.K8sNameValidator])
volume_access_mode = Field(
type=str, validators=[validators.IsLowerValidator,
validators.VolumeAccessModeValidator])

@property
def source_path(self):
Expand All @@ -103,6 +128,8 @@ def source_path(self):
def _postprocess(self):
self._randomize_pipeline_name()
self._set_docker_image()
self._set_volume_storage_class()
self._set_volume_access_mode()
self._sort_volumes()
self._set_abs_working_dir()
self._set_marshal_path()
Expand All @@ -119,6 +146,23 @@ def _set_docker_image(self):
# no K8s config found; use kfp default image
self.docker_image = ""

def _set_volume_storage_class(self):
if not self.storage_class_name:
return
for v in self.volumes:
if not v.storage_class_name:
v.storage_class_name = self.storage_class_name

def _set_volume_access_mode(self):
if not self.volume_access_mode:
self.volume_access_mode = DEFAULT_VOLUME_ACCESS_MODE
else:
self.volume_access_mode = VOLUME_ACCESS_MODE_MAP[
self.volume_access_mode]
for v in self.volumes:
if not v.volume_access_mode:
v.volume_access_mode = self.volume_access_mode

def _sort_volumes(self):
# The Jupyter Web App assumes the first volume of the notebook is the
# working directory, so we make sure to make it appear first in the
Expand Down
17 changes: 14 additions & 3 deletions backend/kale/templates/pipeline_template.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def auto_generated_pipeline({%- for arg in pipeline.pps_names -%}
_kale_volume_name_parameters = []

{% for vol in volumes -%}
{% set name= vol['name'] %}
{% set name = vol['name'] %}
{% set mountpoint = vol['mount_point'] %}
{% set pvc_size = vol['size']|string|default ('') + vol['size_type']|default ('') %}
{% set annotations = vol['annotations']|default({}) %}
{% set storage_class_name = vol['storage_class_name'] %}
_kale_annotations = {{ annotations }}

{% if vol['type'] == 'pv' %}
Expand All @@ -51,7 +52,10 @@ def auto_generated_pipeline({%- for arg in pipeline.pps_names -%}
),
spec=k8s_client.V1PersistentVolumeClaimSpec(
volume_name="{{ name }}",
access_modes=['ReadWriteOnce'],
access_modes={{ vol['volume_access_mode'] }},
{%- if storage_class_name %}
storage_class_name="{{ storage_class_name }}",
{%- endif %}
resources=k8s_client.V1ResourceRequirements(
requests={"storage": "{{ pvc_size }}"}
)
Expand Down Expand Up @@ -82,6 +86,10 @@ def auto_generated_pipeline({%- for arg in pipeline.pps_names -%}
{%- if annotations %}
annotations=_kale_annotations,
{% endif -%}
modes={{ vol['volume_access_mode'] }},
{%- if storage_class_name %}
storage_class="{{ storage_class_name }}",
{%- endif %}
size='{{ pvc_size }}'
)
_kale_volume = _kale_vop{{ loop.index }}.volume
Expand All @@ -98,7 +106,10 @@ def auto_generated_pipeline({%- for arg in pipeline.pps_names -%}
_kale_marshal_vop = _kfp_dsl.VolumeOp(
name="kale-marshal-volume",
resource_name="kale-marshal-pvc",
modes=_kfp_dsl.VOLUME_MODE_RWM,
modes={{ pipeline.config.volume_access_mode }},
{%- if pipeline.config.storage_class_name %}
storage_class="{{ pipeline.config.storage_class_name }}",
{%- endif %}
size="1Gi"
)
_kale_volume_step_names.append(_kale_marshal_vop.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def auto_generated_pipeline(booltest='True', d1='5', d2='6', strtest='test'):
_kale_marshal_vop = _kfp_dsl.VolumeOp(
name="kale-marshal-volume",
resource_name="kale-marshal-pvc",
modes=_kfp_dsl.VOLUME_MODE_RWM,
modes=['ReadWriteMany'],
size="1Gi"
)
_kale_volume_step_names.append(_kale_marshal_vop.name)
Expand Down
2 changes: 1 addition & 1 deletion backend/kale/tests/assets/kfp_dsl/titanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def auto_generated_pipeline():
_kale_marshal_vop = _kfp_dsl.VolumeOp(
name="kale-marshal-volume",
resource_name="kale-marshal-pvc",
modes=_kfp_dsl.VOLUME_MODE_RWM,
modes=['ReadWriteMany'],
size="1Gi"
)
_kale_volume_step_names.append(_kale_marshal_vop.name)
Expand Down
1 change: 1 addition & 0 deletions labextension/src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const Input: React.FunctionComponent<InputProps> = props => {
error={error}
value={value}
margin="dense"
placeholder={placeholder}
spellCheck={false}
helperText={error ? getRegexMessage() : helperText}
InputProps={{
Expand Down
54 changes: 54 additions & 0 deletions labextension/src/components/VolumeAccessModeSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 The Kale Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as React from 'react';
import { Select, ISelectOption } from './Select';

const VOLUME_ACCESS_MODE_ROM: ISelectOption = {
label: 'ReadOnlyMany',
value: 'rom',
};
const VOLUME_ACCESS_MODE_RWO: ISelectOption = {
label: 'ReadWriteOnce',
value: 'rwo',
};
const VOLUME_ACCESS_MODE_RWM: ISelectOption = {
label: 'ReadWriteMany',
value: 'rwm',
};
const VOLUME_ACCESS_MODES: ISelectOption[] = [
VOLUME_ACCESS_MODE_ROM,
VOLUME_ACCESS_MODE_RWO,
VOLUME_ACCESS_MODE_RWM,
];

interface VolumeAccessModeSelectProps {
value: string;
updateValue: Function;
}

export const VolumeAccessModeSelect: React.FunctionComponent<VolumeAccessModeSelectProps> = props => {
return (
<Select
variant="standard"
label="Volume access mode"
index={-1}
values={VOLUME_ACCESS_MODES}
value={props.value}
updateValue={props.updateValue}
/>
);
};
22 changes: 21 additions & 1 deletion labextension/src/widgets/LeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ export interface IKaleNotebookMetadata {
katib_run: boolean;
katib_metadata?: IKatibMetadata;
steps_defaults?: string[];
storage_class_name?: string;
volume_access_mode?: string;
}

export interface IKatibExperiment {
Expand Down Expand Up @@ -191,6 +193,7 @@ export const DefaultState: IState = {
autosnapshot: false,
katib_run: false,
steps_defaults: [],
volume_access_mode: 'rwm',
},
runDeployment: false,
deploymentType: 'compile',
Expand Down Expand Up @@ -258,6 +261,8 @@ export class KubeflowKaleLeftPanel extends React.Component<IProps, IState> {
...prevState.metadata,
volumes: prevState.notebookVolumes,
snapshot_volumes: !prevState.metadata.snapshot_volumes,
storage_class_name: undefined,
volume_access_mode: undefined,
},
}));
};
Expand Down Expand Up @@ -292,11 +297,22 @@ export class KubeflowKaleLeftPanel extends React.Component<IProps, IState> {
deployDebugMessage: !prevState.deployDebugMessage,
}));

updateStorageClassName = (storage_class_name: string) =>
this.setState((prevState, props) => ({
metadata: { ...prevState.metadata, storage_class_name },
}));

updateVolumeAccessMode = (volume_access_mode: string) => {
this.setState((prevState, props) => ({
metadata: { ...prevState.metadata, volume_access_mode },
}));
};

updateKatibRun = () =>
this.setState((prevState, props) => ({
metadata: {
...prevState.metadata,
katib_run: !this.state.metadata.katib_run,
katib_run: !prevState.metadata.katib_run,
},
}));

Expand Down Expand Up @@ -802,6 +818,10 @@ export class KubeflowKaleLeftPanel extends React.Component<IProps, IState> {
updateAutosnapshotSwitch={this.updateAutosnapshotSwitch}
rokError={this.props.rokError}
updateVolumes={this.updateVolumes}
storageClassName={this.state.metadata.storage_class_name}
updateStorageClassName={this.updateStorageClassName}
volumeAccessMode={this.state.metadata.volume_access_mode}
updateVolumeAccessMode={this.updateVolumeAccessMode}
/>
);

Expand Down
25 changes: 25 additions & 0 deletions labextension/src/widgets/VolumesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Select, ISelectOption } from '../components/Select';
import { LightTooltip } from '../components/LightTooltip';
import { AnnotationInput, IAnnotation } from '../components/AnnotationInput';
import { removeIdxFromArray, updateIdxInArray } from '../lib/Utils';
import { VolumeAccessModeSelect } from '../components/VolumeAccessModeSelect';

const DEFAULT_EMPTY_VOLUME: IVolumeMetadata = {
type: 'new_pvc',
Expand Down Expand Up @@ -95,6 +96,10 @@ interface VolumesPanelProps {
updateVolumes: Function;
updateVolumesSwitch: Function;
updateAutosnapshotSwitch: Function;
storageClassName: string;
updateStorageClassName: Function;
volumeAccessMode: string;
updateVolumeAccessMode: Function;
}

export const VolumesPanel: React.FunctionComponent<VolumesPanelProps> = props => {
Expand Down Expand Up @@ -571,10 +576,30 @@ export const VolumesPanel: React.FunctionComponent<VolumesPanelProps> = props =>
</div>
);

// FIXME: There is no separating bottom horizontal bar when there are no
// volumes
const volumesClassNameAndMode = (
<div className="input-container volume-container">
<Input
label="Storage class name"
updateValue={props.updateStorageClassName}
value={props.storageClassName}
placeholder={'default'}
variant="standard"
/>

<VolumeAccessModeSelect
value={props.volumeAccessMode}
updateValue={props.updateVolumeAccessMode}
/>
</div>
);

return (
<React.Fragment>
{useNotebookVolumesSwitch}
{autoSnapshotSwitch}
{!props.useNotebookVolumes && volumesClassNameAndMode}
{props.notebookMountPoints.length > 0 && props.useNotebookVolumes
? null
: vols}
Expand Down