# Info: KCL Kubernetes task schemas for provisioning (Provisioning)
# Author: JesusPerezLorenzo 
# Release: 0.0.4
# Date: 10-12-2023

import regex

schema ETCD_endpoint:
   prot: "http"| "https" = "https"
   name?: str
   addr?: str 
   port: int = 2379

   check:
      addr == Undefined or regex.match(addr,"^\$.*$") or regex.match(addr, "^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])$"), "'addr = ${addr}' check value definition (use $vaule or xx.xx.xx.xx)"

schema Kubernetes:
   name: str = "kubernetes"
   version: str
   major_version: str
   cri: "crio" | "containerd" = "crio"
   runtime_default: "crun" | "runc" | "youki" = "crun"
   runtimes: str = "crun,runc"

   cni: "cilium" | "calico" = "cilium"
   cni_version: str = ""
   bind_port: int = 6443
   timeout_cp: str = "4m0s"
   certs_dir: str = "/etc/kubernetes/pki"
   auth_mode: str = "Node,RBAC"
   taints_effect: "PreferNoSchedule" = "PreferNoSchedule"
   pull_policy: "IfNotPresent" = "IfNotPresent"
   # Kubernetes addons separated with commans
   addons: str
   tpl: str = "kubeadm-config.yaml.j2"
   repo: str = "registry.k8s.io" 
   dns_domain: str = "cluster.local"
   pod_net: str = "10.244.0.0/16"
   service_net: str = "10.96.0.0/12"
   cert_sans: [str] = [ "$hostname", "$cluster_name", "127.0.0.1" ]
   # External IPs separated with commans for ingress
   external_ips: [str]
   # Cluster name
   cluster_name: str
   hostname: str
   # ControlPanel IP 
   cp_ip?: str
   cp_name: str
   # If HOSTNAME == K8S_MASTER it will be MASTER_0
   # othewise set HOSTNAME value to be resolved in same K8S_MASTER network
   # By using -cp- as part of HOSTNAME will be consider node as controlpanel
   # Other options: -wk-0  or -wkr-0 for worker nodes
   ip: str 
   # K8s cluster role: controlpnlane or worker
   mode: "controlplane" | "worker"
   # K8s command task
   cmd_task: "install" | "update" | "remove" | "reinstall" = "install"
   admin_user: str
   target_path: str
   taint_node: bool = True
   etcd_mode: "" | "external" = ""
   etcd_prefix: str = ""
   etcd_endpoints: [ETCD_endpoint] = []
   etcd_ca_path: str = "/etc/kubernetes/pki/etcd/ca.crt"
   etcd_cert_path: str = "/etc/kubernetes/pki/etcd/server.crt"
   etcd_key_path: str = "/etc/kubernetes/pki/etcd/server.key"
   etcd_cluster_name: str = ""
   etcd_peers: str = ""
   # etcd certs path
   prov_etcd_path: str = "etcdcerts"
   # install etcd certs path
   etcd_certs_path: str = "etcd_certs"
   # LOG path for kubeadm
   install_log_path: str = "/tmp/k8s.log"
   # Work path for config generated file
   work_path: str = "$cluster_name"
   check:
      cp_ip == Undefined or regex.match(cp_ip,"^\$.*$") or regex.match(cp_ip, "^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])$"), "'cp_ip = ${cp_ip}' check value definition (use $vaule or xx.xx.xx.xx)"
      regex.match(pod_net,"^\$.*$") or regex.match(pod_net, "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}\/(?:3[0-2]|[0-2]?[0-9])$"), "'pod_net = ${pod_net}' check value definition"
      regex.match(service_net,"^\$.*$") or regex.match(service_net, "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}\/(?:3[0-2]|[0-2]?[0-9])$"), "'service_net = ${service_net}' check value definition"

