Implement python interface to execute k8s CLI services

Implements interface to interact with kubectl commands for
services and pod operations.

Change-Id: I86bd0e5be93e258a14dc750cc6a4a3cb2fa4a448
Implements: blueprint magnum-backend-kubernetes-cli
This commit is contained in:
Pradeep Kilambi
2014-12-03 13:42:30 -05:00
parent 7cb8e559a3
commit 8213e71cb1
2 changed files with 140 additions and 66 deletions
+118 -66
View File
@@ -13,99 +13,151 @@
"""Magnum Kubernetes RPC handler."""
from magnum.openstack.common import log as logging
from magnum.openstack.common import utils
LOG = logging.getLogger(__name__)
# These are the backend operations. They are executed by the backend
# service. API calls via AMQP (within the ReST API) trigger the handlers to
# be called.
class KubeHandler(object):
"""These are the backend operations. They are executed by the backend
service. API calls via AMQP (within the ReST API) trigger the
handlers to be called.
This handler acts as an interface to executes kubectl command line
services.
"""
class Handler(object):
def __init__(self):
super(Handler, self).__init__()
# Bay Operations
def bay_create(id, name, type):
return None
def bay_list():
return None
def bay_delete(uuid):
return None
def bay_show(uuid):
return None
# Service Operations
super(KubeHandler, self).__init__()
@staticmethod
def service_create(uuid, contents):
LOG.debug("service_create %s contents %s" % (uuid, contents))
return None
try:
out, err = utils.trycmd('kubectl', 'create', '-f', contents)
if err:
return False
except Exception as e:
LOG.error("Couldn't create service with contents %s \
due to error %s" % (contents, e))
return False
return True
@staticmethod
def service_list():
LOG.debug("service_list")
return None
try:
out = utils.execute('kubectl', 'get', 'services')
pod_data = [s.split() for s in out.split('\n')]
return pod_data
except Exception as e:
LOG.error("Couldn't get list of services due to error %s" % e)
return None
@staticmethod
def service_delete(uuid):
LOG.debug("service_delete %s" % uuid)
return None
try:
out, err = utils.trycmd('kubectl', 'delete', 'service', uuid)
if err:
return False
except Exception as e:
LOG.error("Couldn't delete service %s due to error %s"
% (uuid, e))
return False
return False
@staticmethod
def service_get(uuid):
LOG.debug("service_get %s" % uuid)
try:
out = utils.execute('kubectl', 'get', 'service', uuid)
# TODO(pkilambi): process the output as needed
return out
except Exception as e:
LOG.error("Couldn't get service %s due to error %s" % (uuid, e))
return None
@staticmethod
def service_show(uuid):
LOG.debug("service_show %s" % uuid)
return None
try:
out = utils.execute('kubectl', 'describe', 'service', uuid)
# TODO(pkilambi): process the output as needed
return out
except Exception as e:
LOG.error("Couldn't describe service %s due to error %s"
% (uuid, e))
return None
# Pod Operations
@staticmethod
def pod_create(contents):
LOG.debug("pod_create contents %s" % contents)
try:
out, err = utils.trycmd('kubectl', 'create', '-f', contents)
if err:
return False
except Exception as e:
LOG.error("Couldn't create pod with contents %s due to error %s"
% (contents, e))
return False
return True
def pod_create(uuid, contents):
LOG.debug("pod_create %s contents %s" % (uuid, contents))
return None
@staticmethod
def pod_update(contents):
LOG.debug("pod_create contents %s" % contents)
try:
out, err = utils.trycmd('kubectl', 'update', '-f', contents)
if err:
return False
except Exception as e:
LOG.error("Couldn't update pod with contents %s due to error %s"
% (contents, e))
return False
return True
@staticmethod
def pod_list():
LOG.debug("pod_list")
return None
try:
out = utils.execute('kubectl', 'get', 'pods')
pod_data = [s.split() for s in out.split('\n')]
return pod_data
except Exception as e:
LOG.error("Couldn't get list of pods due to error %s" % e)
return None
@staticmethod
def pod_delete(uuid):
LOG.debug("pod_delete %s" % uuid)
return None
try:
out, err = utils.trycmd('kubectl', 'delete', 'pod', uuid)
if err:
return False
except Exception as e:
LOG.error("Couldn't delete pod %s due to error %s" % (uuid, e))
return False
return True
@staticmethod
def pod_get(uuid):
LOG.debug("service_get %s" % uuid)
try:
out = utils.execute('kubectl', 'get', 'pod', uuid)
# TODO(pkilambi): process the output as needed
return out
except Exception as e:
LOG.error("Couldn't get service %s due to error %s" % (uuid, e))
return None
@staticmethod
def pod_show(uuid):
LOG.debug("pod_show %s" % uuid)
return None
# Container operations
def container_create(uuid, contents):
return None
def container_list():
return None
def container_delete(uuid):
return None
def container_show(uuid):
return None
def container_reboot(uuid):
return None
def container_stop(uuid):
return None
def container_start(uuid):
return None
def container_pause(uuid):
return None
def container_unpause(uuid):
return None
def container_logs(uuid):
return None
def container_execute(uuid):
return None
try:
out = utils.execute('kubectl', 'describe', 'pod', uuid)
# TODO(pkilambi): process the output as needed
return out
except Exception as e:
LOG.error("Couldn't delete pod %s due to error %s" % (uuid, e))
return None
+22
View File
@@ -0,0 +1,22 @@
# 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.
from oslo.concurrency import processutils
def execute(*cmd, **kwargs):
"""Convenience wrapper around oslo's execute() method."""
return processutils.execute(*cmd, **kwargs)
def trycmd(*args, **kwargs):
"""Convenience wrapper around oslo's trycmd() method."""
return processutils.trycmd(*args, **kwargs)