Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
bfe37a82
Commit
bfe37a82
authored
Nov 17, 2015
by
sreek
Browse files
Options
Downloads
Patches
Plain Diff
Address code review comments
parent
7b73966e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools/gke/kubernetes_api.py
+25
-25
25 additions, 25 deletions
tools/gke/kubernetes_api.py
with
25 additions
and
25 deletions
tools/gke/kubernetes_api.py
+
25
−
25
View file @
bfe37a82
...
@@ -31,9 +31,9 @@
...
@@ -31,9 +31,9 @@
import
requests
import
requests
import
json
import
json
REQUEST_TIMEOUT_SECS
=
10
_
REQUEST_TIMEOUT_SECS
=
10
def
make_pod_config
(
pod_name
,
image_name
,
container_port_list
,
cmd_list
,
def
_
make_pod_config
(
pod_name
,
image_name
,
container_port_list
,
cmd_list
,
arg_list
):
arg_list
):
"""
Creates a string containing the Pod defintion as required by the Kubernetes API
"""
"""
Creates a string containing the Pod defintion as required by the Kubernetes API
"""
body
=
{
body
=
{
...
@@ -69,12 +69,12 @@ def make_pod_config(pod_name, image_name, container_port_list, cmd_list,
...
@@ -69,12 +69,12 @@ def make_pod_config(pod_name, image_name, container_port_list, cmd_list,
return
json
.
dumps
(
body
)
return
json
.
dumps
(
body
)
def
make_service_config
(
service_name
,
pod_name
,
service_port_list
,
def
_
make_service_config
(
service_name
,
pod_name
,
service_port_list
,
container_port_list
,
is_headless
):
container_port_list
,
is_headless
):
"""
Creates a string containing the Service definition as required by the Kubernetes API.
"""
Creates a string containing the Service definition as required by the Kubernetes API.
NOTE:
NOTE:
This creates either a Headless
*
Service or
'
LoadBalancer
'
service depending on
This creates either a Headless Service or
'
LoadBalancer
'
service depending on
the is_headless parameter. For Headless services, there is no
'
type
'
attribute
the is_headless parameter. For Headless services, there is no
'
type
'
attribute
and the
'
clusterIP
'
attribute is set to
'
None
'
. Also, if the service is
and the
'
clusterIP
'
attribute is set to
'
None
'
. Also, if the service is
Headless, Kubernetes creates DNS entries for Pods - i.e creates DNS A-records
Headless, Kubernetes creates DNS entries for Pods - i.e creates DNS A-records
...
@@ -120,44 +120,44 @@ def make_service_config(service_name, pod_name, service_port_list,
...
@@ -120,44 +120,44 @@ def make_service_config(service_name, pod_name, service_port_list,
return
json
.
dumps
(
body
)
return
json
.
dumps
(
body
)
def
print_connection_error
(
msg
):
def
_
print_connection_error
(
msg
):
print
(
'
ERROR: Connection failed. Did you remember to run Kubenetes proxy on
'
print
(
'
ERROR: Connection failed. Did you remember to run Kubenetes proxy on
'
'
localhost (i.e kubectl proxy --port=<proxy_port>) ?. Error: %s
'
%
msg
)
'
localhost (i.e kubectl proxy --port=<proxy_port>) ?. Error: %s
'
%
msg
)
def
do_post
(
post_url
,
api_name
,
request_body
):
def
_
do_post
(
post_url
,
api_name
,
request_body
):
"""
Helper to do HTTP POST.
"""
Helper to do HTTP POST.
Note:
Note:
1) On success, Kubernetes returns a success code of 201(CREATED) not 200(OK)
1) On success, Kubernetes returns a success code of 201(CREATED) not 200(OK)
2) A response code of 509(CONFLICT) is interpreted as a success code (since
2) A response code of 509(CONFLICT) is interpreted as a success code (since
the error is most likely due to the resource already existing). This makes
the error is most likely due to the resource already existing). This makes
do_pos() idempotent which is semantically desirable.
_
do_pos
t
() idempotent which is semantically desirable.
"""
"""
is_success
=
True
is_success
=
True
try
:
try
:
r
=
requests
.
post
(
post_url
,
data
=
request_body
,
timeout
=
REQUEST_TIMEOUT_SECS
)
r
=
requests
.
post
(
post_url
,
data
=
request_body
,
timeout
=
_
REQUEST_TIMEOUT_SECS
)
if
r
.
status_code
==
requests
.
codes
.
conflict
:
if
r
.
status_code
==
requests
.
codes
.
conflict
:
print
(
'
WARN: Looks like the resource already exists. Api: %s, url: %s
'
%
print
(
'
WARN: Looks like the resource already exists. Api: %s, url: %s
'
%
(
api_name
,
post_url
))
(
api_name
,
post_url
))
elif
r
.
status_code
!=
requests
.
codes
.
created
and
r
.
status_code
!=
requests
.
codes
.
ok
:
elif
r
.
status_code
!=
requests
.
codes
.
created
:
print
(
'
ERROR: %s API returned error. HTTP response: %s
'
%
print
(
'
ERROR: %s API returned error. HTTP response:
(%d)
%s
'
%
(
api_name
,
r
.
text
))
(
api_name
,
r
.
status_code
,
r
.
text
))
is_success
=
False
is_success
=
False
except
(
requests
.
exceptions
.
Timeout
,
requests
.
exceptions
.
ConnectionError
)
as
e
:
except
(
requests
.
exceptions
.
Timeout
,
requests
.
exceptions
.
ConnectionError
)
as
e
:
is_success
=
False
is_success
=
False
print_connection_error
(
str
(
e
))
_
print_connection_error
(
str
(
e
))
return
is_success
return
is_success
def
do_delete
(
del_url
,
api_name
):
def
_
do_delete
(
del_url
,
api_name
):
"""
Helper to do HTTP DELETE.
"""
Helper to do HTTP DELETE.
Note: A response code of 404(NOT_FOUND) is treated as success to keep
Note: A response code of 404(NOT_FOUND) is treated as success to keep
do_delete() idempotent.
_
do_delete() idempotent.
"""
"""
is_success
=
True
is_success
=
True
try
:
try
:
r
=
requests
.
delete
(
del_url
,
timeout
=
REQUEST_TIMEOUT_SECS
)
r
=
requests
.
delete
(
del_url
,
timeout
=
_
REQUEST_TIMEOUT_SECS
)
if
r
.
status_code
==
requests
.
codes
.
not_found
:
if
r
.
status_code
==
requests
.
codes
.
not_found
:
print
(
'
WARN: The resource does not exist. Api: %s, url: %s
'
%
print
(
'
WARN: The resource does not exist. Api: %s, url: %s
'
%
(
api_name
,
del_url
))
(
api_name
,
del_url
))
...
@@ -167,20 +167,20 @@ def do_delete(del_url, api_name):
...
@@ -167,20 +167,20 @@ def do_delete(del_url, api_name):
is_success
=
False
is_success
=
False
except
(
requests
.
exceptions
.
Timeout
,
requests
.
exceptions
.
ConnectionError
)
as
e
:
except
(
requests
.
exceptions
.
Timeout
,
requests
.
exceptions
.
ConnectionError
)
as
e
:
is_success
=
False
is_success
=
False
print_connection_error
(
str
(
e
))
_
print_connection_error
(
str
(
e
))
return
is_success
return
is_success
def
create_service
(
kube_host
,
kube_port
,
namespace
,
service_name
,
pod_name
,
def
create_service
(
kube_host
,
kube_port
,
namespace
,
service_name
,
pod_name
,
service_port_list
,
container_port_list
,
is_headless
):
service_port_list
,
container_port_list
,
is_headless
):
"""
Creates
a
either a Headless Service or a LoadBalancer Service depending
"""
Creates either a Headless Service or a LoadBalancer Service depending
on the is_headless parameter.
on the is_headless parameter.
"""
"""
post_url
=
'
http://%s:%d/api/v1/namespaces/%s/services
'
%
(
post_url
=
'
http://%s:%d/api/v1/namespaces/%s/services
'
%
(
kube_host
,
kube_port
,
namespace
)
kube_host
,
kube_port
,
namespace
)
request_body
=
make_service_config
(
service_name
,
pod_name
,
service_port_list
,
request_body
=
_
make_service_config
(
service_name
,
pod_name
,
service_port_list
,
container_port_list
,
is_headless
)
container_port_list
,
is_headless
)
return
do_post
(
post_url
,
'
Create Service
'
,
request_body
)
return
_
do_post
(
post_url
,
'
Create Service
'
,
request_body
)
def
create_pod
(
kube_host
,
kube_port
,
namespace
,
pod_name
,
image_name
,
def
create_pod
(
kube_host
,
kube_port
,
namespace
,
pod_name
,
image_name
,
...
@@ -193,13 +193,13 @@ def create_pod(kube_host, kube_port, namespace, pod_name, image_name,
...
@@ -193,13 +193,13 @@ def create_pod(kube_host, kube_port, namespace, pod_name, image_name,
Controller
'
which creates a configurable number of
'
identical Replicas
'
of
Controller
'
which creates a configurable number of
'
identical Replicas
'
of
Pods and automatically restarts any Pods in case of failures (for eg: Machine
Pods and automatically restarts any Pods in case of failures (for eg: Machine
failures in Kubernetes). This makes it less flexible for our test use cases
failures in Kubernetes). This makes it less flexible for our test use cases
where we might want
to
slightly different set of args to each Pod
and h
ence we
where we might want slightly different set of args to each Pod
. H
ence we
directly create Pods
(
and not care much about Kubernetes failures since those
directly create Pods and not care much about Kubernetes failures since those
are very rare
)
.
are very rare.
"""
"""
post_url
=
'
http://%s:%d/api/v1/namespaces/%s/pods
'
%
(
kube_host
,
kube_port
,
post_url
=
'
http://%s:%d/api/v1/namespaces/%s/pods
'
%
(
kube_host
,
kube_port
,
namespace
)
namespace
)
request_body
=
make_pod_config
(
pod_name
,
image_name
,
container_port_list
,
request_body
=
_
make_pod_config
(
pod_name
,
image_name
,
container_port_list
,
cmd_list
,
arg_list
)
cmd_list
,
arg_list
)
return
do_post
(
post_url
,
'
Create Pod
'
,
request_body
)
return
do_post
(
post_url
,
'
Create Pod
'
,
request_body
)
...
@@ -207,10 +207,10 @@ def create_pod(kube_host, kube_port, namespace, pod_name, image_name,
...
@@ -207,10 +207,10 @@ def create_pod(kube_host, kube_port, namespace, pod_name, image_name,
def
delete_service
(
kube_host
,
kube_port
,
namespace
,
service_name
):
def
delete_service
(
kube_host
,
kube_port
,
namespace
,
service_name
):
del_url
=
'
http://%s:%d/api/v1/namespaces/%s/services/%s
'
%
(
del_url
=
'
http://%s:%d/api/v1/namespaces/%s/services/%s
'
%
(
kube_host
,
kube_port
,
namespace
,
service_name
)
kube_host
,
kube_port
,
namespace
,
service_name
)
return
do_delete
(
del_url
,
'
Delete Service
'
)
return
_
do_delete
(
del_url
,
'
Delete Service
'
)
def
delete_pod
(
kube_host
,
kube_port
,
namespace
,
pod_name
):
def
delete_pod
(
kube_host
,
kube_port
,
namespace
,
pod_name
):
del_url
=
'
http://%s:%d/api/v1/namespaces/%s/pods/%s
'
%
(
kube_host
,
kube_port
,
del_url
=
'
http://%s:%d/api/v1/namespaces/%s/pods/%s
'
%
(
kube_host
,
kube_port
,
namespace
,
pod_name
)
namespace
,
pod_name
)
return
do_delete
(
del_url
,
'
Delete Pod
'
)
return
_
do_delete
(
del_url
,
'
Delete Pod
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment