Assignment problem statement
Write operator to delete the pod, by taking pod name as input in spec CR
Operator-sdk is the framework used to write the operator.
Custom respource with api-version poddelete.example.com/v1alpha1 and kind: PodDelete is created
Spec contains namespace: where pod recides and podName: name of the pod
Reconcile method is called by api server for any operation to PodDelete object
Pod delete logic is implemented in reconcile funtion - pkg/controller/poddelete/poddelete_controller.go
func (r *ReconcilePodDelete) Reconcile(request reconcile.Request) (reconcile.Result, error) {
Currently operator watches all the namespace and can delete the pod in any namespace. Can be restricted via roles.
Operator is able to delete itself, but this can be ignored.
If pod is not found or is already in terminating state delete operation is ignored.
Apply crd, cluster-role, cluster-role-bindings, service-accounts for operator
$ kubectl apply -f deploy/crds/poddelete.example.com_poddeletes_crd.yaml
$ kubectl apply -f deploy/service_account.yaml
$ kubectl apply -f deploy/role.yaml
$ kubectl apply -f deploy/role_binding.yaml
$ kubectl apply -f deploy/operator.yaml
apiVersion: poddelete.example.com/v1alpha1
kind: PodDelete
metadata:
name: <name-of-cr>
spec:
namespace: <namespace where pod resides>
podName: <pod-name>
$ kubectl run nginx --image=nginx
$ kubectl apply -f <(echo "
apiVersion: poddelete.example.com/v1alpha1
kind: PodDelete
metadata:
name: delete-nginx-pod
spec:
namespace: default
podName: nginx
")