Skip to content

Commit

Permalink
redoing the example
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyankasaggu11929 committed May 13, 2021
1 parent 654815b commit 483c5ae
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions examples/deployment_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,15 @@
"""

import datetime
import pytz
from kubernetes import client, config


def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
import pytz

# Create a deployment object with client-python API. The deployment we
# created is same as the `nginx-deployment.yaml` in the /examples folder.
api = client.AppsV1Api()
from kubernetes import client, config

# Uncomment the following lines to enable debug logging
# c = client.Configuration()
# c.debug = True
# apps_v1 = client.AppsV1Api(api_client=client.ApiClient(configuration=c))
DEPLOYMENT_NAME = "nginx-deployment"

DEPLOYMENT_NAME = "nginx-deployment"

def create_deployment_object():
# Configureate Pod template container
container = client.V1Container(
name="nginx",
Expand Down Expand Up @@ -72,6 +60,10 @@ def main():
spec=spec,
)

return deployment


def create_deployment(api, deployment):
# Create deployement
resp = api.create_namespaced_deployment(
body=deployment, namespace="default"
Expand All @@ -89,6 +81,8 @@ def main():
)
)


def update_deployment(api, deployment):
# Update container image
deployment.spec.template.spec.containers[0].image = "nginx:1.16.0"

Expand All @@ -110,6 +104,7 @@ def main():
)


def restart_deployment(api, deployment):
# update `spec.template.metadata` section
# to add `kubectl.kubernetes.io/restartedAt` annotation
deployment.spec.template.metadata.annotations = {
Expand All @@ -132,8 +127,10 @@ def main():
resp.metadata.generation,
resp.spec.template.metadata.annotations,
)
)
)


def delete_deployment(api):
# Delete deployment
resp = api.delete_namespaced_deployment(
name=DEPLOYMENT_NAME,
Expand All @@ -145,5 +142,30 @@ def main():
print("\n[INFO] deployment `nginx-deployment` deleted.")


def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
apps_v1 = client.AppsV1Api()

# Uncomment the following lines to enable debug logging
# c = client.Configuration()
# c.debug = True
# apps_v1 = client.AppsV1Api(api_client=client.ApiClient(configuration=c))

# Create a deployment object with client-python API. The deployment we
# created is same as the `nginx-deployment.yaml` in the /examples folder.
deployment = create_deployment_object()

create_deployment(apps_v1, deployment)

update_deployment(apps_v1, deployment)

restart_deployment(apps_v1, deployment)

delete_deployment(apps_v1)


if __name__ == "__main__":
main()

0 comments on commit 483c5ae

Please sign in to comment.