With Mast, you can create a simple and lightweight Kubernetes operator.
First, you need to create MastJob with your desired action to be performed when certain conditions are met. This is nothing new, you can check how to do it here
Then create a Mastrun, you can override some configuration in it if you need.
It is important to set
template: true, because you don't want to run immediately.
MastOperator is a third CRD in Mast. This is where you set what Mastjob will be started and on what conditions.
Let`s see how looks the definition of MastOperator.
objectsis where you set which object MastOperator should observe. It can be any object type available in Kubernetes cluster, including CRDs.
- Here, you set in which namespaces MastOperator should observe objects set earlier. You can pass desired namespace's names or put
allfor all namespaces in cluster.
- Set actions which performed on object types in set namespaces will trigger MastJob. For now, MastOperator supports
mastRunRefcan have one of two forms. It can contain one field
onEventwhich mean for every event we are subscribed run same MastRun. If
onEventis defined rest of options is ignored. Second option gives oportunity to serve different MastRun's for different actions, you can do it by setting
onDeletefields. In second option, if there is registered action without handler it will raise error.
or it can look like this for custom handlers:
Check code annotations to know how to set up a MastOperator.
passing variables from triggering object to MastJob
More advanced Operators may require to pass some variables from object which triggers MastOperator to MastJob executed on MastOperator`s behalf.
Imagine a case where you deploy a new Java App. You want to automatically create a service for it, but you would like to name it the same name as Java App pod`s for better discovery in the future.
This is similar to rendering variables in Ansible using Jinja2 templating. You have to set variables in vars section either in MastJob or MastRun. See example below.
You want to pass the name of the object which triggered the operator. In MastJob, you will create that var like this:
resource is a key under which the specification of the triggering object is stored. To access a certain value just go down the schema, like above to get the object name which is always in metadata.