OC(1)
π OC(1) β OpenShift CLI User Manuals π
okay bestie, so the manual you're looking at is literally called OpenShift CLI User Manuals and it was dated June 2016 β I'm gonna fold all that cute info in for you at the end, don't worry π
πΈ NAME πΈ
oc observe β observe changes to resources and react to them (experimental)
ngl bestie, the experimental tag is right there in the original, so don't come for me when this thing acts up. it's giving "still cooking" energy π
β¨ SYNOPSIS β¨
oc observe [OPTIONS]
that's literally it β oc observe plus whatever flags you wanna throw at it π
π¦ DESCRIPTION π¦
Observe changes to resources and take action on them.
okay so here's the whole vibe: this command helps you build scripted reactions to changes that happen in Kubernetes or OpenShift resources. Kubernetes people keep calling this a 'controller' and honestly it's a whole thing β it acts to ensure particular conditions are maintained. On startup, observe lists all the resources of a particular type and executes the provided script on each one. Then observe watches the server for changes, and reexecutes the script for every update. lowkey iconic behavior π
observe works best for problems of the form "for every resource X, make sure Y is true". Some examples of ways observe can be used include:
- Ensure every namespace has a quota or limit range object
- Ensure every service is registered in DNS by making calls to a DNS API
- Send an email alert whenever a node reports
'NotReady' - Watch for the
'FailedScheduling'event and write an IRC message - Dynamically provision persistent volumes when a new PVC is created
- Delete pods that have reached successful completion after a period of time
β¨ The simplest pattern is maintaining an invariant on an object β for instance, "every namespace should have an annotation that indicates its owner". If the object is deleted no reaction is necessary. A variation on that pattern is creating another object: "every namespace should have a quota object based on the resources allowed for an owner".
$ cat set_owner.sh
#!/bin/sh
if [[ "$(oc get namespace "$1" --template='{{ .metadata.annotations.owner }}')" == "" ]]; then
oc annotate namespace "$1" owner=bob
fi
$ oc observe namespaces -- ./set_owner.sh
The set_owner.sh script is invoked with a single argument (the namespace name) for each namespace. This simple script ensures that any user without the "owner" annotation gets one set, but preserves any existing value.
π«Ά The next common of controller pattern is provisioning β making changes in an external system to match the state of a Kubernetes resource. These scripts need to account for deletions that may take place while the observe command is not running. You can provide the list of known objects via the --names command, which should return a newline-delimited list of names or namespace/name pairs. Your command will be invoked whenever observe checks the latest state on the server β any resources returned by --names that are not found on the server will be passed to your --delete command.
For example, you may want to ensure that every node that is added to Kubernetes is added to your cluster inventory along with its IP:
$ cat add_to_inventory.sh
#!/bin/sh
echo "$1 $2" >> inventory
sort -u inventory -o inventory
$ cat remove_from_inventory.sh
#!/bin/sh
grep -vE "^$1 " inventory > /tmp/newinventory
mv -f /tmp/newinventory inventory
$ cat known_nodes.sh
#!/bin/sh
touch inventory
cut -f 1-1 -d ' ' inventory
$ oc observe nodes --template '{ .status.addresses[0].address }' \
--names ./known_nodes.sh \
--delete ./remove_from_inventory.sh \
-- ./add_to_inventory.sh
If you stop the observe command and then delete a node, when you launch observe again the contents of inventory will be compared to the list of nodes from the server, and any node in the inventory file that no longer exists will trigger a call to remove_from_inventory.sh with the name of the node.
β οΈ Important: when handling deletes, the previous state of the object may not be available and only the name/namespace of the object will be passed to your --delete command as arguments (all custom arguments are omitted). so likeβ¦ don't expect the full object, bestie. she's gone π
π More complicated interactions build on the two examples above β your inventory script could make a call to allocate storage on your infrastructure as a service, or register node names in DNS, or set complex firewalls. The more complex your integration, the more important it is to record enough data in the remote system that you can identify when resources on either side are deleted.
π OPTIONS π
okay so here's the whole flag menu, no skips, no cap:
-A,--all-namespaces=falseβ if true, list the requested object(s) across all projects. Project in current context is ignored. periodt.--allow-missing-template-keys=trueβ if true, ignore any errors in templates when a field or map key is missing in the template. Only applies togolangandjsonpathoutput formats. she's forgiving like that π-d,--delete=""β a command to run when resources are deleted. Specify multiple times to add arguments. stack 'em up π«Ά--exit-after=0β exit with status code0after the provided duration, optional. like a little timer bestie β°--listen-addr=":11251"β the name of an interface to listen on to expose metrics and health checking.--maximum-errors=20β exit after this many errors have been detected with. May be set to-1for no maximum. go off, infinite errors π₯--names=""β a command that will list all of the currently known names, optional. Specify multiple times to add arguments. Use to get notifications when objects are deleted.--object-env-var=""β the name of an env var to serialize the object to when calling the command, optional.--once=falseβ if true, exit with a status code0after all current objects have been processed. one and done energy β¨-o,--output="jsonpath"β output format. One of: (go-template,go-template-file,template,templatefile,jsonpath,jsonpath-as-json,jsonpath-file).--print-metrics-on-exit=falseβ if true, on exit write all metrics tostdout. metrics dump on the way out, love that for us π-q,--quiet=falseβ if true, skip printing information about each event prior to executing the command. shhh π€«--resync-period=0β when non-zero, periodically reprocess every item from the server as aSyncevent. Use to ensure external systems are kept up to date.--retry-count=2β the number of times to retry a failing command before continuing. she's persistent πͺ--retry-on-exit-code=0β if any command returns this exit code, retry up to--retry-counttimes.-l,--selector=""β selector (label query) to filter on, supports'=','==', and'!='. (e.g.-l key1=value1,key2=value2)--template=""β template string or path to template file to use when-o=go-template,-o=go-template-file. The template format is golang templates [β¨http://golang.org/pkg/text/template/#pkg-overviewβ©]. fancy π--type-env-var=""β the name of an env var to set with the type of event received ('Sync','Updated','Deleted','Added') to the reaction command or--delete.
π OPTIONS INHERITED FROM PARENT COMMANDS π
these come from the parent command, so they're likeβ¦ family heirlooms at this point π
--as=""β username to impersonate for the operation. User could be a regular user or a service account in a namespace.--as-group=[]β group to impersonate for the operation, this flag can be repeated to specify multiple groups.--as-uid=""β UID to impersonate for the operation.--as-user-extra=[]β user extras to impersonate for the operation, this flag can be repeated to specify multiple values for the same key.--cache-dir="/homeless-shelter/.kube/cache"β default cache directory.--certificate-authority=""β path to a cert file for the certificate authority.--client-certificate=""β path to a client certificate file for TLS.--client-key=""β path to a client key file for TLS.--cluster=""β the name of the kubeconfig cluster to use.--context=""β the name of the kubeconfig context to use.--disable-compression=falseβ if true, opt-out of response compression for all requests to the server.--insecure-skip-tls-verify=falseβ if true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure β οΈ (bestie, be careful with this one, fr fr)--kubeconfig=""β path to the kubeconfig file to use for CLI requests.--kuberc=""β path to the kuberc file to use for preferences. This can be disabled by exportingKUBECTL_KUBERC=falsefeature gate or turning off the featureKUBERC=off.--match-server-version=falseβ require server version to match client version.-n,--namespace=""β if present, the namespace scope for this CLI request.--profile="none"β name of profile to capture. One of (none|cpu|heap|goroutine|threadcreate|block|mutex).--profile-output="profile.pprof"β name of the file to write the profile to.--request-timeout="0"β the length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g.1s,2m,3h). A value of zero means don't timeout requests.-s,--server=""β the address and port of the Kubernetes API server.--tls-server-name=""β server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used.--token=""β bearer token for authentication to the API server.--user=""β the name of the kubeconfig user to use.--warnings-as-errors=falseβ treat warnings received from the server as errors and exit with a non-zero exit code.
π EXAMPLE π
here's the vibe check β actual examples, literally:
# Observe changes to services
oc observe services
# Observe changes to services, including the clusterIP and invoke a script for each
oc observe services --template '{ .spec.clusterIP }' -- register_dns.sh
# Observe changes to services filtered by a label selector
oc observe services -l regist-dns=true --template '{ .spec.clusterIP }' -- register_dns.sh
π SEE ALSO π
like, go read the main character's manpage too π«Ά
β HISTORY β
June 2016 β Ported from the Kubernetes man-doc generator.
okay bestie, that's the whole thing π this page was brought to you by OpenShift CLI User Manuals and dated June 2016 β and the original footer said Openshift on the left and OpenShift CLI User Manuals and OC(1) on the right, but I folded all that up here for you because we're cute like that π
now go observe some resources and slay, fr fr β¨π π¦
π¦ where this page comes from
- nixpkgs unstable: openshift 4.22.0-202605222050
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms