Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pier-oliviert/konditionner
Manage conditions for your Custom Resources (K8S)
https://github.com/pier-oliviert/konditionner
kube-builder kubernetes kubernetes-operator
Last synced: 17 days ago
JSON representation
Manage conditions for your Custom Resources (K8S)
- Host: GitHub
- URL: https://github.com/pier-oliviert/konditionner
- Owner: pier-oliviert
- License: mit
- Created: 2024-09-09T20:04:28.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-10-09T17:44:04.000Z (3 months ago)
- Last Synced: 2024-10-31T10:22:40.782Z (2 months ago)
- Topics: kube-builder, kubernetes, kubernetes-operator
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Konditionner: Conditions for K8s Custom Resources
[![Tests](https://github.com/pier-oliviert/konditionner/actions/workflows/tests.yaml/badge.svg)](https://github.com/pier-oliviert/konditionner/actions/workflows/tests.yaml)
[![Go Reference](https://pkg.go.dev/badge/github.com/pier-oliviert/konditionner/pkg/konditions.svg)](https://pkg.go.dev/github.com/pier-oliviert/konditionner/pkg/konditions)This library exists to help manage conditions for people that builds Custom Resources Definitions(CRDs) for Kubernetes. Konditionner is built on the same idea as the utility package from [API Machinery](https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Condition) but with extensibility in mind.
The library is a great addition to your custom resources if your operator uses [controller-runtime](https://github.com/kubernetes-sigs/controller-runtime).
## Features
- Reasonable default values for you to use;
- Extensible Status and Types as you see fit;
- Set of utility functions to create, update and delete conditions;## Works natively with Kubernetes Custom Resources
Conditions are ideally used with [Status subresources](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#subresources):
```go
# A Custom Resource Spec & Statustype MyRecord struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`Spec MySpec `json:"spec,omitempty"`
Status MyStatus `json:"status,omitempty"`
}type MySpec struct {
// ... Fields ...
}type VolumeFinder konditions.ConditionType = "Volumes"
type SecretMapping konditions.ConditionType = "Secrets"type MyStatus struct {
Conditions konditions.Conditions `json:"conditions,omitempty"`
}```
Once the record includes conditions, you can use it to control the handling of your resource within the reconciliation loop.
```go
func (r *MyRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var record MyRecord
if err := r.Get(ctx, req, &record); err != nil {
return ctrl.Result{}, err
}if record.Conditions().AnyWithStatus(konditions.ConditionError) {
// The record has failed, do not touch the record
return ctrl.Result{}, nil
}lock := konditions.NewLock(record, r.Client, VolumeFinder)
err := lock.Execute(ctx, func(condition konditions.Condition) error {
// Volume Finder need to be reconciled.
err := r.ReconcileVolume(ctx, record, &condition)
if err != nil {
return err
}record.Conditions().SetCondition(condition)
return ctrl.Result{}, r.Status().Update(ctx, record)
})// ...
return ctrl.Result{}, err
}func (r *MyRecordReconciler) ReconcileVolume(ctx context.Context, record *MyRecord, condition *konditions.Condition) error {
switch condition.Status {
case konditions.ConditionInitialized:
// ... Start working on the Volume Finder conditionif err != nil {
return err
}condition.Status = konditions.ConditionCreated
condition.Reason = "Volume Found"
return nilcase konditions.ConditionCreated:
// ... Volume was created, let's configure it for our record
}return nil
}```
## Contributing
If you'd like to help or you need to make modification to the project to make it better for you, feel free to create issues/pull requests as you see fit. I don't really know the scope of this project yet, so I'll be pretty flexible as to what can be part of Konditionner.