-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Added additional examples #898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
af35472
6fb0ec9
de09e49
45dda87
c78a923
c0e8011
ee4bd65
36d2fe7
86f37fa
962a2e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes import client, config | ||
|
||
|
||
def main(): | ||
# Fetching and loading Kubernetes Information | ||
config.load_kube_config() | ||
|
||
v1 = client.CoreV1Api() | ||
|
||
# Creating namespace | ||
create_namespace = v1.create_namespace(client.V1Namespace( | ||
metadata=client.V1ObjectMeta( | ||
name="kube-client-test" | ||
) | ||
) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the indentation looks weird. I'm surprised that pycodestyle doesn't catch this |
||
|
||
# Listing pods in the namespace that we created | ||
list_namespaces = v1.list_namespaced_pod("kube-client") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the namespace being queried is different from the namespace you just created before |
||
|
||
# Displaying Pod Information | ||
for i in list_namespaces.items: | ||
print("%s\t%s\t%s" % ( | ||
i.status.pod_ip, | ||
i.metadata.namespace, | ||
i.metadata.name) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for a new namespace there will be no pod in it. I don't think printing pod information will show meaningful information. Maybe just GET the namespace above instead? |
||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes import client, config | ||
|
||
|
||
def main(): | ||
# Fetching and loading Kubernetes Information | ||
config.load_kube_config() | ||
# For incluster details | ||
# config.incluster_kube_config() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
extension = client.ExtensionsV1beta1Api() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the official doc recommends using |
||
|
||
body = client.ExtensionsV1beta1Ingress( | ||
api_version="v1beta1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing api group in |
||
kind="Ingress", | ||
metadata=client.V1ObjectMeta( | ||
name="ingress-example", | ||
annotations={"nginx.ingress.kubernetes.io/rewrite-target": "/"}), | ||
spec=client.ExtensionsV1beta1IngressSpec( | ||
rules=[client.ExtensionsV1beta1IngressRule( | ||
host="abc.xyz.com", | ||
http=client.ExtensionsV1beta1HTTPIngressRuleValue( | ||
paths=list[client.ExtensionsV1beta1HTTPIngressPath( | ||
path="/api", | ||
backend=client.ExtensionsV1beta1IngressBackend( | ||
service_name="ingress", | ||
service_port=5000 | ||
|
||
) | ||
)] | ||
) | ||
) | ||
] | ||
) | ||
) | ||
|
||
extension.create_namespaced_ingress( | ||
namespace="kube-client", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this requires the |
||
body=body | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes import client, config | ||
|
||
|
||
def main(): | ||
# Fetching and loading Kubernetes Information | ||
config.load_kube_config() | ||
# For incluster details | ||
# config.incluster_kube_config() | ||
|
||
extension = client.BatchV1Api() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name the api object corresponding to the api being used, e.g. |
||
|
||
# Volume | ||
volume = client.V1Volume( | ||
name="test-volume", | ||
empty_dir=client.V1EmptyDirVolumeSource(medium="")) | ||
|
||
# Container | ||
container = client.V1Container( | ||
name="jobtest", | ||
image="nginx:1.7.9", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. running nginx doesn't sound like a good use case for job to me |
||
image_pull_policy="IfNotPresent", | ||
ports=[client.V1ContainerPort(container_port=80)], | ||
volume_mounts=[client.V1VolumeMount( | ||
name=volume.name, | ||
mount_path="/kube-example" | ||
)] | ||
) | ||
|
||
# Init-Container | ||
init_container = client.V1Container( | ||
name="init-container", | ||
image="alpine", | ||
image_pull_policy="IfNotPresent", | ||
command=[ | ||
"echo \"Hello World\"" | ||
], | ||
volume_mounts=[client.V1VolumeMount( | ||
name=volume.name, | ||
mount_path="/kube-example" | ||
)] | ||
) | ||
|
||
# Template | ||
template = client.V1PodTemplateSpec( | ||
metadata=client.V1ObjectMeta(labels={"app": "jobtest"}), | ||
spec=client.V1PodSpec( | ||
init_containers=[init_container], | ||
containers=[container], | ||
volumes=[volume], | ||
restart_policy="Never" | ||
) | ||
) | ||
|
||
# Spec | ||
spec_pod = client.V1JobSpec( | ||
ttl_seconds_after_finished=0, | ||
template=template | ||
) | ||
|
||
# job | ||
job = client.V1Job( | ||
kind="Job", | ||
metadata=client.V1ObjectMeta(name="jobtest"), | ||
spec=spec_pod | ||
) | ||
|
||
extension.create_namespaced_job( | ||
namespace="kube-client", | ||
body=job | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will the example print some information showing the job succeeded? I'd suggest creating the job template in the official doc and print the pod logs. I have a snippet written for the first step already #872 (comment) |
||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes import client, config | ||
|
||
|
||
def main(): | ||
# Fetching and loading Kubernetes Information | ||
config.load_kube_config() | ||
# For incluster details | ||
# config.incluster_kube_config() | ||
|
||
extension = client.CoreV1Api() | ||
|
||
body = client.V1Service( | ||
api_version="v1", | ||
kind="Service", | ||
metadata=client.V1ObjectMeta( | ||
name="service-example" | ||
), | ||
spec=client.V1ServiceSpec( | ||
selector={"app": "myapp"}, | ||
ports=[client.V1ServicePort( | ||
port=80, | ||
target_port=6785 | ||
)] | ||
) | ||
) | ||
extension.create_namespaced_service( | ||
namespace="kube-client", | ||
body=body) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes import client, config | ||
|
||
|
||
def main(): | ||
# Fetching and loading Kubernetes Information | ||
config.load_kube_config() | ||
|
||
extension = client.ExtensionsV1beta1Api() | ||
|
||
# Container | ||
container = client.V1Container( | ||
name="nginx", | ||
image="nginx:1.7.9", | ||
image_pull_policy="IfNotPresent", | ||
ports=[client.V1ContainerPort(container_port=80)] | ||
) | ||
|
||
# Template | ||
template = client.V1PodTemplateSpec( | ||
metadata=client.V1ObjectMeta(labels={"app": "nginx", "version": "v1.0"}), | ||
spec=client.V1PodSpec(containers=[container])) | ||
|
||
# Spec | ||
spec = client.ExtensionsV1beta1DeploymentSpec( | ||
replicas=3, | ||
selector=client.V1LabelSelector(match_labels={"app": "nginx"}), | ||
template=template) | ||
|
||
# Deployment | ||
deployment = client.ExtensionsV1beta1Deployment( | ||
api_version="extensions/v1beta1", | ||
kind="Deployment", | ||
metadata=client.V1ObjectMeta(name="nginx-deployment"), | ||
spec=spec) | ||
|
||
# Creation of the Deployment in specified namespace | ||
extension.create_namespaced_deployment( | ||
namespace="kube-client", | ||
body=deployment | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from kubernetes import client, config | ||
|
||
|
||
def main(): | ||
# Fetching and loading Kubernetes Information | ||
config.load_kube_config() | ||
|
||
extension = client.ExtensionsV1beta1Api() | ||
|
||
# Container | ||
container = client.V1Container( | ||
name="nginx", | ||
image="nginx:1.7.9", | ||
image_pull_policy="IfNotPresent", | ||
ports=[client.V1ContainerPort(container_port=80)] | ||
) | ||
|
||
# Template | ||
template = client.V1PodTemplateSpec( | ||
metadata=client.V1ObjectMeta(labels={"app": "nginx", "version": "v2.0"}), | ||
spec=client.V1PodSpec(containers=[container])) | ||
|
||
# Spec | ||
spec = client.ExtensionsV1beta1DeploymentSpec( | ||
replicas=3, | ||
selector=client.V1LabelSelector(match_labels={"app": "nginx"}), | ||
strategy=client.ExtensionsV1beta1DeploymentStrategy( | ||
rolling_update=client.ExtensionsV1beta1RollingUpdateDeployment( | ||
max_surge=1, | ||
max_unavailable=0)), | ||
template=template) | ||
|
||
# Deployment | ||
deployment = client.ExtensionsV1beta1Deployment( | ||
api_version="extensions/v1beta1", | ||
kind="Deployment", | ||
metadata=client.V1ObjectMeta(name="nginx-deployment"), | ||
spec=spec) | ||
|
||
# Creation of the Deployment in specified namespace | ||
extension.patch_namespaced_deployment( | ||
name="nginx-deployment", | ||
namespace="kube-client", | ||
body=deployment) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2019 (also in the other files)