This commit is contained in:
68
pkg/condition/condition.go
Normal file
68
pkg/condition/condition.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package conditions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
type Conditioner interface {
|
||||
SetCondition(condition metav1.Condition)
|
||||
}
|
||||
|
||||
type Patcher func(Conditioner)
|
||||
|
||||
type Ready struct{}
|
||||
|
||||
func NewReady() *Ready {
|
||||
return &Ready{}
|
||||
}
|
||||
|
||||
func (p *Ready) PatcherFailed(msg string) Patcher {
|
||||
return func(c Conditioner) {
|
||||
SetReadyFailedWithMessage(c, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Ready) PatcherWithError(err error) Patcher {
|
||||
return func(c Conditioner) {
|
||||
if err == nil {
|
||||
SetReadyCreated(c)
|
||||
} else {
|
||||
SetReadyFailed(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Ready) PatcherRefResolver(err error, obj interface{}) Patcher {
|
||||
return func(c Conditioner) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if apierrors.IsNotFound(err) {
|
||||
SetReadyFailedWithMessage(c, fmt.Sprintf("%s not found", getType(obj)))
|
||||
return
|
||||
}
|
||||
SetReadyFailedWithMessage(c, fmt.Sprintf("Error getting %s", getType(obj)))
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Ready) PatcherHealthy(err error) Patcher {
|
||||
return func(c Conditioner) {
|
||||
if err == nil {
|
||||
SetReadyHealthty(c)
|
||||
} else {
|
||||
SetReadyUnhealthtyWithError(c, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getType(obj interface{}) string {
|
||||
if t := reflect.TypeOf(obj); t.Kind() == reflect.Ptr {
|
||||
return t.Elem().Name()
|
||||
} else {
|
||||
return t.Name()
|
||||
}
|
||||
}
|
||||
70
pkg/condition/ready.go
Normal file
70
pkg/condition/ready.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package conditions
|
||||
|
||||
import (
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
zitadelv1alpha1 "gitea.corredorconect.com/software-engineering/zitadel-k8s-operator/api/v1alpha1"
|
||||
)
|
||||
|
||||
func SetReadyHealthty(c Conditioner) {
|
||||
c.SetCondition(metav1.Condition{
|
||||
Type: zitadelv1alpha1.ConditionTypeReady,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: zitadelv1alpha1.ConditionReasonHealthy,
|
||||
Message: "Healthy",
|
||||
})
|
||||
}
|
||||
|
||||
func SetReadyUnhealthtyWithError(c Conditioner, err error) {
|
||||
c.SetCondition(metav1.Condition{
|
||||
Type: zitadelv1alpha1.ConditionTypeReady,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: zitadelv1alpha1.ConditionReasonHealthy,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
func SetReadyCreatedWithMessage(c Conditioner, message string) {
|
||||
c.SetCondition(metav1.Condition{
|
||||
Type: zitadelv1alpha1.ConditionTypeReady,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: zitadelv1alpha1.ConditionReasonCreated,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
func SetReadyCreated(c Conditioner) {
|
||||
SetReadyCreatedWithMessage(c, "Created")
|
||||
}
|
||||
|
||||
func SetReadyFailedWithMessage(c Conditioner, message string) {
|
||||
c.SetCondition(metav1.Condition{
|
||||
Type: zitadelv1alpha1.ConditionTypeReady,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: zitadelv1alpha1.ConditionReasonFailed,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
func SetReadyFailed(c Conditioner) {
|
||||
SetReadyFailedWithMessage(c, "Failed")
|
||||
}
|
||||
|
||||
func SetReadyWithDeployment(c Conditioner, sts *appsv1.Deployment) {
|
||||
if sts.Status.Replicas == 0 || sts.Status.ReadyReplicas != sts.Status.Replicas {
|
||||
c.SetCondition(metav1.Condition{
|
||||
Type: zitadelv1alpha1.ConditionTypeReady,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: zitadelv1alpha1.ConditionReasonDeploymentNotReady,
|
||||
Message: "Not ready",
|
||||
})
|
||||
return
|
||||
}
|
||||
c.SetCondition(metav1.Condition{
|
||||
Type: zitadelv1alpha1.ConditionTypeReady,
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: zitadelv1alpha1.ConditionReasonDeploymentReady,
|
||||
Message: "Running",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user