...
1 package sequel
2
3 import (
4 "fmt"
5
6 "k8s.io/client-go/dynamic"
7 "sigs.k8s.io/cli-utils/pkg/kstatus/watcher"
8 ctrl "sigs.k8s.io/controller-runtime"
9 "sigs.k8s.io/controller-runtime/pkg/client"
10
11 "edge-infra.dev/pkg/k8s/runtime/controller/metrics"
12 "edge-infra.dev/pkg/k8s/runtime/controller/reconcile"
13 "edge-infra.dev/pkg/k8s/runtime/patch"
14 "edge-infra.dev/pkg/k8s/runtime/sap"
15 )
16
17 const (
18
19 controllerName = "sequel"
20 )
21
22
23 type Reconciler struct {
24 client.Client
25
26
27
28 Name string
29
30 Namespace string
31
32
33
34 ResourceManager *sap.ResourceManager
35
36
37
38 OwnerGroupLabel string
39
40
41
42 Conditions reconcile.Conditions
43
44
45 Metrics metrics.Metrics
46 }
47
48 func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
49 r.Name = controllerName
50 r.Client = mgr.GetClient()
51
52 if r.OwnerGroupLabel == "" {
53 return fmt.Errorf("reconciler OwnerGroupLabel must be set")
54 }
55 if r.Conditions.IsEmpty() {
56 return fmt.Errorf("non-empty target condition and conditions to summarize "+
57 "reconcile.Conditions is required. reconcile.Conditions: %v", r.Conditions)
58 }
59
60 d, err := dynamic.NewForConfig(mgr.GetConfig())
61 if err != nil {
62 return fmt.Errorf("failed to create config for dynamic client: %w", err)
63 }
64 r.ResourceManager = sap.NewResourceManager(
65 r.Client,
66 watcher.NewDefaultStatusWatcher(d, mgr.GetRESTMapper()),
67 sap.Owner{Field: r.Name, Group: r.OwnerGroupLabel},
68 )
69
70 return nil
71 }
72
73 func (r *Reconciler) PatchOpts() []patch.Option {
74 return []patch.Option{
75 patch.WithOwnedConditions{Conditions: r.Conditions.Owned},
76 patch.WithFieldOwner(r.Name),
77 }
78 }
79
View as plain text