Skip to content

Commit

Permalink
Allow selection of storage class provisioner in mpdev (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcanaa authored Mar 11, 2021
1 parent 29b528f commit b755f59
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 24 deletions.
61 changes: 38 additions & 23 deletions marketplace/deployer_util/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
The manifests include the deployer-related resources.
"""

_DEFAULT_STORAGE_CLASS_PROVISIONER = 'kubernetes.io/gce-pd'


def main():
parser = ArgumentParser(description=_PROG_HELP)
Expand All @@ -40,6 +42,7 @@ def main():
parser.add_argument('--deployer_service_account_name', required=True)
parser.add_argument('--version_repo', default=None)
parser.add_argument('--image_pull_secret', default=None)
parser.add_argument('--storage_class_provisioner', default=None)
args = parser.parse_args()

schema = schema_values_common.load_schema(args)
Expand All @@ -51,12 +54,14 @@ def main():
deployer_entrypoint=args.deployer_entrypoint,
version_repo=args.version_repo,
image_pull_secret=args.image_pull_secret,
deployer_service_account_name=args.deployer_service_account_name)
deployer_service_account_name=args.deployer_service_account_name,
storage_class_provisioner=args.storage_class_provisioner)
print(yaml.safe_dump_all(manifests, default_flow_style=False, indent=2))


def process(schema, values, deployer_image, deployer_entrypoint, version_repo,
image_pull_secret, deployer_service_account_name):
image_pull_secret, deployer_service_account_name,
storage_class_provisioner):
props = {}
manifests = []
app_name = get_name(schema, values)
Expand Down Expand Up @@ -93,7 +98,11 @@ def process(schema, values, deployer_image, deployer_entrypoint, version_repo,
manifests += sa_manifests
elif prop.storage_class:
value, sc_manifests = provision_storage_class(
schema, prop, app_name=app_name, namespace=namespace)
schema,
prop,
app_name=app_name,
namespace=namespace,
provisioner=storage_class_provisioner)
props[prop.name] = value
manifests += sc_manifests
elif prop.xtype == config_helper.XTYPE_ISTIO_ENABLED:
Expand Down Expand Up @@ -658,26 +667,32 @@ def provision_service_account(schema, prop, app_name, namespace,
return sa_name, add_preprovisioned_labels(manifests, prop.name)


def provision_storage_class(schema, prop, app_name, namespace):
if prop.storage_class.ssd:
sc_name = dns1123_name('{}-{}-{}'.format(namespace, app_name, prop.name))
manifests = [{
'apiVersion': 'storage.k8s.io/v1',
'kind': 'StorageClass',
'metadata': {
'name': sc_name,
},
# Some intelligence might go here to determine what
# provisioner and configuration to use here and below.
'provisioner': 'kubernetes.io/gce-pd',
'parameters': {
'type': 'pd-ssd',
}
}]
return sc_name, add_preprovisioned_labels(manifests, prop.name)
else:
raise Exception('Do not know how to provision for property {}'.format(
prop.name))
def provision_storage_class(schema, prop, app_name, namespace, provisioner):
if not provisioner:
provisioner = _DEFAULT_STORAGE_CLASS_PROVISIONER

if provisioner == 'kubernetes.io/vsphere-volume':
parameters = {'diskformat': 'thin'}
elif provisioner == 'kubernetes.io/gce-pd':
if prop.storage_class.ssd:
parameters = {
'type': 'pd-ssd',
}
else:
raise Exception('Do not know how to provision for property {}'.format(
prop.name))

sc_name = dns1123_name('{}-{}-{}'.format(namespace, app_name, prop.name))
manifests = [{
'apiVersion': 'storage.k8s.io/v1',
'kind': 'StorageClass',
'metadata': {
'name': sc_name,
},
'provisioner': provisioner,
'parameters': parameters
}]
return sc_name, add_preprovisioned_labels(manifests, prop.name)


def get_name(schema, values):
Expand Down
68 changes: 67 additions & 1 deletion marketplace/deployer_util/provision_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,10 @@ def run_test_process(self, schema):
version_repo = "1.0.0"
image_pull_secret = "image_pull_secret"
deployer_service_account_name = "deployer_service_account_name"
storage_class_provisioner = None
provision.process(schema, values, deployer_image, deployer_entrypoint,
version_repo, image_pull_secret,
deployer_service_account_name)
deployer_service_account_name, storage_class_provisioner)

def test_process_schema_v1(self):
schema = self.generate_schema_v1()
Expand All @@ -517,3 +518,68 @@ def test_process_schema_v2(self):
def test_process_schema_v2_kalm(self):
schema = self.generate_schema_v2(is_kalm=True)
self.run_test_process(schema)

def test_provision_storage_class_vsphere(self):
schema = self.generate_schema_v1()
prop = config_helper.SchemaProperty(
"storageClass", {
"type": "string",
"x-google-marketplace": {
"type": "STORAGE_CLASS",
"storageClass": {
"type": "SSD"
},
}
}, True)
manifest = provision.provision_storage_class(
schema, prop, "app-1", "mynamespace", "kubernetes.io/vsphere-volume")
self.assertEqual(manifest, ('mynamespace-app-1-storageclass', [{
'apiVersion': 'storage.k8s.io/v1',
'kind': 'StorageClass',
'metadata': {
'name': 'mynamespace-app-1-storageclass',
'labels': {
'app.kubernetes.io/component':
'auto-provisioned.marketplace.cloud.google.com',
'marketplace.cloud.google.com/auto-provisioned-for-property':
'storageClass'
}
},
'provisioner': 'kubernetes.io/vsphere-volume',
'parameters': {
'diskformat': 'thin'
}
}]))

def test_provision_storage_class_gce_pd(self):
schema = self.generate_schema_v1()
prop = config_helper.SchemaProperty(
"storageClass", {
"type": "string",
"x-google-marketplace": {
"type": "STORAGE_CLASS",
"storageClass": {
"type": "SSD"
},
}
}, True)
manifest = provision.provision_storage_class(schema, prop, "app-1",
"mynamespace",
"kubernetes.io/gce-pd")
self.assertEqual(manifest, ('mynamespace-app-1-storageclass', [{
'apiVersion': 'storage.k8s.io/v1',
'kind': 'StorageClass',
'metadata': {
'name': 'mynamespace-app-1-storageclass',
'labels': {
'app.kubernetes.io/component':
'auto-provisioned.marketplace.cloud.google.com',
'marketplace.cloud.google.com/auto-provisioned-for-property':
'storageClass'
}
},
'provisioner': 'kubernetes.io/gce-pd',
'parameters': {
'type': 'pd-ssd'
}
}]))
5 changes: 5 additions & 0 deletions scripts/install
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ case $i in
image_pull_secret="${i#*=}"
shift
;;
--storage_class_provisioner=*)
storage_class_provisioner="${i#*=}"
shift
;;
*)
>&2 echo "Unrecognized flag: $i"
exit 1
Expand Down Expand Up @@ -140,6 +144,7 @@ echo "${parameters}" \
--deployer_service_account_name="${deployer_service_account_name}" \
--version_repo="${version_repo}" \
--image_pull_secret="${image_pull_secret}" \
--storage_class_provisioner="${storage_class_provisioner}" \
| set_app_labels.py \
--manifests=- \
--dest=- \
Expand Down
5 changes: 5 additions & 0 deletions scripts/verify
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ case $i in
image_pull_secret="${i#*=}"
shift
;;
--storage_class_provisioner=*)
storage_class_provisioner="${i#*=}"
shift
;;
*)
echo "Unrecognized flag: $i"
exit 1
Expand Down Expand Up @@ -265,6 +269,7 @@ echo "INFO Initializes the deployer container which will deploy all the applicat
--deployer="$deployer" \
--parameters="$parameters" \
--entrypoint='/bin/deploy_with_tests.sh' \
--storage_class_provisioner="$storage_class_provisioner" \
|| clean_and_exit "ERROR Failed to start deployer"

echo "INFO wait for the deployer to succeed"
Expand Down

0 comments on commit b755f59

Please sign in to comment.