Adds ability to upload config dir to k8s in config

This commit is contained in:
Sam Chow 2018-08-08 14:22:28 -04:00
parent 3d4e43c8d1
commit d387ba171f
10 changed files with 138 additions and 84 deletions

View file

@ -24,7 +24,7 @@ is_kubernetes = IS_KUBERNETES
logger.debug('Configuration is on a kubernetes deployment: %s' % IS_KUBERNETES) logger.debug('Configuration is on a kubernetes deployment: %s' % IS_KUBERNETES)
config_provider = get_config_provider(OVERRIDE_CONFIG_DIRECTORY, 'config.yaml', 'config.py', config_provider = get_config_provider(OVERRIDE_CONFIG_DIRECTORY, 'config.yaml', 'config.py',
testing=is_testing, kubernetes=is_kubernetes) testing=is_testing)
if is_testing: if is_testing:
from test.testconfig import TestConfig from test.testconfig import TestConfig

View file

@ -6,7 +6,7 @@ from config_app.config_endpoints.api.suconfig_models_pre_oci import pre_oci_mode
from config_app.config_endpoints.api import resource, ApiResource, nickname, validate_json_request, kubernetes_only from config_app.config_endpoints.api import resource, ApiResource, nickname, validate_json_request, kubernetes_only
from config_app.c_app import (app, config_provider, superusers, ip_resolver, from config_app.c_app import (app, config_provider, superusers, ip_resolver,
instance_keys, INIT_SCRIPTS_LOCATION, IS_KUBERNETES) instance_keys, INIT_SCRIPTS_LOCATION, IS_KUBERNETES)
from config_app.config_util.k8sinterface import KubernetesAccessInterface from config_app.config_util.k8sinterface import KubernetesAccessInterface, kubernetes_access_instance
from data.database import configure from data.database import configure
from data.runmigration import run_alembic_migration from data.runmigration import run_alembic_migration
@ -266,15 +266,13 @@ class SuperUserKubernetesDeployment(ApiResource):
@kubernetes_only @kubernetes_only
@nickname('scGetNumDeployments') @nickname('scGetNumDeployments')
def get(self): def get(self):
accessor = KubernetesAccessInterface() return kubernetes_access_instance.get_qe_deployments()
res = accessor.get_num_qe_pods()
return res
@resource('/v1/superuser/config/kubernetes') @resource('/v1/superuser/config/kubernetes')
class SuperUserKubernetesConfiguration(ApiResource): class SuperUserKubernetesConfiguration(ApiResource):
""" Resource for fetching the status of config files and overriding them. """ """ Resource for fetching the status of config files and overriding them. """
@kubernetes_only @kubernetes_only
@nickname('scSaveConfigToKube') @nickname('scDeployConfiguration')
def post(self): def post(self):
return config_provider.save_configuration_to_kubernetes() return config_provider.save_configuration_to_kubernetes()

View file

@ -2,19 +2,20 @@ import os
from backports.tempfile import TemporaryDirectory from backports.tempfile import TemporaryDirectory
from config_app.config_util.config.fileprovider import FileConfigProvider from config_app.config_util.config.fileprovider import FileConfigProvider
from config_app.config_util.k8sinterface import kubernetes_access_instance
class TransientDirectoryProvider(FileConfigProvider): class TransientDirectoryProvider(FileConfigProvider):
""" Implementation of the config provider that reads and writes the data """ Implementation of the config provider that reads and writes the data
from/to the file system, only using temporary directories, from/to the file system, only using temporary directories,
deleting old dirs and creating new ones as requested. deleting old dirs and creating new ones as requested.
""" """
def __init__(self, config_volume, yaml_filename, py_filename, kubernetes=False): def __init__(self, config_volume, yaml_filename, py_filename):
# Create a temp directory that will be cleaned up when we change the config path # 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: # This should ensure we have no "pollution" of different configs:
# no uploaded config should ever affect subsequent config modifications/creations # no uploaded config should ever affect subsequent config modifications/creations
temp_dir = TemporaryDirectory() temp_dir = TemporaryDirectory()
self.temp_dir = temp_dir self.temp_dir = temp_dir
self.kubernetes = kubernetes
super(TransientDirectoryProvider, self).__init__(temp_dir.name, yaml_filename, py_filename) super(TransientDirectoryProvider, self).__init__(temp_dir.name, yaml_filename, py_filename)
@property @property
@ -36,7 +37,10 @@ class TransientDirectoryProvider(FileConfigProvider):
return self.config_volume return self.config_volume
def save_configuration_to_kubernetes(self): def save_configuration_to_kubernetes(self):
if not self.kubernetes: config_path = self.get_config_dir_path()
raise Exception("Not on kubernetes, cannot save configuration.")
print('do stuf') for name in os.listdir(config_path):
file_path = os.path.join(self.config_volume, name)
kubernetes_access_instance.save_file_as_secret(name, file_path)
return 200

View file

@ -3,10 +3,10 @@ from config_app.config_util.config.testprovider import TestConfigProvider
from config_app.config_util.config.TransientDirectoryProvider import TransientDirectoryProvider from config_app.config_util.config.TransientDirectoryProvider import TransientDirectoryProvider
def get_config_provider(config_volume, yaml_filename, py_filename, testing=False, kubernetes=False): def get_config_provider(config_volume, yaml_filename, py_filename, testing=False):
""" Loads and returns the config provider for the current environment. """ """ Loads and returns the config provider for the current environment. """
if testing: if testing:
return TestConfigProvider() return TestConfigProvider()
return TransientDirectoryProvider(config_volume, yaml_filename, py_filename, kubernetes=kubernetes) return TransientDirectoryProvider(config_volume, yaml_filename, py_filename)

View file

@ -21,7 +21,7 @@ QE_NAMESPACE = os.environ.get('QE_K8S_NAMESPACE', 'quay-enterprise')
QE_CONFIG_SECRET = os.environ.get('QE_K8S_CONFIG_SECRET', 'quay-enterprise-config-secret') 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 # 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') QE_DEPLOYMENT_SELECTOR = os.environ.get('QE_DEPLOYMENT_SELECTOR', 'app')
class KubernetesAccessInterface: class KubernetesAccessInterface:
""" Implementation of the config provider that reads and writes configuration """ Implementation of the config provider that reads and writes configuration
@ -80,84 +80,89 @@ class KubernetesAccessInterface:
# try: # try:
# flask_file.save(buf) # flask_file.save(buf)
# except IOError as ioe: # except IOError as ioe:
# raise CannotWriteConfigException(str(ioe)) # raise Exception(str(ioe))
# #
# self._update_secret_file(relative_file_path, buf.getvalue()) # self._update_secret_file(relative_file_path, buf.getvalue())
# finally: # finally:
# buf.close() # buf.close()
def get_num_qe_pods(self): def save_file_as_secret(self, name, file_path):
# TODO: change to get /deployments?labelSelector={labelName}%3D{labelValue} with open(file_path) as f:
# right now just get the hardcoded deployment name value = f.read()
deployment_url = 'namespaces/%s/deployments/%s' % (QE_NAMESPACE, QE_DEPLOYMENT_SELECTOR) self._update_secret_file(name, value)
response = self._execute_k8s_api('GET', deployment_url, api_prefix='apis/extensions/v1beta1')
def get_qe_deployments(self):
pod_selector_url = 'namespaces/%s/deployments?labelSelector=quay-enterprise-component%%3D%s' % (QE_NAMESPACE, QE_DEPLOYMENT_SELECTOR)
response = self._execute_k8s_api('GET', pod_selector_url, api_prefix='apis/extensions/v1beta1')
if response.status_code != 200: if response.status_code != 200:
return None return None
return json.loads(response.text) return json.loads(response.text)
# def _assert_success(self, response): def _assert_success(self, response):
# if response.status_code != 200: if response.status_code != 200:
# logger.error('Kubernetes API call failed with response: %s => %s', response.status_code, logger.error('Kubernetes API call failed with response: %s => %s', response.status_code,
# response.text) response.text)
# raise CannotWriteConfigException('Kubernetes API call failed: %s' % response.text) raise Exception('Kubernetes API call failed: %s' % response.text)
# def _update_secret_file(self, relative_file_path, value=None): def _update_secret_file(self, relative_file_path, value=None):
# if '/' in relative_file_path: if '/' in relative_file_path:
# raise Exception('Expected path from get_volume_path, but found slashes') 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 # 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. # as an error, as it seems to be a common issue.
# namespace_url = 'namespaces/%s' % (QE_NAMESPACE) namespace_url = 'namespaces/%s' % (QE_NAMESPACE)
# response = self._execute_k8s_api('GET', namespace_url) response = self._execute_k8s_api('GET', namespace_url)
# if response.status_code // 100 != 2: if response.status_code // 100 != 2:
# msg = 'A Kubernetes namespace with name `%s` must be created to save config' % QE_NAMESPACE msg = 'A Kubernetes namespace with name `%s` must be created to save config' % QE_NAMESPACE
# raise CannotWriteConfigException(msg) raise Exception(msg)
#
# # Check if the secret exists. If not, then we create an empty secret and then update the file # Check if the secret exists. If not, then we create an empty secret and then update the file
# # inside. # inside.
# secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET) secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET)
# secret = self._lookup_secret() secret = self._lookup_secret()
# if secret is None: if secret is None:
# self._assert_success(self._execute_k8s_api('POST', secret_url, { self._assert_success(self._execute_k8s_api('POST', secret_url, {
# "kind": "Secret", "kind": "Secret",
# "apiVersion": "v1", "apiVersion": "v1",
# "metadata": { "metadata": {
# "name": QE_CONFIG_SECRET "name": QE_CONFIG_SECRET
# }, },
# "data": {} "data": {}
# })) }))
#
# # Update the secret to reflect the file change. # Update the secret to reflect the file change.
# secret['data'] = secret.get('data', {}) secret['data'] = secret.get('data', {})
#
# if value is not None: if value is not None:
# secret['data'][relative_file_path] = base64.b64encode(value) secret['data'][relative_file_path] = base64.b64encode(value)
# else: else:
# secret['data'].pop(relative_file_path) secret['data'].pop(relative_file_path)
#
# self._assert_success(self._execute_k8s_api('PUT', secret_url, secret)) self._assert_success(self._execute_k8s_api('PUT', secret_url, secret))
#
# # Wait until the local mounted copy of the secret has been updated, as # Wait until the local mounted copy of the secret has been updated, as
# # this is an eventual consistency operation, but the caller expects immediate # this is an eventual consistency operation, but the caller expects immediate
# # consistency. # consistency.
# while True:
# matching_files = set() # while True:
# for secret_filename, encoded_value in secret['data'].iteritems(): # matching_files = set()
# expected_value = base64.b64decode(encoded_value) # for secret_filename, encoded_value in secret['data'].iteritems():
# try: # expected_value = base64.b64decode(encoded_value)
# with self.get_volume_file(secret_filename) as f: # try:
# contents = f.read() # with self.get_volume_file(secret_filename) as f:
# # contents = f.read()
# if contents == expected_value: #
# matching_files.add(secret_filename) # if contents == expected_value:
# except IOError: # matching_files.add(secret_filename)
# continue #
# # except IOError:
# if matching_files == set(secret['data'].keys()): # continue
# break #
# # if matching_files == set(secret['data'].keys()):
# # Sleep for a second and then try again. # break
# time.sleep(1) #
# # Sleep for a second and then try again.
# time.sleep(1)
def _lookup_secret(self): def _lookup_secret(self):
secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET) secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET)
@ -180,3 +185,5 @@ class KubernetesAccessInterface:
request = Request(method, url, data=data, headers=headers) request = Request(method, url, data=data, headers=headers)
return session.send(request.prepare(), verify=False, timeout=2) return session.send(request.prepare(), verify=False, timeout=2)
kubernetes_access_instance = KubernetesAccessInterface()

View file

@ -0,0 +1,5 @@
<div class="co-m-loader co-an-fade-in-out">
<div class="co-m-loader-dot__one"></div>
<div class="co-m-loader-dot__two"></div>
<div class="co-m-loader-dot__three"></div>
</div>

View file

@ -0,0 +1,15 @@
const templateUrl = require('./cor-loader.html');
angular.module('quay-config')
.directive('corLoader', function() {
var directiveDefinitionObject = {
templateUrl,
replace: true,
restrict: 'C',
scope: {
},
controller: function($rootScope, $scope, $element) {
}
};
return directiveDefinitionObject;
});

View file

@ -18,7 +18,15 @@
</div> </div>
<!-- Body --> <!-- Body -->
<div class="modal-body"> <div class="modal-body">
<span>gotteeem</span> <div class="cor-loader" ng-if="$ctrl.loading"></div>
The following deployments will be affected:
<li ng-repeat="deployment in $ctrl.deployments">
name: {{deployment}}
</li>
<button class="btn btn-lg" ng-click="$ctrl.deployConfiguration()">
<i class="far fa-paper-plane" style="margin-right: 10px;"></i>
Populate configuration to deployments
</button>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
</div> </div>

View file

@ -7,14 +7,30 @@ const templateUrl = require('./kube-deploy-modal.component.html');
}) })
export class KubeDeployModalComponent { export class KubeDeployModalComponent {
private loading: boolean = true; private loading: boolean = true;
private deployments: any; private deployments: [string];
constructor(@Inject('ApiService') private ApiService) { constructor(@Inject('ApiService') private ApiService) {
ApiService.scGetNumDeployments().then(resp => { ApiService.scGetNumDeployments().then(resp => {
console.log(resp) console.log(resp)
this.deployments = resp.items.map(dep => dep.metadata.name);
console.log(this.deployments);
this.loading = false; this.loading = false;
}).catch(err => { }).catch(err => {
this.loading = false; this.loading = false;
}) })
} }
deployConfiguration(): void {
console.log('calling deploy conf')
this.ApiService.scDeployConfiguration().then(resp => {
console.log('resp from deploy was', resp)
this.ApiService.scCycleQEDeployment().then(() => {
console.log('merp')
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
})
}
} }

View file

@ -26,6 +26,7 @@ rules:
- deployments - deployments
verbs: verbs:
- get - get
- list
- put - put
- patch - patch
- update - update