Add fetching of qe deployments in config tool
This commit is contained in:
parent
2c61c87712
commit
3d4e43c8d1
24 changed files with 484 additions and 18 deletions
|
@ -8,12 +8,13 @@ class TransientDirectoryProvider(FileConfigProvider):
|
|||
from/to the file system, only using temporary directories,
|
||||
deleting old dirs and creating new ones as requested.
|
||||
"""
|
||||
def __init__(self, config_volume, yaml_filename, py_filename):
|
||||
def __init__(self, config_volume, yaml_filename, py_filename, kubernetes=False):
|
||||
# Create a temp directory that will be cleaned up when we change the config path
|
||||
# This should ensure we have no "pollution" of different configs:
|
||||
# no uploaded config should ever affect subsequent config modifications/creations
|
||||
temp_dir = TemporaryDirectory()
|
||||
self.temp_dir = temp_dir
|
||||
self.kubernetes = kubernetes
|
||||
super(TransientDirectoryProvider, self).__init__(temp_dir.name, yaml_filename, py_filename)
|
||||
|
||||
@property
|
||||
|
@ -33,3 +34,9 @@ class TransientDirectoryProvider(FileConfigProvider):
|
|||
|
||||
def get_config_dir_path(self):
|
||||
return self.config_volume
|
||||
|
||||
def save_configuration_to_kubernetes(self):
|
||||
if not self.kubernetes:
|
||||
raise Exception("Not on kubernetes, cannot save configuration.")
|
||||
|
||||
print('do stuf')
|
||||
|
|
|
@ -3,10 +3,10 @@ from config_app.config_util.config.testprovider import TestConfigProvider
|
|||
from config_app.config_util.config.TransientDirectoryProvider import TransientDirectoryProvider
|
||||
|
||||
|
||||
def get_config_provider(config_volume, yaml_filename, py_filename, testing=False):
|
||||
def get_config_provider(config_volume, yaml_filename, py_filename, testing=False, kubernetes=False):
|
||||
""" Loads and returns the config provider for the current environment. """
|
||||
|
||||
if testing:
|
||||
return TestConfigProvider()
|
||||
|
||||
return TransientDirectoryProvider(config_volume, yaml_filename, py_filename)
|
||||
return TransientDirectoryProvider(config_volume, yaml_filename, py_filename, kubernetes=kubernetes)
|
||||
|
|
182
config_app/config_util/k8sinterface.py
Normal file
182
config_app/config_util/k8sinterface.py
Normal file
|
@ -0,0 +1,182 @@
|
|||
import os
|
||||
import logging
|
||||
import json
|
||||
import base64
|
||||
import time
|
||||
|
||||
from cStringIO import StringIO
|
||||
from requests import Request, Session
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
KUBERNETES_API_HOST = os.environ.get('KUBERNETES_SERVICE_HOST', '')
|
||||
port = os.environ.get('KUBERNETES_SERVICE_PORT')
|
||||
if port:
|
||||
KUBERNETES_API_HOST += ':' + port
|
||||
|
||||
SERVICE_ACCOUNT_TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token'
|
||||
|
||||
QE_NAMESPACE = os.environ.get('QE_K8S_NAMESPACE', 'quay-enterprise')
|
||||
QE_CONFIG_SECRET = os.environ.get('QE_K8S_CONFIG_SECRET', 'quay-enterprise-config-secret')
|
||||
|
||||
# The name of the quay enterprise deployment (not config app) that is used to query & rollout
|
||||
QE_DEPLOYMENT_SELECTOR = os.environ.get('QE_DEPLOYMENT_SELECTOR', 'quay-enterprise-app')
|
||||
|
||||
class KubernetesAccessInterface:
|
||||
""" Implementation of the config provider that reads and writes configuration
|
||||
data from a Kubernetes Secret. """
|
||||
def __init__(self, api_host=None, service_account_token_path=None):
|
||||
service_account_token_path = service_account_token_path or SERVICE_ACCOUNT_TOKEN_PATH
|
||||
api_host = api_host or KUBERNETES_API_HOST
|
||||
|
||||
# Load the service account token from the local store.
|
||||
if not os.path.exists(service_account_token_path):
|
||||
raise Exception('Cannot load Kubernetes service account token')
|
||||
|
||||
with open(service_account_token_path, 'r') as f:
|
||||
self._service_token = f.read()
|
||||
|
||||
self._api_host = api_host
|
||||
|
||||
# def volume_file_exists(self, relative_file_path):
|
||||
# if '/' in relative_file_path:
|
||||
# raise Exception('Expected path from get_volume_path, but found slashes')
|
||||
#
|
||||
# # NOTE: Overridden because we don't have subdirectories, which aren't supported
|
||||
# # in Kubernetes secrets.
|
||||
# secret = self._lookup_secret()
|
||||
# if not secret or not secret.get('data'):
|
||||
# return False
|
||||
# return relative_file_path in secret['data']
|
||||
#
|
||||
# def list_volume_directory(self, path):
|
||||
# # NOTE: Overridden because we don't have subdirectories, which aren't supported
|
||||
# # in Kubernetes secrets.
|
||||
# secret = self._lookup_secret()
|
||||
#
|
||||
# if not secret:
|
||||
# return []
|
||||
#
|
||||
# paths = []
|
||||
# for filename in secret.get('data', {}):
|
||||
# if filename.startswith(path):
|
||||
# paths.append(filename[len(path) + 1:])
|
||||
# return paths
|
||||
#
|
||||
# def save_config(self, config_obj):
|
||||
# self._update_secret_file(self.yaml_filename, get_yaml(config_obj))
|
||||
#
|
||||
# def remove_volume_file(self, relative_file_path):
|
||||
# try:
|
||||
# self._update_secret_file(relative_file_path, None)
|
||||
# except IOError as ioe:
|
||||
# raise CannotWriteConfigException(str(ioe))
|
||||
#
|
||||
# def save_volume_file(self, flask_file, relative_file_path):
|
||||
# # Write the file to a temp location.
|
||||
# buf = StringIO()
|
||||
# try:
|
||||
# try:
|
||||
# flask_file.save(buf)
|
||||
# except IOError as ioe:
|
||||
# raise CannotWriteConfigException(str(ioe))
|
||||
#
|
||||
# self._update_secret_file(relative_file_path, buf.getvalue())
|
||||
# finally:
|
||||
# buf.close()
|
||||
|
||||
def get_num_qe_pods(self):
|
||||
# TODO: change to get /deployments?labelSelector={labelName}%3D{labelValue}
|
||||
# right now just get the hardcoded deployment name
|
||||
deployment_url = 'namespaces/%s/deployments/%s' % (QE_NAMESPACE, QE_DEPLOYMENT_SELECTOR)
|
||||
response = self._execute_k8s_api('GET', deployment_url, api_prefix='apis/extensions/v1beta1')
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
return json.loads(response.text)
|
||||
|
||||
# def _assert_success(self, response):
|
||||
# if response.status_code != 200:
|
||||
# logger.error('Kubernetes API call failed with response: %s => %s', response.status_code,
|
||||
# response.text)
|
||||
# raise CannotWriteConfigException('Kubernetes API call failed: %s' % response.text)
|
||||
|
||||
# def _update_secret_file(self, relative_file_path, value=None):
|
||||
# if '/' in relative_file_path:
|
||||
# raise Exception('Expected path from get_volume_path, but found slashes')
|
||||
#
|
||||
# # Check first that the namespace for Quay Enterprise exists. If it does not, report that
|
||||
# # as an error, as it seems to be a common issue.
|
||||
# namespace_url = 'namespaces/%s' % (QE_NAMESPACE)
|
||||
# response = self._execute_k8s_api('GET', namespace_url)
|
||||
# if response.status_code // 100 != 2:
|
||||
# msg = 'A Kubernetes namespace with name `%s` must be created to save config' % QE_NAMESPACE
|
||||
# raise CannotWriteConfigException(msg)
|
||||
#
|
||||
# # Check if the secret exists. If not, then we create an empty secret and then update the file
|
||||
# # inside.
|
||||
# secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET)
|
||||
# secret = self._lookup_secret()
|
||||
# if secret is None:
|
||||
# self._assert_success(self._execute_k8s_api('POST', secret_url, {
|
||||
# "kind": "Secret",
|
||||
# "apiVersion": "v1",
|
||||
# "metadata": {
|
||||
# "name": QE_CONFIG_SECRET
|
||||
# },
|
||||
# "data": {}
|
||||
# }))
|
||||
#
|
||||
# # Update the secret to reflect the file change.
|
||||
# secret['data'] = secret.get('data', {})
|
||||
#
|
||||
# if value is not None:
|
||||
# secret['data'][relative_file_path] = base64.b64encode(value)
|
||||
# else:
|
||||
# secret['data'].pop(relative_file_path)
|
||||
#
|
||||
# self._assert_success(self._execute_k8s_api('PUT', secret_url, secret))
|
||||
#
|
||||
# # Wait until the local mounted copy of the secret has been updated, as
|
||||
# # this is an eventual consistency operation, but the caller expects immediate
|
||||
# # consistency.
|
||||
# while True:
|
||||
# matching_files = set()
|
||||
# for secret_filename, encoded_value in secret['data'].iteritems():
|
||||
# expected_value = base64.b64decode(encoded_value)
|
||||
# try:
|
||||
# with self.get_volume_file(secret_filename) as f:
|
||||
# contents = f.read()
|
||||
#
|
||||
# if contents == expected_value:
|
||||
# matching_files.add(secret_filename)
|
||||
# except IOError:
|
||||
# continue
|
||||
#
|
||||
# if matching_files == set(secret['data'].keys()):
|
||||
# break
|
||||
#
|
||||
# # Sleep for a second and then try again.
|
||||
# time.sleep(1)
|
||||
|
||||
def _lookup_secret(self):
|
||||
secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET)
|
||||
response = self._execute_k8s_api('GET', secret_url)
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
return json.loads(response.text)
|
||||
|
||||
def _execute_k8s_api(self, method, relative_url, data=None, api_prefix='api/v1'):
|
||||
headers = {
|
||||
'Authorization': 'Bearer ' + self._service_token
|
||||
}
|
||||
|
||||
if data:
|
||||
headers['Content-Type'] = 'application/json'
|
||||
|
||||
data = json.dumps(data) if data else None
|
||||
session = Session()
|
||||
url = 'https://%s/%s/%s' % (self._api_host, api_prefix, relative_url)
|
||||
|
||||
request = Request(method, url, data=data, headers=headers)
|
||||
return session.send(request.prepare(), verify=False, timeout=2)
|
Reference in a new issue