Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/event recorder #104

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion operator-application/controllers/application/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

applicationsamplev1beta1 "github.com/ibm/operator-sample-go/operator-application/api/v1beta1"
"github.com/ibm/operator-sample-go/operator-application/utilities"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/log"
)
Expand All @@ -22,6 +23,7 @@ func (reconciler *ApplicationReconciler) setConditionResourceFound(ctx context.C
application *applicationsamplev1beta1.Application) error {

if !reconciler.containsCondition(ctx, application, CONDITION_REASON_RESOURCE_FOUND) {
reconciler.Recorder.Event(application, corev1.EventTypeNormal, CONDITION_REASON_RESOURCE_FOUND, CONDITION_MESSAGE_RESOURCE_FOUND)
return utilities.AppendCondition(ctx, reconciler.Client, application, CONDITION_TYPE_RESOURCE_FOUND, CONDITION_STATUS_TRUE,
CONDITION_REASON_RESOURCE_FOUND, CONDITION_MESSAGE_RESOURCE_FOUND)
}
Expand All @@ -38,6 +40,7 @@ func (reconciler *ApplicationReconciler) setConditionInstallReady(ctx context.Co

reconciler.deleteCondition(ctx, application, CONDITION_TYPE_FAILED, CONDITION_REASON_FAILED_INSTALL_READY)
if !reconciler.containsCondition(ctx, application, CONDITION_REASON_INSTALL_READY) {
reconciler.Recorder.Event(application, corev1.EventTypeNormal, CONDITION_TYPE_INSTALL_READY, CONDITION_MESSAGE_INSTALL_READY)
return utilities.AppendCondition(ctx, reconciler.Client, application, CONDITION_TYPE_INSTALL_READY, CONDITION_STATUS_TRUE,
CONDITION_REASON_INSTALL_READY, CONDITION_MESSAGE_INSTALL_READY)
}
Expand All @@ -59,6 +62,7 @@ func (reconciler *ApplicationReconciler) setConditionFailed(ctx context.Context,
}

if !reconciler.containsCondition(ctx, application, reason) {
reconciler.Recorder.Event(application, corev1.EventTypeWarning, CONDITION_TYPE_FAILED, CONDITION_MESSAGE_FAILED_INSTALL_READY)
return utilities.AppendCondition(ctx, reconciler.Client, application, CONDITION_TYPE_FAILED, CONDITION_STATUS_TRUE,
reason, message)
}
Expand All @@ -79,6 +83,7 @@ func (reconciler *ApplicationReconciler) setConditionDatabaseExists(ctx context.
} else {
currentStatus := reconciler.getConditionStatus(ctx, application, CONDITION_TYPE_DATABASE_EXISTS)
if currentStatus != status {
reconciler.Recorder.Event(application, corev1.EventTypeWarning, CONDITION_TYPE_DATABASE_EXISTS, CONDITION_MESSAGE_DATABASE_EXISTS)
reconciler.deleteCondition(ctx, application, CONDITION_TYPE_DATABASE_EXISTS, CONDITION_REASON_DATABASE_EXISTS)
return utilities.AppendCondition(ctx, reconciler.Client, application, CONDITION_TYPE_DATABASE_EXISTS, status,
CONDITION_REASON_DATABASE_EXISTS, CONDITION_MESSAGE_DATABASE_EXISTS)
Expand All @@ -96,6 +101,7 @@ func (reconciler *ApplicationReconciler) setConditionSucceeded(ctx context.Conte
application *applicationsamplev1beta1.Application) error {

if !reconciler.containsCondition(ctx, application, CONDITION_REASON_SUCCEEDED) {
reconciler.Recorder.Event(application, corev1.EventTypeNormal, CONDITION_REASON_SUCCEEDED, CONDITION_MESSAGE_SUCCEEDED)
return utilities.AppendCondition(ctx, reconciler.Client, application, CONDITION_TYPE_SUCCEEDED, CONDITION_STATUS_TRUE,
CONDITION_REASON_SUCCEEDED, CONDITION_MESSAGE_SUCCEEDED)
}
Expand All @@ -111,6 +117,7 @@ func (reconciler *ApplicationReconciler) setConditionDeletionRequestReceived(ctx
application *applicationsamplev1beta1.Application) error {

if !reconciler.containsCondition(ctx, application, CONDITION_REASON_DELETION_REQUEST_RECEIVED) {
reconciler.Recorder.Event(application, corev1.EventTypeNormal, CONDITION_TYPE_DELETION_REQUEST_RECEIVED, CONDITION_MESSAGE_DELETION_REQUEST_RECEIVED)
return utilities.AppendCondition(ctx, reconciler.Client, application, CONDITION_TYPE_DELETION_REQUEST_RECEIVED, CONDITION_STATUS_TRUE,
CONDITION_REASON_DELETION_REQUEST_RECEIVED, CONDITION_MESSAGE_DELETION_REQUEST_RECEIVED)
}
Expand All @@ -129,6 +136,11 @@ func (reconciler *ApplicationReconciler) getConditionStatus(ctx context.Context,
return output
}

// Note: Status of DELETION_REQUEST_RECEIVED can only be True
const CONDITION_TYPE_DELETECONDITION = "Update failed"
const CONDITION_REASON_DELETECONDITION = "Update failed"
const CONDITION_MESSAGE_DELETECONDITION = "Application resource status update failed."

func (reconciler *ApplicationReconciler) deleteCondition(ctx context.Context, application *applicationsamplev1beta1.Application,
typeName string, reason string) error {

Expand All @@ -143,7 +155,7 @@ func (reconciler *ApplicationReconciler) deleteCondition(ctx context.Context, ap

err := reconciler.Client.Status().Update(ctx, application)
if err != nil {
log.Info("Application resource status update failed.")
reconciler.Recorder.Event(application, corev1.EventTypeWarning, CONDITION_REASON_DELETECONDITION, CONDITION_MESSAGE_DELETECONDITION)
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion operator-application/controllers/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -34,7 +35,8 @@ var countReconcileLaunched = prometheus.NewCounter(

type ApplicationReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
Recorder record.EventRecorder
}

//+kubebuilder:rbac:groups=database.sample.third.party,resources=databases,verbs=get;list;watch;create;update;patch;delete
Expand Down
1 change: 1 addition & 0 deletions operator-application/controllers/application/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (reconciler *ApplicationReconciler) reconcileDeployment(ctx context.Context
log.Info("Failed to create deployment resource. Re-running reconcile.")
return ctrl.Result{}, err
}
reconciler.Recorder.Eventf(application, corev1.EventTypeNormal, "Created", "Created deployment %s", deploymentDefinition.Name)
} else {
log.Info("Failed to get deployment resource " + variables.DeploymentName + ". Re-running reconcile.")
return ctrl.Result{}, err
Expand Down
6 changes: 4 additions & 2 deletions operator-application/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ func main() {
}

if err = (&applicationcontroller.ApplicationReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("application-controller"),
//recorder: mgr.GetRecorder("containerset-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Application")
os.Exit(1)
Expand Down