-
Notifications
You must be signed in to change notification settings - Fork 53
/
resources.go
87 lines (74 loc) · 2.42 KB
/
resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package resources
import (
"fmt"
"github.com/banzaicloud/operator-tools/pkg/reconciler"
"github.com/go-logr/logr"
"github.com/rancher/opni/pkg/util/k8sutil"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const (
InternalIndexingUser = "opni-indexer"
)
// ComponentReconciler reconciler interface
type ComponentReconciler func() (*reconcile.Result, error)
// Resource redeclaration of function with return type kubernetes Object
type Resource func() (runtime.Object, reconciler.DesiredState, error)
// ResourceWithLog redeclaration of function with logging parameter and return type kubernetes Object
type ResourceWithLog func(log logr.Logger) runtime.Object
func Absent(obj client.Object) Resource {
return func() (runtime.Object, reconciler.DesiredState, error) {
return obj, reconciler.StateAbsent, nil
}
}
func Present(obj client.Object) Resource {
return func() (runtime.Object, reconciler.DesiredState, error) {
return obj, reconciler.StatePresent, nil
}
}
func Created(obj client.Object) Resource {
return func() (runtime.Object, reconciler.DesiredState, error) {
return obj, reconciler.StateCreated, nil
}
}
func Error(obj client.Object, err error) Resource {
return func() (runtime.Object, reconciler.DesiredState, error) {
return obj, reconciler.StatePresent, err
}
}
func PresentIff(condition bool, obj client.Object) Resource {
return func() (runtime.Object, reconciler.DesiredState, error) {
if condition {
return obj, reconciler.StatePresent, nil
}
return obj, reconciler.StateAbsent, nil
}
}
func CreatedIff(condition bool, obj client.Object) Resource {
return func() (runtime.Object, reconciler.DesiredState, error) {
if condition {
return obj, reconciler.StateCreated, nil
}
return obj, reconciler.StateAbsent, nil
}
}
func ReconcileAll(r reconciler.ResourceReconciler, resources []Resource) k8sutil.RequeueOp {
for _, factory := range resources {
o, state, err := factory()
if err != nil {
return k8sutil.RequeueErr(fmt.Errorf("failed to create object: %w", err))
}
if o == nil {
panic(fmt.Sprintf("reconciler %#v created a nil object", factory))
}
result, err := r.ReconcileResource(o, state)
if err != nil {
return k8sutil.RequeueErr(fmt.Errorf("failed to reconcile resource %#T: %w", o, err))
}
if result != nil {
return k8sutil.LoadResult(result, err)
}
}
return k8sutil.DoNotRequeue()
}