Add ConfigMap support for k8s. #714

Merged
telackey merged 10 commits from telackey/k8s_001 into main 2024-01-31 05:09:49 +00:00
6 changed files with 40 additions and 31 deletions
Showing only changes of commit a9b476c326 - Show all commits

View File

@ -33,6 +33,7 @@ from stack_orchestrator.base import get_npm_registry_url
# TODO: find a place for this
# epilog="Config provided either in .env or settings.ini or env vars: CERC_REPO_BASE_DIR (defaults to ~/cerc)"
def make_container_build_env(dev_root_path: str,
container_build_dir: str,
debug: bool,
@ -122,6 +123,7 @@ def process_container(stack: str,
else:
print("Skipped")
@click.command()
@click.option('--include', help="only build these containers")
@click.option('--exclude', help="don\'t build these containers")

View File

@ -54,7 +54,7 @@ def _get_ports(stack):
def _get_named_volumes(stack):
# Parse the compose files looking for named volumes
named_volumes = {
named_volumes = {
"rw": [],
"ro": []
}
@ -123,9 +123,9 @@ def _fixup_pod_file(pod, spec, compose_dir):
_create_bind_dir_if_relative(volume, volume_spec, compose_dir)
new_volume_spec = {"driver": "local",
"driver_opts": {
"type": "none",
"device": volume_spec_fixedup,
"o": "bind"
"type": "none",
"device": volume_spec_fixedup,
"o": "bind"
}
}
pod["volumes"][volume] = new_volume_spec

View File

@ -34,10 +34,10 @@ def remote_tag_for_image(image: str, remote_repo_url: str):
major_parts = image.split("/", 2)
org = None
if 2 == len(major_parts):
org = major_parts[0]
image_name_with_version = major_parts[1]
org = major_parts[0]
image_name_with_version = major_parts[1]
else:
image_name_with_version = major_parts[0]
image_name_with_version = major_parts[0]
(image_name, image_version) = image_name_with_version.split(":")
if image_version == "local":
return f"{remote_repo_url}/{image_name}:deploy"

View File

@ -115,9 +115,9 @@ class ClusterInfo:
for service_name in services:
service_info = services[service_name]
if "ports" in service_info:
port = int(service_info["ports"][0])
if opts.o.debug:
print(f"service port: {port}")
port = int(service_info["ports"][0])
if opts.o.debug:
print(f"service port: {port}")
service = client.V1Service(
metadata=client.V1ObjectMeta(name=f"{self.app_name}-service"),
spec=client.V1ServiceSpec(
@ -133,21 +133,27 @@ class ClusterInfo:
def get_pvcs(self):
result = []
volumes = named_volumes_from_pod_files(self.parsed_pod_yaml_map)
spec_volumes = self.spec.get_volumes()
named_volumes = named_volumes_from_pod_files(self.parsed_pod_yaml_map)
if opts.o.debug:
print(f"Volumes: {volumes}")
for volume_name in volumes:
print(f"Spec Volumes: {spec_volumes}")
print(f"Named Volumes: {named_volumes}")
for volume_name in spec_volumes:
if volume_name not in named_volumes:
if opts.o.debug:
print(f"{volume_name} not in pod files")
continue
spec = client.V1PersistentVolumeClaimSpec(
access_modes=["ReadWriteOnce"],
storage_class_name="manual",
resources=client.V1ResourceRequirements(
requests={"storage": "2Gi"}
),
volume_name=volume_name
volume_name=f"{self.app_name}-{volume_name}"
)
pvc = client.V1PersistentVolumeClaim(
metadata=client.V1ObjectMeta(name=volume_name,
labels={"volume-label": volume_name}),
metadata=client.V1ObjectMeta(name=f"{self.app_name}-{volume_name}",
labels={"volume-label": f"{self.app_name}-{volume_name}"}),
spec=spec,
)
result.append(pvc)
@ -172,7 +178,7 @@ class ClusterInfo:
data[f] = open(full_path, 'rt').read()
spec = client.V1ConfigMap(
metadata=client.V1ObjectMeta(name=cfg_map_name,
metadata=client.V1ObjectMeta(name=f"{self.app_name}-{cfg_map_name}",
labels={"configmap-label": cfg_map_name}),
data=data
)
@ -195,8 +201,8 @@ class ClusterInfo:
host_path=client.V1HostPathVolumeSource(path=get_node_pv_mount_path(volume_name))
)
pv = client.V1PersistentVolume(
metadata=client.V1ObjectMeta(name=volume_name,
labels={"volume-label": volume_name}),
metadata=client.V1ObjectMeta(name=f"{self.app_name}-{volume_name}",
labels={"volume-label": f"{self.app_name}-{volume_name}"}),
spec=spec,
)
result.append(pv)

View File

@ -1,5 +1,4 @@
# Copyright © 2023 Vulcanize
import sys
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
@ -13,6 +12,8 @@ import sys
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
import sys
from datetime import datetime, timezone
from pathlib import Path
@ -163,7 +164,6 @@ class K8sDeployer(Deployer):
if opts.o.debug:
print(f"No ingress configured")
def down(self, timeout, volumes):
self.connect_api()
# Delete the k8s objects
@ -234,14 +234,14 @@ class K8sDeployer(Deployer):
if not self.is_kind():
ingress: client.V1Ingress = self.cluster_info.get_ingress()
if ingress:
if opts.o.debug:
print(f"Deleting this ingress: {ingress}")
try:
self.networking_api.delete_namespaced_ingress(
name=ingress.metadata.name, namespace=self.k8s_namespace
)
except client.exceptions.ApiException as e:
_check_delete_exception(e)
if opts.o.debug:
print(f"Deleting this ingress: {ingress}")
try:
self.networking_api.delete_namespaced_ingress(
name=ingress.metadata.name, namespace=self.k8s_namespace
)
except client.exceptions.ApiException as e:
_check_delete_exception(e)
else:
if opts.o.debug:
print(f"No ingress to delete")

View File

@ -102,7 +102,8 @@ def volume_mounts_for_service(parsed_pod_files, service):
volume_name = parts[0]
mount_path = parts[1]
mount_options = parts[2] if len(parts) == 3 else None
volume_device = client.V1VolumeMount(mount_path=mount_path, name=volume_name, read_only="ro" == mount_options)
volume_device = client.V1VolumeMount(
mount_path=mount_path, name=volume_name, read_only="ro" == mount_options)
result.append(volume_device)
return result
@ -166,7 +167,7 @@ def _generate_kind_mounts(parsed_pod_files, deployment_dir):
volume_definitions.append(
f" - hostPath: {_make_absolute_host_path(volume_host_path_map[volume_name], deployment_dir)}\n"
f" containerPath: {get_node_pv_mount_path(volume_name)}"
)
)
return (
"" if len(volume_definitions) == 0 else (
" extraMounts:\n"