Skip to content

Commit

Permalink
ODF-545 | Add event controller (#154)
Browse files Browse the repository at this point in the history
* add event controller and event permissions
* change poolog event name and reason
* add fencecompleted const
---------

Signed-off-by: Tal Yichye <[email protected]>
  • Loading branch information
tyichye19 authored Jul 18, 2023
1 parent 649561b commit 62ede83
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ spec:
- events
verbs:
- create
- delete
- get
- list
- patch
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ rules:
- events
verbs:
- create
- delete
- get
- list
- patch
Expand Down
108 changes: 108 additions & 0 deletions controllers/event/event_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright contributors to the ibm-storage-odf-operator project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package event

import (
"context"
"github.com/IBM/ibm-storage-odf-operator/controllers/util"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strings"
)

func reconcileEvent(obj runtime.Object) bool {
evt, ok := obj.(*corev1.Event)
if !ok {
return false
}
return strings.HasPrefix(evt.Name, util.PoolOGChangedReason) ||
strings.HasPrefix(evt.Name, util.FenceCompletedReason)

}

var eventPredicate = predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return reconcileEvent(e.Object)
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
}

type EventWatcher struct {
Client client.Client
Scheme *runtime.Scheme
Log logr.Logger
Namespace string
}

func (r *EventWatcher) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Event{}, builder.WithPredicates(eventPredicate)).
WithOptions(controller.Options{MaxConcurrentReconciles: 1}).
Complete(r)
}

//+kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;

// Reconcile Event
func (r *EventWatcher) Reconcile(_ context.Context, request reconcile.Request) (result reconcile.Result, err error) {
result = reconcile.Result{}
prevLogger := r.Log

defer func() {
r.Log = prevLogger
if errors.IsConflict(err) {
r.Log.Info("requeue due to resource update conflicts")
result = reconcile.Result{Requeue: true}
err = nil
}
}()

r.Log = r.Log.WithValues("Event", request.NamespacedName)
r.Log.Info("reconciling Event")

evt := &corev1.Event{}

if err = r.Client.Get(context.TODO(), request.NamespacedName, evt); err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Event not found")
return result, nil
}
return result, err
}

// TODO - Handle PoolOwnershipGroupChanged & FenceCompleted events

return result, nil
}
7 changes: 4 additions & 3 deletions controllers/util/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const (
FailedCreateStorageClassReason = "FailedCreateStorageClass"
DeletedDuplicatedStorageClassReason = "DeletedDuplicatedStorageClass"
FailedCreatePromRuleReason = "FailedCreatePromRule"
PoolOGChangeReason = "PoolOwnershipGroupChange"
PoolOGChangedReason = "PoolOwnershipGroupChanged"
FenceCompletedReason = "FenceCompleted"
)

func InitK8sEvent(instance *odfv1alpha1.FlashSystemCluster, eventType, reason, message string) *corev1.Event {
Expand Down Expand Up @@ -75,7 +76,7 @@ func InitK8sPoolOGChangeEvent(instance *odfv1alpha1.FlashSystemCluster, poolName
selectLabels := GetLabels()
return &corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v_for_%v", PoolOGChangeReason, poolName),
Name: fmt.Sprintf("%v_for_%v", PoolOGChangedReason, poolName),
Namespace: instance.Namespace,
Labels: selectLabels,
},
Expand All @@ -87,7 +88,7 @@ func InitK8sPoolOGChangeEvent(instance *odfv1alpha1.FlashSystemCluster, poolName
ResourceVersion: instance.ResourceVersion,
APIVersion: instance.APIVersion,
},
Reason: PoolOGChangeReason,
Reason: PoolOGChangedReason,
Message: fmt.Sprintf("Pool %v changed ownership group to %v", poolName, ogName),
FirstTimestamp: t,
LastTimestamp: t,
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"flag"
"github.com/IBM/ibm-storage-odf-operator/controllers/event"
"os"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -148,6 +149,16 @@ func main() {
os.Exit(1)
}

if err = (&event.EventWatcher{
Client: mgr.GetClient(),
Namespace: ns,
Log: ctrl.Log.WithName("controllers").WithName("EventWatcher"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EventWatcher")
os.Exit(1)
}

setupLog.Info("enable console")
if err := mgr.Add(manager.RunnableFunc(func(context.Context) error {
err = console.EnableIBMConsoleByDefault(mgr.GetClient())
Expand Down

0 comments on commit 62ede83

Please sign in to comment.