forked from stolostron/multicloud-operators-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_controller.go
71 lines (60 loc) · 2.1 KB
/
action_controller.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
package controllers
import (
"context"
"github.com/go-logr/logr"
actionv1beta1 "github.com/open-cluster-management/multicloud-operators-foundation/pkg/apis/action/v1beta1"
restutils "github.com/open-cluster-management/multicloud-operators-foundation/pkg/utils/rest"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ActionReconciler reconciles a Action object
type ActionReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
DynamicClient dynamic.Interface
KubeControl restutils.KubeControlInterface
EnableImpersonation bool
}
func NewActionReconciler(client client.Client,
log logr.Logger, scheme *runtime.Scheme,
dynamicClient dynamic.Interface,
kubeControl restutils.KubeControlInterface,
enableImpersonation bool) *ActionReconciler {
return &ActionReconciler{
Client: client,
Log: log,
Scheme: scheme,
DynamicClient: dynamicClient,
KubeControl: kubeControl,
EnableImpersonation: enableImpersonation,
}
}
func (r *ActionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("ManagedClusterAction", req.NamespacedName)
action := &actionv1beta1.ManagedClusterAction{}
err := r.Get(ctx, req.NamespacedName, action)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if meta.IsStatusConditionTrue(action.Status.Conditions, actionv1beta1.ConditionActionCompleted) {
return ctrl.Result{}, nil
}
if err := r.handleAction(action); err != nil {
log.Error(err, "unable to handle ManagedClusterAction")
}
if err := r.Client.Status().Update(ctx, action); err != nil {
log.Error(err, "unable to update status of ManagedClusterAction")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *ActionReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&actionv1beta1.ManagedClusterAction{}).
Complete(r)
}