With this namespace, the controller script constructs the URL to the Kubernetes API endpoint to watch the ConfigMaps. Note the  watch=true  query parameter in Example 22-2. This parameter indicates to the API Server not to close the HTTP con‐ nection but to send events along the response channel as soon as they happen ( hanging GET  or  Comet  are other names for this kind of technique). The loop reads every individual event as it arrives as a single item to process. As you can see, our controller contacts the Kubernetes API Server via localhost. We won’t deploy this script directly on the Kubernetes API master node, but then how can we use localhost in the script? As you may have probably guessed, another pat‐ tern kicks in here. We deploy this script in a Pod together with an  Ambassador  con‐ tainer that exposes port 8001 on localhost and proxies it to the real Kubernetes Service. See Chapter 17 for more details on this pattern. We see the actual Pod defini‐ tion with this  Ambassador  in detail later in this chapter. Watching events this way is not very robust, of course. The connection can stop any‐ time, so there should be a way to restart the loop. Also, one could miss events, so production-grade controllers should not only watch on events but from time to time also query the API Server for the entire current state and use that as the new base. For the sake of demonstrating the pattern, this is good enough. Within the loop, the logic shown in Example 22-4 is performed. Example 22-4. Controller reconciliation loop curl -N -s $base/api/v1/${ns}/configmaps?watch=true |  \ while  read -r event do type= $( echo "$event" | jq -r '.type' ) config_map= $( echo "$event" | jq -r '.object.metadata.name' ) annotations= $( echo "$event" | jq -r '.object.metadata.annotations' ) if  [ "$annotations" != "null" ];  then selector= $( echo $annotations |  \  jq -r "\ to_entries |\ .[] |\ select(.key == "k8spatterns.io/podDeleteSelector") |\ .value |\ @uri \ " ) fi if  [ $type = "MODIFIED" ] && [ -n "$selector" ];  then Solution  |  181