You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/libs/status.md
+23-50Lines changed: 23 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,34 +3,19 @@
3
3
## Conditions
4
4
5
5
The `pkg/conditions` package helps with managing condition lists which can often be found in the status of k8s resources.
6
+
It expects the conditions to be of the `Condition` type from the `k8s.io/apimachinery/pkg/apis/meta/v1` package.
6
7
7
-
The managed condition implementation must satisfy the `Condition[T comparable]` interface:
8
-
```go
9
-
typeCondition[T comparable] interface {
10
-
GetType() string
11
-
SetType(conType string)
12
-
GetStatus() T
13
-
SetStatus(status T)
14
-
GetLastTransitionTime() time.Time
15
-
SetLastTransitionTime(timestamp time.Time)
16
-
GetReason() string
17
-
SetReason(reason string)
18
-
GetMessage() string
19
-
SetMessage(message string)
20
-
}
21
-
```
22
-
23
-
To manage conditions, use the `ConditionUpdater` function and pass in a constructor function for your condition implementation and the old list of conditions. The bool argument determines whether old conditions that are not updated remain in the returned list (`false`) or are removed, so that the returned list contains only the conditions that were touched (`true`).
8
+
To manage conditions, use the `ConditionUpdater` function and pass in the old list of conditions. The bool argument determines whether old conditions that are not updated remain in the returned list (`false`) or are removed, so that the returned list contains only the conditions that were touched (`true`).
Note that the `ConditionUpdater` stores the current time upon initialization and will set each updated condition's timestamp to this value, if the status of that condition changed as a result of the update. To use a different timestamp, manually overwrite the `Now` field of the updater.
30
15
31
16
Use `UpdateCondition` or `UpdateConditionFromTemplate` to update a condition:
If all conditions are updated, use the `Conditions` method to generate the new list of conditions. The originally passed in list of conditions is not modified by the updater.
-`v1alpha1.MyResource` is the resource type being reconciled in this example.
119
-
-`v1alpha1.MyResourcePhase` is the type of the `Phase` field used in the status of `MyResource`.
120
-
- It must be a string-like type, e.g. `type MyResourcePhase string`.
121
-
- If the resource status doesn't have a `Phase` or updating it is not desired, simply set this type argument to `string`.
122
-
-`v1alpha1.ConditionStatus` is the type of the `Status` field within the conditions. It must be `comparable`.
123
-
- Usually, this will either be a boolean or a string-like type.
124
-
- If the resource status doesn't have conditions or updating them is not desired, simply set this type argument to `bool`.
125
-
- The conditions must be a list of a type `T`, where either `T` or `*T` implements the `conditions.Condition[ConType]` interface.
126
-
- `ConType` is `v1alpha1.ConditionStatus` in this example.
127
-
100
+
`v1alpha1.MyResource` is the resource type being reconciled in this example.
128
101
129
102
### How to use the status updater
130
103
131
104
It is recommended to move the actual reconciliation logic into a helper function (`reconcile` in the example). This makes it easier to ensure that the status updater is always called, no matter where the reconciliation exits, e.g. due to an error. This helper function should then return the `ReconcileResult` required by the status updater.
It takes the type of the reconciled resource, the type of its `Phase` attribute and the type of the `Status` attribute of its conditions as type arguments.
110
+
It takes the type of the reconciled resourceas type argument.
138
111
139
112
If you want to update the phase, you have to pass in a function that computes the new phase based on the the current state of the object and the returned reconcile result. Note that the function just has to return the phase, not to set it in the object. Failing to provide this function causes the updater to use a dummy implementation that sets the phase to the empty string.
If the conditions should be updated, the `WithConditionUpdater` method must be called. Similarly to the condition updater from the `conditions` package - which is used internally - it requires a constructor function that returns a new, empty instance of the controller-specific `conditions.Condition` implementation. The second argument specifies whether existing conditions that are not part of the updated conditions in the `ReconcileResult` should be removed or kept.
129
+
If the conditions should be updated, the `WithConditionUpdater` method must be called. The argument specifies whether existing conditions that are not part of the updated conditions in the `ReconcileResult` should be removed or kept.
157
130
158
131
You can then `Build()` the status updater and run `UpdateStatus()` to do the actual status update. The return values of this method are meant to be returned by the `Reconcile` function.
// UpdateCondition updates or creates the condition with the specified type.
50
48
// All fields of the condition are updated with the values given in the arguments, but the condition's LastTransitionTime is only updated (with the timestamp contained in the receiver struct) if the status changed.
0 commit comments