Skip to content

Commit

Permalink
Merge pull request #57 from hightall/issue52
Browse files Browse the repository at this point in the history
[Fix #52] Fix k8s 1.16 later version not support.
  • Loading branch information
hightall authored Oct 30, 2019
2 parents 292f8fd + 36ccbd9 commit 7b9d5fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
31 changes: 26 additions & 5 deletions src/agent/kubernetes-agent/src/utils/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def __init__(self, config_file=None):
self._config_file = config_file
config.load_kube_config(config_file)

self._major, self._minor = self._get_version_code()

def list_pods(self):
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
Expand All @@ -25,6 +27,20 @@ def list_pods(self):
% (i.status.pod_ip, i.metadata.namespace, i.metadata.name)
)

def _get_version_code(self):
version_api = client.VersionApi()
major = 1
minor = 16

try:
api_response = version_api.get_code()
major = int(api_response.major)
minor = int(api_response.minor)
except ApiException as e:
LOG.error("Exception when calling VersionApi->get_code: %s", e)

return major, minor

def get_pod(self, namespace=None, deploy_name=None):
v1 = client.CoreV1Api()
pod = None
Expand Down Expand Up @@ -157,22 +173,27 @@ def _generate_pod_template(self, *args, **kwargs):
metadata=spec_metadata, spec=pod_spec
)

LOG.info("template spec %s", template_spec)

return template_spec

def create_deployment(self, namespace=None, *args, **kwargs):
deploy_name = kwargs.get("name")
deployment_metadata = client.V1ObjectMeta(name=deploy_name)
template_spec = self._generate_pod_template(*args, **kwargs)
body = client.ExtensionsV1beta1Deployment(
api_version="extensions/v1beta1",
body = client.V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=deployment_metadata,
spec=client.ExtensionsV1beta1DeploymentSpec(
spec=client.V1DeploymentSpec(
selector=client.V1LabelSelector(match_labels={
"app": kwargs.get("name"),
}),
template=template_spec
),
)

api_instance = client.ExtensionsV1beta1Api()
api_instance = client.AppsV1Api()

try:
api_instance.create_namespaced_deployment(
Expand Down Expand Up @@ -306,7 +327,7 @@ def delete_job(self, namespace=None, name=None):
LOG.error("Exception when call AppsV1beta1Api: %s", e)

def delete_deployment(self, namespace=None, name=None):
api_instance = client.ExtensionsV1beta1Api()
api_instance = client.AppsV1Api()
delete_options = client.V1DeleteOptions(
propagation_policy="Foreground"
)
Expand Down
2 changes: 1 addition & 1 deletion src/api-engine/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ watchdog<=0.9.0
django-allauth<=0.39.1
django-rest-auth<=0.9.3
djangorestframework-jwt<=1.11.0
kubernetes<=9.0.0
kubernetes<=10.0.1
django-cors-headers

0 comments on commit 7b9d5fd

Please sign in to comment.