Added support for template variables

- These changes necessary to enable template re-use.  Enables the
  specification of additional variables to be passed to each template
  referenced in ./etc/kolla-kubernetes/service_resources.yml
- Template.vars are additional vars in the form of a dict passed to
  the jinja templating engine. Templates may access this dict with
  "kolla_kubernetes.template.vars.<subkey>".  This enables arguments
  to be passed to templates so that they may be re-used or configured.
  These template.vars may not contain any other nested jinja
  references, and are passed unmodified directly to the template.
- Used mariadb templates as an example for template.vars
- Changes to ./etc/kolla-kubernetes/service_resources.yml
    resources:
      disk:
      - name: mariadb-disk <-- Name is new
        template: services/mariadb/mariadb-disk.sh.j2  <-- New
        vars:             <-- Template variables are new
          size_in_gb: 10  <-- Template var example
- CLI Syntax Change: Resource templates are specified by name (defined
  in service_resources.yml) instead of path.  This is necessary
  because the same template may be referenced multiple times in
  service_resources.yml (with different template args), and a name is
  necessary to distinguish between the different
  template/template_args combinations.
  FROM: kolla-kubernetes resource-template \
          <action> <resource_type> <service> <TEMPLATE-FILE>
  TO:   kolla-kubernetes resource-template \
          <action> <service> <resource_type> <RESOURCE-NAME>
- CLI Syntax Change: Ordering changed to match resource-template
  FROM: kolla-kubernetes resource <action> <resource_type> <service>
  TO:   kolla-kubernetes resource <action> <service> <resource_type>

Change-Id: Ie1e0c973206a239bc3759827149b4e8d67029752
Partially-implements: blueprint kolla-kubernetes-cli
This commit is contained in:
David C Wang 2016-08-04 19:01:46 +00:00
parent cd25d0df0c
commit 4d34815721
7 changed files with 300 additions and 160 deletions

View File

@ -142,11 +142,12 @@ was prior mounted by the mariadb-bootstrap job, until the
mariadb-bootstrap job is deleted. The same should also occur for AWS
and Ceph.
#2. When running Kubernetes version < 1.3, Ceph RBD volumes will
auto-detach when Kubernetes nodes disappear, causing problems when a
pod migrates to a new node and cannot mount the required volume.
Details are found in the in this `kubernetes pull
request<https://github.com/kubernetes/kubernetes/pull/26351>`_.
#2. When running Kubernetes, Ceph RBD volumes will not auto-unlock
when Kubernetes nodes disappear, causing problems when a pod migrates
to a new node and cannot mount the required volume. This was supposed
to have been fixed in Kubernetes 1.3, but we have been unable to
verify it working. Details are found in the in this `kubernetes pull
request <https://github.com/kubernetes/kubernetes/pull/26351>`_.
Create all Kolla-Kubernetes Resources
@ -169,7 +170,7 @@ Operator Create Resources
kolla-kubernetes bootstrap mariadb
sleep 30 # wait for mariadb bootstrap to finish
kolla-kubernetes resource delete bootstrap mariadb # workaround known issue #1
kolla-kubernetes resource delete mariadb bootstrap # workaround known issue #1
kolla-kubernetes run mariadb
kolla-kubernetes run memcached
sleep 30 # wait for mariadb and memcached to start up
@ -183,33 +184,59 @@ Operator Create Resources
Workflow Engine Create Resources
--------------------------------
A future Ansible Workflow Engine would discretely call the individual
A future Ansible Workflow Engine would individually call the discrete
bits of logic.
::
kolla-kubernetes resource create disk mariadb
kolla-kubernetes resource create pv mariadb
kolla-kubernetes resource create pvc mariadb
kolla-kubernetes resource create svc mariadb
kolla-kubernetes resource create configmap mariadb
kolla-kubernetes resource create bootstrap mariadb
kolla-kubernetes resource create mariadb disk
kolla-kubernetes resource create mariadb pv
kolla-kubernetes resource create mariadb pvc
kolla-kubernetes resource create mariadb svc
kolla-kubernetes resource create mariadb configmap
kolla-kubernetes resource create mariadb bootstrap
sleep 30 # wait for mariadb bootstrap to finish
kolla-kubernetes resource delete bootstrap mariadb # workaround known issue #1
kolla-kubernetes resource create pod mariadb
kolla-kubernetes resource create svc memcached
kolla-kubernetes resource create configmap memcached
kolla-kubernetes resource create pod memcached
kolla-kubernetes resource create svc keystone
kolla-kubernetes resource create configmap keystone
kolla-kubernetes resource delete mariadb bootstrap # workaround known issue #1
kolla-kubernetes resource create mariadb pod
kolla-kubernetes resource create memcached svc
kolla-kubernetes resource create memcached configmap
kolla-kubernetes resource create memcached pod
kolla-kubernetes resource create keystone svc
kolla-kubernetes resource create keystone configmap
sleep 30 # wait for mariadb and memcached to start up
kolla-kubernetes resource create bootstrap keystone
kolla-kubernetes resource create keystone bootstrap
sleep 30 # wait for keystone to bootstrap in mariadb
kolla-kubernetes resource create pod keystone
kolla-kubernetes resource create svc horizon
kolla-kubernetes resource create configmap horizon
kolla-kubernetes resource create keystone pod
kolla-kubernetes resource create horizon svc
kolla-kubernetes resource create horizon configmap
sleep 30 # wait for keystone to start up
kolla-kubernetes resource create pod horizon
kolla-kubernetes resource create horizon pod
Check Status of all Kolla-Kubernetes Resources
==============================================
Checking status is the same whether for operators or workflow engine.
::
kolla-kubernetes resource status mariadb disk
kolla-kubernetes resource status mariadb pv
kolla-kubernetes resource status mariadb pvc
kolla-kubernetes resource status mariadb svc
kolla-kubernetes resource status mariadb configmap
kolla-kubernetes resource status mariadb bootstrap
kolla-kubernetes resource status mariadb pod
kolla-kubernetes resource status memcached svc
kolla-kubernetes resource status memcached configmap
kolla-kubernetes resource status memcached pod
kolla-kubernetes resource status keystone svc
kolla-kubernetes resource status keystone configmap
kolla-kubernetes resource status keystone bootstrap
kolla-kubernetes resource status keystone pod
kolla-kubernetes resource status horizon svc
kolla-kubernetes resource status horizon configmap
kolla-kubernetes resource status horizon pod
Delete all Kolla-Kubernetes Resources
@ -234,20 +261,20 @@ Workflow Engine Delete Resources
::
kolla-kubernetes resource delete pod horizon
kolla-kubernetes resource delete configmap horizon
kolla-kubernetes resource delete svc horizon
kolla-kubernetes resource delete pod keystone
kolla-kubernetes resource delete bootstrap keystone
kolla-kubernetes resource delete configmap keystone
kolla-kubernetes resource delete svc keystone
kolla-kubernetes resource delete pod memcached
kolla-kubernetes resource delete configmap memcached
kolla-kubernetes resource delete svc memcached
kolla-kubernetes resource delete pod mariadb
kolla-kubernetes resource delete bootstrap mariadb
kolla-kubernetes resource delete configmap mariadb
kolla-kubernetes resource delete svc mariadb
kolla-kubernetes resource delete pvc mariadb
kolla-kubernetes resource delete pv mariadb
kolla-kubernetes resource delete disk mariadb
kolla-kubernetes resource delete horizon pod
kolla-kubernetes resource delete horizon configmap
kolla-kubernetes resource delete horizon svc
kolla-kubernetes resource delete keystone pod
kolla-kubernetes resource delete keystone bootstrap
kolla-kubernetes resource delete keystone configmap
kolla-kubernetes resource delete keystone svc
kolla-kubernetes resource delete memcached pod
kolla-kubernetes resource delete memcached configmap
kolla-kubernetes resource delete memcached svc
kolla-kubernetes resource delete mariadb pod
kolla-kubernetes resource delete mariadb bootstrap
kolla-kubernetes resource delete mariadb configmap
kolla-kubernetes resource delete mariadb svc
kolla-kubernetes resource delete mariadb pvc
kolla-kubernetes resource delete mariadb pv
kolla-kubernetes resource delete mariadb disk

View File

@ -12,6 +12,14 @@
# persistent state.
# The (pod) resources may be destroyed willy-nilly, since they hold no
# state.
# Template.vars are additional vars in the form of a dict passed to
# the jinja templating engine. Templates may access this dict with
# "kolla_kubernetes.template.vars.<subkey>". This enables arguments
# to be passed to templates so that they may be re-used or
# configured. These template.vars may not contain any other nested
# jinja references, and are passed unmodified directly to the
# template.
kolla-kubernetes:
services:
@ -22,17 +30,27 @@ kolla-kubernetes:
- name: mariadb
resources:
disk:
- services/mariadb/mariadb-disk.sh.j2
- name: mariadb-disk
template: services/mariadb/mariadb-disk.sh.j2
vars:
size_in_gb: 10
pv:
- services/mariadb/mariadb-pv.yml.j2
- name: mariadb-pv
template: services/mariadb/mariadb-pv.yml.j2
vars:
size_in_gb: 10
pvc:
- services/mariadb/mariadb-pvc.yml.j2
- name: mariadb-pvc
template: services/mariadb/mariadb-pvc.yml.j2
svc:
- services/mariadb/mariadb-service.yml.j2
- name: mariadb-service
template: services/mariadb/mariadb-service.yml.j2
bootstrap:
- services/mariadb/mariadb-bootstrap-job.yml.j2
- name: mariadb-bootstrap-job
template: services/mariadb/mariadb-bootstrap-job.yml.j2
pod:
- services/mariadb/mariadb-pod.yml.j2
- name: mariadb-pod
template: services/mariadb/mariadb-pod.yml.j2
- name: memcached
pods:
- name: memcached
@ -43,10 +61,12 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/memcached/memcached-service.yml.j2
- name: memcached-service
template: services/memcached/memcached-service.yml.j2
bootstrap:
pod:
- services/memcached/memcached-pod.yml.j2
- name: memcached-pod
template: services/memcached/memcached-pod.yml.j2
- name: keystone
pods:
- name: keystone
@ -57,12 +77,16 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/keystone/keystone-service-admin.yml.j2
- services/keystone/keystone-service-public.yml.j2
- name: keystone-service-admin
template: services/keystone/keystone-service-admin.yml.j2
- name: keystone-service-public
template: services/keystone/keystone-service-public.yml.j2
bootstrap:
- services/keystone/keystone-bootstrap-job.yml.j2
- name: keystone-bootstrap-job
template: services/keystone/keystone-bootstrap-job.yml.j2
pod:
- services/keystone/keystone-pod.yml.j2
- name: keystone-pod
template: services/keystone/keystone-pod.yml.j2
- name: horizon
pods:
- name: horizon
@ -73,10 +97,12 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/horizon/horizon-service.yml.j2
- name: horizon-service
template: services/horizon/horizon-service.yml.j2
bootstrap:
pod:
- services/horizon/horizon-pod.yml.j2
- name: horizon-pod
template: services/horizon/horizon-pod.yml.j2
- name: rabbitmq
pods:
- name: rabbitmq
@ -87,12 +113,16 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/rabbitmq/rabbitmq-service-management.yml.j2
- services/rabbitmq/rabbitmq-service.yml.j2
- name: rabbitmq-service-management
template: services/rabbitmq/rabbitmq-service-management.yml.j2
- name: rabbitmq-service
template: services/rabbitmq/rabbitmq-service.yml.j2
bootstrap:
- services/rabbitmq/rabbitmq-bootstrap-job.yml.j2
- name: rabbitmq-bootstrap-job
template: services/rabbitmq/rabbitmq-bootstrap-job.yml.j2
pod:
- services/rabbitmq/rabbitmq-pod.yml.j2
- name: rabbitmq-pod
template: services/rabbitmq/rabbitmq-pod.yml.j2
- name: glance
pods:
- name: glance
@ -104,13 +134,18 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/glance/glance-api-service.yml.j2
- services/glance/glance-registry-service.yml.j2
- name: glance-api-service
template: services/glance/glance-api-service.yml.j2
- name: glance-registry-service
template: services/glance/glance-registry-service.yml.j2
bootstrap:
- services/glance/glance-bootstrap-job.yml.j2
- name: glance-bootstrap-job
template: services/glance/glance-bootstrap-job.yml.j2
pod:
- services/glance/glance-api-pod.yml.j2
- services/glance/glance-registry-pod.yml.j2
- name: glance-api-pod
template: services/glance/glance-api-pod.yml.j2
- name: glance-registry-pod
template: services/glance/glance-registry-pod.yml.j2
- name: nova
pods:
@ -128,13 +163,18 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/nova/nova-service-compute.yml.j2
- name: nova-service-compute
template: services/nova/nova-service-compute.yml.j2
bootstrap:
- services/nova/nova-compute-bootstrap-job.yml.j2
- services/nova/nova-control-bootstrap-job.yml.j2
- name: nova-compute-bootstrap-job
template: services/nova/nova-compute-bootstrap-job.yml.j2
- name: nova-control-bootstrap-job
template: services/nova/nova-control-bootstrap-job.yml.j2
pod:
- services/nova/nova-compute-pod.yml.j2
- services/nova/nova-control-pod.yml.j2
- name: nova-compute-pod
template: services/nova/nova-compute-pod.yml.j2
- name: nova-control-pod
template: services/nova/nova-control-pod.yml.j2
- name: neutron
pods:
- name: neutron-compute
@ -156,15 +196,22 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/neutron/neutron-server-service.yml.j2
- name: neutron-server-service
template: services/neutron/neutron-server-service.yml.j2
bootstrap:
- services/neutron/neutron-bootstrap-job.yml.j2
- name: neutron-bootstrap-job
template: services/neutron/neutron-bootstrap-job.yml.j2
pod:
- services/neutron/neutron-compute-pod.yml.j2
- services/neutron/neutron-control-pod.yml.j2
- services/neutron/neutron-dhcp-agent-pod.yml.j2
- services/neutron/neutron-l3-agent-pod.yml.j2
- services/neutron/neutron-metadata-agent-pod.yml.j2
- name: neutron-compute-pod
template: services/neutron/neutron-compute-pod.yml.j2
- name: neutron-control-pod
template: services/neutron/neutron-control-pod.yml.j2
- name: neutron-dhcp-agent-pod
template: services/neutron/neutron-dhcp-agent-pod.yml.j2
- name: neutron-l3-agent-pod
template: services/neutron/neutron-l3-agent-pod.yml.j2
- name: neutron-metadata-agent-pod
template: services/neutron/neutron-metadata-agent-pod.yml.j2
- name: swift
pods:
- name: swift-account
@ -197,17 +244,26 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/swift/swift-account-service.yml.j2
- services/swift/swift-container-service.yml.j2
- services/swift/swift-object-service.yml.j2
- services/swift/swift-proxy-service.yml.j2
- services/swift/swift-rsync-service.yml.j2
- name: swift-account-service
template: services/swift/swift-account-service.yml.j2
- name: swift-container-service
template: services/swift/swift-container-service.yml.j2
- name: swift-object-service
template: services/swift/swift-object-service.yml.j2
- name: swift-proxy-service
template: services/swift/swift-proxy-service.yml.j2
- name: swift-rsync-service
template: services/swift/swift-rsync-service.yml.j2
bootstrap:
pod:
- services/swift/swift-account-pod.yml.j2
- services/swift/swift-container-pod.yml.j2
- services/swift/swift-object-pod.yml.j2
- services/swift/swift-proxy-pod.yml.j2
- name: swift-account-pod
template: services/swift/swift-account-pod.yml.j2
- name: swift-container-pod
template: services/swift/swift-container-pod.yml.j2
- name: swift-object-pod
template: services/swift/swift-object-pod.yml.j2
- name: swift-proxy-pod
template: services/swift/swift-proxy-pod.yml.j2
- name: skydns
pods:
- name: skydns
@ -221,7 +277,9 @@ kolla-kubernetes:
pv:
pvc:
svc:
- services/skydns/skydns-service.yml.j2
- name: skydns-service
template: services/skydns/skydns-service.yml.j2
bootstrap:
pod:
- services/skydns/skydns-pod.yml.j2
- name: skydns-pod
template: services/skydns/skydns-pod.yml.j2

View File

@ -36,16 +36,16 @@ class Resource(KollaKubernetesBaseCommand):
metavar="<action>",
help=("One of [%s]" % ("|".join(Service.VALID_ACTIONS)))
)
parser.add_argument(
"resource_type",
metavar="<resource-type>",
help=("One of [%s]" % ("|".join(Service.VALID_RESOURCE_TYPES)))
)
parser.add_argument(
"service_name",
metavar="<service-name>",
help=("One of [%s]" % ("|".join(KKR.getServices().keys())))
)
parser.add_argument(
"resource_type",
metavar="<resource-type>",
help=("One of [%s]" % ("|".join(Service.VALID_RESOURCE_TYPES)))
)
return parser
def take_action(self, args):
@ -59,20 +59,20 @@ class Resource(KollaKubernetesBaseCommand):
args.action,
"|".join(Service.VALID_ACTIONS)))
raise Exception(msg)
if args.resource_type not in Service.VALID_RESOURCE_TYPES:
msg = ("resource_type [{}] not in valid resource_types [{}]"
.format(args.resource_type,
"|".join(Service.VALID_RESOURCE_TYPES)))
raise Exception(msg)
if args.service_name not in KKR.getServices().keys():
msg = ("service_name [{}] not in valid service_names [{}]".format(
args.service_name,
"|".join(KKR.getServices().keys())))
raise Exception(msg)
if args.resource_type not in Service.VALID_RESOURCE_TYPES:
msg = ("resource_type [{}] not in valid resource_types [{}]"
.format(args.resource_type,
"|".join(Service.VALID_RESOURCE_TYPES)))
raise Exception(msg)
service = KKR.getServiceByName(args.service_name)
if (args.resource_type != 'configmap') and (
len(service.getResourceFilesByType(args.resource_type)) == 0):
len(service.getResourceTemplatesByType(args.resource_type)) == 0):
msg = ("service_name [{}] has no resource"
" files defined for type [{}]".format(
args.service_name, args.resource_type))
@ -93,9 +93,9 @@ class ResourceTemplate(Resource):
def get_parser(self, prog_name):
parser = super(ResourceTemplate, self).get_parser(prog_name)
parser.add_argument(
"template_file",
metavar="<template-file>",
help=("One of [%s]" % ("|".join(KKR.getServices().keys())))
"resource_name",
metavar="<resource-name>",
help=("The unique resource-name under service->resource_type")
)
parser.add_argument(
'--print-jinja-vars',
@ -126,9 +126,17 @@ class ResourceTemplate(Resource):
"is not yet supported".format(args.resource_type))
raise Exception(msg)
variables = KKR.GetJinjaDict(args.service_name, vars(args),
service = KKR.getServiceByName(args.service_name)
rt = service.getResourceTemplateByTypeAndName(
args.resource_type, args.resource_name)
variables = KKR.GetJinjaDict(service.getName(), vars(args),
args.print_jinja_keys_regex)
# Merge the template vars with the jinja vars before processing
variables['kolla_kubernetes'].update(
{"template": {"vars": rt.getVars()}})
# handle the debug option --print-jinja-vars
if args.print_jinja_vars is True:
print(YamlUtils.yaml_dict_to_string(variables), file=sys.stderr)
@ -136,7 +144,7 @@ class ResourceTemplate(Resource):
# process the template
print(JinjaUtils.render_jinja(
variables,
FileUtils.read_string_from_file(args.template_file)))
FileUtils.read_string_from_file(rt.getTemplatePath())))
class ResourceMap(KollaKubernetesBaseCommand):
@ -184,11 +192,11 @@ class ResourceMap(KollaKubernetesBaseCommand):
if t == 'configmap':
continue
resource_files = s.getResourceFilesByType(t)
resourceTemplates = s.getResourceTemplatesByType(t)
print(' resource_type[{}] num_items[{}]'.format(
t, len(resource_files)))
t, len(resourceTemplates)))
# Print the resource files
for rf in s.getResourceFilesByType(t):
print(' ' + rf)
for rt in s.getResourceTemplatesByType(t):
print(' ' + str(rt))

View File

@ -10,7 +10,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
from oslo_log import log
@ -23,7 +22,7 @@ LOG = log.getLogger(__name__)
class KubeResourceTypeStatus(object):
def __init__(self, service_obj, service_name, resource_type):
def __init__(self, service_obj, resource_type):
# Check input args
if resource_type == 'disk':
@ -32,7 +31,6 @@ class KubeResourceTypeStatus(object):
# Initialize internal vars
self.service_obj = service_obj
self.service_name = service_name
self.resource_type = resource_type
self.resource_templates = []
@ -42,7 +40,7 @@ class KubeResourceTypeStatus(object):
def asDict(self):
res = {}
res['meta'] = {}
res['meta']['service_name'] = self.service_name
res['meta']['service_name'] = self.service_obj.getName()
res['meta']['resource_type'] = self.resource_type
res['results'] = {}
@ -64,9 +62,9 @@ class KubeResourceTypeStatus(object):
def doTemplateAndCheck(self):
"""Checks service resource_type resources in Kubernetes
For each resource file of resource_type
For each resourceTemplate of resource_type
Process the template (which may contain a stream of yaml definitions)
For each yaml definition
For each individual yaml definition
Send to kubernetes
Compare input definition to output status (do checks!)
Note: This is kube check only. Other subcommands should
@ -75,9 +73,10 @@ class KubeResourceTypeStatus(object):
Prints results dict to stdout as yaml status string
"""
resource_files = self.service_obj.getResourceFilesByType(
resourceTemplates = self.service_obj.getResourceTemplatesByType(
self.resource_type)
for file_ in resource_files:
for rt in resourceTemplates:
file_ = rt.getTemplatePath()
# Skip unsupported script templates
if file_.endswith('.sh.j2'):
@ -86,7 +85,7 @@ class KubeResourceTypeStatus(object):
continue
krt = KubeResourceTemplateStatus(
self.service_name, self.resource_type, file_)
self.service_obj, self.resource_type, rt)
self.resource_templates.append(krt)
@ -100,16 +99,13 @@ class KubeResourceTemplateStatus(object):
may print nothing or whitespace (NO-OP).
"""
def __init__(self, service_name, resource_type,
kube_resource_template_path):
# Check input args
assert os.path.exists(kube_resource_template_path)
def __init__(self, service_obj, resource_type,
resource_template_obj):
# Initialize internal vars
self.service_name = service_name
self.service_obj = service_obj
self.resource_type = resource_type
self.template = kube_resource_template_path
self.resource_template_obj = resource_template_obj
self.errors = []
self.oks = []
@ -120,7 +116,7 @@ class KubeResourceTemplateStatus(object):
def asDict(self):
res = {}
res['meta'] = {}
res['meta']['template'] = self.template
res['meta']['template'] = self.resource_template_obj.getTemplatePath()
res['results'] = {}
res['results']['status'] = self.getStatus()
@ -145,7 +141,8 @@ class KubeResourceTemplateStatus(object):
# Build the templating command
cmd = "kolla-kubernetes resource-template {} {} {} {}".format(
'create', self.resource_type, self.service_name, self.template)
'create', self.service_obj.getName(), self.resource_type,
self.resource_template_obj.getName())
# Execute the command to get the processed template output
template_out, err = ExecUtils.exec_command(cmd)
@ -160,7 +157,7 @@ class KubeResourceTemplateStatus(object):
return
elif re.match("^\s+$", template_out):
msg = "template {} produced empty output (NO-OP)".format(
self.template)
self.resource_template_obj.getTemplatePath())
self.oks.append(msg)
return

View File

@ -133,6 +133,17 @@ class Service(object):
self.pods = collections.OrderedDict()
for i in self.y['pods']:
self.pods[i['name']] = Pod(i)
self.resourceTemplates = {}
for rt in self.VALID_RESOURCE_TYPES:
# Initialize instance resourceTemplates hash
if rt not in self.resourceTemplates:
self.resourceTemplates[rt] = []
# Skip empty definitions
if rt not in self.y['resources']:
continue
# Handle definitions
for i in self.y['resources'][rt]:
self.resourceTemplates[rt].append(ResourceTemplate(i))
def getName(self):
return self.y['name']
@ -147,6 +158,24 @@ class Service(object):
sys.exit(1)
return r[name]
def getResourceTemplatesByType(self, resource_type):
assert resource_type in self.resourceTemplates
return self.resourceTemplates[resource_type]
def getResourceTemplateByTypeAndName(
self, resource_type, resource_name):
# create an inverted hash[name]=resourceTemplate
resourceTemplates = self.getResourceTemplatesByType(resource_type)
h = {i.getName(): i for i in resourceTemplates}
# validate
if resource_name not in h.keys():
print("unable to find resource_name={}", resource_name)
sys.exit(1)
return h[resource_name]
def __str__(self):
s = self.__class__.__name__ + " " + self.getName()
for k, v in self.getPods().items():
@ -159,8 +188,9 @@ class Service(object):
Example: service.apply("create", "disk")
Example: service.apply("create", ["disk", "pv", "pvc"])
Example: service.apply("delete", "all")
Example: service.apply("status", "all")
ACTION: string value of (create|delete)
ACTION: string value of (create|delete|status)
RESOURCE_TYPES: string value of one resource type, or list of
string values of many resource types
(configmap|disk|pv|pvc|svc|bootstrap|pod).
@ -194,7 +224,7 @@ class Service(object):
if action == "status":
if rt == "disk":
raise Exception('resource type for disk not supported yet')
krs = KubeResourceTypeStatus(self, self.getName(), rt)
krs = KubeResourceTypeStatus(self, rt)
print(YamlUtils.yaml_dict_to_string(krs.asDict()))
continue
@ -213,41 +243,26 @@ class Service(object):
# Handle all other resource_types as the same
self._ensureResource(action, rt)
def getResourceFilesByType(self, type_):
assert type_ in Service.VALID_RESOURCE_TYPES
assert 'resources' in self.y
# Handle where resource files not defined for type.
# i.e Not all services may require 'disk' resources
if type_ not in self.y['resources']:
return []
if type(self.y['resources'][type_]) is not list:
return []
# Fully resolve each resource file
ret = []
kkdir = PathFinder.find_kolla_kubernetes_dir()
for i in self.y['resources'][type_]:
file_ = os.path.join(kkdir, i)
assert os.path.exists(file_)
ret.append(file_)
return ret
def _ensureResource(self, action, resource_type):
# Check input args
assert action in Service.VALID_ACTIONS
assert resource_type in Service.VALID_RESOURCE_TYPES
assert resource_type in self.resourceTemplates
resource_files = self.getResourceFilesByType(resource_type)
resourceTemplates = self.resourceTemplates[resource_type]
# If action is delete, then delete the resource files in
# If action is delete, then delete the resourceTemplates in
# reverse order.
if action == 'delete':
resource_files = reversed(resource_files)
resourceTemplates = reversed(resourceTemplates)
for file_ in resource_files:
for resourceTemplate in resourceTemplates:
# Build the command based on if shell script or not. If
# shell script, pipe to sh. Else, pipe to kubectl
cmd = "kolla-kubernetes resource-template {} {} {} {}".format(
action, resource_type, self.getName(), file_)
if file_.endswith('.sh.j2'):
action, self.getName(), resource_type,
resourceTemplate.getName())
if resourceTemplate.getTemplatePath().endswith('.sh.j2'):
cmd += " | sh"
else:
cmd += " | kubectl {} -f -".format(action)
@ -317,3 +332,38 @@ class Container(object):
# Execute the command
ExecUtils.exec_command(cmd)
class ResourceTemplate(object):
def __init__(self, y):
# Checks
assert 'template' in y # not optional
assert 'name' in y # not optional
# Construct
self.y = y
def getName(self):
return self.y['name']
def getTemplate(self):
return self.y['template']
def getVars(self):
return (self.y['vars']
if 'vars' in self.y else None) # optional
def getTemplatePath(self):
kkdir = PathFinder.find_kolla_kubernetes_dir()
path = os.path.join(kkdir, self.getTemplate())
assert os.path.exists(path)
return path
def __str__(self):
s = self.__class__.__name__
s += " name[{}]".format(
self.getName() if self.getName() is not None else "")
s += " template[{}]".format(self.getTemplate())
s += " vars[{}]".format(
self.getVars() if self.getVars() is not None else "")
return s

View File

@ -1,5 +1,4 @@
{%- set resourceName = kolla_kubernetes.cli.args.service_name %}
{%- set size = '10' %}
{%- if storage_provider == "host" %}
{# Host storage provider uses storage on the local filesystem #}
@ -12,7 +11,7 @@
{%- endif %}
{%- elif storage_provider == "ceph" %}
{%- if kolla_kubernetes.cli.args.action == "create" %}
ssh {{ storage_ceph.ssh_user -}} @ {{- storage_ceph.monitors[0] }} rbd create {{ storage_ceph.pool -}}/{{- resourceName }} --size "{{ size }}G" --image-feature layering
ssh {{ storage_ceph.ssh_user -}} @ {{- storage_ceph.monitors[0] }} rbd create {{ storage_ceph.pool -}}/{{- resourceName }} --size "{{ kolla_kubernetes.template.vars.size_in_gb | int * 1024 }}" --image-feature layering
{%- elif kolla_kubernetes.cli.args.action == "delete" %}
ssh {{ storage_ceph.ssh_user -}} @ {{- storage_ceph.monitors[0] }} rbd delete {{ storage_ceph.pool -}}/{{- resourceName }}
{%- else %}
@ -22,7 +21,7 @@
{%- elif storage_provider == "gce" %}
{%- set type = 'pd-standard' %}
{%- if kolla_kubernetes.cli.args.action == "create" %}
gcloud compute disks create "{{ resourceName }}" --size "{{ size }}" --type "{{ type }}"
gcloud compute disks create "{{ resourceName }}" --OAsize "{{ kolla_kubernetes.template.vars.size_in_gb }}" --type "{{ type }}"
{%- elif kolla_kubernetes.cli.args.action == "delete" %}
gcloud compute disks delete "{{ resourceName }}" -q
{%- else %}

View File

@ -1,5 +1,4 @@
{%- set resourceName = kolla_kubernetes.cli.args.service_name %}
{%- set size = '10Gi' %}
{%- if storage_provider in ["host", "ceph", "gce"] -%}
@ -11,14 +10,16 @@ spec:
accessModes:
- ReadWriteOnce
capacity:
storage: {{ size }}
storage: {{ kolla_kubernetes.template.vars.size_in_gb }}Gi
{%- if storage_provider == "host" %}
hostPath:
path: /var/lib/kolla/volumes/{{ resourceName }}
{%- elif storage_provider == "gce" %}
gcePersistentDisk:
pdName: {{ resourceName }}
fsType: ext4
{%- elif storage_provider == "ceph" %}
rbd:
monitors: