cidp 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cidp-0.0.1/PKG-INFO +11 -0
- cidp-0.0.1/cidp/KubernetesController.py +68 -0
- cidp-0.0.1/cidp/SparkSessionBuilder.py +54 -0
- cidp-0.0.1/cidp/__init__.py +0 -0
- cidp-0.0.1/cidp.egg-info/PKG-INFO +11 -0
- cidp-0.0.1/cidp.egg-info/SOURCES.txt +8 -0
- cidp-0.0.1/cidp.egg-info/dependency_links.txt +1 -0
- cidp-0.0.1/cidp.egg-info/top_level.txt +1 -0
- cidp-0.0.1/setup.cfg +4 -0
- cidp-0.0.1/setup.py +22 -0
cidp-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cidp
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: CIDP Python SDK
|
|
5
|
+
Home-page: https://gitlab.cidp.io/common/pypi/cidp
|
|
6
|
+
Author: Example Author
|
|
7
|
+
Author-email: skt.mccho@sk.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.6
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
class KubernetesController():
|
|
2
|
+
def __init__(self, config_path):
|
|
3
|
+
def load_kubeconfig(config_path):
|
|
4
|
+
from kubernetes import config, client
|
|
5
|
+
config.load_kube_config(config_file=config_path)
|
|
6
|
+
return client.CoreV1Api()
|
|
7
|
+
|
|
8
|
+
self.api = load_kubeconfig(config_path)
|
|
9
|
+
|
|
10
|
+
def get_my_pod_spec(self):
|
|
11
|
+
import os
|
|
12
|
+
pods = self.api.list_pod_for_all_namespaces(watch=False)
|
|
13
|
+
for pod in pods.items:
|
|
14
|
+
if pod.metadata.labels is not None and pod.metadata.labels.get("statefulset.kubernetes.io/pod-name",
|
|
15
|
+
"none") == os.environ["HOSTNAME"]:
|
|
16
|
+
return pod
|
|
17
|
+
|
|
18
|
+
def get_pod_namespace(self, pod_spec):
|
|
19
|
+
return pod_spec.metadata.namespace
|
|
20
|
+
|
|
21
|
+
def get_my_pod_namespace(self):
|
|
22
|
+
return self.get_pod_namespace(self.get_my_pod_spec())
|
|
23
|
+
|
|
24
|
+
def get_pod_selector(self, pod_spec, key):
|
|
25
|
+
return pod_spec.metadata.labels[key]
|
|
26
|
+
|
|
27
|
+
def get_my_pod_app_selector(self):
|
|
28
|
+
return self.get_pod_selector(self.get_my_pod_spec(), "app")
|
|
29
|
+
|
|
30
|
+
def get_list_used_nodeport(self):
|
|
31
|
+
used_ports = []
|
|
32
|
+
services = self.api.list_service_for_all_namespaces(watch=False)
|
|
33
|
+
for service in services.items:
|
|
34
|
+
if service.spec.type == "NodePort":
|
|
35
|
+
for port in service.spec.ports:
|
|
36
|
+
used_ports.append(port.node_port)
|
|
37
|
+
return used_ports
|
|
38
|
+
|
|
39
|
+
def get_available_ports(self, start=32000, end=32600, count=1):
|
|
40
|
+
import random
|
|
41
|
+
used_ports = self.get_list_used_nodeport()
|
|
42
|
+
port_range = range(start, end + 1)
|
|
43
|
+
ports = [port for port in port_range if port not in used_ports]
|
|
44
|
+
return random.sample(ports, count)
|
|
45
|
+
|
|
46
|
+
def create_spark_nodeport(self, namespace, service_name, app_name, driver_port, blockmanager_port):
|
|
47
|
+
from kubernetes import client
|
|
48
|
+
service = client.V1Service(api_version="v1",
|
|
49
|
+
kind="Service",
|
|
50
|
+
metadata=client.V1ObjectMeta(name=service_name),
|
|
51
|
+
spec=client.V1ServiceSpec(selector={"app": app_name},
|
|
52
|
+
type="NodePort",
|
|
53
|
+
ports=[client.V1ServicePort(name="driver-port",
|
|
54
|
+
protocol="TCP",
|
|
55
|
+
port=driver_port,
|
|
56
|
+
target_port=driver_port,
|
|
57
|
+
node_port=driver_port),
|
|
58
|
+
client.V1ServicePort(name="blockmanager-port",
|
|
59
|
+
protocol="TCP",
|
|
60
|
+
port=blockmanager_port,
|
|
61
|
+
target_port=blockmanager_port,
|
|
62
|
+
node_port=blockmanager_port)]
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
self.api.create_namespaced_service(namespace=namespace, body=service)
|
|
66
|
+
|
|
67
|
+
def remove_spark_nodeport(self, namespace, service_name):
|
|
68
|
+
self.api.delete_namespaced_service(service_name, namespace)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from typing import (Any, Dict)
|
|
2
|
+
from pyspark.sql import SparkSession
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SparkSessionBuilder:
|
|
6
|
+
def __init__(self, k8s_config="~/.kube/ciap_prd.conf") -> None:
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from pyspark import SparkConf
|
|
9
|
+
import uuid
|
|
10
|
+
self.spark_session = None
|
|
11
|
+
self._k8s_api = KubernetesController(k8s_config)
|
|
12
|
+
self._timestamp = datetime.now().strftime("%Y%m%d%H%M")
|
|
13
|
+
self._suffix = uuid.uuid4().hex[:8]
|
|
14
|
+
self._app_id = f"{self._timestamp}-{self._suffix}"
|
|
15
|
+
self._service_name = f"spark-{self._app_id}"
|
|
16
|
+
self._options = SparkConf()
|
|
17
|
+
self._my_namespace = self._k8s_api.get_my_pod_namespace()
|
|
18
|
+
self._my_appname = self._k8s_api.get_my_pod_app_selector()
|
|
19
|
+
self.appName(f"{self._my_appname}-{self._app_id}")
|
|
20
|
+
|
|
21
|
+
def config(self, k: str, v: Any) -> "SparkSessionBuilder":
|
|
22
|
+
self._options.set(k, v)
|
|
23
|
+
return self
|
|
24
|
+
|
|
25
|
+
def master(self, master: str) -> "SparkSessionBuilder":
|
|
26
|
+
return self.config("spark.master", master)
|
|
27
|
+
|
|
28
|
+
def appName(self, name: str) -> "SparkSessionBuilder":
|
|
29
|
+
return self.config("spark.app.name", name)
|
|
30
|
+
|
|
31
|
+
def getOrCreate(self) -> SparkSession:
|
|
32
|
+
def get_spark_nodeports():
|
|
33
|
+
return self._k8s_api.get_available_ports(count=2)
|
|
34
|
+
|
|
35
|
+
driver_port, blockmanager_port = get_spark_nodeports()
|
|
36
|
+
self._k8s_api.create_spark_nodeport(self._my_namespace,
|
|
37
|
+
self._service_name,
|
|
38
|
+
self._my_appname,
|
|
39
|
+
driver_port,
|
|
40
|
+
blockmanager_port)
|
|
41
|
+
(self.config("spark.driver.host", f"{self._my_appname}-{self._app_id}.spark-endpoint.cidp.io")
|
|
42
|
+
.config("spark.driver.port", driver_port)
|
|
43
|
+
.config("spark.blockManager.port", blockmanager_port))
|
|
44
|
+
|
|
45
|
+
from pyspark.sql import SparkSession
|
|
46
|
+
if self.spark_session is None:
|
|
47
|
+
self.spark_session = SparkSession.builder.config(conf=self._options).getOrCreate()
|
|
48
|
+
return self.spark_session
|
|
49
|
+
|
|
50
|
+
# def __del__(self):
|
|
51
|
+
# if self.spark_session is not None:
|
|
52
|
+
# self.spark_session.stop()
|
|
53
|
+
# self._k8s_api.remove_spark_nodeport(self._my_namespace, self._service_name)
|
|
54
|
+
# print("할당 해제")
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: cidp
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: CIDP Python SDK
|
|
5
|
+
Home-page: https://gitlab.cidp.io/common/pypi/cidp
|
|
6
|
+
Author: Example Author
|
|
7
|
+
Author-email: skt.mccho@sk.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.6
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cidp
|
cidp-0.0.1/setup.cfg
ADDED
cidp-0.0.1/setup.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import setuptools
|
|
2
|
+
|
|
3
|
+
# with open("README.md", "r") as fh:
|
|
4
|
+
# long_description = fh.read()
|
|
5
|
+
|
|
6
|
+
setuptools.setup(
|
|
7
|
+
name="cidp", # Replace with your own username
|
|
8
|
+
version="0.0.1",
|
|
9
|
+
author="Example Author",
|
|
10
|
+
author_email="skt.mccho@sk.com",
|
|
11
|
+
description="CIDP Python SDK",
|
|
12
|
+
# long_description=long_description,
|
|
13
|
+
# long_description_content_type="text/markdown",
|
|
14
|
+
url="https://gitlab.cidp.io/common/pypi/cidp",
|
|
15
|
+
packages=setuptools.find_packages(),
|
|
16
|
+
classifiers=[
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
],
|
|
21
|
+
python_requires='>=3.6',
|
|
22
|
+
)
|