Add more volume file operations and cleanup k8s provider code
This commit is contained in:
parent
29d6abddb5
commit
f1c9965edf
3 changed files with 49 additions and 25 deletions
|
@ -88,6 +88,16 @@ class BaseProvider(object):
|
||||||
""" Writes the given contents to the config override volumne, with the given filename. """
|
""" Writes the given contents to the config override volumne, with the given filename. """
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def remove_volume_file(self, filename):
|
||||||
|
""" Removes the config override volume file with the given filename. """
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def list_volume_directory(self, path):
|
||||||
|
""" Returns a list of strings representing the names of the files found in the config override
|
||||||
|
directory under the given path. If the path doesn't exist, returns None.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def save_volume_file(self, filename, flask_file):
|
def save_volume_file(self, filename, flask_file):
|
||||||
""" Saves the given flask file to the config override volume, with the given
|
""" Saves the given flask file to the config override volume, with the given
|
||||||
filename.
|
filename.
|
||||||
|
|
|
@ -59,6 +59,17 @@ class FileConfigProvider(BaseProvider):
|
||||||
|
|
||||||
return filepath
|
return filepath
|
||||||
|
|
||||||
|
def remove_volume_file(self, filename):
|
||||||
|
filepath = os.path.join(self.config_volume, filename)
|
||||||
|
os.remove(filepath)
|
||||||
|
|
||||||
|
def list_volume_directory(self, path):
|
||||||
|
dirpath = os.path.join(self.config_volume, path)
|
||||||
|
if not os.path.exists(dirpath):
|
||||||
|
return None
|
||||||
|
|
||||||
|
return os.listdir(dirpath)
|
||||||
|
|
||||||
def save_volume_file(self, filename, flask_file):
|
def save_volume_file(self, filename, flask_file):
|
||||||
filepath = os.path.join(self.config_volume, filename)
|
filepath = os.path.join(self.config_volume, filename)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -55,53 +55,56 @@ class KubernetesConfigProvider(FileConfigProvider):
|
||||||
except IOError as ioe:
|
except IOError as ioe:
|
||||||
raise CannotWriteConfigException(str(ioe))
|
raise CannotWriteConfigException(str(ioe))
|
||||||
|
|
||||||
def save_volume_file(self, filename, flask_file):
|
def remove_volume_file(self, filename):
|
||||||
filepath = super(KubernetesConfigProvider, self).save_volume_file(filename, flask_file)
|
super(KubernetesConfigProvider, self).remove_volume_file(filename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(filepath, 'r') as f:
|
self._update_secret_file(filename, None)
|
||||||
self._update_secret_file(filename, f.read())
|
|
||||||
except IOError as ioe:
|
except IOError as ioe:
|
||||||
raise CannotWriteConfigException(str(ioe))
|
raise CannotWriteConfigException(str(ioe))
|
||||||
|
|
||||||
|
def save_volume_file(self, filename, flask_file):
|
||||||
|
filepath = super(KubernetesConfigProvider, self).save_volume_file(filename, flask_file)
|
||||||
|
with open(filepath, 'r') as f:
|
||||||
|
self.write_volume_file(filename, f.read())
|
||||||
|
|
||||||
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 CannotWriteConfigException('Kubernetes API call failed: %s' % response.text)
|
||||||
|
|
||||||
def _update_secret_file(self, filename, value):
|
def _update_secret_file(self, filename, value=None):
|
||||||
# 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 != 200:
|
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 CannotWriteConfigException(msg)
|
||||||
|
|
||||||
# Save the secret to the namespace.
|
# Check if the secret exists. If not, then we create an empty secret and then update the file
|
||||||
secret_data = {}
|
# inside.
|
||||||
secret_data[filename] = base64.b64encode(value)
|
secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET)
|
||||||
|
secret = self._lookup_secret()
|
||||||
data = {
|
if secret is None:
|
||||||
|
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": secret_data
|
"data": {}
|
||||||
}
|
}))
|
||||||
|
|
||||||
secret_url = 'namespaces/%s/secrets/%s' % (QE_NAMESPACE, QE_CONFIG_SECRET)
|
# Update the secret to reflect the file change.
|
||||||
secret = self._lookup_secret()
|
secret['data'] = secret.get('data', {})
|
||||||
if not secret:
|
|
||||||
self._assert_success(self._execute_k8s_api('POST', secret_url, data))
|
|
||||||
return
|
|
||||||
|
|
||||||
if not 'data' in secret:
|
|
||||||
secret['data'] = {}
|
|
||||||
|
|
||||||
|
if value is not None:
|
||||||
secret['data'][filename] = base64.b64encode(value)
|
secret['data'][filename] = base64.b64encode(value)
|
||||||
|
else:
|
||||||
|
secret['data'].pop(filename)
|
||||||
|
|
||||||
self._assert_success(self._execute_k8s_api('PUT', secret_url, secret))
|
self._assert_success(self._execute_k8s_api('PUT', secret_url, secret))
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue