def update_config_map(auth_config_map: V1ConfigMap, data: Dict): if "mapRoles" in data: auth_config_map.data["mapRoles"] = yaml.dump(data["mapRoles"], default_flow_style=False) if "mapUsers" in data: auth_config_map.data["mapUsers"] = yaml.dump(data["mapUsers"], default_flow_style=False) auth_config_map.metadata.namespace = None auth_config_map.metadata.uid = None auth_config_map.metadata.annotations = None auth_config_map.metadata.resource_version = None return auth_config_map
def __init__(self, api_client, name, variables=[], namespace='default'): self.api = client.CoreV1Api(api_client) self.name = name self.namespace = namespace self._variables = variables context = {'name': name, 'variables': variables} config = yaml.safe_load( self.generate_template('configmap.yaml.j2', context)) V1ConfigMap.__init__(self, api_version=config['apiVersion'], kind=config['kind'], metadata=config['metadata'], data=config['data'])
def test_kube_config_store_2(replace_mock, read_mock, incluster_config_mock, config_mock): import yaml existing = V1ConfigMap() existing.data = { '.plumber.checkpoint.yml': yaml.dump({'name': 'I have no name!'}) } incluster_config_mock.return_value = None config_mock.return_value = None read_mock.return_value = existing replace_mock.return_value = None try: os.environ['KUBERNETES_SERVICE_HOST'] = '127.0.0.1' from plumber.io import KubeConfigStore store = KubeConfigStore() store.configure({}) data = store.get_data() assert len(data) == 1 assert data['name'] == 'I have no name!' data = {'name': 'i have a name now, but i forgot'} store.save_data(data) read_mock.assert_called() read_mock.assert_called_with('plumber-checkpoint', 'default', export=True) replace_mock.assert_called_once() finally: del os.environ['KUBERNETES_SERVICE_HOST']
def define_configmap(self, data): """This returns a k8s configmap using the data from the new-workflow POST. """ with start_action(action_type="define_configmap"): ni_cmd = data["command"] idkey = list_digest(ni_cmd) cm_name = "command.{}.json".format(idkey) k8s_vol = V1Volume( name="noninteractive-command", config_map=V1ConfigMapVolumeSource(name=cm_name), ) k8s_mt = V1VolumeMount( name="noninteractive-command", mount_path=("/opt/lsst/software/jupyterlab/" + "noninteractive/command/"), read_only=True, ) self.cmd_vol = k8s_vol self.cmd_mt = k8s_mt # Now the configmap cm_data = {} cm_data.update(data) del cm_data["image"] del cm_data["size"] jd = json.dumps(data, sort_keys=True, indent=4) k8s_configmap = V1ConfigMap( metadata=V1ObjectMeta(name=cm_name), data={"command.json": json.dumps(data)}, ) self.log.debug("Created configmap '{}': {}".format(cm_name, jd)) self.cfg_map = k8s_configmap
def test_config_map_spec_source(self, mock_core_client): name = "hellodolly" namespace = "ns1" jinja_d = {"biz": "{{ buzz }}"} mock_core_client.read_namespaced_config_map.return_value = V1ConfigMap( metadata=V1ObjectMeta(name=name), data={name: yaml.dump(jinja_d)}) c = ConfigMapSpecSource(name, namespace) assert {"biz": "foo"} == c.get(template_args={"buzz": "foo"}) mock_core_client.read_namespaced_config_map.assert_called_once_with( name=name, namespace=namespace)
def create_config_map(self, namespace: str, name: str, data: dict): not_empty(namespace) not_empty(name) not_none(data) self.core_api.create_namespaced_config_map( namespace=namespace, body=V1ConfigMap( data=data, metadata=V1ObjectMeta(name=name, namespace=namespace), ), )
async def _patch_key(self, key: str, value: Optional[str]) -> Optional[V1ObjectMeta]: body = V1ConfigMap(api_version='v1', kind='ConfigMap', data={ key: value, }) configmap = await asyncio.to_thread( # type: ignore self._v1.patch_namespaced_config_map, name=self.name, namespace=self.namespace, body=body) return configmap.metadata
def create_config_maps(self, file_names): """Create Kubernetes ConfigMaps for the input files to the simulation. """ for file_name, id_ in file_names: with open(file_name, 'r') as fd: data = fd.read() metadata = V1ObjectMeta(name=id_, namespace='default') configmap = V1ConfigMap(api_version='v1', kind='ConfigMap', data={os.path.basename(file_name): data}, metadata=metadata) self.core_v1.create_namespaced_config_map(namespace='default', body=configmap)
def create_prometheus_adapter_configmap( kube_client: KubeClient, config: PrometheusAdapterConfig) -> None: kube_client.core.create_namespaced_config_map( namespace=PROMETHEUS_ADAPTER_CONFIGMAP_NAMESPACE, body=V1ConfigMap( metadata=V1ObjectMeta(name=PROMETHEUS_ADAPTER_CONFIGMAP_NAME), data={ PROMETHEUS_ADAPTER_CONFIGMAP_FILENAME: yaml.dump(config, default_flow_style=False, explicit_start=True) }, ), )
def save_data(self, content, info=None): try: configmap_body = V1ConfigMap( data={self.file_placeholder: self.parser.dump(content)}, metadata={ 'name': self.configmap_name, 'namespace': self.namespace }) if self._cm_exists(): self.core_api.replace_namespaced_config_map( self.configmap_name, self.namespace, configmap_body) else: self.core_api.create_namespaced_config_map( self.namespace, configmap_body) except PlumberError: raise except Exception as e: raise IOError('Could now write data', e)
def upload(self, locustfile: os.PathLike) -> None: logging.info("Creating ConfigMap %r...", CONFIGMAP_NAME) config_map = V1ConfigMap( data={CONFIGMAP_KEY: Path(locustfile).read_text()}, metadata={ "name": CONFIGMAP_NAME, "labels": self.labels }, ) try: logging.debug("Creating ConfigMap %r...", CONFIGMAP_NAME) CoreV1Api().create_namespaced_config_map(namespace=self.namespace, body=config_map) logging.debug("ConfigMap %r created.", CONFIGMAP_NAME) except ApiException as err: logging.error("Failed to create ConfigMap %r: %s", CONFIGMAP_NAME, err.reason) raise
def write_config_map(self, config_map_name, key_name, data): cm = V1ConfigMap() cm.metadata = V1ObjectMeta(name=config_map_name, labels={'app': 'jupyterhub'}) cm.data = {key_name: yaml.dump(data, default_flow_style=False)} try: api_response = self.api_client.replace_namespaced_config_map( config_map_name, self.namespace, cm) except ApiException as e: if e.status == 404: try: api_response = self.api_client.create_namespaced_config_map( self.namespace, cm) except ApiException as e: _LOGGER.error( "Exception when calling CoreV1Api->create_namespaced_config_map: %s\n" % e) else: raise
def __init__(self) -> None: self.config_map = V1ConfigMap( metadata=V1ObjectMeta( name="postgres-init", ), data={ "connections.sql": dedent( """ ALTER SYSTEM SET max_connections = 5000; """ ), "schemas.sql": dedent( """ CREATE SCHEMA consensus; CREATE SCHEMA catalog; CREATE SCHEMA storage; """ ), }, )
def to_aws_auth(mappings: typing.List[Mapping]) -> V1ConfigMap: """ Converts the given list of mappings to a AWS auth ConfigMap. :param mappings: list of mappings :returns: a ConfigMap containing the mappings in AWS auth format. """ return V1ConfigMap( metadata={"name": "aws-auth"}, data={ "mapUsers": yaml.dump([ m.to_aws_auth_entry() for m in mappings if m.is_iam_user_mapping ]), "mapRoles": yaml.dump([ m.to_aws_auth_entry() for m in mappings if m.is_iam_role_mapping ]), }, )
def create_hub_pod(kube_client, kube_ns, pod_name="hub", ssl=False): config_map_name = pod_name + "-config" secret_name = pod_name + "-secret" with open(jupyterhub_config_py) as f: config = f.read() print("Creating hub config map") ensure_not_exists(kube_client, kube_ns, config_map_name, "config_map") config_map_manifest = V1ConfigMap(metadata={"name": config_map_name}, data={"jupyterhub_config.py": config}) config_map = kube_client.create_namespaced_config_map( body=config_map_manifest, namespace=kube_ns, ) print("Creating hub pod {}".format(pod_name)) ensure_not_exists(kube_client, kube_ns, pod_name, "pod") volumes = [{"name": "config", "configMap": {"name": config_map_name}}] volume_mounts = [{ "mountPath": "/etc/jupyterhub/jupyterhub_config.py", "subPath": "jupyterhub_config.py", "name": "config", }] if ssl: volumes.append({ "name": "secret", "secret": { "secretName": secret_name } }) volume_mounts.append({ "mountPath": "/etc/jupyterhub/secret", "name": "secret", }) pod_manifest = V1Pod( metadata={"name": pod_name}, spec=V1PodSpec( volumes=volumes, containers=[{ "image": "jupyterhub/jupyterhub:1.1", "name": "hub", "volumeMounts": volume_mounts, "args": [ "jupyterhub", "-f", "/etc/jupyterhub/jupyterhub_config.py", ], "env": [{ "name": "PYTHONUNBUFFERED", "value": "1" }], }], termination_grace_period_seconds=0, ), ) pod = kube_client.create_namespaced_pod(body=pod_manifest, namespace=kube_ns) return wait_for_pod(kube_client, kube_ns, pod_name)
# 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 unittest.mock import MagicMock from kubernetes.client import V1ConfigMap import pytest from nauta.config import NautaPlatformConfig fake_cm = V1ConfigMap( data={ 'registry': '127.0.0.1:30303', 'image.activity-proxy': 'activity-proxy:dev', 'image.tensorflow': 'nauta/tensorflow/1.9.0py3/haswell/base:1.9.0-py3' }) @pytest.fixture def nauta_platform_config_mocked(): # noinspection PyTypeChecker config = NautaPlatformConfig(k8s_api_client=MagicMock()) return config def test_incluster_init(mocker): mocker.patch('nauta.config.config')
def create_hub_pod(kube_client, kube_ns, pod_name="hub", ssl=False): config_map_name = pod_name + "-config" secret_name = pod_name + "-secret" with open(jupyterhub_config_py) as f: config = f.read() config_map_manifest = V1ConfigMap( metadata={"name": config_map_name}, data={"jupyterhub_config.py": config} ) config_map = create_resource( kube_client, kube_ns, "config_map", config_map_manifest, delete_first=True, ) volumes = [{"name": "config", "configMap": {"name": config_map_name}}] volume_mounts = [ { "mountPath": "/etc/jupyterhub/jupyterhub_config.py", "subPath": "jupyterhub_config.py", "name": "config", } ] if ssl: volumes.append({"name": "secret", "secret": {"secretName": secret_name}}) volume_mounts.append( { "mountPath": "/etc/jupyterhub/secret", "name": "secret", } ) pod_manifest = V1Pod( metadata={ "name": pod_name, "labels": {"component": "hub", "hub-name": pod_name}, }, spec=V1PodSpec( volumes=volumes, containers=[ { "image": "jupyterhub/jupyterhub:1.3", "name": "hub", "volumeMounts": volume_mounts, "args": [ "jupyterhub", "-f", "/etc/jupyterhub/jupyterhub_config.py", ], "env": [{"name": "PYTHONUNBUFFERED", "value": "1"}], "readinessProbe": { "tcpSocket": { "port": 8081, }, "periodSeconds": 1, }, } ], ), ) pod = create_resource(kube_client, kube_ns, "pod", pod_manifest) return wait_for_pod(kube_client, kube_ns, pod_name)