Kubernetes node role refactored.
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / caas / config.py
index 9b70047..12059c2 100644 (file)
@@ -18,6 +18,8 @@ from cmdatahandlers.api import configerror
 from serviceprofiles import profiles
 import yaml
 import jinja2
+import string
+from random import choice
 
 CAAS_CONFIG_FILE_PATH = "/etc/cmframework/config/"
 CAAS_CONFIG_FILE = "caas.yaml"
@@ -26,7 +28,12 @@ VNF_EMBEDDED_SOFT_EVICTION_THRESHOLD = "300Mi"
 BM_SOFT_EVICTION_THRESHOLD = "4Gi"
 VNF_EMBEDDED_HARD_EVICTION_THRESHOLD = "200Mi"
 BM_HARD_EVICTION_THRESHOLD = "2Gi"
-DEFAULT_CAAS_INFRA_LOG_TYPE = 'remote_syslog'
+ADMIN_PWD_LENGTH = 20
+DEFAULT_CAAS_INFRA_LOG_TYPE = 'elasticsearch'
+AUDIT_DISK_LIMIT = 0.87
+CAAS_AUDIT_DISK_RATIO = 0.25
+DEFAULT_VALUES_MAP = { "docker0_cidr": "127.17.0.1/16",
+                       "oam_cidr": "10.244.0.0/16" }
 
 
 class Config(config.Config):
@@ -40,7 +47,8 @@ class Config(config.Config):
     def init(self):
         pass
 
-    def validate(self):
+    @staticmethod
+    def validate():
         print("validate")
 
     def flavour_set(self):
@@ -49,11 +57,13 @@ class Config(config.Config):
         for host in hostsconf.get_hosts():
             if 'caas_master' in hostsconf.get_service_profiles(host):
                 caas_masters.append(host)
-
-        if len(caas_masters) > 1:
-            return "multi"
+        return "multi" if len(caas_masters) > 1 else "single"
+    def get_caas_max_audit_size(self):
+        if self.is_caas_deployment():
+            return self.get_audit_disk_limit()*self.get_audit_disk_ratio()
         else:
-            return "single"
+            return 0
 
     def set_dynamic_config(self):
         if utils.is_virtualized():
@@ -61,12 +71,19 @@ class Config(config.Config):
         user_conf = self.confman.get_users_config_handler()
         self.set_caas_parameter('helm_home', "/home/{}/.helm".format(user_conf.get_admin_user()))
         self.set_caas_parameter('flavour', self.flavour_set())
+        self.config[self.ROOT]['caas_max_audit_size'] = self.get_caas_max_audit_size()
+        admin_pwd = self.get_caas_parameter('admin_password')
+        self.config[self.ROOT]['admin_password'] = \
+                admin_pwd if admin_pwd != '' else self.generate_pwd(ADMIN_PWD_LENGTH)
         if not self.get_caas_parameter('dns_domain'):
             self.set_caas_parameter('dns_domain', DEFAULT_CAAS_DNS_DOMAIN)
         if not self.get_caas_parameter('infra_log_store'):
             self.set_caas_parameter('infra_log_store', DEFAULT_CAAS_INFRA_LOG_TYPE)
         if not self.get_caas_parameter('log_forwarding'):
             self.set_caas_parameter('log_forwarding', [])
+        hostsconf = self.confman.get_hosts_config_handler()
+        hostsconf.set_nodeindex()
+        hostsconf.set_noderole()
 
     def set_static_config(self):
         try:
@@ -80,7 +97,20 @@ class Config(config.Config):
         except jinja2.exceptions.TemplateNotFound:
             return
         except Exception:
-            raise configerror.ConfigError("Unexpected issue occured!")
+            raise configerror.ConfigError("Unexpected issue has occured!")
+
+    def set_post_config(self):
+        self.config[self.ROOT]['swift_credential'] = \
+            dict(
+                user=self.get_caas_parameter('swift_credential').get('user'),
+                tenant=self.get_caas_parameter('swift_credential').get('tenant'),
+                password=self.generate_pwd(ADMIN_PWD_LENGTH)
+            )
+
+    def set_default_values_to_optional_params(self):
+        for parameter_name, default_value in DEFAULT_VALUES_MAP.iteritems():
+            if self.config[self.ROOT].get(parameter_name, '') == '':
+                self.config[self.ROOT][parameter_name] = default_value
 
     @staticmethod
     def _template_config(template, base_config, initial_data):
@@ -100,6 +130,8 @@ class Config(config.Config):
             return
         self.set_dynamic_config()
         self.set_static_config()
+        self.set_default_values_to_optional_params()
+        self.set_post_config()
 
     def is_vnf_embedded_deployment(self):
         return self.get_caas_only() and self.get_vnf_flag()
@@ -168,6 +200,14 @@ class Config(config.Config):
     def set_caas_parameter(self, parameter, value):
         self.config[self.ROOT][parameter] = value
 
+    def get_admin_password(self):
+        return self.config.get(self.ROOT, {}).get('admin_password')
+
+    @staticmethod
+    def generate_pwd(pwd_length):
+        character_pool = string.ascii_letters + string.digits
+        return ''.join(choice(character_pool) for i in range(pwd_length))
+
     def get_kubernetes_domain(self):
         return 'kubernetes.default.svc.{}'.format(
             self.config.get(self.ROOT, {}).get('dns_domain', ''))
@@ -183,3 +223,9 @@ class Config(config.Config):
             return VNF_EMBEDDED_HARD_EVICTION_THRESHOLD
         else:
             return BM_HARD_EVICTION_THRESHOLD
+
+    def get_audit_disk_ratio(self):
+        return CAAS_AUDIT_DISK_RATIO
+
+    def get_audit_disk_limit(self):
+        return AUDIT_DISK_LIMIT