Skip to content

Commit

Permalink
Allow disabling waiting for specific resources
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Dec 15, 2024
1 parent ae42baf commit 75e8c51
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
3 changes: 3 additions & 0 deletions api/v1alpha1/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ var (
// IfNotPresentAction is the annotation that defines if a Kubernetes resource
// should be applied only if it doesn't exist on the cluster.
IfNotPresentAction = fmt.Sprintf("action.%s/one-off", GroupVersion.Group)

// WaitAction is the annotation that defines if a Kubernetes resource should be included in the readiness check.
WaitAction = fmt.Sprintf("action.%s/wait", GroupVersion.Group)
)
45 changes: 38 additions & 7 deletions docs/cue/module/apply-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ with the `action.timoni.sh` annotations.

## Annotations

| CUE | Generated YAML |
|--------------------------|-------------------------------------|
| `timoniv1.Action.Force` | `action.timoni.sh/force: enabled` |
| `timoniv1.Action.OneOff` | `action.timoni.sh/one-off: enabled` |
| `timoniv1.Action.Keep` | `action.timoni.sh/prune: disabled` |
| CUE | Generated YAML |
|----------------------------------|-------------------------------------|
| `timoniv1.Action.Force` | `action.timoni.sh/force: enabled` |
| `timoniv1.Action.OneOff` | `action.timoni.sh/one-off: enabled` |
| `timoniv1.Action.Keep` | `action.timoni.sh/prune: disabled` |
| `timoniv1.Action.DisableWaiting` | `action.timoni.sh/wait: disabled` |

### Force Apply

Expand Down Expand Up @@ -72,10 +73,9 @@ import (
### Disable Pruning

To prevent Timoni's garbage collector from deleting certain
resources such as Kubernetes Persistent Volumes,
resources such as Kubernetes Persistent Volume Claims,
these resources can be annotated with `action.timoni.sh/prune: "disabled"`.


Example:

```cue
Expand All @@ -99,3 +99,34 @@ import (
}
```


### Disable Waiting

To prevent Timoni's readiness check from waiting for certain
resources such as Kubernetes Persistent Volumes,
these resources can be annotated with `action.timoni.sh/wait: "disabled"`.

Example:

```cue
package templates
import (
corev1 "k8s.io/api/core/v1"
timoniv1 "timoni.sh/core/v1alpha1"
)
#DatabasePV: corev1.#PersistentVolume & {
#config: #Config
apiVersion: "v1"
kind: "PersistentVolume"
metadata: timoniv1.#MetaComponent & {
#Meta: #config.metadata
#Component: "database"
}
metadata: annotations: timoniv1.Action.DisableWaiting
spec: {...}
}
```
21 changes: 16 additions & 5 deletions internal/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"cuelang.org/go/cue"
"github.com/fluxcd/pkg/ssa"
ssautil "github.com/fluxcd/pkg/ssa/utils"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kerrors "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -127,20 +128,30 @@ func (r *Reconciler) ApplyInstance(ctx context.Context, log logr.Logger, builder
})
}

func (a *Reconciler) Wait(ctx context.Context, log logr.Logger, _ *ssa.ChangeSet, rs *engine.ResourceSet) error {
func (r *Reconciler) Wait(ctx context.Context, log logr.Logger, _ *ssa.ChangeSet, rs *engine.ResourceSet) error {
doneMsg := ""
if rs != nil && rs.Name != "" {
doneMsg = fmt.Sprintf("%s resources ready", rs.Name)
}
return a.doWait(ctx, log, rs, "waiting for %d resource(s) to become ready", doneMsg)
return r.doWait(ctx, log, rs, "waiting for %d resource(s) to become ready", doneMsg)
}

func (r *Reconciler) doWait(_ context.Context, log logr.Logger, rs *engine.ResourceSet, progressMsgFmt string, doneMsg string) error {
if !r.opts.Wait {
if !r.opts.Wait || rs == nil || len(rs.Objects) == 0 {
return nil
}
progress := r.progressStartFn(fmt.Sprintf(progressMsgFmt, len(rs.Objects)))
err := r.resourceManager.Wait(rs.Objects, r.waitOptions)

var waitForObjects []*unstructured.Unstructured
for _, obj := range rs.Objects {
if !ssautil.AnyInMetadata(obj, map[string]string{
apiv1.WaitAction: apiv1.DisabledValue,
}) {
waitForObjects = append(waitForObjects, obj)
}
}

progress := r.progressStartFn(fmt.Sprintf(progressMsgFmt, len(waitForObjects)))
err := r.resourceManager.Wait(waitForObjects, r.waitOptions)
progress.Stop()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions schemas/timoni.sh/core/v1alpha1/action.cue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Action: {
Keep: {
"action.timoni.sh/prune": ActionStatus.Disabled
}
// DisableWaiting annotation is for excluding resouces from Timoni's readiness check.
DisableWaiting: {
"action.timoni.sh/wait": ActionStatus.Disabled
}
}

ActionStatus: {
Expand Down

0 comments on commit 75e8c51

Please sign in to comment.