"Fossies" - the Fresh Open Source Software Archive

Member "magnum-8.1.0/magnum/drivers/heat/k8s_fedora_template_def.py" (1 Oct 2019, 7956 Bytes) of package /linux/misc/openstack/magnum-8.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "k8s_fedora_template_def.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 8.0.0_vs_8.1.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #      http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 from oslo_log import log as logging
   14 from oslo_utils import strutils
   15 
   16 from magnum.common import exception
   17 from magnum.common.x509 import operations as x509
   18 from magnum.conductor.handlers.common import cert_manager
   19 from magnum.drivers.heat import k8s_template_def
   20 from magnum.drivers.heat import template_def
   21 from magnum.i18n import _
   22 from oslo_config import cfg
   23 import six
   24 
   25 CONF = cfg.CONF
   26 
   27 LOG = logging.getLogger(__name__)
   28 
   29 
   30 class ServerAddressOutputMapping(template_def.OutputMapping):
   31 
   32     public_ip_output_key = None
   33     private_ip_output_key = None
   34 
   35     def __init__(self, dummy_arg, cluster_attr=None):
   36         self.cluster_attr = cluster_attr
   37         self.heat_output = self.public_ip_output_key
   38 
   39     def set_output(self, stack, cluster_template, cluster):
   40         if not cluster_template.floating_ip_enabled:
   41             self.heat_output = self.private_ip_output_key
   42 
   43         LOG.debug("Using heat_output: %s", self.heat_output)
   44         super(ServerAddressOutputMapping,
   45               self).set_output(stack, cluster_template, cluster)
   46 
   47 
   48 class MasterAddressOutputMapping(ServerAddressOutputMapping):
   49     public_ip_output_key = 'kube_masters'
   50     private_ip_output_key = 'kube_masters_private'
   51 
   52 
   53 class NodeAddressOutputMapping(ServerAddressOutputMapping):
   54     public_ip_output_key = 'kube_minions'
   55     private_ip_output_key = 'kube_minions_private'
   56 
   57 
   58 class K8sFedoraTemplateDefinition(k8s_template_def.K8sTemplateDefinition):
   59     """Kubernetes template for a Fedora."""
   60 
   61     def __init__(self):
   62         super(K8sFedoraTemplateDefinition, self).__init__()
   63         self.add_parameter('docker_volume_size',
   64                            cluster_attr='docker_volume_size')
   65         self.add_parameter('docker_storage_driver',
   66                            cluster_template_attr='docker_storage_driver')
   67         self.add_output('kube_minions',
   68                         cluster_attr='node_addresses',
   69                         mapping_type=NodeAddressOutputMapping)
   70         self.add_output('kube_masters',
   71                         cluster_attr='master_addresses',
   72                         mapping_type=MasterAddressOutputMapping)
   73 
   74     def get_params(self, context, cluster_template, cluster, **kwargs):
   75         extra_params = kwargs.pop('extra_params', {})
   76 
   77         extra_params['username'] = context.user_name
   78         osc = self.get_osc(context)
   79         extra_params['region_name'] = osc.cinder_region_name()
   80 
   81         # set docker_volume_type
   82         # use the configuration default if None provided
   83         docker_volume_type = cluster.labels.get(
   84             'docker_volume_type', CONF.cinder.default_docker_volume_type)
   85         extra_params['docker_volume_type'] = docker_volume_type
   86 
   87         extra_params['nodes_affinity_policy'] = \
   88             CONF.cluster.nodes_affinity_policy
   89 
   90         if cluster_template.network_driver == 'flannel':
   91             extra_params["pods_network_cidr"] = \
   92                 cluster.labels.get('flannel_network_cidr', '10.100.0.0/16')
   93         if cluster_template.network_driver == 'calico':
   94             extra_params["pods_network_cidr"] = \
   95                 cluster.labels.get('calico_ipv4pool', '192.168.0.0/16')
   96 
   97         # check cloud provider and cinder options. If cinder is selected,
   98         # the cloud provider needs to be enabled.
   99         cloud_provider_enabled = cluster.labels.get(
  100             'cloud_provider_enabled',
  101             'true' if CONF.trust.cluster_user_trust else 'false')
  102         if (not CONF.trust.cluster_user_trust
  103                 and cloud_provider_enabled.lower() == 'true'):
  104             raise exception.InvalidParameterValue(_(
  105                 '"cluster_user_trust" must be set to True in magnum.conf when '
  106                 '"cloud_provider_enabled" label is set to true.'))
  107         if (cluster_template.volume_driver == 'cinder'
  108                 and cloud_provider_enabled.lower() == 'false'):
  109             raise exception.InvalidParameterValue(_(
  110                 '"cinder" volume driver needs "cloud_provider_enabled" label '
  111                 'to be true or unset.'))
  112         extra_params['cloud_provider_enabled'] = cloud_provider_enabled
  113 
  114         label_list = ['kube_tag', 'container_infra_prefix',
  115                       'availability_zone', 'cgroup_driver',
  116                       'calico_tag', 'calico_cni_tag',
  117                       'calico_kube_controllers_tag', 'calico_ipv4pool',
  118                       'etcd_tag', 'flannel_tag', 'flannel_cni_tag',
  119                       'cloud_provider_tag',
  120                       'prometheus_tag', 'grafana_tag',
  121                       'heat_container_agent_tag',
  122                       'keystone_auth_enabled', 'k8s_keystone_auth_tag',
  123                       'monitoring_enabled',
  124                       'tiller_enabled',
  125                       'tiller_tag',
  126                       'tiller_namespace',
  127                       'traefik_ingress_controller_tag',
  128                       'node_problem_detector_tag',
  129                       'auto_healing_enabled', 'auto_scaling_enabled',
  130                       'draino_tag', 'autoscaler_tag',
  131                       'min_node_count', 'max_node_count',
  132                       'nginx_ingress_controller_tag']
  133 
  134         for label in label_list:
  135             label_value = cluster.labels.get(label)
  136             if label_value:
  137                 extra_params[label] = label_value
  138 
  139         csr_keys = x509.generate_csr_and_key(u"Kubernetes Service Account")
  140 
  141         extra_params['kube_service_account_key'] = \
  142             csr_keys["public_key"].replace("\n", "\\n")
  143         extra_params['kube_service_account_private_key'] = \
  144             csr_keys["private_key"].replace("\n", "\\n")
  145 
  146         extra_params['project_id'] = cluster.project_id
  147 
  148         if not extra_params.get('max_node_count'):
  149             extra_params['max_node_count'] = cluster.node_count + 1
  150 
  151         self._set_cert_manager_params(cluster, extra_params)
  152 
  153         return super(K8sFedoraTemplateDefinition,
  154                      self).get_params(context, cluster_template, cluster,
  155                                       extra_params=extra_params,
  156                                       **kwargs)
  157 
  158     def _set_cert_manager_params(self, cluster, extra_params):
  159         cert_manager_api = cluster.labels.get('cert_manager_api')
  160         if strutils.bool_from_string(cert_manager_api):
  161             extra_params['cert_manager_api'] = cert_manager_api
  162             ca_cert = cert_manager.get_cluster_ca_certificate(cluster)
  163             if six.PY3 and isinstance(ca_cert.get_private_key_passphrase(),
  164                                       six.text_type):
  165                 extra_params['ca_key'] = x509.decrypt_key(
  166                     ca_cert.get_private_key(),
  167                     ca_cert.get_private_key_passphrase().encode()
  168                 ).decode().replace("\n", "\\n")
  169             else:
  170                 extra_params['ca_key'] = x509.decrypt_key(
  171                     ca_cert.get_private_key(),
  172                     ca_cert.get_private_key_passphrase()).replace("\n", "\\n")
  173 
  174     def get_env_files(self, cluster_template, cluster):
  175         env_files = []
  176 
  177         template_def.add_priv_net_env_file(env_files, cluster_template)
  178         template_def.add_etcd_volume_env_file(env_files, cluster_template)
  179         template_def.add_volume_env_file(env_files, cluster)
  180         template_def.add_lb_env_file(env_files, cluster_template)
  181         template_def.add_fip_env_file(env_files, cluster_template, cluster)
  182 
  183         return env_files