Merge "Added support for template variables"

This commit is contained in:
Jenkins 2016-08-08 21:42:28 +00:00 committed by Gerrit Code Review
commit 462bbed796
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 mariadb-bootstrap job is deleted. The same should also occur for AWS
and Ceph. and Ceph.
#2. When running Kubernetes version < 1.3, Ceph RBD volumes will #2. When running Kubernetes, Ceph RBD volumes will not auto-unlock
auto-detach when Kubernetes nodes disappear, causing problems when a when Kubernetes nodes disappear, causing problems when a pod migrates
pod migrates to a new node and cannot mount the required volume. to a new node and cannot mount the required volume. This was supposed
Details are found in the in this `kubernetes pull to have been fixed in Kubernetes 1.3, but we have been unable to
request<https://github.com/kubernetes/kubernetes/pull/26351>`_. 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 Create all Kolla-Kubernetes Resources
@ -169,7 +170,7 @@ Operator Create Resources
kolla-kubernetes bootstrap mariadb kolla-kubernetes bootstrap mariadb
sleep 30 # wait for mariadb bootstrap to finish 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 mariadb
kolla-kubernetes run memcached kolla-kubernetes run memcached
sleep 30 # wait for mariadb and memcached to start up sleep 30 # wait for mariadb and memcached to start up
@ -183,33 +184,59 @@ Operator Create Resources
Workflow Engine 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. bits of logic.
:: ::
kolla-kubernetes resource create disk mariadb kolla-kubernetes resource create mariadb disk
kolla-kubernetes resource create pv mariadb kolla-kubernetes resource create mariadb pv
kolla-kubernetes resource create pvc mariadb kolla-kubernetes resource create mariadb pvc
kolla-kubernetes resource create svc mariadb kolla-kubernetes resource create mariadb svc
kolla-kubernetes resource create configmap mariadb kolla-kubernetes resource create mariadb configmap
kolla-kubernetes resource create bootstrap mariadb kolla-kubernetes resource create mariadb bootstrap
sleep 30 # wait for mariadb bootstrap to finish 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 resource create pod mariadb kolla-kubernetes resource create mariadb pod
kolla-kubernetes resource create svc memcached kolla-kubernetes resource create memcached svc
kolla-kubernetes resource create configmap memcached kolla-kubernetes resource create memcached configmap
kolla-kubernetes resource create pod memcached kolla-kubernetes resource create memcached pod
kolla-kubernetes resource create svc keystone kolla-kubernetes resource create keystone svc
kolla-kubernetes resource create configmap keystone kolla-kubernetes resource create keystone configmap
sleep 30 # wait for mariadb and memcached to start up 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 sleep 30 # wait for keystone to bootstrap in mariadb
kolla-kubernetes resource create pod keystone kolla-kubernetes resource create keystone pod
kolla-kubernetes resource create svc horizon kolla-kubernetes resource create horizon svc
kolla-kubernetes resource create configmap horizon kolla-kubernetes resource create horizon configmap
sleep 30 # wait for keystone to start up 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 Delete all Kolla-Kubernetes Resources
@ -234,20 +261,20 @@ Workflow Engine Delete Resources
:: ::
kolla-kubernetes resource delete pod horizon kolla-kubernetes resource delete horizon pod
kolla-kubernetes resource delete configmap horizon kolla-kubernetes resource delete horizon configmap
kolla-kubernetes resource delete svc horizon kolla-kubernetes resource delete horizon svc
kolla-kubernetes resource delete pod keystone kolla-kubernetes resource delete keystone pod
kolla-kubernetes resource delete bootstrap keystone kolla-kubernetes resource delete keystone bootstrap
kolla-kubernetes resource delete configmap keystone kolla-kubernetes resource delete keystone configmap
kolla-kubernetes resource delete svc keystone kolla-kubernetes resource delete keystone svc
kolla-kubernetes resource delete pod memcached kolla-kubernetes resource delete memcached pod
kolla-kubernetes resource delete configmap memcached kolla-kubernetes resource delete memcached configmap
kolla-kubernetes resource delete svc memcached kolla-kubernetes resource delete memcached svc
kolla-kubernetes resource delete pod mariadb kolla-kubernetes resource delete mariadb pod
kolla-kubernetes resource delete bootstrap mariadb kolla-kubernetes resource delete mariadb bootstrap
kolla-kubernetes resource delete configmap mariadb kolla-kubernetes resource delete mariadb configmap
kolla-kubernetes resource delete svc mariadb kolla-kubernetes resource delete mariadb svc
kolla-kubernetes resource delete pvc mariadb kolla-kubernetes resource delete mariadb pvc
kolla-kubernetes resource delete pv mariadb kolla-kubernetes resource delete mariadb pv
kolla-kubernetes resource delete disk mariadb kolla-kubernetes resource delete mariadb disk

View File

@ -12,6 +12,14 @@
# persistent state. # persistent state.
# The (pod) resources may be destroyed willy-nilly, since they hold no # The (pod) resources may be destroyed willy-nilly, since they hold no
# state. # 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: kolla-kubernetes:
services: services:
@ -22,17 +30,27 @@ kolla-kubernetes:
- name: mariadb - name: mariadb
resources: resources:
disk: disk:
- services/mariadb/mariadb-disk.sh.j2 - name: mariadb-disk
template: services/mariadb/mariadb-disk.sh.j2
vars:
size_in_gb: 10
pv: pv:
- services/mariadb/mariadb-pv.yml.j2 - name: mariadb-pv
template: services/mariadb/mariadb-pv.yml.j2
vars:
size_in_gb: 10
pvc: pvc:
- services/mariadb/mariadb-pvc.yml.j2 - name: mariadb-pvc
template: services/mariadb/mariadb-pvc.yml.j2
svc: svc:
- services/mariadb/mariadb-service.yml.j2 - name: mariadb-service
template: services/mariadb/mariadb-service.yml.j2
bootstrap: bootstrap:
- services/mariadb/mariadb-bootstrap-job.yml.j2 - name: mariadb-bootstrap-job
template: services/mariadb/mariadb-bootstrap-job.yml.j2
pod: pod:
- services/mariadb/mariadb-pod.yml.j2 - name: mariadb-pod
template: services/mariadb/mariadb-pod.yml.j2
- name: memcached - name: memcached
pods: pods:
- name: memcached - name: memcached
@ -43,10 +61,12 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/memcached/memcached-service.yml.j2 - name: memcached-service
template: services/memcached/memcached-service.yml.j2
bootstrap: bootstrap:
pod: pod:
- services/memcached/memcached-pod.yml.j2 - name: memcached-pod
template: services/memcached/memcached-pod.yml.j2
- name: keystone - name: keystone
pods: pods:
- name: keystone - name: keystone
@ -57,12 +77,16 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/keystone/keystone-service-admin.yml.j2 - name: keystone-service-admin
- services/keystone/keystone-service-public.yml.j2 template: services/keystone/keystone-service-admin.yml.j2
- name: keystone-service-public
template: services/keystone/keystone-service-public.yml.j2
bootstrap: bootstrap:
- services/keystone/keystone-bootstrap-job.yml.j2 - name: keystone-bootstrap-job
template: services/keystone/keystone-bootstrap-job.yml.j2
pod: pod:
- services/keystone/keystone-pod.yml.j2 - name: keystone-pod
template: services/keystone/keystone-pod.yml.j2
- name: horizon - name: horizon
pods: pods:
- name: horizon - name: horizon
@ -73,10 +97,12 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/horizon/horizon-service.yml.j2 - name: horizon-service
template: services/horizon/horizon-service.yml.j2
bootstrap: bootstrap:
pod: pod:
- services/horizon/horizon-pod.yml.j2 - name: horizon-pod
template: services/horizon/horizon-pod.yml.j2
- name: rabbitmq - name: rabbitmq
pods: pods:
- name: rabbitmq - name: rabbitmq
@ -87,12 +113,16 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/rabbitmq/rabbitmq-service-management.yml.j2 - name: rabbitmq-service-management
- services/rabbitmq/rabbitmq-service.yml.j2 template: services/rabbitmq/rabbitmq-service-management.yml.j2
- name: rabbitmq-service
template: services/rabbitmq/rabbitmq-service.yml.j2
bootstrap: bootstrap:
- services/rabbitmq/rabbitmq-bootstrap-job.yml.j2 - name: rabbitmq-bootstrap-job
template: services/rabbitmq/rabbitmq-bootstrap-job.yml.j2
pod: pod:
- services/rabbitmq/rabbitmq-pod.yml.j2 - name: rabbitmq-pod
template: services/rabbitmq/rabbitmq-pod.yml.j2
- name: glance - name: glance
pods: pods:
- name: glance - name: glance
@ -104,13 +134,18 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/glance/glance-api-service.yml.j2 - name: glance-api-service
- services/glance/glance-registry-service.yml.j2 template: services/glance/glance-api-service.yml.j2
- name: glance-registry-service
template: services/glance/glance-registry-service.yml.j2
bootstrap: bootstrap:
- services/glance/glance-bootstrap-job.yml.j2 - name: glance-bootstrap-job
template: services/glance/glance-bootstrap-job.yml.j2
pod: pod:
- services/glance/glance-api-pod.yml.j2 - name: glance-api-pod
- services/glance/glance-registry-pod.yml.j2 template: services/glance/glance-api-pod.yml.j2
- name: glance-registry-pod
template: services/glance/glance-registry-pod.yml.j2
- name: nova - name: nova
pods: pods:
@ -128,13 +163,18 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/nova/nova-service-compute.yml.j2 - name: nova-service-compute
template: services/nova/nova-service-compute.yml.j2
bootstrap: bootstrap:
- services/nova/nova-compute-bootstrap-job.yml.j2 - name: nova-compute-bootstrap-job
- services/nova/nova-control-bootstrap-job.yml.j2 template: services/nova/nova-compute-bootstrap-job.yml.j2
- name: nova-control-bootstrap-job
template: services/nova/nova-control-bootstrap-job.yml.j2
pod: pod:
- services/nova/nova-compute-pod.yml.j2 - name: nova-compute-pod
- services/nova/nova-control-pod.yml.j2 template: services/nova/nova-compute-pod.yml.j2
- name: nova-control-pod
template: services/nova/nova-control-pod.yml.j2
- name: neutron - name: neutron
pods: pods:
- name: neutron-compute - name: neutron-compute
@ -156,15 +196,22 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/neutron/neutron-server-service.yml.j2 - name: neutron-server-service
template: services/neutron/neutron-server-service.yml.j2
bootstrap: bootstrap:
- services/neutron/neutron-bootstrap-job.yml.j2 - name: neutron-bootstrap-job
template: services/neutron/neutron-bootstrap-job.yml.j2
pod: pod:
- services/neutron/neutron-compute-pod.yml.j2 - name: neutron-compute-pod
- services/neutron/neutron-control-pod.yml.j2 template: services/neutron/neutron-compute-pod.yml.j2
- services/neutron/neutron-dhcp-agent-pod.yml.j2 - name: neutron-control-pod
- services/neutron/neutron-l3-agent-pod.yml.j2 template: services/neutron/neutron-control-pod.yml.j2
- services/neutron/neutron-metadata-agent-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 - name: swift
pods: pods:
- name: swift-account - name: swift-account
@ -197,17 +244,26 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/swift/swift-account-service.yml.j2 - name: swift-account-service
- services/swift/swift-container-service.yml.j2 template: services/swift/swift-account-service.yml.j2
- services/swift/swift-object-service.yml.j2 - name: swift-container-service
- services/swift/swift-proxy-service.yml.j2 template: services/swift/swift-container-service.yml.j2
- services/swift/swift-rsync-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: bootstrap:
pod: pod:
- services/swift/swift-account-pod.yml.j2 - name: swift-account-pod
- services/swift/swift-container-pod.yml.j2 template: services/swift/swift-account-pod.yml.j2
- services/swift/swift-object-pod.yml.j2 - name: swift-container-pod
- services/swift/swift-proxy-pod.yml.j2 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 - name: skydns
pods: pods:
- name: skydns - name: skydns
@ -221,7 +277,9 @@ kolla-kubernetes:
pv: pv:
pvc: pvc:
svc: svc:
- services/skydns/skydns-service.yml.j2 - name: skydns-service
template: services/skydns/skydns-service.yml.j2
bootstrap: bootstrap:
pod: 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>", metavar="<action>",
help=("One of [%s]" % ("|".join(Service.VALID_ACTIONS))) 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( parser.add_argument(
"service_name", "service_name",
metavar="<service-name>", metavar="<service-name>",
help=("One of [%s]" % ("|".join(KKR.getServices().keys()))) 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 return parser
def take_action(self, args): def take_action(self, args):
@ -59,20 +59,20 @@ class Resource(KollaKubernetesBaseCommand):
args.action, args.action,
"|".join(Service.VALID_ACTIONS))) "|".join(Service.VALID_ACTIONS)))
raise Exception(msg) 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(): if args.service_name not in KKR.getServices().keys():
msg = ("service_name [{}] not in valid service_names [{}]".format( msg = ("service_name [{}] not in valid service_names [{}]".format(
args.service_name, args.service_name,
"|".join(KKR.getServices().keys()))) "|".join(KKR.getServices().keys())))
raise Exception(msg) 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) service = KKR.getServiceByName(args.service_name)
if (args.resource_type != 'configmap') and ( 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" msg = ("service_name [{}] has no resource"
" files defined for type [{}]".format( " files defined for type [{}]".format(
args.service_name, args.resource_type)) args.service_name, args.resource_type))
@ -93,9 +93,9 @@ class ResourceTemplate(Resource):
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ResourceTemplate, self).get_parser(prog_name) parser = super(ResourceTemplate, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
"template_file", "resource_name",
metavar="<template-file>", metavar="<resource-name>",
help=("One of [%s]" % ("|".join(KKR.getServices().keys()))) help=("The unique resource-name under service->resource_type")
) )
parser.add_argument( parser.add_argument(
'--print-jinja-vars', '--print-jinja-vars',
@ -126,9 +126,17 @@ class ResourceTemplate(Resource):
"is not yet supported".format(args.resource_type)) "is not yet supported".format(args.resource_type))
raise Exception(msg) 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) 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 # handle the debug option --print-jinja-vars
if args.print_jinja_vars is True: if args.print_jinja_vars is True:
print(YamlUtils.yaml_dict_to_string(variables), file=sys.stderr) print(YamlUtils.yaml_dict_to_string(variables), file=sys.stderr)
@ -136,7 +144,7 @@ class ResourceTemplate(Resource):
# process the template # process the template
print(JinjaUtils.render_jinja( print(JinjaUtils.render_jinja(
variables, variables,
FileUtils.read_string_from_file(args.template_file))) FileUtils.read_string_from_file(rt.getTemplatePath())))
class ResourceMap(KollaKubernetesBaseCommand): class ResourceMap(KollaKubernetesBaseCommand):
@ -184,11 +192,11 @@ class ResourceMap(KollaKubernetesBaseCommand):
if t == 'configmap': if t == 'configmap':
continue continue
resource_files = s.getResourceFilesByType(t) resourceTemplates = s.getResourceTemplatesByType(t)
print(' resource_type[{}] num_items[{}]'.format( print(' resource_type[{}] num_items[{}]'.format(
t, len(resource_files))) t, len(resourceTemplates)))
# Print the resource files # Print the resource files
for rf in s.getResourceFilesByType(t): for rt in s.getResourceTemplatesByType(t):
print(' ' + rf) print(' ' + str(rt))

View File

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

View File

@ -133,6 +133,17 @@ class Service(object):
self.pods = collections.OrderedDict() self.pods = collections.OrderedDict()
for i in self.y['pods']: for i in self.y['pods']:
self.pods[i['name']] = Pod(i) 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): def getName(self):
return self.y['name'] return self.y['name']
@ -147,6 +158,24 @@ class Service(object):
sys.exit(1) sys.exit(1)
return r[name] 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): def __str__(self):
s = self.__class__.__name__ + " " + self.getName() s = self.__class__.__name__ + " " + self.getName()
for k, v in self.getPods().items(): for k, v in self.getPods().items():
@ -159,8 +188,9 @@ class Service(object):
Example: service.apply("create", "disk") Example: service.apply("create", "disk")
Example: service.apply("create", ["disk", "pv", "pvc"]) Example: service.apply("create", ["disk", "pv", "pvc"])
Example: service.apply("delete", "all") 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 RESOURCE_TYPES: string value of one resource type, or list of
string values of many resource types string values of many resource types
(configmap|disk|pv|pvc|svc|bootstrap|pod). (configmap|disk|pv|pvc|svc|bootstrap|pod).
@ -194,7 +224,7 @@ class Service(object):
if action == "status": if action == "status":
if rt == "disk": if rt == "disk":
raise Exception('resource type for disk not supported yet') 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())) print(YamlUtils.yaml_dict_to_string(krs.asDict()))
continue continue
@ -213,41 +243,26 @@ class Service(object):
# Handle all other resource_types as the same # Handle all other resource_types as the same
self._ensureResource(action, rt) 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): 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. # reverse order.
if action == 'delete': 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 # Build the command based on if shell script or not. If
# shell script, pipe to sh. Else, pipe to kubectl # shell script, pipe to sh. Else, pipe to kubectl
cmd = "kolla-kubernetes resource-template {} {} {} {}".format( cmd = "kolla-kubernetes resource-template {} {} {} {}".format(
action, resource_type, self.getName(), file_) action, self.getName(), resource_type,
if file_.endswith('.sh.j2'): resourceTemplate.getName())
if resourceTemplate.getTemplatePath().endswith('.sh.j2'):
cmd += " | sh" cmd += " | sh"
else: else:
cmd += " | kubectl {} -f -".format(action) cmd += " | kubectl {} -f -".format(action)
@ -317,3 +332,38 @@ class Container(object):
# Execute the command # Execute the command
ExecUtils.exec_command(cmd) 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 resourceName = kolla_kubernetes.cli.args.service_name %}
{%- set size = '10' %}
{%- if storage_provider == "host" %} {%- if storage_provider == "host" %}
{# Host storage provider uses storage on the local filesystem #} {# Host storage provider uses storage on the local filesystem #}
@ -12,7 +11,7 @@
{%- endif %} {%- endif %}
{%- elif storage_provider == "ceph" %} {%- elif storage_provider == "ceph" %}
{%- if kolla_kubernetes.cli.args.action == "create" %} {%- 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" %} {%- elif kolla_kubernetes.cli.args.action == "delete" %}
ssh {{ storage_ceph.ssh_user -}} @ {{- storage_ceph.monitors[0] }} rbd delete {{ storage_ceph.pool -}}/{{- resourceName }} ssh {{ storage_ceph.ssh_user -}} @ {{- storage_ceph.monitors[0] }} rbd delete {{ storage_ceph.pool -}}/{{- resourceName }}
{%- else %} {%- else %}
@ -22,7 +21,7 @@
{%- elif storage_provider == "gce" %} {%- elif storage_provider == "gce" %}
{%- set type = 'pd-standard' %} {%- set type = 'pd-standard' %}
{%- if kolla_kubernetes.cli.args.action == "create" %} {%- 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" %} {%- elif kolla_kubernetes.cli.args.action == "delete" %}
gcloud compute disks delete "{{ resourceName }}" -q gcloud compute disks delete "{{ resourceName }}" -q
{%- else %} {%- else %}

View File

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