All checks were successful
Build and Publish / build-release (push) Successful in 8m29s
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
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()
|
|
}
|
|
}
|