|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * operators/operator_sdk/osdk-generating-csvs.adoc |
| 4 | + |
| 5 | +[id="osdk-operatorconditions_{context}"] |
| 6 | += Enabling Operator conditions |
| 7 | + |
| 8 | +Operator Lifecycle Manager (OLM) provides Operators with a channel to communicate complex states that influence OLM behavior while managing the Operator. By default, OLM creates an `OperatorCondition` custom resource definition (CRD) when it installs an Operator. Based on the conditions set in the `OperatorCondition` custom resource (CR), the behavior of OLM changes accordingly. |
| 9 | + |
| 10 | +To support Operator conditions, an Operator must be able to read the `OperatorCondition` CR created by OLM and have the ability to: |
| 11 | + |
| 12 | +* Get the specific condition. |
| 13 | +* Set the status of a specific condition. |
| 14 | + |
| 15 | +This can be accomplished by using the link:https://github.com/operator-framework/operator-lib/tree/v0.3.0[`operator-lib`] library. An Operator author can provide a link:https://github.com/kubernetes-sigs/controller-runtime/tree/master/pkg/client[`controller-runtime` client] in their Operator for the library to access the `OperatorCondition` CR owned by the Operator in the cluster. |
| 16 | + |
| 17 | +The library provides a generic `Conditions` interface, which has the following methods to `Get` and `Set` a `conditionType` in the `OperatorCondition` CR: |
| 18 | + |
| 19 | +`Get`:: To get the specific condition, the library uses the `client.Get` function from `controller-runtime`, which requires an `ObjectKey` of type `types.NamespacedName` present in `conditionAccessor`. |
| 20 | + |
| 21 | +`Set`:: To update the status of the specific condition, the library uses the `client.Update` function from `controller-runtime`. An error occurs if the `conditionType` is not present in the CRD. |
| 22 | + |
| 23 | +The Operator is allowed to modify only the `status` subresource of the CR. Operators can either delete or update the `status.conditions` array to include the condition. For more details on the format and description of the fields present in the conditions, see the upstream link:https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#Condition[Condition GoDocs]. |
| 24 | + |
| 25 | +[NOTE] |
| 26 | +==== |
| 27 | +Operator SDK v1.3.0 supports `operator-lib` v0.3.0. |
| 28 | +==== |
| 29 | + |
| 30 | +.Prerequisites |
| 31 | + |
| 32 | +* An Operator project generated using the Operator SDK. |
| 33 | + |
| 34 | +.Procedure |
| 35 | + |
| 36 | +To enable Operator conditions in your Operator project: |
| 37 | + |
| 38 | +. In the `go.mod` file of your Operator project, add `operator-framework/operator-lib` as a required library: |
| 39 | ++ |
| 40 | +[source,go] |
| 41 | +---- |
| 42 | +module github.com/example-inc/memcached-operator |
| 43 | +
|
| 44 | +go 1.15 |
| 45 | +
|
| 46 | +require ( |
| 47 | + k8s.io/apimachinery v0.19.2 |
| 48 | + k8s.io/client-go v0.19.2 |
| 49 | + sigs.k8s.io/controller-runtime v0.7.0 |
| 50 | + operator-framework/operator-lib v0.3.0 |
| 51 | +) |
| 52 | +---- |
| 53 | + |
| 54 | +. Write your own constructor in your Operator logic that: |
| 55 | ++ |
| 56 | +-- |
| 57 | +* Accepts a `controller-runtime` client. |
| 58 | +* Accepts a `conditionType`. |
| 59 | +* Returns a `Condition` interface to update or add conditions. |
| 60 | +-- |
| 61 | ++ |
| 62 | +Because OLM currently supports the `Upgradeable` condition, you can create an interface that has methods to access the `Upgradeable` condition. For example: |
| 63 | ++ |
| 64 | +[source,go] |
| 65 | +---- |
| 66 | +import ( |
| 67 | + ... |
| 68 | + apiv1 "github.com/operator-framework/api/pkg/operators/v1" |
| 69 | +) |
| 70 | +
|
| 71 | +func NewUpgradeable(cl client.Client) (Condition, error) { |
| 72 | + return NewCondition(cl, "apiv1.OperatorUpgradeable") |
| 73 | +} |
| 74 | +
|
| 75 | +cond, err := NewUpgradeable(cl); |
| 76 | +---- |
| 77 | ++ |
| 78 | +In this example, the `NewUpgradeable` constructor is further used to create a variable `cond` of type `Condition`. The `cond` variable would in turn have `Get` and `Set` methods, which can be used for handling the OLM `Upgradeable` condition. |
0 commit comments