GoogleCloudPlatform / deploymentmanager-samples

Deployment Manager samples and templates.
Apache License 2.0
939 stars 718 forks source link

fix openapi/v2 support in gke python sample code #545

Closed ingernet closed 6 months ago

ingernet commented 4 years ago

With Kubernetes' move to OpenAPI v2, the sample code in the gke directory broke. This PR fixes the Python code.

note: the reason the diffs look SO different is that the original code didn't observe PEP-8 indentation guidelines, so I fixed that while I was in there..

This code works after refactor:

# DEPLOY THE CLUSTER
$ gcloud deployment-manager deployments create ingernet --template cluster.py --properties zone:$ZONE
The fingerprint of the deployment is o3UIihCshClYWmcZCgwrcA==
Waiting for create [operation-1583425714879-5a01e07437c54-9a198cf2-650426d5]...done.          
Create operation operation-1583425714879-5a01e07437c54-9a198cf2-650426d5 completed successfully.
NAME                      TYPE                                   STATE      ERRORS  INTENT
ingernet-cluster-py       container.v1.cluster                   COMPLETED  []
ingernet-cluster-py-type  deploymentmanager.v2beta.typeProvider  COMPLETED  []

# DEPLOY THE DEPLOYMENT
$ gcloud deployment-manager deployments create ${NAME}-web --template deployment.py \
> --properties clusterType:${NAME}-cluster-py-type,image:${IMAGE},port:${PORT}
The fingerprint of the deployment is cuBGZETtHX7AHd3MeJTcmw==
Waiting for create [operation-1583426395133-5a01e2fcf578e-817735f2-1c6f1eb3]...done.          
Create operation operation-1583426395133-5a01e2fcf578e-817735f2-1c6f1eb3 completed successfully.
NAME                                   TYPE                           STATE      ERRORS  INTENT
ingernet-web-deployment-py-deployment  <REDACTED-PROJECT>/ingernet-clu  COMPLETED  []
                                       ster-py-type:/apis/apps/v1bet
                                       a1/namespaces/{namespace}/dep
                                       loyments/{name}
ingernet-web-deployment-py-service    <REDACTED-PROJECT>/ingernet-clu  COMPLETED  []
                                       ster-py-type:/api/v1/namespac
                                       es/{namespace}/services/{name
                                       }

# GET CREDENTIALS
$ gcloud container clusters get-credentials ${NAME}-cluster-py --zone ${ZONE}
Fetching cluster endpoint and auth data.
kubeconfig entry generated for ingernet-cluster-py.

# LOOK FOR DEPLOYMENTS AND SERVICES
$ kubectl get deployments
NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
ingernet-web-deployment-py-deployment   1/1     1            1           55s

$ kubectl get services
NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
ingernet-web-deployment-py-service   NodePort    <REDACTED-IP>   <none>        80:32709/TCP   66s
kubernetes                           ClusterIP   <REDACTED-CLUSTER-IP>     <none>        443/TCP        9m59s

# FIRE UP PORT FORWARDING
$ kubectl port-forward $(\
>   kubectl get pods --output=jsonpath="{.items[0].metadata.name}") \
>   9999:${PORT}
Forwarding from 127.0.0.1:9999 -> 80
Forwarding from [::1]:9999 -> 80

# IN A NEW WINDOW, CURL localhost:9999
$ curl http://localhost:9999
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...SNIP...

# IN THAT SAME NEW WINDOW, SET UP YOUR ENV VARIABLES
$NODES=$(kubectl get nodes --output=name | sed 's|node/||g')
$NODE_HOST=$(shuf -n1 -e ${NODES})
$ NODE_PORT=$(\
>   kubectl get services \
>   --selector=id=deployment-manager \
>   --output=jsonpath="{.items[0].spec.ports[0].nodePort}")

# ALSO IN THE NEW WINDOW, SSH INTO THE NODE
# (i had to specify the zone in my command because it was different from the profile i was using)
$ gcloud compute ssh ${NODE_HOST} --ssh-flag="-L ${NODE_PORT}:localhost:${NODE_PORT}" --zone=us-central1-b
Warning: Permanently added 'compute.1740778203171821088' (ED25519) to the list of known hosts.

Welcome to Kubernetes v1.14.10-gke.17!
...SNIP...