2018-02-14 22:39:42 +01:00
|
|
|
"""OpenShift plugin for integration tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from . import (
|
2018-02-14 22:39:42 +01:00
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
2019-03-01 03:25:49 +01:00
|
|
|
CloudEnvironmentConfig,
|
2018-02-14 22:39:42 +01:00
|
|
|
)
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
from ..io import (
|
|
|
|
read_text_file,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2018-02-14 22:39:42 +01:00
|
|
|
display,
|
|
|
|
)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
from ..containers import (
|
|
|
|
run_support_container,
|
|
|
|
wait_for_file,
|
2018-02-14 22:39:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class OpenShiftCloudProvider(CloudProvider):
|
|
|
|
"""OpenShift cloud provider plugin. Sets up cloud resources before delegation."""
|
|
|
|
DOCKER_CONTAINER_NAME = 'openshift-origin'
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
|
|
|
super(OpenShiftCloudProvider, self).__init__(args, config_extension='.kubeconfig')
|
|
|
|
|
|
|
|
# The image must be pinned to a specific version to guarantee CI passes with the version used.
|
2018-11-16 13:44:59 +01:00
|
|
|
self.image = 'openshift/origin:v3.9.0'
|
2018-02-14 22:39:42 +01:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
self.uses_docker = True
|
|
|
|
self.uses_config = True
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Setup the cloud resource before delegation and register a cleanup callback."""
|
|
|
|
super(OpenShiftCloudProvider, self).setup()
|
|
|
|
|
|
|
|
if self._use_static_config():
|
|
|
|
self._setup_static()
|
|
|
|
else:
|
|
|
|
self._setup_dynamic()
|
|
|
|
|
|
|
|
def _setup_static(self):
|
|
|
|
"""Configure OpenShift tests for use with static configuration."""
|
2020-02-04 20:21:53 +01:00
|
|
|
config = read_text_file(self.config_static_path)
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
match = re.search(r'^ *server: (?P<server>.*)$', config, flags=re.MULTILINE)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
if not match:
|
|
|
|
display.warning('Could not find OpenShift endpoint in kubeconfig.')
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
def _setup_dynamic(self):
|
|
|
|
"""Create a OpenShift container using docker."""
|
|
|
|
port = 8443
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
ports = [
|
|
|
|
port,
|
|
|
|
]
|
|
|
|
|
|
|
|
cmd = ['start', 'master', '--listen', 'https://0.0.0.0:%d' % port]
|
|
|
|
|
|
|
|
descriptor = run_support_container(
|
|
|
|
self.args,
|
|
|
|
self.platform,
|
|
|
|
self.image,
|
|
|
|
self.DOCKER_CONTAINER_NAME,
|
|
|
|
ports,
|
|
|
|
allow_existing=True,
|
|
|
|
cleanup=True,
|
|
|
|
cmd=cmd,
|
|
|
|
)
|
|
|
|
|
|
|
|
descriptor.register(self.args)
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
if self.args.explain:
|
|
|
|
config = '# Unknown'
|
|
|
|
else:
|
2021-02-02 20:47:38 +01:00
|
|
|
config = self._get_config(self.DOCKER_CONTAINER_NAME, 'https://%s:%s/' % (self.DOCKER_CONTAINER_NAME, port))
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
self._write_config(config)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
def _get_config(self, container_name, server):
|
2018-02-14 22:39:42 +01:00
|
|
|
"""Get OpenShift config from container.
|
2021-02-02 20:47:38 +01:00
|
|
|
:type container_name: str
|
2018-02-14 22:39:42 +01:00
|
|
|
:type server: str
|
|
|
|
:rtype: dict[str, str]
|
|
|
|
"""
|
2021-02-02 20:47:38 +01:00
|
|
|
stdout = wait_for_file(self.args, container_name, '/var/lib/origin/openshift.local.config/master/admin.kubeconfig', sleep=10, tries=30)
|
2018-02-14 22:39:42 +01:00
|
|
|
|
|
|
|
config = stdout
|
|
|
|
config = re.sub(r'^( *)certificate-authority-data: .*$', r'\1insecure-skip-tls-verify: true', config, flags=re.MULTILINE)
|
|
|
|
config = re.sub(r'^( *)server: .*$', r'\1server: %s' % server, config, flags=re.MULTILINE)
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
class OpenShiftCloudEnvironment(CloudEnvironment):
|
|
|
|
"""OpenShift cloud environment plugin. Updates integration test environment after delegation."""
|
2019-03-01 03:25:49 +01:00
|
|
|
def get_environment_config(self):
|
2018-02-14 22:39:42 +01:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
:rtype: CloudEnvironmentConfig
|
2018-02-14 22:39:42 +01:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
env_vars = dict(
|
2018-02-14 22:39:42 +01:00
|
|
|
K8S_AUTH_KUBECONFIG=self.config_path,
|
|
|
|
)
|
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
env_vars=env_vars,
|
|
|
|
)
|