def test_from_config(self): config = mock.Mock() config.k8s_include_namespaces = ["foo", "bar", "baz"] config.k8s_ignore_namespaces = ["baz"] test_filter = K8sNamespaceFilter.from_config(global_config=config) self.assertTrue(test_filter.passes("foo")) self.assertTrue(test_filter.passes("bar")) self.assertFalse(test_filter.passes("baz")) self.assertFalse(test_filter.passes("bez"))
def test_from_config_with_local(self): config = mock.Mock() config.k8s_include_namespaces = Configuration.DEFAULT_K8S_INCLUDE_NAMESPACES config.k8s_ignore_namespaces = Configuration.DEFAULT_K8S_IGNORE_NAMESPACES monitor_config = { "k8s_ignore_namespaces": ["baz"], } test_filter = K8sNamespaceFilter.from_config( global_config=config, local_config=monitor_config) self.assertTrue(test_filter.passes("foo")) self.assertTrue(test_filter.passes("bar")) self.assertFalse(test_filter.passes("baz"))
def _initialize(self): # our disk logger and handler self.__disk_logger = None self.__log_handler = None self.__log_watcher = None self.__module = None self._node_name = None # configure the logger and path self.__message_log = self._config.get("message_log") self.log_config = { "parser": "k8sEvents", "path": self.__message_log, } self._leader_check_interval = self._config.get("leader_check_interval") self._leader_node = self._config.get("leader_node") self._check_labels = self._config.get("check_labels") self._ignore_master = self._config.get("ignore_master") self._current_leader = self._leader_node self.__flush_delay = self._config.get("log_flush_delay") try: attributes = JsonObject({"monitor": "json"}) self.log_config["attributes"] = attributes except Exception: global_log.error( "Error setting monitor attribute in KubernetesEventMonitor" ) # The namespace whose logs we should collect. self.__k8s_namespaces_to_include = K8sNamespaceFilter.from_config( global_config=self._global_config ) ( default_rotation_count, default_max_bytes, ) = self._get_log_rotation_configuration() self.__event_object_filter = self._config.get( "event_object_filter", [ "CronJob", "DaemonSet", "Deployment", "Job", "Node", "Pod", "ReplicaSet", "ReplicationController", "StatefulSet", ], ) self.__max_log_size = self._config.get("max_log_size") if self.__max_log_size is None: self.__max_log_size = default_max_bytes self.__max_log_rotations = self._config.get("max_log_rotations") if self.__max_log_rotations is None: self.__max_log_rotations = default_rotation_count # Support legacy disabling of k8s_events via the K8S_EVENTS_DISABLE environment variable k8s_events_disable_envar = compat.os_environ_unicode.get("K8S_EVENTS_DISABLE") if k8s_events_disable_envar is not None: global_log.warn( "The K8S_EVENTS_DISABLE environment variable is deprecated. Please use SCALYR_K8S_EVENTS_DISABLE instead." ) legacy_disable = six.text_type(k8s_events_disable_envar).lower() else: legacy_disable = "false" # Note, accepting just a single `t` here due to K8s ConfigMap issues with having a value of `true` self.__disable_monitor = ( legacy_disable == "true" or legacy_disable == "t" or self._global_config.k8s_events_disable )