def set_volume(self, volume): """Erstelle ein Volume Object""" self.config.activ_modul = "volume" if self.config.has_option(volume, "label"): label = self.config.get_object(volume, "label").split(",") if len(label) < 2: label = ["volume", label[0]] self.config.set(volume, "label", ",".join(label)) label = {label[0]: label[1]} else: label = None k8_drive = self.k8_drives.get(self.config.get_object(volume, "drive"), None) if k8_drive is None: self.config.config_status = ErrorHandling.print_error_config(self.config, self.language["vol01"], locals(), bold=True) return None drive = k8_drive(self.config, volume) if drive is None: self.config.config_status = ErrorHandling.print_error_config(self.config, self.language["vol02"], locals(), bold=True) return None metadata = K8Meta(annotations=drive["metadata"].annotations, name=self.config.get_object(volume, "name"), labels=label) spec = K8Spec(storage_class_name=self.config.get_object(volume, "class"), capacity={"storage": self.config.get_object(volume, "capacity")}, access_modes=[self.config.get_object(volume, "accessmodes")], nfs=drive["spec"].nfs) if metadata.name is None or spec.storage_class_name is None or spec.capacity["storage"] is None or spec.access_modes is None: self.config.config_status = ErrorHandling.print_error_config(self.config, self.language["vol03"], locals(), bold=True) return None return K8Volume(metadata=metadata, spec=spec)
def set_deploy(self, deploy): """Erstelt ein Deployment Object""" deploy_index = deploy.replace('deploy', '') metadata = None if not self.config.has_option(deploy, "name"): self.config.set(deploy, "name", "%s-%s" % (self.config.project_name, deploy)) metadata = K8Meta(name=self.config.get_object(deploy, "name")) if not self.config.has_option(deploy, "replicas"): self.config.set(deploy, "replicas", "1") replicas = self.config.get_object(deploy, "replicas", integer=True) template = self.set_template(deploy_index) if template is None: self.config.config_status = ErrorHandling.print_error_config(self.config, self.language["dep01"], locals(), bold=True) return None label = self.config.get_object("template%s" % deploy_index, "label", modul="template") label = label.split(",") selector = K8Selector(match_labels={label[0]: label[1]}) spec = K8Spec(replicas=replicas, selector=selector, template=template) return K8Deployment(metadata=metadata, spec=spec)
def set_template(self, deploy_index): """Erstelt ein Template Object""" template = "template%s" % deploy_index self.config.activ_modul = 'template' spec = {"containers": None} label = self.config.get_object(template, "label") if label is not None: label = label.split(",") if len(label) == 2: label = {label[0]: label[1]} elif len(label) == 1: self.config.set(template, "label", "app,%s" % label[0]) label = {"app": label[0]} else: self.config.config_status = ErrorHandling.print_error_config( self.config, self.language["tem05"], locals(), bold=True) return None else: self.config.config_status = ErrorHandling.print_error_config( self.config, self.language["tem01"], locals(), bold=True) return None if self.config.has_section("namespace%s" % deploy_index): namespace = self.config.get_object("namespace%s" % deploy_index, "name", modul="namespace") else: namespace = self.config.get_object("namespace1", "name", modul="namespace") if not self.config.has_option(template, "name"): self.config.set( template, "name", "%s-%s" % (self.config.project_name, "template%s" % deploy_index)) metadata = K8Meta(name=self.config.get_object(template, "name"), namespace=namespace, labels=label) volumes = self.set_template_volumes(template) if self.config.has_option(template, "restart"): restart = self.config.get_object(template, "restart") else: restart = None container = self.set_container(deploy_index) if container is None: self.config.config_status = ErrorHandling.print_error_config( self.config, self.language["tem02"], locals(), bold=True) return None spec = K8spec(containers=container, volumes=volumes, restart_policy=restart) return K8Template(metadata=metadata, spec=spec)
def set_metadata(self, secret): """Ermitellt den Namen des Secret Objectes""" metadata = {"name": None} if not self.config.has_option(secret, "name"): self.config.set(secret, "name", "%s-%s" % (self.config.project_name, secret)) metadata = K8Meta(name=self.config.get_object(secret, "name")) return metadata
def set_claim(self, claim): """Erstelle ein Claim Object""" self.config.activ_modul = "claim" claim_options = {} claim_options.update(self.k8pg_option_to_section["claim"]) del claim_options["__section__"] volume = claim.replace("claim", "volume") spec = { "storageClassName": None, "accessModes": None, "resources": { "requests": { "storage": None } } } for option in claim_options.keys(): self.config.copy_option_to_option(volume, claim, claim_options[option][3], to_option=option, add=claim_options[option][4], iteral=False) option_value = self.config.get_object(claim, option) if option_value is None: self.config.config_status = ErrorHandling.print_error_config( self.config, "%s (%s)" % (self.language[claim_options[option][5]], claim), locals(), bold=True) return None elif option_value.startswith("@v"): self.config.copy_option_to_option(option_value[1:], claim, claim_options[option][3], to_option=option, add=claim_options[option][4], change=True, iteral=False) metadata = K8Meta(name=self.config.get_object(claim, "name")) resource = K8Resource( requests={"storage": self.config.get_object(claim, "requests")}) spec = K8Spec( access_modes=[self.config.get_object(claim, "accessmodes")], resources=resource, storage_class_name=self.config.get_object(claim, "volume_class")) return K8Claim(metadata=metadata, spec=spec)
def set_service(self, service): """Erstelt ein Service Object""" index = service.replace("service", "") self.config.activ_modul = "service" template_index = "template%s" % index if not self.config.has_option(service, "name"): self.config.set(service, "name", "%s-%s" % (self.config.project_name, service)) name = self.config.get_object(service, "name") if not self.config.has_option(service, template_index) and self.config.has_option(template_index, "label"): self.config.copy_option_to_option(template_index, service, "label", to_option="template") if self.config.has_option(service, "template"): template = self.config.get_object(service, "template") if template.startswith("@t"): self.config.copy_option_to_option(template[1:], service, "label", to_option="template", iteral=False, change=True) template = self.config.get_object(service, "template") template = template.split(",") if not len(template) == 2: self.config.config_status = ErrorHandling.print_error_config(self.config, "%s (%s)" % (self.language["ser06"], service), locals(), bold=True) else: self.config.config_status = ErrorHandling.print_error_config(self.config, "%s (%s)" % (self.language["ser01"], service), locals(), bold=True) return None if self.config.has_option(service, "extern_ips"): external_i_ps = self.config.get_object(service, "extern_ips").split(",") else: external_i_ps = None ports = [] for port in self.config.config_iteral(service, "port", no="_"): service_port = self.set_service_port(service, port, template_index) if service_port is None: self.config.config_status = ErrorHandling.print_error("%s (%s)" % (self.language["ser02"], service)) return None ports.append(self.set_service_port(service, port, template_index)) if not ports: self.config.config_status = ErrorHandling.print_error_config(self.config, "%s (%s)" % (self.language["ser03"], name), locals(), bold=True) ports = self.config.config_iteral(service, "port", no="_") if len(ports) == 0: self.config.config_status = ErrorHandling.print_error("%s (%s)" % (self.language["ser01"], service)) return None metadata = K8Meta(name=name, labels={"name": name}) spec = K8Spec(selector={template[0]: template[1]}, external_i_ps=external_i_ps, ports=ports) return K8Service(metadata=metadata, spec=spec)
def set_volume_nfs(config, volume): """Kubernetes Object Einträge für NFS Server""" metadata = K8Meta(annotations={ "volume.beta.kubernetes.io/mount-options": "nolock,local_lock=none" }) server = config.get_object(volume, "server") path = config.get_object(volume, "path") if server is None or path is None: ErrorHandling.print_error("no server info", locals()) return None device = K8NFSDivice(server=config.get_object(volume, "server"), path=config.get_object(volume, "path")) spec = K8Spec(nfs=device) return {"metadata": metadata, "spec": spec}
def set_namespace(self): """Erstelt das NameSpace Object""" self.config.activ_modul = "namespace" namespace = False for item in self.config.sections(): if not item.startswith("volume"): namespace = True if namespace: if not self.config.has_section("namespace1"): self.config.add_section("namespace1") self.config.set("namespace1", "name", self.config.project_name) metadata = K8Meta( name=self.config.get_object("namespace1", "name")) else: return None return [K8NameSpace(metadata=metadata)]
def set_ingress(self): """Erstelt Ingress Object""" self.config.activ_modul = "ingress" metadata = {"name": None} spec = {} if not self.config.has_section("ingress1"): return None if not self.config.has_option("ingress1", "name"): if self.config.has_option("template1", "label"): name = self.config.get_object("template1", "label", modul="template") name = name.split(",")[1] self.config.set("ingress1", "name", name) else: self.config.set("ingress1", "name", self.config.project_name) annotations = {} if self.config.has_option("ingress1", "whitelist"): annotations.update(self.insert_whitelist()) if len(self.config.config_iteral("ingress1", "rule", pre="_host")) > 0: rules = self.set_ingress_rules() else: rules = None if self.config.has_option("ingress1", "ssl_backends"): ssl_backend = self.insert_ssl_backend() if ssl_backend is None: self.config.config_status = ErrorHandling.print_error_config( self.config, self.language["ing01"], locals(), bold=True) return None annotations.update(self.insert_ssl_backend()) if len(self.config.config_iteral("ingress1", "annotation", no="_value")) > 0: annotations.update(self.set_ingress_annotations()) if annotations == {}: annotations = None if self.config.has_option("ingress1", "backend_host"): backend = K8IngressBackend( self.config.get_object("ingress1", "backend_host"), self.config.get_object("ingress1", "backend_port", integer=True)) else: backend = None if self.config.has_option("ingress1", "tls_hosts") or self.config.has_option( "ingress1", "tls1_hosts"): tls = self.set_ingress_tls_s() else: tls = None if rules is None and backend is None: self.config.config_status = ErrorHandling.print_error_config( self.config, self.language["ing02"], locals(), bold=True) return None metadata = K8Meta(annotations=annotations, name=self.config.get_object("ingress1", "name")) spec = K8Spec(backend=backend, rules=rules, tls=tls) return [K8Ingress(metadata=metadata, spec=spec)]