diff --git a/examples/create_and_list_namespaces.py b/examples/create_and_list_namespaces.py new file mode 100644 index 0000000000..527ad584c9 --- /dev/null +++ b/examples/create_and_list_namespaces.py @@ -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" + ) + ) + ) + + # Listing pods in the namespace that we created + list_namespaces = v1.list_namespaced_pod("kube-client") + + # 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) + ) + + +if __name__ == "__main__": + main() diff --git a/examples/create_ingress.py b/examples/create_ingress.py new file mode 100644 index 0000000000..4314bcd219 --- /dev/null +++ b/examples/create_ingress.py @@ -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() + + extension = client.ExtensionsV1beta1Api() + + body = client.ExtensionsV1beta1Ingress( + api_version="v1beta1", + 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", + body=body + ) + + +if __name__ == "__main__": + main() diff --git a/examples/create_job.py b/examples/create_job.py new file mode 100644 index 0000000000..5796cc65e9 --- /dev/null +++ b/examples/create_job.py @@ -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() + + # Volume + volume = client.V1Volume( + name="test-volume", + empty_dir=client.V1EmptyDirVolumeSource(medium="")) + + # Container + container = client.V1Container( + name="jobtest", + image="nginx:1.7.9", + 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 + ) + + +if __name__ == "__main__": + main() diff --git a/examples/create_service.py b/examples/create_service.py new file mode 100644 index 0000000000..d774fbbbd0 --- /dev/null +++ b/examples/create_service.py @@ -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() diff --git a/examples/deployment-strategies/deployment_ramped_v1.py b/examples/deployment-strategies/deployment_ramped_v1.py new file mode 100644 index 0000000000..5d928ece4c --- /dev/null +++ b/examples/deployment-strategies/deployment_ramped_v1.py @@ -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() diff --git a/examples/deployment-strategies/deployment_ramped_v2.py b/examples/deployment-strategies/deployment_ramped_v2.py new file mode 100644 index 0000000000..243dbe680e --- /dev/null +++ b/examples/deployment-strategies/deployment_ramped_v2.py @@ -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() diff --git a/examples/deployment-strategies/deployment_recreate_v1.py b/examples/deployment-strategies/deployment_recreate_v1.py new file mode 100644 index 0000000000..5da2bb053f --- /dev/null +++ b/examples/deployment-strategies/deployment_recreate_v1.py @@ -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"}), + strategy=client.ExtensionsV1beta1DeploymentStrategy(type="Recreate"), + 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() diff --git a/examples/deployment-strategies/deployment_recreate_v2.py b/examples/deployment-strategies/deployment_recreate_v2.py new file mode 100644 index 0000000000..3ff79dc53e --- /dev/null +++ b/examples/deployment-strategies/deployment_recreate_v2.py @@ -0,0 +1,59 @@ +# 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(type="Recreate"), + 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() diff --git a/examples/deployment_examples.py b/examples/deployment_examples.py index 29f55d34d3..5f6808602e 100644 --- a/examples/deployment_examples.py +++ b/examples/deployment_examples.py @@ -46,7 +46,7 @@ def create_deployment_object(): def create_deployment(api_instance, deployment): - # Create deployement + # Create deployment api_response = api_instance.create_namespaced_deployment( body=deployment, namespace="default") diff --git a/examples/post_start_hook.py b/examples/post_start_hook.py new file mode 100644 index 0000000000..7d8dd561a3 --- /dev/null +++ b/examples/post_start_hook.py @@ -0,0 +1,72 @@ +# 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.ExtensionsV1beta1Api() + + # Container + container = client.V1Container( + name="hooktest", + image="nginx:1.7.9", + image_pull_policy="IfNotPresent", + ports=[client.V1ContainerPort(container_port=80)], + + lifecycle=client.V1Lifecycle( + post_start=client.V1Handler( + _exec=client.V1ExecAction( + command=[ + 'echo \'Hello World\'' + ] + + ) + + ) + + ) + ) + + # Template + template = client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "hooktest"}), + spec=client.V1PodSpec(containers=[container])) + + # Spec + spec = client.ExtensionsV1beta1DeploymentSpec( + replicas=1, + template=template) + + # Deployment + deployment = client.ExtensionsV1beta1Deployment( + api_version="extensions/v1beta1", + kind="Deployment", + metadata=client.V1ObjectMeta(name="hooktest-deployment"), + spec=spec) + + # Creation of the Deployment in specified namespace + extension.create_namespaced_deployment( + namespace="kube-client", + body=deployment + ) + + +if __name__ == "__main__": + main() diff --git a/examples/pre_stop_hook.py b/examples/pre_stop_hook.py new file mode 100644 index 0000000000..916834303c --- /dev/null +++ b/examples/pre_stop_hook.py @@ -0,0 +1,71 @@ +# 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.ExtensionsV1beta1Api() + + # Container + container = client.V1Container( + name="nginx", + image="nginx:1.7.9", + image_pull_policy="IfNotPresent", + ports=[client.V1ContainerPort(container_port=80)], + lifecycle=client.V1Lifecycle( + pre_stop=client.V1Handler( + _exec=client.V1ExecAction( + command=[ + # Commands to be executed in the prestop hook + "touch kube-test.txt" + ] + ) + ) + ), + ) + + # Template + template = client.V1PodTemplateSpec( + metadata=client.V1ObjectMeta(labels={"app": "nginx"}), + spec=client.V1PodSpec(containers=[container]), + ) + + # Spec + spec = client.ExtensionsV1beta1DeploymentSpec( + replicas=1, + 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() diff --git a/examples/remote_cluster.py b/examples/remote_cluster.py index 8cf39efec5..5d3d5a79eb 100644 --- a/examples/remote_cluster.py +++ b/examples/remote_cluster.py @@ -23,7 +23,9 @@ def main(): # Define the barer token we are going to use to authenticate. # See here to create the token: # https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/ - aToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + aToken = "XXXXXXXXXXXXXXXXXXXXXX" \ + "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ + "XXXXXXXXXXXXXXXXXXXXXXXX" # Create a configuration object aConfiguration = client.Configuration()