Skip to content

Commit

Permalink
refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Nov 23, 2023
1 parent 36c06a2 commit fdcb398
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cp

import (
"context"
"fmt"

"github.com/wso2/apk/adapter/pkg/logging"
"github.com/wso2/apk/common-controller/internal/cache"
Expand All @@ -37,7 +36,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

Expand Down Expand Up @@ -86,19 +84,14 @@ func NewApplicationController(mgr manager.Manager, subscriptionStore *cache.Subs
func (applicationReconciler *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
applicationKey := req.NamespacedName
var applicationList = new(cpv1alpha2.ApplicationList)

loggers.LoggerAPKOperator.Infof("Reconciling application: %v", applicationKey.String())
if err := applicationReconciler.client.List(ctx, applicationList); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get applications %s/%s",
applicationKey.Namespace, applicationKey.Name)
}
loggers.LoggerAPKOperator.Debugf("Reconciling application: %v", applicationKey.String())
var application cpv1alpha2.Application
if err := applicationReconciler.client.Get(ctx, req.NamespacedName, &application); err != nil {
if k8error.IsNotFound(err) {
applicationSpec, found := applicationReconciler.ods.GetApplicationFromStore(applicationKey)
loggers.LoggerAPKOperator.Infof("Application cr not available in k8s")
loggers.LoggerAPKOperator.Infof("cached Application spec: %v,%v", applicationSpec, found)
loggers.LoggerAPKOperator.Debugf("Application cr not available in k8s")
loggers.LoggerAPKOperator.Debugf("cached Application spec: %v,%v", applicationSpec, found)
if found {
utils.SendAppDeletionEvent(applicationKey.Name, applicationSpec)
applicationReconciler.ods.DeleteApplicationFromStore(applicationKey)
Expand Down
7 changes: 4 additions & 3 deletions common-controller/internal/utils/event_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SendAppUpdateEvent(applicationUUID string, oldApplicationSpec cpv1alpha2.Ap
Attributes: newApplicationSpec.Attributes,
},
}
loggers.LoggerAPKOperator.Infof("Sending event to all clients: %v", &event)
loggers.LoggerAPKOperator.Debugf("Sending event to all clients: %v", &event)
sendEvent(&event)
sendDeleteApplicationKeyMappingEvent(applicationUUID, oldApplicationSpec)
sendApplicationKeyMappingEvent(applicationUUID, newApplicationSpec)
Expand Down Expand Up @@ -192,13 +192,13 @@ func sendApplicationKeyMappingEvent(applicationUUID string, applicationSpec cpv1
}
}
func sendEvent(event *subscription.Event) {
loggers.LoggerAPKOperator.Infof("Sending event to all clients: %v", event)
loggers.LoggerAPKOperator.Debugf("Sending event to all clients: %v", event)
for clientID, stream := range GetAllClientConnections() {
err := stream.Send(event)
if err != nil {
loggers.LoggerAPKOperator.Errorf("Error sending event to client %s: %v", clientID, err)
} else {
loggers.LoggerAPKOperator.Infof("Event sent to client %s", clientID)
loggers.LoggerAPKOperator.Debugf("Event sent to client %s", clientID)
}
}
}
Expand All @@ -213,5 +213,6 @@ func SendInitialEvent(srv apkmgt.EventStreamService_StreamEventsServer) {
Type: constants.AllEvnts,
TimeStamp: milliseconds,
}
loggers.LoggerAPKOperator.Debugf("Sending initial event to client: %v", &event)
srv.Send(&event)
}

0 comments on commit fdcb398

Please sign in to comment.