client-go/examples/create-update-delete-deployment
John Kelly d6f3ab164c client-go: fix err scope in CRUD example
This fixes some scope isses that were introduced by shadowing vars inside anonymous functions as well as using a naked return.
Fixed by using unique err names and explicitly returning errors.
Additional improvement is using the HomeDir() util function provided by client-go instead of including a helper function at the bottom of this example.

Signed-off-by: John Kelly <jekohk@gmail.com>

Kubernetes-commit: e71c9f1b4beb06bcd3694163e6bf1764fb49edd6
2017-10-08 13:28:22 -04:00
..
BUILD client-go: fix err scope in CRUD example 2017-10-08 13:28:22 -04:00
main.go client-go: fix err scope in CRUD example 2017-10-08 13:28:22 -04:00
README.md client-go: use retry util in CRUD example 2017-10-05 19:07:57 -04:00

Create, Update & Delete Deployment

This example program demonstrates the fundamental operations for managing on Deployment resources, such as Create, List, Update and Delete.

You can adopt the source code from this example to write programs that manage other types of resources through the Kubernetes API.

Running this example

Make sure you have a Kubernetes cluster and kubectl is configured:

kubectl get nodes

Compile this example on your workstation:

cd create-update-delete-deployment
go build -o ./app

Now, run this application on your workstation with your local kubeconfig file:

./app
# or specify a kubeconfig file with flag
./app -kubeconfig=$HOME/.kube/config

Running this command will execute the following operations on your cluster:

  1. Create Deployment: This will create a 2 replica Deployment. Verify with kubectl get pods.
  2. Update Deployment: This will update the Deployment resource created in previous step by setting the replica count to 1 and changing the container image to nginx:1.13. You are encouraged to inspect the retry loop that handles conflicts. Verify the new replica count and container image with kubectl describe deployment demo.
  3. Rollback Deployment: This will rollback the Deployment to the last revision. In this case, it's the revision that was created in Step 1. Use kubectl describe to verify the container image is now nginx:1.12. Also note that the Deployment's replica count is still 1; this is because a Deployment revision is created if and only if the Deployment's pod template (.spec.template) is changed.
  4. List Deployments: This will retrieve Deployments in the default namespace and print their names and replica counts.
  5. Delete Deployment: This will delete the Deployment object and its dependent ReplicaSet resource. Verify with kubectl get deployments.

Each step is separated by an interactive prompt. You must hit the Return key to proceeed to the next step. You can use these prompts as a break to take time to run kubectl and inspect the result of the operations executed.

You should see an output like the following:

Creating deployment...
Created deployment "demo-deployment".
-> Press Return key to continue.

Updating deployment...
Updated deployment...
-> Press Return key to continue.

Rolling back deployment...
Rolled back deployment...
-> Press Return key to continue.

Listing deployments in namespace "default":
 * demo-deployment (1 replicas)
-> Press Return key to continue.

Deleting deployment...
Deleted deployment.

Cleanup

Successfully running this program will clean the created artifacts. If you terminate the program without completing, you can clean up the created deployment with:

kubectl delete deploy demo-deployment

Troubleshooting

If you are getting the following error, make sure Kubernetes version of your cluster is v1.6 or above in kubectl version:

panic: the server could not find the requested resource