Exemplo n.º 1
0
    def __init__(self,
                 kubragen: KubraGen,
                 options: Optional[RabbitMQOnlineOptions] = None):
        super().__init__(kubragen)
        if options is None:
            options = RabbitMQOnlineOptions()
        self.options = options
        self.configfile = None

        self._namespace = self.option_get('namespace')

        if self.option_get(
                'config.authorization.serviceaccount_create') is not False:
            serviceaccount_name = self.basename()
        else:
            serviceaccount_name = self.option_get(
                'config.authorization.serviceaccount_use')
            if serviceaccount_name == '':
                serviceaccount_name = None

        if self.option_get('config.authorization.roles_create') is not False:
            role_name = self.basename()
        else:
            role_name = None

        if self.option_get('config.authorization.roles_bind') is not False:
            if serviceaccount_name is None:
                raise InvalidParamError(
                    'To bind roles a service account is required')
            if role_name is None:
                raise InvalidParamError(
                    'To bind roles the role must be created')
            rolebinding_name = self.basename()
        else:
            rolebinding_name = None

        self.object_names_init({
            'config': self.basename('-config'),
            'config-secret': self.basename('-config-secret'),
            'service-headless': self.basename('-headless'),
            'service': self.basename(),
            'service-account': serviceaccount_name,
            'role': role_name,
            'role-binding': rolebinding_name,
            'statefulset': self.basename(),
            'pod-label-app': self.basename(),
        })
        self._downloadedfiles = None
Exemplo n.º 2
0
    def __init__(self,
                 kubragen: KubraGen,
                 options: Optional[PromtailOptions] = None):
        super().__init__(kubragen)
        if options is None:
            options = PromtailOptions()
        self.options = options
        self.configfile = None

        self._namespace = self.option_get('namespace')

        if self.option_get(
                'config.authorization.serviceaccount_create') is not False:
            serviceaccount_name = self.basename()
        else:
            serviceaccount_name = self.option_get(
                'config.authorization.serviceaccount_use')
            if serviceaccount_name == '':
                serviceaccount_name = None

        if self.option_get('config.authorization.roles_bind') is not False:
            if serviceaccount_name is None:
                raise InvalidParamError(
                    'To bind roles a service account is required')

        self.object_names_init({
            'config': self.basename('-config'),
            'service-account': serviceaccount_name,
            'cluster-role': self.basename(),
            'cluster-role-binding': self.basename(),
            'daemonset': self.basename(),
            'pod-label-app': self.basename(),
        })
Exemplo n.º 3
0
    def build(self, provider: Provider, resources: 'KResourceDatabase',
              name: str, config: Optional[Any],
              merge_config: Optional[Any]) -> ObjectItem:
        pvdata: Dict[Any, Any] = {
            'apiVersion': 'v1',
            'kind': 'PersistentVolume',
            'metadata': {
                'name': name,
            },
            'spec': {
                'csi': None,
            },
        }
        if self.storageclass is not None:
            pvdata['spec']['storageClassName'] = self.storageclass
        if config is not None:
            if 'name' in config:
                pvdata['metadata']['name'] = config['name']
            if 'csi' in config:
                pvdata['spec']['csi'] = config['csi']

        ret = Merger.merge(pvdata,
                           merge_config if merge_config is not None else {})

        if ret['spec']['csi'] is None:
            raise InvalidParamError('"csi" config is required')

        return ret
Exemplo n.º 4
0
    def get_value(self, options: OptionGetter) -> ConfigFileOutput:
        if not isinstance(options, Builder):
            raise InvalidParamError('Options must be a "Builder" instance')

        ret = {}
        if self.option_get('enable.log_level'):
            ret.update({
                'log.console.level':
                options.option_get('config.loglevel'),
            })
        if self.option_get('enable.cluster_formation'):
            ret.update({
                'cluster_formation.peer_discovery_backend':
                'k8s',
                'cluster_formation.k8s.host':
                'kubernetes.default.svc.cluster.local',
                'cluster_formation.k8s.address_type':
                'hostname',
                'cluster_formation.k8s.service_name':
                options.object_name('service-headless'),
                'queue_master_locator':
                'min-masters',
            })

        load_definitions = options.option_get('config.load_definitions')
        if load_definitions is not None:
            ret.update({
                'load_definitions':
                '/etc/rabbitmq-load-definition/load_definition.json',
            })

        if self.option_get('config.merge_config') is not None:
            Merger.merge(ret, self.option_get('config.merge_config'))

        return ConfigFileOutput_DictSingleLevel(ret)
Exemplo n.º 5
0
    def __init__(self,
                 kubragen: KubraGen,
                 options: Optional[KubeResourceReportOptions] = None):
        super().__init__(kubragen)
        if options is None:
            options = KubeResourceReportOptions()
        self.options = options

        self._namespace = self.option_get('namespace')

        if self.option_get(
                'config.authorization.serviceaccount_create') is not False:
            serviceaccount_name = self.basename()
        else:
            serviceaccount_name = self.option_get(
                'config.authorization.serviceaccount_use')
            if serviceaccount_name == '':
                serviceaccount_name = None

        if self.option_get('config.authorization.roles_create') is not False:
            role_name = self.basename()
        else:
            role_name = None

        if self.option_get('config.authorization.roles_bind') is not False:
            if serviceaccount_name is None:
                raise InvalidParamError(
                    'To bind roles a service account is required')
            if role_name is None:
                raise InvalidParamError(
                    'To bind roles the role must be created')
            rolebinding_name = self.basename()
        else:
            rolebinding_name = None

        self.object_names_init({
            'service': self.basename(),
            'service-account': serviceaccount_name,
            'cluster-role': role_name,
            'cluster-role-binding': rolebinding_name,
            'deployment': self.basename(),
            'pod-label-app': self.basename(),
        })
Exemplo n.º 6
0
    def _create_kubestatemetrics_config(self) -> KubeStateMetricsBuilder:
        if self.option_get('enable.kube-state-metrics') is False:
            raise InvalidParamError('Kube-state-metrics is not enabled')

        try:
            ret = KubeStateMetricsBuilder(
                kubragen=self.kubragen,
                options=KubeStateMetricsOptions({
                    'basename':
                    self.basename('-kube-state-metrics'),
                    'namespace':
                    self.namespace(),
                    'config': {
                        'prometheus_annotation':
                        self.option_get('config.prometheus_annotation'),
                        'node_selector':
                        self.option_get(
                            'config.kubestatemetrics.node_selector'),
                        'authorization': {
                            'serviceaccount_create':
                            False,
                            'serviceaccount_use':
                            self.object_name('service-account'),
                            'roles_create':
                            self.option_get(
                                'config.authorization.roles_create'),
                            'roles_bind':
                            self.option_get('config.authorization.roles_bind'),
                        },
                    },
                    'container': {
                        'kube-state-metrics':
                        self.option_get('container.kube-state-metrics'),
                    },
                    'kubernetes': {
                        'resources': {
                            'deployment':
                            self.option_get(
                                'kubernetes.resources.kube-state-metrics-deployment'
                            ),
                        },
                    },
                }))
            ret.object_names_change(
                self._object_names_changed('kube-state-metrics-'))
            return ret
        except OptionError as e:
            raise OptionError('Kube state metrics option error: {}'.format(
                str(e))) from e
        except TypeError as e:
            raise OptionError('Kube state metrics type error: {}'.format(
                str(e))) from e
Exemplo n.º 7
0
    def __init__(self,
                 kubragen: KubraGen,
                 options: Optional[EFKOptions] = None):
        super().__init__(kubragen)
        if options is None:
            options = EFKOptions()
        self.options = options
        self.configfile = None

        self._namespace = self.option_get('namespace')

        if self.option_get(
                'config.authorization.serviceaccount_create') is not False:
            serviceaccount_name = self.basename()
        else:
            serviceaccount_name = self.option_get(
                'config.authorization.serviceaccount_use')
            if serviceaccount_name == '':
                serviceaccount_name = None

        if self.option_get('config.authorization.roles_bind') is not False:
            if serviceaccount_name is None:
                raise InvalidParamError(
                    'To bind roles a service account is required')

        self.object_names_init({
            'service-account':
            serviceaccount_name,
            'elasticsearch-service':
            self.basename('-elasticsearch'),
            'elasticsearch-statefulset':
            self.basename('-elasticsearch'),
            'elasticsearch-pod-label-app':
            self.basename('-elasticsearch'),
            'fluentd-cluster-role':
            self.basename('-fluentd'),
            'fluentd-cluster-role-binding':
            self.basename('-fluentd'),
            'fluentd-daemonset':
            self.basename('-fluentd'),
            'fluentd-pod-label-app':
            self.basename('-fluentd'),
        })
        if self.option_get('enable.kibana'):
            self.object_names_init({
                'kibana-service':
                self.basename('-kibana'),
                'kibana-deployment':
                self.basename('-kibana'),
                'kibana-pod-label-app':
                self.basename('-kibana'),
            })
Exemplo n.º 8
0
    def _create_nodeexporter_config(self) -> NodeExporterBuilder:
        if self.option_get('enable.node-exporter') is False:
            raise InvalidParamError('Node exporter is not enabled')

        try:
            ret = NodeExporterBuilder(
                kubragen=self.kubragen,
                options=NodeExporterOptions({
                    'basename':
                    self.basename('-node-exporter'),
                    'namespace':
                    self.namespace(),
                    'config': {
                        'prometheus_annotation':
                        self.option_get('config.prometheus_annotation'),
                    },
                    'container': {
                        'node-exporter':
                        self.option_get('container.node-exporter'),
                    },
                    'kubernetes': {
                        'resources': {
                            'daemonset':
                            self.option_get(
                                'kubernetes.resources.node-exporter-daemonset'
                            ),
                        },
                    },
                }))
            ret.object_names_change(
                self._object_names_changed('kube-state-metrics-'))
            return ret
        except OptionError as e:
            raise OptionError('Node exporter option error: {}'.format(
                str(e))) from e
        except TypeError as e:
            raise OptionError('Node exporter type error: {}'.format(
                str(e))) from e
Exemplo n.º 9
0
    def build(self, provider: Provider, resources: 'KResourceDatabase',
              name: str, config: Optional[Any],
              merge_config: Optional[Any]) -> ObjectItem:
        pvcdata: Dict[Any, Any] = {
            'apiVersion': 'v1',
            'kind': 'PersistentVolumeClaim',
            'metadata': {
                'name': name,
            },
            'spec': {},
        }
        if self.storageclass is not None:
            pvcdata['spec']['storageClassName'] = self.storageclass

        persistentVolume: Optional[ObjectItem] = None
        if config is not None:
            if 'name' in config:
                pvcdata['metadata']['name'] = config['name']
            if 'namespace' in config:
                pvcdata['metadata']['namespace'] = config['namespace']
            if 'persistentVolume' in config:
                persistentVolume = resources.persistentvolume_build(
                    provider, config['persistentVolume'])[0]

        if persistentVolume is not None:
            if dict_has_name(persistentVolume, 'spec.storageClassName'):
                pv_storageclassname = dict_get_value(persistentVolume,
                                                     'spec.storageClassName')
                if pv_storageclassname is not None:
                    pvcdata['spec']['storageClassName'] = pv_storageclassname

            if not dict_has_name(pvcdata,
                                 'spec.accessModes') and dict_has_name(
                                     persistentVolume, 'spec.accessModes'):
                pvcdata['spec']['accessModes'] = persistentVolume['spec'][
                    'accessModes']

            if not dict_has_name(
                    pvcdata,
                    'spec.resources.requests.storage') and dict_has_name(
                        persistentVolume, 'spec.capacity.storage'):
                Merger.merge(
                    pvcdata, {
                        'spec': {
                            'resources': {
                                'requests': {
                                    'storage':
                                    dict_get_value(persistentVolume,
                                                   'spec.capacity.storage'),
                                }
                            }
                        }
                    })

        ret = Merger.merge(pvcdata,
                           merge_config if merge_config is not None else {})

        if not self.allow_selector:
            if 'selector' in ret['spec']:
                if persistentVolume is None:
                    raise InvalidParamError(
                        'PersistentVolumeClaim does not support selector but a PersistentVolume was not supplied'
                    )
                del ret['spec']['selector']
                ret['spec']['storageClassName'] = ''
                ret['spec']['volumeName'] = persistentVolume['metadata'][
                    'name']

        return ret
Exemplo n.º 10
0
    def _create_granana_config(self) -> GrafanaBuilder:
        if self.option_get('enable.grafana') is False:
            raise InvalidParamError('Grafana is not enabled')

        try:
            ret = GrafanaBuilder(
                kubragen=self.kubragen,
                options=GrafanaOptions({
                    'basename': self.basename('-grafana'),
                    'namespace': self.namespace(),
                    'config': {
                        'grafana_config':
                        self.option_get('config.grafana.grafana_config'),
                        'install_plugins':
                        self.option_get('config.grafana.install_plugins'),
                        'service_port':
                        self.option_get('config.grafana.service_port'),
                        'probes':
                        self.option_get('config.probes'),
                        'provisioning': {
                            'datasources':
                            self.option_get(
                                'config.grafana.provisioning.datasources'),
                            'plugins':
                            self.option_get(
                                'config.grafana.provisioning.plugins'),
                            'dashboards':
                            self.option_get(
                                'config.grafana.provisioning.dashboards'),
                        },
                        'dashboards':
                        self.option_get('config.grafana.dashboards'),
                        'dashboards_path':
                        self.option_get('config.grafana.dashboards_path'),
                        'admin': {
                            'user':
                            self.option_get('config.grafana.admin.user'),
                            'password':
                            self.option_get('config.grafana.admin.password'),
                        },
                    },
                    'container': {
                        'grafana': self.option_get('container.grafana'),
                    },
                    'kubernetes': {
                        'volumes': {
                            'data':
                            self.option_get('kubernetes.volumes.grafana-data'),
                        },
                        'resources': {
                            'deployment':
                            self.option_get(
                                'kubernetes.resources.grafana-deployment'),
                        },
                    },
                }))
            ret.object_names_change(self._object_names_changed('grafana-'))
            return ret
        except OptionError as e:
            raise OptionError('Grafana option error: {}'.format(str(e))) from e
        except TypeError as e:
            raise OptionError('Grafana type error: {}'.format(str(e))) from e