Ejemplo n.º 1
0
    def test_delete_topic(self, mock_os, mock_resource_exists):
        mock_resource_exists.return_value = True
        result = self.runner.invoke(kfk, [
            'topics', '--delete', '--topic', self.topic, '-c', self.cluster,
            '-n', self.namespace
        ])

        assert result.exit_code == 0

        mock_os.system.assert_called_with(Kubectl().delete().kafkatopics(
            self.topic).namespace(self.namespace).build())
Ejemplo n.º 2
0
 def test_describe_topic_native(self, mock_os):
     result = self.runner.invoke(kfk, [
         'topics', '--describe', '--topic', self.topic, '-c', self.cluster,
         '-n', self.namespace, '--native'
     ])
     assert result.exit_code == 0
     native_command = "bin/kafka-topics.sh --bootstrap-server {cluster}-kafka-bootstrap:9092 --describe --topic {" \
                      "topic}"
     mock_os.system.assert_called_with(Kubectl().exec(
         "-it", "{cluster}-kafka-0").container("kafka").namespace(
             self.namespace).exec_command(native_command).build().format(
                 topic=self.topic, cluster=self.cluster))
    def test_console_consumer_with_from_beginning(self, mock_os):
        from_beginning = True
        result = self.runner.invoke(kfk, ['console-consumer', '--topic', self.topic, '-c', self.cluster, '-n',
                                          self.namespace, '--from-beginning'])
        assert result.exit_code == 0

        native_command = "bin/kafka-console-consumer.sh --bootstrap-server my-cluster-kafka-bootstrap:9092 --topic {" \
                         "topic} {from_beginning}"
        mock_os.system.assert_called_with(
            Kubectl().exec("-it", "{cluster}-kafka-0").container("kafka").namespace(self.namespace).exec_command(
                native_command).build().format(cluster=self.cluster, topic=self.topic,
                                               from_beginning=(from_beginning and '--from-beginning' or '')))
Ejemplo n.º 4
0
def alter(user, authentication_type, authorization_type, add_acl, delete_acl,
          operation_tuple, host, type, resource_type, resource_name,
          resource_pattern_type, quota_tuple, delete_quota_tuple, cluster,
          namespace):
    if resource_exists("kafkausers", user, cluster, namespace):
        file = get_resource_as_file("kafkausers", user, namespace)
        user_dict = yaml.full_load(file)

        if authentication_type is not None:
            user_dict["spec"]["authentication"]["type"] = authentication_type

        if authorization_type is not None:
            if user_dict["spec"].get("authorization") is None:
                user_dict["spec"]["authorization"] = {}
            user_dict["spec"]["authorization"]["type"] = authorization_type

        if add_acl:
            if user_dict["spec"].get("authorization") is None:
                user_dict["spec"]["authorization"] = {}
            add_acl_option(user_dict, operation_tuple, host, type,
                           resource_type, resource_name, resource_pattern_type)

        if delete_acl:
            if user_dict["spec"].get("authorization") is None:
                user_dict["spec"]["authorization"] = {}
            delete_acl_option(user_dict, operation_tuple, host, type,
                              resource_type, resource_name,
                              resource_pattern_type)

        delete_last_applied_configuration(user_dict)

        if len(quota_tuple) > 0:
            if user_dict["spec"].get("quotas") is None:
                user_dict["spec"]["quotas"] = {}
            add_resource_kv_config(quota_tuple, user_dict["spec"]["quotas"],
                                   snake_to_camel_case)

        if len(delete_quota_tuple) > 0:
            if user_dict["spec"].get("quotas") is not None:
                delete_resource_config(delete_quota_tuple,
                                       user_dict["spec"]["quotas"],
                                       snake_to_camel_case)

        user_yaml = yaml.dump(user_dict)
        user_temp_file = create_temp_file(user_yaml)
        os.system(Kubectl().apply().from_file(
            "{user_temp_file_path}").namespace(namespace).build().format(
                user_temp_file_path=user_temp_file.name))
        user_temp_file.close()
    else:
        print_resource_found_msg(cluster, namespace)
    def test_console_producer_with_producer_config(self, mock_os, mock_transfer_file_to_container):
        result = self.runner.invoke(kfk, ['console-producer', '--topic', self.topic, '--producer.config',
                                          'files/client.properties', '-c', self.cluster, '-n', self.namespace])
        assert result.exit_code == 0
        native_command = "bin/kafka-console-producer.sh --broker-list {cluster}-kafka-brokers:9093 --topic {topic} " \
                         "--producer-property security.protocol=SSL --producer-property " \
                         "ssl.truststore.location=/tmp/truststore.jks --producer-property " \
                         "ssl.truststore.password=123456 --producer-property ssl.keystore.location=/tmp/user.p12 " \
                         "--producer-property ssl.keystore.password=123456;rm -rf /tmp/truststore.jks;rm -rf " \
                         "/tmp/user.p12;"

        mock_os.system.assert_called_with(
            Kubectl().exec("-it", "{cluster}-kafka-0").container("kafka").namespace(self.namespace).exec_command(
                native_command).build().format(cluster=self.cluster, topic=self.topic))
def console_producer(topic, producer_config, cluster, namespace):
    """The console producer is a tool that reads data from standard input and publish it to Kafka."""
    native_command = "bin/kafka-console-producer.sh --broker-list {cluster}-kafka-brokers:{port} --topic {topic}"
    pod = cluster + "-kafka-0"
    container = "kafka"
    if producer_config is not None:
        native_command = apply_client_config_from_file(native_command,
                                                       producer_config,
                                                       "--producer-property",
                                                       container, pod,
                                                       namespace)
    os.system(Kubectl().exec("-it", pod).container(container).namespace(
        namespace).exec_command(native_command).build().format(
            port=KAFKA_PORT, topic=topic, cluster=cluster))
def console_consumer(topic, consumer_config, from_beginning, cluster,
                     namespace):
    """The console consumer is a tool that reads data from Kafka and outputs it to standard output."""
    native_command = "bin/kafka-console-consumer.sh --bootstrap-server {cluster}-kafka-bootstrap:{port} --topic {" \
                     "topic} {from_beginning}"
    pod = cluster + "-kafka-0"
    container = "kafka"
    if consumer_config is not None:
        native_command = apply_client_config_from_file(native_command,
                                                       consumer_config,
                                                       "--consumer-property",
                                                       container, pod,
                                                       namespace)
    os.system(Kubectl().exec("-it", pod).container(container).namespace(
        namespace).exec_command(native_command).build().format(
            port=KAFKA_PORT,
            topic=topic,
            cluster=cluster,
            from_beginning=(from_beginning and '--from-beginning' or '')))
Ejemplo n.º 8
0
def acls(list, topic, cluster, group, add, allow_principal, deny_principal,
         operation_tuple, allow_host, deny_host, resource_pattern_type, remove,
         kafka_cluster, namespace):
    """This tool helps to manage ACLs on Kafka."""
    if list:
        native_command = "bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:12181 --list {topic}" \
                         "{cluster} {group}"
        os.system(Kubectl().exec(
            "-it",
            "{kafka_cluster}-zookeeper-0").container("zookeeper").namespace(
                namespace).exec_command(native_command).build().format(
                    kafka_cluster=kafka_cluster,
                    topic=(topic and '--topic ' + topic or ''),
                    cluster=(cluster and '--cluster ' + cluster or ''),
                    group=(group and '--group ' + group or '')))
    elif add or remove:
        alter(topic, cluster, group, add, remove, allow_principal,
              deny_principal, operation_tuple, allow_host, deny_host,
              resource_pattern_type, kafka_cluster, namespace)
    else:
        print_missing_options_for_command("acls")
Ejemplo n.º 9
0
def create(topic, partitions, replication_factor, config, cluster, namespace):
    with open('{strimzi_path}/examples/topic/kafka-topic.yaml'.format(
            strimzi_path=STRIMZI_PATH).format(
                version=STRIMZI_VERSION)) as file:
        topic_dict = yaml.full_load(file)

        topic_dict["metadata"]["name"] = topic
        topic_dict["metadata"]["labels"]["strimzi.io/cluster"] = cluster
        topic_dict["spec"]["partitions"] = int(partitions)
        topic_dict["spec"]["replicas"] = int(replication_factor)

        if len(config) > 0:
            if topic_dict["spec"].get("config") is None:
                topic_dict["spec"]["config"] = {}
            add_resource_kv_config(config, topic_dict["spec"]["config"])

        topic_yaml = yaml.dump(topic_dict)
        topic_temp_file = create_temp_file(topic_yaml)
        os.system(Kubectl().create().from_file(
            "{topic_temp_file_path}").namespace(namespace).build().format(
                topic_temp_file_path=topic_temp_file.name))
        topic_temp_file.close()
Ejemplo n.º 10
0
def create(user, authentication_type, quota, cluster, namespace):
    with open('{strimzi_path}/examples/user/kafka-user.yaml'.format(
            strimzi_path=STRIMZI_PATH).format(
                version=STRIMZI_VERSION)) as file:
        user_dict = yaml.full_load(file)

        user_dict["metadata"]["name"] = user
        user_dict["metadata"]["labels"]["strimzi.io/cluster"] = cluster
        user_dict["spec"]["authentication"]["type"] = authentication_type

        del user_dict["spec"]["authorization"]

        if len(quota) > 0:
            if user_dict["spec"].get("quotas") is None:
                user_dict["spec"]["quotas"] = {}
            add_resource_kv_config(quota, user_dict["spec"]["quotas"])

        user_yaml = yaml.dump(user_dict)
        user_temp_file = create_temp_file(user_yaml)
        os.system(Kubectl().create().from_file(
            "{user_temp_file_path}").namespace(namespace).build().format(
                user_temp_file_path=user_temp_file.name))
        user_temp_file.close()
 def test_create(self):
     self.assertEqual(Kubectl().create().build(),
                      "{kubectl} create".format(kubectl=KUBECTL_PATH))
Ejemplo n.º 12
0
def transfer_file_to_container(source_file_path, dest_file_path, container, pod, namespace):
    os.system(Kubectl().cp(source_file_path, "{namespace}/{pod}:" + dest_file_path).container(container).build().format(
        namespace=namespace, pod=pod))
Ejemplo n.º 13
0
def delete(topic, cluster, namespace):
    if resource_exists("kafkatopics", topic, cluster, namespace):
        os.system(
            Kubectl().delete().kafkatopics(topic).namespace(namespace).build())
Ejemplo n.º 14
0
def resource_exists(resource_type, resource_name, cluster, namespace):
    return resource_name in os.popen(
        Kubectl().get().resource(resource_type).label("strimzi.io/cluster={cluster}").namespace(
            namespace).build().format(
            cluster=cluster)).read()
Ejemplo n.º 15
0
def get_resource_yaml(resource_type, resource_name, namespace):
    return os.popen(
        Kubectl().get().resource(resource_type, resource_name).namespace(namespace).output("yaml").build()).read()
 def test_get(self):
     self.assertEqual(Kubectl().get().build(),
                      "{kubectl} get".format(kubectl=KUBECTL_PATH))
 def test_kafkausers_specific(self):
     self.assertEqual(
         Kubectl().get().kafkausers("my-user").build(),
         "{kubectl} get kafkausers my-user".format(kubectl=KUBECTL_PATH))
Ejemplo n.º 18
0
def list(cluster, namespace):
    os.system(Kubectl().get().kafkausers().label(
        "strimzi.io/cluster={cluster}").namespace(namespace).build().format(
            cluster=cluster))
Ejemplo n.º 19
0
def delete(cluster, namespace, user):
    if resource_exists("kafkausers", user, cluster, namespace):
        os.system(
            Kubectl().delete().kafkausers(user).namespace(namespace).build())
 def test_delete(self):
     self.assertEqual(Kubectl().delete().build(),
                      "{kubectl} delete".format(kubectl=KUBECTL_PATH))
 def test_labels(self):
     self.assertEqual(
         Kubectl().get().kafkatopics().label(
             "app=test, included=true").build(),
         "{kubectl} get kafkatopics -l app=test, included=true".format(
             kubectl=KUBECTL_PATH))
 def test_cluster(self):
     self.assertEqual(
         Kubectl().get().kafkas().container("test-container").build(),
         "{kubectl} get kafkas -c test-container".format(
             kubectl=KUBECTL_PATH))
 def test_label_namespace(self):
     self.assertEqual(
         Kubectl().get().kafkatopics().label("app=test").namespace(
             "test").build(),
         "{kubectl} get kafkatopics -l app=test -n test".format(
             kubectl=KUBECTL_PATH))
 def test_namespace_all(self):
     self.assertEqual(
         Kubectl().get().kafkatopics().namespace().build(),
         "{kubectl} get kafkatopics --all-namespaces".format(
             kubectl=KUBECTL_PATH))
Ejemplo n.º 25
0
 def test_list_clusters_all_namespaces(self, mock_os):
     result = self.runner.invoke(kfk, ['clusters', '--list'])
     assert result.exit_code == 0
     mock_os.system.assert_called_with(
         Kubectl().get().kafkas().namespace().build())
 def test_apply(self):
     self.assertEqual(Kubectl().apply().build(),
                      "{kubectl} apply".format(kubectl=KUBECTL_PATH))
 def test_output(self):
     self.assertEqual(
         Kubectl().get().kafkas().output("yaml").build(),
         "{kubectl} get kafkas -o yaml".format(kubectl=KUBECTL_PATH))
 def test_from_file(self):
     self.assertEqual(
         Kubectl().create().from_file("/a/test/path/testfile.yaml").build(),
         "{kubectl} create -f /a/test/path/testfile.yaml".format(
             kubectl=KUBECTL_PATH))
 def test_describe_user(self, mock_os, mock_resource_exists):
     mock_resource_exists.return_value = True
     result = self.runner.invoke(kfk, ['users', '--describe', '--user', self.user, '-c', self.cluster, '-n',
                                       self.namespace])
     assert result.exit_code == 0
     mock_os.system.assert_called_with(Kubectl().describe().kafkausers(self.user).namespace(self.namespace).build())
 def test_kafkausers_all(self):
     self.assertEqual(
         Kubectl().get().kafkausers().build(),
         "{kubectl} get kafkausers".format(kubectl=KUBECTL_PATH))