Skip to content

Commit

Permalink
Adjust code for other changes in controller-runtime interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Sekretenko <[email protected]>
  • Loading branch information
asekretenko committed May 25, 2021
1 parent b761930 commit 867a21c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
59 changes: 29 additions & 30 deletions pkg/controller/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,33 @@ type Reconciler struct {
// SetupWithManager registers this reconciler with the controller manager
func (r *Reconciler) SetupWithManager(
mgr ctrl.Manager) error {
addOvRelatedInstancesToReconcile := handler.ToRequestsFunc(
func(obj handler.MapObject) []reconcile.Request {
requests := make([]reconcile.Request, 0)
instances := &kudoapi.InstanceList{}
// we are listing all instances here, which could come with some performance penalty
// obj possible optimization is to introduce filtering based on operatorversion (or operator)
err := mgr.GetClient().List(
context.TODO(),
instances,
)
if err != nil {
log.Printf("InstanceController: Error fetching instances list for operator %v: %v", obj.Meta.GetName(), err)
return nil
}
for _, instance := range instances.Items {
// we need to pick only those instances, that belong to the OperatorVersion we're reconciling
if instance.Spec.OperatorVersion.Name == obj.Meta.GetName() &&
instance.OperatorVersionNamespace() == obj.Meta.GetNamespace() {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: instance.Name,
Namespace: instance.Namespace,
},
})
}
addOvRelatedInstancesToReconcile := func(obj client.Object) []reconcile.Request {
requests := make([]reconcile.Request, 0)
instances := &kudoapi.InstanceList{}
// we are listing all instances here, which could come with some performance penalty
// obj possible optimization is to introduce filtering based on operatorversion (or operator)
err := mgr.GetClient().List(
context.TODO(),
instances,
)
if err != nil {
log.Printf("InstanceController: Error fetching instances list for operator %v: %v", obj.GetName(), err)
return nil
}
for _, instance := range instances.Items {
// we need to pick only those instances, that belong to the OperatorVersion we're reconciling
if instance.Spec.OperatorVersion.Name == obj.GetName() &&
instance.OperatorVersionNamespace() == obj.GetNamespace() {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: instance.Name,
Namespace: instance.Namespace,
},
})
}
return requests
})
}
return requests
}

return ctrl.NewControllerManagedBy(mgr).
// Owns(&kudoapi.Instance{}) is equivalent to Watches(&source.Kind{Type: <ForType-apiType>},
Expand All @@ -112,7 +111,7 @@ func (r *Reconciler) SetupWithManager(
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Pod{}).
WithEventFilter(eventFilter()).
Watches(&source.Kind{Type: &kudoapi.OperatorVersion{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: addOvRelatedInstancesToReconcile}).
Watches(&source.Kind{Type: &kudoapi.OperatorVersion{}}, handler.EnqueueRequestsFromMapFunc(addOvRelatedInstancesToReconcile)).
Complete(r)
}

Expand All @@ -137,7 +136,7 @@ func eventFilter() predicate.Funcs {
}

func isForPipePod(e event.DeleteEvent) bool {
return e.Meta.GetAnnotations() != nil && funk.Contains(e.Meta.GetAnnotations(), task.PipePodAnnotation)
return e.Object.GetAnnotations() != nil && funk.Contains(e.Object.GetAnnotations(), task.PipePodAnnotation)
}

// Reconcile is the main controller method that gets called every time something about the instance changes
Expand Down Expand Up @@ -166,7 +165,7 @@ func isForPipePod(e event.DeleteEvent) bool {
// +-------------------------------+
//
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
func (r *Reconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
// ---------- 1. Query the current state ----------

log.Printf("InstanceController: Received Reconcile request for instance %s", request.NamespacedName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/operator/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (r *Reconciler) SetupWithManager(
// Reconcile reads that state of the cluster for an Operator object and makes changes based on the state read
// and what is in the Operator.Spec
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
func (r *Reconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
// Fetch the operator
operator := &kudoapi.Operator{}
err := r.Get(context.TODO(), request.NamespacedName, operator)
err := r.Get(ctx, request.NamespacedName, operator)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/operatorversion/operatorversion_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (r *Reconciler) SetupWithManager(
// and what is in the OperatorVersion.Spec.
//
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
func (r *Reconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) {
func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
// Fetch the operator version
operatorVersion := &kudoapi.OperatorVersion{}
err := r.Get(context.TODO(), request.NamespacedName, operatorVersion)
err := r.Get(ctx, request.NamespacedName, operatorVersion)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/task/podexec/pod_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (pe *PodExec) Run() error {
Version: "v1",
Kind: "pods",
},
false,
pe.RestCfg,
codec)
if err != nil {
Expand Down

0 comments on commit 867a21c

Please sign in to comment.