Skip to content

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

Closed
wants to merge 10 commits into from
45 changes: 45 additions & 0 deletions examples/create_and_list_namespaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2016 The Kubernetes Authors.
Copy link
Member

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)

#
# 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"
)
)
)
Copy link
Member

Choose a reason for hiding this comment

The 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")
Copy link
Member

Choose a reason for hiding this comment

The 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 kube-client-test


# 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)
)
Copy link
Member

Choose a reason for hiding this comment

The 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()
57 changes: 57 additions & 0 deletions examples/create_ingress.py
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_incluster_config (also in the other files)


extension = client.ExtensionsV1beta1Api()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the official doc recommends using networking.k8s.io/v1beta1 api instead of extensions v1beta1


body = client.ExtensionsV1beta1Ingress(
api_version="v1beta1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing api group in api_version field

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this requires the kube-client namespace to be created first. I'd suggest using the default namespace for a standalone example (also in the other files)

body=body
)


if __name__ == "__main__":
main()
88 changes: 88 additions & 0 deletions examples/create_job.py
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name the api object corresponding to the api being used, e.g. batch_v1


# Volume
volume = client.V1Volume(
name="test-volume",
empty_dir=client.V1EmptyDirVolumeSource(medium=""))

# Container
container = client.V1Container(
name="jobtest",
image="nginx:1.7.9",
Copy link
Member

Choose a reason for hiding this comment

The 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
)
Copy link
Member

Choose a reason for hiding this comment

The 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()
46 changes: 46 additions & 0 deletions examples/create_service.py
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()
58 changes: 58 additions & 0 deletions examples/deployment-strategies/deployment_ramped_v1.py
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()
62 changes: 62 additions & 0 deletions examples/deployment-strategies/deployment_ramped_v2.py
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()
Loading